Documentation ¶
Overview ¶
Package participle constructs parsers from definitions in struct tags and parses directly into those structs. The approach is philosophically similar to how other marshallers work in Go, "unmarshalling" an instance of a grammar into a struct.
The supported annotation syntax is:
- `@<expr>` Capture expression into the field.
- `@@` Recursively capture using the fields own type.
- `<identifier>` Match named lexer token.
- `{ ... }` Match 0 or more times.
- `( ... )` Group.
- `[ ... ]` Optional.
- `"..."[:<identifier>]` Match the literal, optionally specifying the exact lexer token type to match.
- `<expr> <expr> ...` Match expressions.
- `<expr> | <expr>` Match one of the alternatives.
Here's an example of an EBNF grammar.
type Group struct { Expression *Expression `"(" @@ ")"` } type Option struct { Expression *Expression `"[" @@ "]"` } type Repetition struct { Expression *Expression `"{" @@ "}"` } type Literal struct { Start string `@String` // lexer.Lexer token "String" End string `[ "…" @String ]` } type Term struct { Name string `@Ident |` Literal *Literal `@@ |` Group *Group `@@ |` Option *Option `@@ |` Repetition *Repetition `@@` } type Sequence struct { Terms []*Term `@@ { @@ }` } type Expression struct { Alternatives []*Sequence `@@ { "|" @@ }` } type Expressions []*Expression type Production struct { Name string `@Ident "="` Expressions Expressions `@@ { @@ } "."` } type EBNF struct { Productions []*Production `{ @@ }` }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // NextMatch should be returned by Parseable.Parse() method implementations to indicate // that the node did not match and that other matches should be attempted, if appropriate. NextMatch = errors.New("no match") // nolint: golint )
Functions ¶
This section is empty.
Types ¶
type Capture ¶
Capture can be implemented by fields in order to transform captured tokens into field values.
type Parseable ¶
type Parseable interface { // Parse into the receiver. // // Should return NextMatch if no tokens matched and parsing should continue. // Nil should be returned if parsing was successful. Parse(lex lexer.Lexer) error }
The Parseable interface can be implemented by any element in the grammar to provide custom parsing.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
A Parser for a particular grammar and lexer.
func Build ¶
func Build(grammar interface{}, lex lexer.Definition) (parser *Parser, err error)
Build constructs a parser for the given grammar.
If "lex" is nil, the default lexer based on text/scanner will be used. This scans typical Go- like tokens.
See documentation for details
func MustBuild ¶
func MustBuild(grammar interface{}, lex lexer.Definition) *Parser
MustBuild calls Build(grammar, lex) and panics if an error occurs.
func (*Parser) Parse ¶
Parse from r into grammar v which must be of the same type as the grammar passed to participle.Build().
func (*Parser) ParseBytes ¶
ParseBytes is a convenience around Parse().
func (*Parser) ParseString ¶
ParseString is a convenience around Parse().
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
_examples
|
|
expr
Package main implements
|
Package main implements |
hcl
Package main implements a parser for HashiCorp's HCL configuration syntax.
|
Package main implements a parser for HashiCorp's HCL configuration syntax. |
thrift
Package main implements a parser for Thrift files (https://thrift.apache.org/) It parses namespaces, exceptions, services, structs, consts, typedefs and enums, but is easily extensible to more.
|
Package main implements a parser for Thrift files (https://thrift.apache.org/) It parses namespaces, exceptions, services, structs, consts, typedefs and enums, but is easily extensible to more. |
Package lexer defines interfaces and implementations used by Participle to perform lexing.
|
Package lexer defines interfaces and implementations used by Participle to perform lexing. |