parser

package
v2.0.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package parser is used to parse an input program from its tokens and produce an abstract syntax tree (AST) as output.

A parser is created by calling New() with a lexer as input. The parser should then be used only once, by calling parser.Parse() to produce the AST.

Index

Constants

View Source
const (
	LOWEST      int
	PIPE        // |
	COND        // OR or AND
	ASSIGN      // =
	DECLARE     // :=
	TERNARY     // ? :
	EQUALS      // == or !=
	LESSGREATER // > or <
	SUM         // + or -
	PRODUCT     // * or /
	POWER       // **
	MOD         // %
	PREFIX      // -X or !X
	CALL        // myFunction(X)
	IN          // X in Y
	RANGE       // range X
	INDEX       // array[index], map[key]
	HIGHEST
)

Precedence order for operators

Variables

This section is empty.

Functions

func Parse

func Parse(ctx context.Context, input string, options ...Option) (*ast.Program, error)

Parse the provided input as Tamarin source code and return the AST. This is shorthand way to create a Lexer and Parser and then call Parse on that.

Types

type BaseParserError

type BaseParserError struct {
	// contains filtered or unexported fields
}

BaseParserError is the simplest implementation of ParserError.

func NewParserError

func NewParserError(opts ErrorOpts) *BaseParserError

NewBaseParserError returns a new BaseParserError populated with the given error data.

func (*BaseParserError) Cause

func (e *BaseParserError) Cause() error

func (*BaseParserError) EndPosition

func (e *BaseParserError) EndPosition() token.Position

func (*BaseParserError) Error

func (e *BaseParserError) Error() string

func (*BaseParserError) File

func (e *BaseParserError) File() string

func (*BaseParserError) FriendlyMessage

func (e *BaseParserError) FriendlyMessage() string

func (*BaseParserError) Line

func (e *BaseParserError) Line() int

func (*BaseParserError) Message

func (e *BaseParserError) Message() string

func (*BaseParserError) SourceCode

func (e *BaseParserError) SourceCode() string

func (*BaseParserError) StartPosition

func (e *BaseParserError) StartPosition() token.Position

func (*BaseParserError) Type

func (e *BaseParserError) Type() string

func (*BaseParserError) Unwrap

func (e *BaseParserError) Unwrap() error

type ErrorOpts

type ErrorOpts struct {
	ErrType       string
	Message       string
	Cause         error
	File          string
	StartPosition token.Position
	EndPosition   token.Position
	SourceCode    string
}

ErrorOpts is a struct that holds a variety of error data. All fields are optional, although one of `Cause` or `Message` are recommended. If `Cause` is set, `Message` will be ignored.

type Option

type Option func(*Parser)

Option is a configuration function for a Lexer.

func WithFile

func WithFile(file string) Option

WithFile sets the file name for the Lexer.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser object

func New

func New(l *lexer.Lexer, options ...Option) *Parser

New returns a Parser for the program provided by the given Lexer.

func (*Parser) Parse

func (p *Parser) Parse(ctx context.Context) (*ast.Program, error)

Parse the program that is provided via the lexer.

type ParserError

type ParserError interface {
	Type() string
	Message() string
	Cause() error
	File() string
	StartPosition() token.Position
	EndPosition() token.Position
	SourceCode() string
	Error() string
	FriendlyMessage() string
}

ParserError is an interface that all parser errors implement.

type SyntaxError

type SyntaxError struct {
	*BaseParserError
}

func NewSyntaxError

func NewSyntaxError(opts ErrorOpts) *SyntaxError

NewSyntaxError returns a new SyntaxError populated with the given error data

Jump to

Keyboard shortcuts

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