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.
- `( ... )` Group.
- `"..."` Match the literal (note that the lexer must emit tokens matching this literal exactly).
- `"...":<identifier>` Match the literal, specifying the exact lexer token type to match.
- `<expr> <expr> ...` Match expressions.
- `<expr> | <expr>` Match one of the alternatives.
The following modifiers can be used after any expression:
- `*` Expression can match zero or more times.
- `+` Expression must match one or more times.
- `?` Expression can match zero or once.
- `!` Require a non-empty match (this is useful with a sequence of optional matches eg. `("a"? "b"? "c"?)!`).
Supported but deprecated:
- `{ ... }` Match 0 or more times (**DEPRECATED** - prefer `( ... )*`).
- `[ ... ]` Optional (**DEPRECATED** - prefer `( ... )?`).
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 ( // MaxIterations limits the number of elements capturable by {}. MaxIterations = 1000000 // 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 )
var DropToken = errors.New("drop token") // nolint: golint
DropToken can be returned by a Mapper to remove a token from the stream.
Functions ¶
This section is empty.
Types ¶
type Capture ¶
Capture can be implemented by fields in order to transform captured tokens into field values.
type Error ¶
type Error string
Error is an error returned by the parser internally to differentiate from non-Participle errors.
type Mapper ¶
Mapper function for mutating tokens before being applied to the AST.
If the Mapper func returns an error of DropToken, the token will be removed from the stream.
type Option ¶
An Option to modify the behaviour of the Parser.
func CaseInsensitive ¶ added in v0.2.2
CaseInsensitive allows the specified token types to be matched case-insensitively.
func Lexer ¶
func Lexer(def lexer.Definition) Option
Lexer is an Option that sets the lexer to use with the given grammar.
func Map ¶
Map is an Option that configures the Parser to apply a mapping function to each Token from the lexer.
This can be useful to eg. upper-case all tokens of a certain type, or dequote strings.
"symbols" specifies the token symbols that the Mapper will be applied to. If empty, all tokens will be mapped.
func Unquote ¶
Unquote applies strconv.Unquote() to tokens of the given types.
Tokens of type "String" will be unquoted if no other types are provided.
func Upper ¶
Upper is an Option that upper-cases all tokens of the given type. Useful for case normalisation.
func UseLookahead ¶
UseLookahead allows branch lookahead up to "n" tokens.
If parsing cannot be disambiguated before "n" tokens of lookahead, parsing will fail.
Note that increasing lookahead has a minor performance impact, but also reduces the accuracy of error reporting.
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.PeekingLexer) 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 ¶
Build constructs a parser for the given grammar.
If "Lexer()" is not provided as an option, a default lexer based on text/scanner will be used. This scans typical Go- like tokens.
See documentation for details
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
|
|
basic
nolint: golint nolint: golint, dupl nolint: golint, dupl
|
nolint: golint nolint: golint, dupl nolint: golint, dupl |
expr
nolint: govet
|
nolint: govet |
hcl
Package main implements a parser for HashiCorp's HCL configuration syntax.
|
Package main implements a parser for HashiCorp's HCL configuration syntax. |
protobuf
nolint: govet, golint
|
nolint: govet, golint |
sql
nolint: govet
|
nolint: govet |
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. |
ebnf/internal
Package internal is a library for EBNF grammars.
|
Package internal is a library for EBNF grammars. |