diff options
Diffstat (limited to 'vendor/github.com/d5/tengo/compiler')
6 files changed, 77 insertions, 52 deletions
diff --git a/vendor/github.com/d5/tengo/compiler/compiler.go b/vendor/github.com/d5/tengo/compiler/compiler.go index 141ea8fd..d8bc05fd 100644 --- a/vendor/github.com/d5/tengo/compiler/compiler.go +++ b/vendor/github.com/d5/tengo/compiler/compiler.go @@ -5,11 +5,11 @@ import ( "io" "reflect" + "github.com/d5/tengo" "github.com/d5/tengo/compiler/ast" "github.com/d5/tengo/compiler/source" "github.com/d5/tengo/compiler/token" "github.com/d5/tengo/objects" - "github.com/d5/tengo/stdlib" ) // Compiler compiles the AST into a bytecode. @@ -54,9 +54,6 @@ func NewCompiler(file *source.File, symbolTable *SymbolTable, constants []object // builtin modules if builtinModules == nil { builtinModules = make(map[string]bool) - for name := range stdlib.Modules { - builtinModules[name] = true - } } return &Compiler{ @@ -195,6 +192,10 @@ func (c *Compiler) Compile(node ast.Node) error { } case *ast.StringLit: + if len(node.Value) > tengo.MaxStringLen { + return c.error(node, objects.ErrStringLimit) + } + c.emit(node, OpConstant, c.addConstant(&objects.String{Value: node.Value})) case *ast.CharLit: @@ -332,6 +333,9 @@ func (c *Compiler) Compile(node ast.Node) error { case *ast.MapLit: for _, elt := range node.Elements { // key + if len(elt.Key) > tengo.MaxStringLen { + return c.error(node, objects.ErrStringLimit) + } c.emit(node, OpConstant, c.addConstant(&objects.String{Value: elt.Key})) // value @@ -507,6 +511,10 @@ func (c *Compiler) Compile(node ast.Node) error { case *ast.ImportExpr: if c.builtinModules[node.ModuleName] { + if len(node.ModuleName) > tengo.MaxStringLen { + return c.error(node, objects.ErrStringLimit) + } + c.emit(node, OpConstant, c.addConstant(&objects.String{Value: node.ModuleName})) c.emit(node, OpGetBuiltinModule) } else { @@ -610,6 +618,14 @@ func (c *Compiler) fork(file *source.File, moduleName string, symbolTable *Symbo return child } +func (c *Compiler) error(node ast.Node, err error) error { + return &Error{ + fileSet: c.file.Set(), + node: node, + error: err, + } +} + func (c *Compiler) errorf(node ast.Node, format string, args ...interface{}) error { return &Error{ fileSet: c.file.Set(), diff --git a/vendor/github.com/d5/tengo/compiler/compiler_module.go b/vendor/github.com/d5/tengo/compiler/compiler_module.go index 8f63abb3..d069bfab 100644 --- a/vendor/github.com/d5/tengo/compiler/compiler_module.go +++ b/vendor/github.com/d5/tengo/compiler/compiler_module.go @@ -77,11 +77,8 @@ func (c *Compiler) doCompileModule(moduleName string, src []byte) (*objects.Comp symbolTable := NewSymbolTable() // inherit builtin functions - for idx, fn := range objects.Builtins { - s, _, ok := c.symbolTable.Resolve(fn.Name) - if ok && s.Scope == ScopeBuiltin { - symbolTable.DefineBuiltin(idx, fn.Name) - } + for _, sym := range c.symbolTable.BuiltinSymbols() { + symbolTable.DefineBuiltin(sym.Index, sym.Name) } // no global scope for the module diff --git a/vendor/github.com/d5/tengo/compiler/parser/parse_file.go b/vendor/github.com/d5/tengo/compiler/parser/parse_file.go deleted file mode 100644 index 0482c775..00000000 --- a/vendor/github.com/d5/tengo/compiler/parser/parse_file.go +++ /dev/null @@ -1,28 +0,0 @@ -package parser - -import ( - "io" - - "github.com/d5/tengo/compiler/ast" - "github.com/d5/tengo/compiler/source" -) - -// ParseFile parses a file with a given src. -func ParseFile(file *source.File, src []byte, trace io.Writer) (res *ast.File, err error) { - p := NewParser(file, src, trace) - - defer func() { - if e := recover(); e != nil { - if _, ok := e.(bailout); !ok { - panic(e) - } - } - - p.errors.Sort() - err = p.errors.Err() - }() - - res, err = p.ParseFile() - - return -} diff --git a/vendor/github.com/d5/tengo/compiler/parser/parse_source.go b/vendor/github.com/d5/tengo/compiler/parser/parse_source.go index 5d242db4..5c71436d 100644 --- a/vendor/github.com/d5/tengo/compiler/parser/parse_source.go +++ b/vendor/github.com/d5/tengo/compiler/parser/parse_source.go @@ -12,5 +12,6 @@ func ParseSource(filename string, src []byte, trace io.Writer) (res *ast.File, e fileSet := source.NewFileSet() file := fileSet.AddFile(filename, -1, len(src)) - return ParseFile(file, src, trace) + p := NewParser(file, src, trace) + return p.ParseFile() } diff --git a/vendor/github.com/d5/tengo/compiler/parser/parser.go b/vendor/github.com/d5/tengo/compiler/parser/parser.go index 93f04f7c..1f609ab5 100644 --- a/vendor/github.com/d5/tengo/compiler/parser/parser.go +++ b/vendor/github.com/d5/tengo/compiler/parser/parser.go @@ -57,7 +57,18 @@ func NewParser(file *source.File, src []byte, trace io.Writer) *Parser { } // ParseFile parses the source and returns an AST file unit. -func (p *Parser) ParseFile() (*ast.File, error) { +func (p *Parser) ParseFile() (file *ast.File, err error) { + defer func() { + if e := recover(); e != nil { + if _, ok := e.(bailout); !ok { + panic(e) + } + } + + p.errors.Sort() + err = p.errors.Err() + }() + if p.trace { defer un(trace(p, "File")) } @@ -71,10 +82,12 @@ func (p *Parser) ParseFile() (*ast.File, error) { return nil, p.errors.Err() } - return &ast.File{ + file = &ast.File{ InputFile: p.file, Stmts: stmts, - }, nil + } + + return } func (p *Parser) parseExpr() ast.Expr { @@ -1002,16 +1015,26 @@ func (p *Parser) parseMapElementLit() *ast.MapElementLit { defer un(trace(p, "MapElementLit")) } - // key: read identifier token but it's not actually an identifier - ident := p.parseIdent() + pos := p.pos + name := "_" + + if p.token == token.Ident { + name = p.tokenLit + } else if p.token == token.String { + v, _ := strconv.Unquote(p.tokenLit) + name = v + } else { + p.errorExpected(pos, "map key") + } + + p.next() colonPos := p.expect(token.Colon) - valueExpr := p.parseExpr() return &ast.MapElementLit{ - Key: ident.Name, - KeyPos: ident.NamePos, + Key: name, + KeyPos: pos, ColonPos: colonPos, Value: valueExpr, } diff --git a/vendor/github.com/d5/tengo/compiler/symbol_table.go b/vendor/github.com/d5/tengo/compiler/symbol_table.go index da55a826..fb029b31 100644 --- a/vendor/github.com/d5/tengo/compiler/symbol_table.go +++ b/vendor/github.com/d5/tengo/compiler/symbol_table.go @@ -2,12 +2,13 @@ package compiler // SymbolTable represents a symbol table. type SymbolTable struct { - parent *SymbolTable - block bool - store map[string]*Symbol - numDefinition int - maxDefinition int - freeSymbols []*Symbol + parent *SymbolTable + block bool + store map[string]*Symbol + numDefinition int + maxDefinition int + freeSymbols []*Symbol + builtinSymbols []*Symbol } // NewSymbolTable creates a SymbolTable. @@ -37,6 +38,10 @@ func (t *SymbolTable) Define(name string) *Symbol { // DefineBuiltin adds a symbol for builtin function. func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol { + if t.parent != nil { + return t.parent.DefineBuiltin(index, name) + } + symbol := &Symbol{ Name: name, Index: index, @@ -45,6 +50,8 @@ func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol { t.store[name] = symbol + t.builtinSymbols = append(t.builtinSymbols, symbol) + return symbol } @@ -101,6 +108,15 @@ func (t *SymbolTable) FreeSymbols() []*Symbol { return t.freeSymbols } +// BuiltinSymbols returns builtin symbols for the scope. +func (t *SymbolTable) BuiltinSymbols() []*Symbol { + if t.parent != nil { + return t.parent.BuiltinSymbols() + } + + return t.builtinSymbols +} + // Names returns the name of all the symbols. func (t *SymbolTable) Names() []string { var names []string |