parser

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ASSIGN     = "="
	INCREMENT  = "++"
	DECREMENT  = "--"
	ADDASSIGN  = "+="
	SUBASSIGN  = "-="
	DIVASSIGN  = "/="
	MODASSIGN  = "%="
	QOTASSIGN  = "//="
	MULTASSIGN = "*="
)
View Source
const (
	Int        = "int"
	String     = "string"
	Char       = "char"
	Float      = "float"
	Bool       = "bool"
	Map        = "map"
	Function   = "function"
	ArrayStart = "["
	Any        = "any"

	// keywords
	Var    = "var"
	Return = "return"
	Range  = "range"
	Import = "import"
	For    = "for"
	While  = "while"
	If     = "if"
	Else   = "else"
	Null   = "null"
	Struct = "struct"
	Murloc = "mgrlmgrl"

	// built-in functions
	TypeOf = "typeOf"
	Eval   = "eval"
	Len    = "len"
	SizeOf = "sizeOf"
	Append = "append"
)
View Source
const (
	LowestPrecedence  = 0
	HighestPrecedence = 7
)

Variables

View Source
var (
	Keywords = map[string]interface{}{
		Var:      nil,
		Function: nil,
		Return:   nil,
		Range:    nil,
		Import:   nil,
		For:      nil,
		While:    nil,
		If:       nil,
		Else:     nil,
		Null:     nil,
		Any:      nil,
		Struct:   nil,
		Murloc:   nil,
	}
	BuiltInFunctions = map[string]interface{}{
		TypeOf: nil,
		Eval:   nil,
		Len:    nil,
		SizeOf: nil,
		Append: nil,
	}
	VarTypes = map[string]interface{}{
		Int:        nil,
		Float:      nil,
		String:     nil,
		Char:       nil,
		Bool:       nil,
		Map:        nil,
		Function:   nil,
		ArrayStart: nil,
		Any:        nil,
	}
	DefaultVarTypes = map[string]interface{}{
		Int:        nil,
		Float:      nil,
		String:     nil,
		Char:       nil,
		Bool:       nil,
		Map:        nil,
		Function:   nil,
		ArrayStart: nil,
		Any:        nil,
	}
)
View Source
var (
	AssignOperators = map[string]interface{}{
		ASSIGN:     nil,
		INCREMENT:  nil,
		DECREMENT:  nil,
		ADDASSIGN:  nil,
		SUBASSIGN:  nil,
		DIVASSIGN:  nil,
		MODASSIGN:  nil,
		QOTASSIGN:  nil,
		MULTASSIGN: nil,
	}
)

Functions

func DuplicateParam

func DuplicateParam(params []FunctionParams, newParam string) bool

DuplicateParam checks if a parameter is already in the list of function parameters

func GetPackageNameByPath

func GetPackageNameByPath(path string) string

func TokenPrecedence

func TokenPrecedence(tok lexer.Token) int

Types

type AST

type AST struct {
	Operations []Node
}

The AST struct contains all the information needed for the interpreter to run

type AnonymousFunctionCallExpr

type AnonymousFunctionCallExpr struct {
	AnonymousFunction AnonymousFunctionExpr
	LeftParen         lexer.Token
	RightParen        lexer.Token
	Args              []Expr
}

func (AnonymousFunctionCallExpr) EndLine

func (f AnonymousFunctionCallExpr) EndLine() int

func (AnonymousFunctionCallExpr) EndPos

func (f AnonymousFunctionCallExpr) EndPos() int

func (AnonymousFunctionCallExpr) StartLine

func (f AnonymousFunctionCallExpr) StartLine() int

func (AnonymousFunctionCallExpr) StartPos

func (f AnonymousFunctionCallExpr) StartPos() int

type AnonymousFunctionExpr

type AnonymousFunctionExpr struct {
	FunctionToken lexer.Token
	Prototype     FunctionPrototype
	Body          []Node
}

func (AnonymousFunctionExpr) EndLine

func (f AnonymousFunctionExpr) EndLine() int

func (AnonymousFunctionExpr) EndPos

func (f AnonymousFunctionExpr) EndPos() int

func (AnonymousFunctionExpr) StartLine

func (f AnonymousFunctionExpr) StartLine() int

func (AnonymousFunctionExpr) StartPos

func (f AnonymousFunctionExpr) StartPos() int

type ArrayLiteral

type ArrayLiteral struct {
	LBRACKET lexer.Token
	Values   []Expr
	RBRACKET lexer.Token
}

func (ArrayLiteral) EndLine

func (p ArrayLiteral) EndLine() int

func (ArrayLiteral) EndPos

func (p ArrayLiteral) EndPos() int

func (ArrayLiteral) StartLine

func (p ArrayLiteral) StartLine() int

func (ArrayLiteral) StartPos

func (p ArrayLiteral) StartPos() int

type BinaryExpr

type BinaryExpr struct {
	LeftExpr  Expr
	Operator  lexer.Token
	RightExpr Expr
}

BinaryExpr is a struct that defines a binary operation between two expressions

func (BinaryExpr) EndLine

func (b BinaryExpr) EndLine() int

func (BinaryExpr) EndPos

func (b BinaryExpr) EndPos() int

func (BinaryExpr) StartLine

func (b BinaryExpr) StartLine() int

func (BinaryExpr) StartPos

func (b BinaryExpr) StartPos() int

type BlockScopeStmt

type BlockScopeStmt struct {
	LeftBrace  lexer.Token
	RightBrace lexer.Token
	Body       []Node
}

func (BlockScopeStmt) EndLine

func (b BlockScopeStmt) EndLine() int

func (BlockScopeStmt) EndPos

func (b BlockScopeStmt) EndPos() int

func (BlockScopeStmt) StartLine

func (b BlockScopeStmt) StartLine() int

func (BlockScopeStmt) StartPos

func (b BlockScopeStmt) StartPos() int

type Decl

type Decl interface {
	Node
	// contains filtered or unexported methods
}

Decl is a declaration

type ElseStmt

type ElseStmt struct {
	ElseToken  lexer.Token
	LeftBrace  lexer.Token
	RightBrace lexer.Token
	Body       []Node
	IfStmt     *IfStmt
}

func (ElseStmt) EndLine

func (e ElseStmt) EndLine() int

func (ElseStmt) EndPos

func (e ElseStmt) EndPos() int

func (ElseStmt) StartLine

func (e ElseStmt) StartLine() int

func (ElseStmt) StartPos

func (e ElseStmt) StartPos() int

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

Expr is an expression

type File

type File struct {
	ParseTree *AST
	Imports   []string
	// TODO: use a map instead of a slice for better performance
	Dependencies     []string
	VariableDecl     []string
	StructInstances  []string
	FunctionDecl     []string
	ConsumedComments []string
	Trace            string
}

func (*File) AddDependency

func (f *File) AddDependency(dep string) error

AddDependency adds a new dependency to the file that is currently being parsed

func (*File) AddImport

func (f *File) AddImport(imp string)

func (*File) ConsumeComments

func (f *File) ConsumeComments(tokens []lexer.Token) []lexer.Token

ConsumeComments examines all the tokens, consumes them and deletes them from the token slice

func (*File) DepChecker

func (f *File) DepChecker() (bool, []string)

DepChecker checks if all dependencies in the current file are resolved by the specified imports

func (*File) IsImported

func (f *File) IsImported(imp string) bool

func (*File) RemoveDependency

func (f *File) RemoveDependency(dep string)

type ForStmt

type ForStmt struct {
	ForToken             lexer.Token
	LeftParen            lexer.Token
	RightParen           lexer.Token
	InitDecl             Decl
	CondExpr             Expr
	PostAssignStmt       Stmt
	KeyToken, ValueToken lexer.Token
	RangeToken           lexer.Token
	RangeExpr            Expr
	LeftBrace            lexer.Token
	RightBrace           lexer.Token
	Body                 []Node
}

func (ForStmt) EndLine

func (f ForStmt) EndLine() int

func (ForStmt) EndPos

func (f ForStmt) EndPos() int

func (ForStmt) StartLine

func (f ForStmt) StartLine() int

func (ForStmt) StartPos

func (f ForStmt) StartPos() int

type FunctionCallExpr

type FunctionCallExpr struct {
	FunctionCallToken lexer.Token
	Name              string
	LeftParen         lexer.Token
	RightParen        lexer.Token
	Args              []Expr
}

func (FunctionCallExpr) EndLine

func (f FunctionCallExpr) EndLine() int

func (FunctionCallExpr) EndPos

func (f FunctionCallExpr) EndPos() int

func (FunctionCallExpr) StartLine

func (f FunctionCallExpr) StartLine() int

func (FunctionCallExpr) StartPos

func (f FunctionCallExpr) StartPos() int

type FunctionDecl

type FunctionDecl struct {
	FunctionToken lexer.Token
	Name          string
	Prototype     FunctionPrototype
	Body          []Node
}

func (FunctionDecl) EndLine

func (f FunctionDecl) EndLine() int

func (FunctionDecl) EndPos

func (f FunctionDecl) EndPos() int

func (FunctionDecl) StartLine

func (f FunctionDecl) StartLine() int

func (FunctionDecl) StartPos

func (f FunctionDecl) StartPos() int

type FunctionParams

type FunctionParams struct {
	Name string
	Type string
}

type FunctionPrototype

type FunctionPrototype struct {
	LeftParamParen  lexer.Token
	RightParamParen lexer.Token
	Parameters      []FunctionParams
	LeftRetsParen   lexer.Token
	RightRetsParen  lexer.Token
	ReturnTypes     []string
	LeftBrace       lexer.Token
	RightBrace      lexer.Token
}

type IfStmt

type IfStmt struct {
	IfToken    lexer.Token
	LeftParen  lexer.Token
	RightParen lexer.Token
	Cond       Expr
	LeftBrace  lexer.Token
	RightBrace lexer.Token
	Body       []Node
	ElseStmt   *ElseStmt
}

func (IfStmt) EndLine

func (i IfStmt) EndLine() int

func (IfStmt) EndPos

func (i IfStmt) EndPos() int

func (IfStmt) StartLine

func (i IfStmt) StartLine() int

func (IfStmt) StartPos

func (i IfStmt) StartPos() int

type ImportStmt

type ImportStmt struct {
	ImportToken lexer.Token
	ModulePath  string
}

func (ImportStmt) EndLine

func (i ImportStmt) EndLine() int

func (ImportStmt) EndPos

func (i ImportStmt) EndPos() int

func (ImportStmt) StartLine

func (i ImportStmt) StartLine() int

func (ImportStmt) StartPos

func (i ImportStmt) StartPos() int

type IndexableAccessExpr

type IndexableAccessExpr struct {
	VariableToken lexer.Token
	VariableName  string
	Indexes       []Expr
	LastBracket   lexer.Token
}

func (IndexableAccessExpr) EndLine

func (a IndexableAccessExpr) EndLine() int

func (IndexableAccessExpr) EndPos

func (a IndexableAccessExpr) EndPos() int

func (IndexableAccessExpr) StartLine

func (a IndexableAccessExpr) StartLine() int

func (IndexableAccessExpr) StartPos

func (a IndexableAccessExpr) StartPos() int

type Literal

type Literal struct {
	Token lexer.Token
	Type  string
	Value string
}

Literal is a struct that defines a literal value for all types

func (Literal) EndLine

func (l Literal) EndLine() int

func (Literal) EndPos

func (l Literal) EndPos() int

func (Literal) StartLine

func (l Literal) StartLine() int

func (Literal) StartPos

func (l Literal) StartPos() int

type MapLiteral

type MapLiteral struct {
	LBRACE lexer.Token
	Keys   []Expr
	Values []Expr
	RBRACE lexer.Token
}

func (MapLiteral) EndLine

func (m MapLiteral) EndLine() int

func (MapLiteral) EndPos

func (m MapLiteral) EndPos() int

func (MapLiteral) StartLine

func (m MapLiteral) StartLine() int

func (MapLiteral) StartPos

func (m MapLiteral) StartPos() int

type MurlocStmt

type MurlocStmt struct {
	MurlocToken lexer.Token
}

func (MurlocStmt) EndLine

func (m MurlocStmt) EndLine() int

func (MurlocStmt) EndPos

func (m MurlocStmt) EndPos() int

func (MurlocStmt) StartLine

func (m MurlocStmt) StartLine() int

func (MurlocStmt) StartPos

func (m MurlocStmt) StartPos() int

type Node

type Node interface {
	StartPos() int
	EndPos() int
	StartLine() int
	EndLine() int
}

Node is the interface that all nodes in the AST must implement

type ParenExpr

type ParenExpr struct {
	Lparen     lexer.Token
	Expression Expr
	Rparen     lexer.Token
}

ParenExpr is a struct that defines a parenthesized expression

func (ParenExpr) EndLine

func (p ParenExpr) EndLine() int

func (ParenExpr) EndPos

func (p ParenExpr) EndPos() int

func (ParenExpr) StartLine

func (p ParenExpr) StartLine() int

func (ParenExpr) StartPos

func (p ParenExpr) StartPos() int

type Parser

type Parser struct {
	ErrorHandler *errorHandler.ErrorHandler
	Tokens       []lexer.Token
	TokenIndex   int
	CurrentToken lexer.Token
	CurrentFile  *File
	IsEndOfBrace bool
	VarTypes     map[string]interface{}
}

func (*Parser) Back

func (p *Parser) Back()

Back moves the parser back one token

func (*Parser) DisableEOLChecking

func (p *Parser) DisableEOLChecking()

DisableEOLChecking disables the EOL checking for the parser to allow for semicolon-less syntax on conditionals statements and loops

func (*Parser) HandleError

func (p *Parser) HandleError(message string)

HandleError handles an error level error

func (*Parser) HandleFatal

func (p *Parser) HandleFatal(message string)

HandleFatal handles a fatal level error

func (*Parser) HandleWarning

func (p *Parser) HandleWarning(message string)

HandleWarning handles a warning level error

func (*Parser) MultiBack

func (p *Parser) MultiBack(steps int)

MultiBack moves the parser back n times given as parameter

func (*Parser) MultiStep

func (p *Parser) MultiStep(steps int)

MultiStep moves the parser forward n times givens as parameter

func (*Parser) Parse

func (p *Parser) Parse() *File

Parse is the main function of the parser. It parses the tokens within itself and returns a File struct containing the AST and the parsed declarations of variables and functions It also runs the dependency checker to find any missing dependencies and notifies the user

func (*Parser) ParseAnonymousFunctionExpr

func (p *Parser) ParseAnonymousFunctionExpr() Expr

func (*Parser) ParseArrayLiteral

func (p *Parser) ParseArrayLiteral() Expr

ParseArrayLiteral parses an array literal

func (*Parser) ParseArrayType

func (p *Parser) ParseArrayType() string

ParseArrayType parses an array type

func (*Parser) ParseBinaryExpr

func (p *Parser) ParseBinaryExpr(Lhs Expr, precedence int) Expr

ParseBinaryExpr parses a binary expression with the given precedence

func (*Parser) ParseBlock

func (p *Parser) ParseBlock() Node

ParseBlock parses a block scope

func (*Parser) ParseBody

func (p *Parser) ParseBody() []Node

ParseBody parses a body of a function,a loop or a conditional statement

func (*Parser) ParseElseStmt

func (p *Parser) ParseElseStmt() *ElseStmt

ParseElseStmt parses an else statement

func (*Parser) ParseExpr

func (p *Parser) ParseExpr() Expr

ParseExpr parse an expression

func (*Parser) ParseFile

func (p *Parser) ParseFile() *File

ParseFile parses the tokens and returns a File struct containing the AST and the parsed declarations of variables and functions

func (*Parser) ParseForStmt

func (p *Parser) ParseForStmt() Stmt

ParseForStmt parses a for statement

func (*Parser) ParseFunctionCallExpr

func (p *Parser) ParseFunctionCallExpr() Expr

ParseFunctionCallExpr parse a function call expression

func (*Parser) ParseFunctionDecl

func (p *Parser) ParseFunctionDecl() Node

ParseFunctionDecl parses a function declaration

func (*Parser) ParseFunctionType

func (p *Parser) ParseFunctionType() string

func (*Parser) ParseIdent

func (p *Parser) ParseIdent() Node

ParseIdent parses an identifier and checking if it is function or method call,a variable declaration or an indexable variable access

func (*Parser) ParseIfStmt

func (p *Parser) ParseIfStmt() Stmt

ParseIfStmt parses an if statement

func (*Parser) ParseImplicitVariableDecl

func (p *Parser) ParseImplicitVariableDecl() Decl

func (*Parser) ParseImportStmt

func (p *Parser) ParseImportStmt() Stmt

ParseImportStmt parses an import statement

func (*Parser) ParseIndexableAccessExpr

func (p *Parser) ParseIndexableAccessExpr() Expr

ParseIndexableAccessExpr parses an indexable variable access expression

func (*Parser) ParseKeyword

func (p *Parser) ParseKeyword() Node

ParseKeyword parses a keyword and calls the appropriate parsing function

func (*Parser) ParseLiteral

func (p *Parser) ParseLiteral() Expr

ParseLiteral parses a literal

func (*Parser) ParseMapLiteral

func (p *Parser) ParseMapLiteral() Expr

ParseMapLiteral parses a map literal

func (*Parser) ParseMapType

func (p *Parser) ParseMapType() string

ParseMapType parses a map type

func (*Parser) ParseNode

func (p *Parser) ParseNode() Node

ParseNode parses a node from the current token deciding checking if the token is text or other

func (*Parser) ParseOperand

func (p *Parser) ParseOperand() Expr

ParseOperand parses an operand

func (*Parser) ParseParenExpr

func (p *Parser) ParseParenExpr() Expr

ParseParenExpr parses a parenthesized expression

func (*Parser) ParsePrimaryExpr

func (p *Parser) ParsePrimaryExpr(exp Expr) Expr

ParsePrimaryExpr parses a primary expression

func (*Parser) ParsePrototype

func (p *Parser) ParsePrototype() FunctionPrototype

func (*Parser) ParseReturnStmt

func (p *Parser) ParseReturnStmt() Node

ParseReturnStmt parses a return statement

func (*Parser) ParseSelector

func (p *Parser) ParseSelector(x Expr) Expr

func (*Parser) ParseStructDecl

func (p *Parser) ParseStructDecl() Node

ParseStructDecl parses a struct declaration

func (*Parser) ParseStructField

func (p *Parser) ParseStructField() StructField

ParseStructField parses a struct field

func (*Parser) ParseStructInstantiation

func (p *Parser) ParseStructInstantiation() StructInstantiationExpr

func (*Parser) ParseText

func (p *Parser) ParseText() Node

ParseText parses a text node and checking if it is a keyword from Keywords

func (*Parser) ParseType

func (p *Parser) ParseType() (string, bool)

ParseType parses a valid type

func (*Parser) ParseUnaryExpr

func (p *Parser) ParseUnaryExpr() Expr

ParseUnaryExpr parses a unary expression

func (*Parser) ParseVariableAccess

func (p *Parser) ParseVariableAccess() Expr

ParseVariableAccess parses a variable access

func (*Parser) ParseVariableAssign

func (p *Parser) ParseVariableAssign(lhs Expr) Stmt

ParseVariableAssign parses a variable assignment

func (*Parser) ParseVariableAssignSide

func (p *Parser) ParseVariableAssignSide() []Expr

ParseVariableAssignSide parses the right hand side or the left hand side of a variable assignment

func (*Parser) ParseVariableDecl

func (p *Parser) ParseVariableDecl() Decl

ParseVariableDecl parses a variable declaration

func (*Parser) ParseWhileStmt

func (p *Parser) ParseWhileStmt() Stmt

ParseWhileStmt parses a while statement

func (*Parser) Peek

func (p *Parser) Peek(lookAhead int) lexer.Token

Peek returns the token n steps ahead of the current token without moving the parser

func (*Parser) PrintBacktrace

func (p *Parser) PrintBacktrace()

PrintBacktrace prints the last 10 tokens for debugging purposes

func (*Parser) Step

func (p *Parser) Step()

Step moves the parser to the next token

type ReturnStmt

type ReturnStmt struct {
	ReturnToken  lexer.Token
	ReturnValues []Expr
}

func (ReturnStmt) EndLine

func (r ReturnStmt) EndLine() int

func (ReturnStmt) EndPos

func (r ReturnStmt) EndPos() int

func (ReturnStmt) StartLine

func (r ReturnStmt) StartLine() int

func (ReturnStmt) StartPos

func (r ReturnStmt) StartPos() int

type SelectorExpr

type SelectorExpr struct {
	Field lexer.Token
	Expr  Expr
	Sel   Expr
}

func (SelectorExpr) EndLine

func (s SelectorExpr) EndLine() int

func (SelectorExpr) EndPos

func (s SelectorExpr) EndPos() int

func (SelectorExpr) StartLine

func (s SelectorExpr) StartLine() int

func (SelectorExpr) StartPos

func (s SelectorExpr) StartPos() int

type Stmt

type Stmt interface {
	Node
	// contains filtered or unexported methods
}

Stmt is a statement

type StructDecl

type StructDecl struct {
	StructToken lexer.Token
	Name        string
	LeftBrace   lexer.Token
	Fields      []StructField
	RightBrace  lexer.Token
}

func (StructDecl) EndLine

func (s StructDecl) EndLine() int

func (StructDecl) EndPos

func (s StructDecl) EndPos() int

func (StructDecl) StartLine

func (s StructDecl) StartLine() int

func (StructDecl) StartPos

func (s StructDecl) StartPos() int

type StructField

type StructField struct {
	Name string
	Type string
}

type StructInstantiationExpr

type StructInstantiationExpr struct {
	StructNameToken lexer.Token
	Name            string
	LeftBrace       lexer.Token
	RightBrace      lexer.Token
	Args            []Expr
}

func (StructInstantiationExpr) EndLine

func (s StructInstantiationExpr) EndLine() int

func (StructInstantiationExpr) EndPos

func (s StructInstantiationExpr) EndPos() int

func (StructInstantiationExpr) StartLine

func (s StructInstantiationExpr) StartLine() int

func (StructInstantiationExpr) StartPos

func (s StructInstantiationExpr) StartPos() int

type UnaryExpr

type UnaryExpr struct {
	Operator  lexer.Token
	RightExpr Expr
}

UnaryExpr is a struct that defines a unary operation on an expression

func (UnaryExpr) EndLine

func (u UnaryExpr) EndLine() int

func (UnaryExpr) EndPos

func (u UnaryExpr) EndPos() int

func (UnaryExpr) StartLine

func (u UnaryExpr) StartLine() int

func (UnaryExpr) StartPos

func (u UnaryExpr) StartPos() int

type VariableAssignStmt

type VariableAssignStmt struct {
	VarToken lexer.Token
	Names    []Expr
	Operator string
	Values   []Expr
}

func (VariableAssignStmt) EndLine

func (v VariableAssignStmt) EndLine() int

func (VariableAssignStmt) EndPos

func (v VariableAssignStmt) EndPos() int

func (VariableAssignStmt) StartLine

func (v VariableAssignStmt) StartLine() int

func (VariableAssignStmt) StartPos

func (v VariableAssignStmt) StartPos() int

type VariableDecl

type VariableDecl struct {
	VarToken lexer.Token
	Name     string
	Type     string
	Value    Expr
}

func (VariableDecl) EndLine

func (v VariableDecl) EndLine() int

func (VariableDecl) EndPos

func (v VariableDecl) EndPos() int

func (VariableDecl) StartLine

func (v VariableDecl) StartLine() int

func (VariableDecl) StartPos

func (v VariableDecl) StartPos() int

type WhileStmt

type WhileStmt struct {
	WhileToken lexer.Token
	LeftParen  lexer.Token
	RightParen lexer.Token
	Cond       Expr
	LeftBrace  lexer.Token
	RightBrace lexer.Token
	Body       []Node
}

func (WhileStmt) EndLine

func (w WhileStmt) EndLine() int

func (WhileStmt) EndPos

func (w WhileStmt) EndPos() int

func (WhileStmt) StartLine

func (w WhileStmt) StartLine() int

func (WhileStmt) StartPos

func (w WhileStmt) StartPos() int

Jump to

Keyboard shortcuts

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