parse

package
Version: v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LOWEST      precedence
	LOR         // ||
	LAND        // &&
	EQUALS      // ==
	LESSGREATER // > or <
	SUM         // +
	PRODUCT     // *
	PREFIX      // -X or !X
	CALL        // function(X)
)
View Source
const (
	CURRENT peekNumber = iota
	NEXT
)

Variables

View Source
var TokenTypeMap = map[TokenType]string{
	Illegal: "ILLEGAL",

	Ident:    "IDENT",
	Int:      "INT",
	String:   "STRING",
	Function: "FUNCTION",
	Contract: "CONTRACT",

	IntType:    "INT_TYPE",
	StringType: "STRING_TYPE",
	BoolType:   "BOOL_TYPE",

	Assign:   "ASSIGN",
	Plus:     "PLUS",
	Minus:    "MINUS",
	Bang:     "BANG",
	Asterisk: "ASTERISK",
	Slash:    "SLASH",
	Mod:      "MOD",

	PlusAssign:     "PLUS_ASSIGN",
	MinusAssign:    "MINUS_ASSIGN",
	AsteriskAssign: "ASTERISK_ASSIGN",
	SlashAssign:    "SLASH_ASSIGN",
	ModAssign:      "MOD_ASSIGN",

	Land: "LAND",
	Lor:  "LOR",
	Inc:  "INC",
	Dec:  "DEC",

	LT:     "LT",
	GT:     "GT",
	LTE:    "LTE",
	GTE:    "GTE",
	EQ:     "EQ",
	NOT_EQ: "NOT_EQ",

	Comma: "COMMA",

	Lparen: "LPAREN",
	Rparen: "RPAREN",
	Lbrace: "LBRACE",
	Rbrace: "RBRACE",

	True:   "TRUE",
	False:  "FALSE",
	If:     "IF",
	Else:   "ELSE",
	Return: "RETURN",

	Eof:       "EOF",
	Eol:       "EOL",
	Semicolon: "SEMICOLON",
}

TokenTypeMap mapping TokenType with its string, this helps debugging

Functions

func Parse

func Parse(buf TokenBuffer) (*ast.Contract, error)

Parse creates an abstract syntax tree

Types

type DefaultTokenBuffer

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

DefaultTokenBuffer is implementation for TokenBuffer interface providing buffers to the client.

cur, next work as buffer. Each store token as below

----------------------------------------------

client   <- |  cur  |  next | <-  lexer
======   <- | token | token | <-  =====

----------------------------------------------

func NewTokenBuffer

func NewTokenBuffer(l *Lexer) *DefaultTokenBuffer

func (*DefaultTokenBuffer) Peek

func (b *DefaultTokenBuffer) Peek(n peekNumber) Token

Peek returns token based on the peekNumber, this doesn't change token value

func (*DefaultTokenBuffer) Read

func (b *DefaultTokenBuffer) Read() Token

Read returns current token, then read from lexer then change the cur, next token value

type DupSymError

type DupSymError struct {
	Source Token
}

dupSymError occur when there is duplicated symbol

func (DupSymError) Error

func (e DupSymError) Error() string

type Error

type Error struct {
	Source Token
	Reason string
}

Error contains error which happened during parsing tokens

func (Error) Error

func (e Error) Error() string

type ExpectError

type ExpectError struct {
	Source   Token
	Expected TokenType
}

ExpectError happens during parsing expectNext

func (ExpectError) Error

func (e ExpectError) Error() string

type Lexer

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

func NewLexer

func NewLexer(input string) *Lexer

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken returns the next token from the input. Called by the parser, not in the lexing goroutine.

type NotExistSymError

type NotExistSymError struct {
	Source Token
}

NotExistSymError occur when there is no target symbol

func (NotExistSymError) Error

func (e NotExistSymError) Error() string

type Pos

type Pos int

Pos represents a byte position in the original input text from which this template was parsed.

type PrefixError

type PrefixError struct {
	Source Token
	Right  ast.Expression
}

prefixError occur when there is invalid prefix type

func (PrefixError) Error

func (e PrefixError) Error() string

type Token

type Token struct {
	Type   TokenType
	Val    string
	Column Pos
	Line   int
}

func (Token) String

func (t Token) String() string

type TokenBuffer

type TokenBuffer interface {
	// Read retrieve token from buffer. and change the
	// buffer states
	Read() Token

	// Peek take token as many as n from buffer but not change the
	// buffer states
	Peek(n peekNumber) Token
}

TokenBuffer provide tokenized token, we can read from this buffer or just peek the token

type TokenType

type TokenType int
const (
	// ILLEGAL Token
	Illegal TokenType = iota

	// Identifiers + literals
	Ident    // add, foobar, x, y, ...
	Int      // 1343456
	String   // "hello world"
	Function // func
	Contract // contract

	IntType
	StringType
	BoolType
	VoidType

	Assign   // =
	Plus     // +
	Minus    // -
	Bang     // !
	Asterisk // *
	Slash    // /
	Mod      // %

	PlusAssign     // +=
	MinusAssign    // -=
	AsteriskAssign // *=
	SlashAssign    // /=
	ModAssign      // %=

	Land // &&
	Lor  // ||
	Inc  // ++
	Dec  //--

	LT     // <
	GT     // >
	LTE    // <=
	GTE    // >=
	EQ     // ==
	NOT_EQ // !=

	Comma // ,

	Lparen // (
	Rparen // )
	Lbrace // {
	Rbrace // }

	True   // true
	False  // false
	If     // if
	Else   // else
	Return // return
	Eof    // end of file
	Eol    // end of line
	Semicolon
)

func LookupIdent

func LookupIdent(ident string) TokenType

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL