parser

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 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(input string) (*ast.Program, error)

Parse is a shortcut that can be used to parse the given Tamarin source code. The lexer and parser are created internally and not exposed. ParseWithOpts should be used in production in order to pass a context.

func ParseWithOpts

func ParseWithOpts(ctx context.Context, opts Opts) (*ast.Program, error)

ParseWithOpts is a shortcut that can be used to parse the given Tamarin source code. The lexer and parser are created internally and not exposed.

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 Opts

type Opts struct {
	// Input is the string being parsed.
	Input string
	// File is the name of the file being parsed (optional).
	File string
}

Opts contains options for the parser.

type Parser

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

Parser object

func New

func New(l *lexer.Lexer) *Parser

New returns a Parser for the program provided by the 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