parser

package
v0.0.0-...-dbeee19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2021 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
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

func TopDownParse(lex Lexer) (interface{}, []error)

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.

func (Any) Ref

func (a Any) Ref() *SourceRef

Ref funcrion return current value by reference

func (Any) String

func (a Any) String() string

func (Any) Value

func (a Any) Value() interface{}

Value function return value of current node

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

type Error struct {
	Msg  string
	Code ErrorCode
	Ref  *SourceRef
}

Error provides more details about a given parser error with references to the source. It satisfies the error interface.

func (Error) Error

func (e Error) Error() string

type ErrorCode

type ErrorCode int

ErrorCode type to parser errors

func (ErrorCode) String

func (c ErrorCode) String() string

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

func NewSource(input string) *Source

NewSource creates a new source object from the provided utf8 string. It will ignore all invalid codepoint.

func (*Source) Line

func (s *Source) Line(i int) (line []rune, err error)

Line returns the line corresponding to the 0-based provided index. Returns a non-nil error if the value is out of bound.

func (*Source) Scan

func (s *Source) Scan() <-chan rune

Scan returns a channel providing all the rune of the source in order one at a time. Channel will be closed at the end of the source.

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.

func (*SourceRef) Context

func (ref *SourceRef) Context(prefix string) string

Context generates a description of the provided source reference. It will end with a new line and may contain multiple lines.

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) Advance

func (p *TopDown) Advance() Token

Advance returns the incoming token, and move to the next one.

func (*TopDown) Error

func (p *TopDown) Error(err error)

Error implements the Context interface and allows a token nud&led to indicates that there was an issue.

func (*TopDown) Expression

func (p *TopDown) Expression(rbp int) interface{}

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).

func (*TopDown) Peek

func (p *TopDown) Peek() Token

Peek returns the incoming token.

type Value

type Value interface {
	Value() interface{}
	String() string
	Ref() *SourceRef
}

Value encapsulate all data that the language manipulate. There is a layer of indirection between glop and go in order to allow for extra annotation and reduce type conversions needed when writing code with glop.

func NewAny

func NewAny(v interface{}, ref *SourceRef) Value

NewAny create new instance of Any

func Parse

func Parse(src *Source) (ret Value, err error)

Parse will take the provided source, parse it, and ensure that only one root node is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL