parser

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Message       string
	StartPosition ast.Position
	EndPosition   ast.Position
}

Error represents an error encountered during parsing

func (Error) Error

func (e Error) Error() string

type Errors

type Errors []*Error

Errors represents multiple Errors

func (Errors) Error

func (e Errors) Error() string

type Parser

type Parser struct {
	Tokenizer    *ast.Tokenizer
	CurrentToken *ast.Token
	NextToken    *ast.Token
	PrevToken    *ast.Token
	// if true, there was whitespace between CurrentToken and NextToken
	NextWouldBeWhitespace bool
	// if true, current token was preceeded by whitespace
	SkippedWhitespace bool
	// using an interface of ourself to call the parsing-methods allows them to be overridden by 'subclasses'
	This YololParserFunctions
	// Contains all errors encountered during parsing
	Errors Errors
	// If true, return all found errors, not only one per line
	AllErrors bool
	// If true, print debug logs
	DebugLog bool
}

Parser parses a yolol programm into an AST

func NewParser

func NewParser() *Parser

NewParser creates a new parser

func (*Parser) Advance

func (p *Parser) Advance() *ast.Token

Advance advances the current token to the next (non whitespace) token in the list

func (*Parser) Error

func (p *Parser) Error(msg string, start ast.Position, end ast.Position)

Error appends an error to the list of errors encountered during parsing if p.AllErrors is false, only the first error per line is appended to the list of errors

func (*Parser) ErrorCurrent

func (p *Parser) ErrorCurrent(msg string)

ErrorCurrent calls Error() with the position of the current token

func (*Parser) Expect

func (p *Parser) Expect(tokenType string, tokenValue string) ast.Position

Expect checks if the current token has the given type and value if true, the tokens position is returned, otherwise an error is logged alsways advances to the next token

func (*Parser) HasNext

func (p *Parser) HasNext() bool

HasNext returns true if there is a next token

func (*Parser) IsCurrent added in v0.0.13

func (p *Parser) IsCurrent(t, value string) bool

IsCurrent checks if the current token matches the given type and value The comparison of the value is case-insensitive

func (*Parser) IsCurrentType added in v0.0.13

func (p *Parser) IsCurrentType(t string) bool

IsCurrentType checks if the type of the current token is equal to the given type

func (*Parser) IsCurrentValue added in v0.0.13

func (p *Parser) IsCurrentValue(value string) bool

IsCurrentValue checks if the value of the current token is equal to the given value The comparison is case-insensitive

func (*Parser) IsCurrentValueIn added in v0.0.13

func (p *Parser) IsCurrentValueIn(values []string) bool

IsCurrentValueIn checks if the value of the current token is one of the provided values The comparison is case-insensitive

func (*Parser) Log

func (p *Parser) Log()

Log logs the visiting of a parsing function

func (*Parser) Parse

func (p *Parser) Parse(prog string) (*ast.Program, error)

Parse is the main method of the parser. Parses a yolol-program into an AST.

func (*Parser) ParseAssignment

func (p *Parser) ParseAssignment() ast.Statement

ParseAssignment parses an assignment-node

func (*Parser) ParseBracketExpression

func (p *Parser) ParseBracketExpression() ast.Expression

ParseBracketExpression parses a racketed expression

func (*Parser) ParseCompareExpression

func (p *Parser) ParseCompareExpression() ast.Expression

ParseCompareExpression parses a compare expression

func (*Parser) ParseExpression

func (p *Parser) ParseExpression() ast.Expression

ParseExpression parses an expression

func (*Parser) ParseGoto

func (p *Parser) ParseGoto() ast.Statement

ParseGoto parse parses a goto-node

func (*Parser) ParseIf

func (p *Parser) ParseIf() ast.Statement

ParseIf parses an if-node

func (*Parser) ParseLine

func (p *Parser) ParseLine() *ast.Line

ParseLine parses a line-node

func (*Parser) ParseLogicExpression

func (p *Parser) ParseLogicExpression() ast.Expression

ParseLogicExpression parses a logical expression

func (*Parser) ParsePostOpExpression

func (p *Parser) ParsePostOpExpression() ast.Expression

ParsePostOpExpression parse post-expression

func (*Parser) ParsePreOpExpression

func (p *Parser) ParsePreOpExpression() ast.Expression

ParsePreOpExpression parse pre-expression

func (*Parser) ParsePreOrPostOperation

func (p *Parser) ParsePreOrPostOperation() ast.Statement

ParsePreOrPostOperation parses a pre-/post operation (x++, ++x) as a statement

func (*Parser) ParseProdExpression

func (p *Parser) ParseProdExpression() ast.Expression

ParseProdExpression parses a product expression

func (*Parser) ParseProgram

func (p *Parser) ParseProgram() *ast.Program

ParseProgram parses a programm-node

func (*Parser) ParseSingleExpression

func (p *Parser) ParseSingleExpression() ast.Expression

ParseSingleExpression parses a single expression

func (*Parser) ParseStatement

func (p *Parser) ParseStatement() ast.Statement

ParseStatement parses a statement-node

func (*Parser) ParseSumExpression

func (p *Parser) ParseSumExpression() ast.Expression

ParseSumExpression parses a sum-expression

func (*Parser) ParseUnaryExpression

func (p *Parser) ParseUnaryExpression() ast.Expression

ParseUnaryExpression parses an unary expression

func (*Parser) Reset added in v0.0.10

func (p *Parser) Reset()

Reset prepares all internal fields for a new parsing run is called automatically by Parse(). Overriding structs must call this in their Parse()

func (*Parser) SkipLine

func (p *Parser) SkipLine()

SkipLine skips tokens up to the next newline

type Printer

type Printer struct {
	// This function is called whenever an unknown node-type is encountered.
	// It can be used to add support for additional types to the generator
	// returns the yolol-code for the giben node or an error
	UnknownHandlerFunc func(node ast.Node, visitType int, p *Printer) error
	// If true, only insert spaces where absolutely necessary
	Mode Printermode

	// If true, at position-information to every printed token.
	// Does not produce valid yolol, but is usefull for debugging
	DebugPositions bool
	// contains filtered or unexported fields
}

Printer generates yolol-code from an AST

func (*Printer) Newline added in v0.0.14

func (p *Printer) Newline()

Newline adds a newline to the source-code that is currently build

func (*Printer) OptionalSpace added in v0.0.14

func (p *Printer) OptionalSpace()

OptionalSpace adds a space to the source-code that is currently build, IF we are not producing compressed output

func (*Printer) Print

func (p *Printer) Print(prog ast.Node) (string, error)

Print returns the yolol-code the ast-node and it's children represent

func (*Printer) Space added in v0.0.14

func (p *Printer) Space()

Space adds a space to the source-code that is currently build

func (*Printer) StatementSeparator added in v0.0.14

func (p *Printer) StatementSeparator()

StatementSeparator writes spaces to seperate statements on one line. Amount of spaces depends on settings

func (*Printer) Write added in v0.0.14

func (p *Printer) Write(content string)

Write adds text to the source-code that is currently build

type Printermode added in v0.0.14

type Printermode int

Printermode describes various modes for the printer

const (
	// PrintermodeReadable inserts spaces the improve readability
	PrintermodeReadable Printermode = 0
	// PrintermodeCompact inserts only spaces that are reasonably necessary
	PrintermodeCompact Printermode = 1
)

type YololParserFunctions

type YololParserFunctions interface {
	ParseStatement() ast.Statement
	ParsePreOrPostOperation() ast.Statement
	ParseGoto() ast.Statement
	ParseAssignment() ast.Statement
	ParseIf() ast.Statement
	ParseExpression() ast.Expression
	ParseLogicExpression() ast.Expression
	ParseCompareExpression() ast.Expression
	ParseSumExpression() ast.Expression
	ParseProdExpression() ast.Expression
	ParseUnaryExpression() ast.Expression
	ParseBracketExpression() ast.Expression
	ParseSingleExpression() ast.Expression
	ParsePreOpExpression() ast.Expression
	ParsePostOpExpression() ast.Expression
}

YololParserFunctions is used together with Parser.This to allow 'subclasses' to override 'virtual functions'

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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