Documentation ¶
Index ¶
Constants ¶
const ( // ErrMissingClosingParenthesis a closing parenthesis was expected, found something else instead. ErrMissingClosingParenthesis = iota // ErrInvalidNudToken an unknown/invalid token was found at the beginning of an expression. ErrInvalidNudToken // ErrInvalidLedToken an unknown/invalid token was found in an expression. ErrInvalidLedToken )
Variables ¶
This section is empty.
Functions ¶
func TopDownParse ¶
TopDownParse run the topdown parser on the tokens provided by the lexer. It will return whatever the tokens have built through nud/led methods and the list of errors encountered during the parsing.
Types ¶
type Any ¶
type Any struct {
// contains filtered or unexported fields
}
Any implements Value interface, provided an encapsulation for any valid Go type.
type Context ¶
type Context interface { // Expression allows a token nud/led function to call a sub-expression // parsing (e.g., for things within parenthesis). Expression(rbp int) interface{} // Advance tells the parse to discard the incoming token and move to the next // one. Advance() Token // Peek returns the incoming token. That can be used to check whether the // next token is what is expected (and then discard it with Advance()). Peek() Token // Error indicates that something went wrong. The parsing will still continue // after that, allowing for multiple errors to be found. Error(error) }
Context is provided to the token function when the topdown parser is proceeding through the data.
type Error ¶
Error provides more details about a given parser error with references to the source. It satisfies the error interface.
type Identifier ¶
type Identifier string
Identifier represents a parsed identifier - as opposed to a regular string constant.
func (Identifier) String ¶
func (id Identifier) String() string
type Lexer ¶
type Lexer interface { // Next provide the next available token. Next() Token }
Lexer is the interface that the parser expects to get input tokens.
type MultiError ¶
type MultiError struct {
Errors []error
}
MultiError holds details about parsing errors obtained when calling Parse.
func (MultiError) Error ¶
func (e MultiError) Error() string
type Source ¶
type Source struct {
// contains filtered or unexported fields
}
Source holds a rune representation of source code.
func NewSource ¶
NewSource creates a new source object from the provided utf8 string. It will ignore all invalid codepoint.
type SourceRef ¶
type SourceRef struct { // Source to which this refers to. Source *Source // Line indicates the line in the file. 0-indexed. Line int // Column indicates the rune index (ignoring invalid sequences) in the line. // 0-indexed. Column int }
SourceRef contains information to trace code back to its implementation.
type Token ¶
type Token interface { // Nud is 'Null denotation'. This is used in a top down parser when a token // is encountered at the beginning of an expression. Nud(ctx Context) interface{} // Led is 'Left denotation'. This is used in a top down parser when a token // is encountered after the beginning of the expression. 'left' contains what // was previously obtained with Nud/Led of the previous token. Led(ctx Context, left interface{}) interface{} // Lbp is 'Left Binding Priority'. The value 0 is used as right binding // priority in TopDownParse - which usually means that "end of stream" should // have an lbp of 0. Lbp() int }
Token must be implemented by the data returned by the lexer provided to the parser.
type TopDown ¶
type TopDown struct {
// contains filtered or unexported fields
}
TopDown implements a generic TopDown parser.
func (*TopDown) Error ¶
Error implements the Context interface and allows a token nud&led to indicates that there was an issue.
func (*TopDown) Expression ¶
Expression continue the parsing and returns the parsed data. The actual data depends on what nud&led functions of the tokens coming from the lexer return. Parameter rbp is the right-binding-priority; when parsing a full expression, it should be zero usually (it depends on the priorities the tokens provide; usually, a token with priority zero indicates end of stream).