2015-01-23 23:15:10 +01:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-01-03 00:19:28 +01:00
|
|
|
"sort"
|
2014-05-28 01:34:41 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/scanner"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errTooManyErrors = errors.New("too many errors")
|
|
|
|
|
|
|
|
const maxErrors = 1
|
|
|
|
|
|
|
|
type ParseError struct {
|
|
|
|
Err error
|
|
|
|
Pos scanner.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ParseError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s", e.Pos, e.Err)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
type File struct {
|
|
|
|
Defs []Definition
|
|
|
|
Comments []Comment
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(filename string, r io.Reader, scope *Scope) (file *File, errs []error) {
|
|
|
|
|
2015-01-03 00:19:28 +01:00
|
|
|
p := newParser(r, scope)
|
2014-05-28 01:34:41 +02:00
|
|
|
p.scanner.Filename = filename
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if r == errTooManyErrors {
|
|
|
|
errs = p.errors
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
defs := p.parseDefinitions()
|
2014-05-28 01:34:41 +02:00
|
|
|
p.accept(scanner.EOF)
|
|
|
|
errs = p.errors
|
2015-01-08 23:56:03 +01:00
|
|
|
comments := p.comments
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
return &File{
|
|
|
|
Defs: defs,
|
|
|
|
Comments: comments,
|
|
|
|
}, errs
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type parser struct {
|
2015-01-08 23:56:03 +01:00
|
|
|
scanner scanner.Scanner
|
|
|
|
tok rune
|
|
|
|
errors []error
|
|
|
|
scope *Scope
|
|
|
|
parseComments bool
|
|
|
|
comments []Comment
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-01-03 00:19:28 +01:00
|
|
|
func newParser(r io.Reader, scope *Scope) *parser {
|
2014-05-28 01:34:41 +02:00
|
|
|
p := &parser{}
|
2015-01-03 00:19:28 +01:00
|
|
|
p.scope = scope
|
2015-01-08 23:56:03 +01:00
|
|
|
p.parseComments = true
|
2014-05-28 01:34:41 +02:00
|
|
|
p.scanner.Init(r)
|
|
|
|
p.scanner.Error = func(sc *scanner.Scanner, msg string) {
|
|
|
|
p.errorf(msg)
|
|
|
|
}
|
|
|
|
p.scanner.Mode = scanner.ScanIdents | scanner.ScanStrings |
|
2015-01-08 23:56:03 +01:00
|
|
|
scanner.ScanRawStrings | scanner.ScanComments
|
|
|
|
if !p.parseComments {
|
|
|
|
p.scanner.Mode |= scanner.SkipComments
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
p.next()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) errorf(format string, args ...interface{}) {
|
|
|
|
pos := p.scanner.Position
|
|
|
|
if !pos.IsValid() {
|
|
|
|
pos = p.scanner.Pos()
|
|
|
|
}
|
|
|
|
err := &ParseError{
|
|
|
|
Err: fmt.Errorf(format, args...),
|
|
|
|
Pos: pos,
|
|
|
|
}
|
|
|
|
p.errors = append(p.errors, err)
|
|
|
|
if len(p.errors) >= maxErrors {
|
|
|
|
panic(errTooManyErrors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) accept(toks ...rune) bool {
|
|
|
|
for _, tok := range toks {
|
|
|
|
if p.tok != tok {
|
|
|
|
p.errorf("expected %s, found %s", scanner.TokenString(tok),
|
|
|
|
scanner.TokenString(p.tok))
|
|
|
|
return false
|
|
|
|
}
|
2015-01-08 23:56:03 +01:00
|
|
|
p.next()
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) next() {
|
|
|
|
if p.tok != scanner.EOF {
|
|
|
|
p.tok = p.scanner.Scan()
|
2015-01-08 23:56:03 +01:00
|
|
|
for p.tok == scanner.Comment {
|
|
|
|
p.comments = append(p.comments, Comment{p.scanner.TokenText(), p.scanner.Position})
|
|
|
|
p.tok = p.scanner.Scan()
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) parseDefinitions() (defs []Definition) {
|
|
|
|
for {
|
|
|
|
switch p.tok {
|
|
|
|
case scanner.Ident:
|
|
|
|
ident := p.scanner.TokenText()
|
|
|
|
pos := p.scanner.Position
|
|
|
|
|
|
|
|
p.accept(scanner.Ident)
|
|
|
|
|
|
|
|
switch p.tok {
|
|
|
|
case '=':
|
|
|
|
defs = append(defs, p.parseAssignment(ident, pos))
|
2015-01-15 02:04:13 +01:00
|
|
|
case '{', '(':
|
2014-05-28 01:34:41 +02:00
|
|
|
defs = append(defs, p.parseModule(ident, pos))
|
|
|
|
default:
|
2015-01-15 02:04:13 +01:00
|
|
|
p.errorf("expected \"=\" or \"{\" or \"(\", found %s",
|
2014-05-28 01:34:41 +02:00
|
|
|
scanner.TokenString(p.tok))
|
|
|
|
}
|
|
|
|
case scanner.EOF:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
p.errorf("expected assignment or module definition, found %s",
|
|
|
|
scanner.TokenString(p.tok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) parseAssignment(name string,
|
2015-01-08 23:56:03 +01:00
|
|
|
namePos scanner.Position) (assignment *Assignment) {
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
assignment = new(Assignment)
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
pos := p.scanner.Position
|
2014-05-28 01:34:41 +02:00
|
|
|
if !p.accept('=') {
|
|
|
|
return
|
|
|
|
}
|
2015-01-03 00:47:54 +01:00
|
|
|
value := p.parseExpression()
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
assignment.Name = Ident{name, namePos}
|
2014-05-28 01:34:41 +02:00
|
|
|
assignment.Value = value
|
|
|
|
assignment.Pos = pos
|
|
|
|
|
2015-01-03 00:19:28 +01:00
|
|
|
if p.scope != nil {
|
|
|
|
p.scope.Add(assignment)
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) parseModule(typ string,
|
2015-01-08 23:56:03 +01:00
|
|
|
typPos scanner.Position) (module *Module) {
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
module = new(Module)
|
2015-01-15 02:04:13 +01:00
|
|
|
compat := false
|
2015-01-08 23:56:03 +01:00
|
|
|
lbracePos := p.scanner.Position
|
2015-01-15 02:04:13 +01:00
|
|
|
if p.tok == '{' {
|
|
|
|
compat = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.accept(p.tok) {
|
2014-05-28 01:34:41 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-15 02:04:13 +01:00
|
|
|
properties := p.parsePropertyList(true, compat)
|
2015-01-08 23:56:03 +01:00
|
|
|
rbracePos := p.scanner.Position
|
2015-01-15 02:04:13 +01:00
|
|
|
if !compat {
|
|
|
|
p.accept(')')
|
|
|
|
} else {
|
|
|
|
p.accept('}')
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
module.Type = Ident{typ, typPos}
|
2014-05-28 01:34:41 +02:00
|
|
|
module.Properties = properties
|
2015-01-08 23:56:03 +01:00
|
|
|
module.LbracePos = lbracePos
|
|
|
|
module.RbracePos = rbracePos
|
2014-05-28 01:34:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-15 02:04:13 +01:00
|
|
|
func (p *parser) parsePropertyList(isModule, compat bool) (properties []*Property) {
|
2014-05-28 01:34:41 +02:00
|
|
|
for p.tok == scanner.Ident {
|
2015-01-15 02:04:13 +01:00
|
|
|
property := p.parseProperty(isModule, compat)
|
2014-05-28 01:34:41 +02:00
|
|
|
properties = append(properties, property)
|
|
|
|
|
|
|
|
if p.tok != ',' {
|
|
|
|
// There was no comma, so the list is done.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
p.accept(',')
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-15 02:04:13 +01:00
|
|
|
func (p *parser) parseProperty(isModule, compat bool) (property *Property) {
|
2014-05-28 01:34:41 +02:00
|
|
|
property = new(Property)
|
|
|
|
|
|
|
|
name := p.scanner.TokenText()
|
2015-01-08 23:56:03 +01:00
|
|
|
namePos := p.scanner.Position
|
|
|
|
p.accept(scanner.Ident)
|
2014-05-28 01:34:41 +02:00
|
|
|
pos := p.scanner.Position
|
2015-01-15 02:04:13 +01:00
|
|
|
|
|
|
|
if isModule {
|
|
|
|
if compat && p.tok == ':' {
|
|
|
|
p.accept(':')
|
|
|
|
} else {
|
|
|
|
if !p.accept('=') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !p.accept(':') {
|
|
|
|
return
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
2015-01-08 23:56:03 +01:00
|
|
|
|
2015-01-03 00:47:54 +01:00
|
|
|
value := p.parseExpression()
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
property.Name = Ident{name, namePos}
|
2014-05-28 01:34:41 +02:00
|
|
|
property.Value = value
|
|
|
|
property.Pos = pos
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-03 00:47:54 +01:00
|
|
|
func (p *parser) parseExpression() (value Value) {
|
|
|
|
value = p.parseValue()
|
|
|
|
switch p.tok {
|
|
|
|
case '+':
|
|
|
|
return p.parseOperator(value)
|
|
|
|
default:
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
func (p *parser) parseOperator(value1 Value) Value {
|
2015-01-03 00:47:54 +01:00
|
|
|
operator := p.tok
|
2015-01-08 23:56:03 +01:00
|
|
|
pos := p.scanner.Position
|
2015-01-03 00:47:54 +01:00
|
|
|
p.accept(operator)
|
|
|
|
|
|
|
|
value2 := p.parseExpression()
|
|
|
|
|
|
|
|
if value1.Type != value2.Type {
|
|
|
|
p.errorf("mismatched type in operator %c: %s != %s", operator,
|
|
|
|
value1.Type, value2.Type)
|
2015-01-08 23:56:03 +01:00
|
|
|
return Value{}
|
2015-01-03 00:47:54 +01:00
|
|
|
}
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
value := value1
|
|
|
|
value.Variable = ""
|
|
|
|
|
2015-01-03 00:47:54 +01:00
|
|
|
switch operator {
|
|
|
|
case '+':
|
|
|
|
switch value1.Type {
|
|
|
|
case String:
|
2015-01-08 23:56:03 +01:00
|
|
|
value.StringValue = value1.StringValue + value2.StringValue
|
2015-01-03 00:47:54 +01:00
|
|
|
case List:
|
2015-01-08 23:56:03 +01:00
|
|
|
value.ListValue = append([]Value{}, value1.ListValue...)
|
|
|
|
value.ListValue = append(value.ListValue, value2.ListValue...)
|
2015-01-03 00:47:54 +01:00
|
|
|
default:
|
|
|
|
p.errorf("operator %c not supported on type %s", operator, value1.Type)
|
2015-01-08 23:56:03 +01:00
|
|
|
return Value{}
|
2015-01-03 00:47:54 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("unknown operator " + string(operator))
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
value.Expression = &Expression{
|
|
|
|
Args: [2]Value{value1, value2},
|
|
|
|
Operator: operator,
|
|
|
|
Pos: pos,
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
2015-01-03 00:47:54 +01:00
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
func (p *parser) parseValue() (value Value) {
|
|
|
|
switch p.tok {
|
|
|
|
case scanner.Ident:
|
2015-01-03 00:19:28 +01:00
|
|
|
return p.parseVariable()
|
2014-05-28 01:34:41 +02:00
|
|
|
case scanner.String:
|
|
|
|
return p.parseStringValue()
|
|
|
|
case '[':
|
|
|
|
return p.parseListValue()
|
2014-07-26 02:01:20 +02:00
|
|
|
case '{':
|
|
|
|
return p.parseMapValue()
|
2014-05-28 01:34:41 +02:00
|
|
|
default:
|
|
|
|
p.errorf("expected bool, list, or string value; found %s",
|
|
|
|
scanner.TokenString(p.tok))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 00:19:28 +01:00
|
|
|
func (p *parser) parseVariable() (value Value) {
|
2014-05-28 01:34:41 +02:00
|
|
|
switch text := p.scanner.TokenText(); text {
|
|
|
|
case "true":
|
2015-01-03 00:19:28 +01:00
|
|
|
value.Type = Bool
|
2014-05-28 01:34:41 +02:00
|
|
|
value.BoolValue = true
|
|
|
|
case "false":
|
2015-01-03 00:19:28 +01:00
|
|
|
value.Type = Bool
|
2014-05-28 01:34:41 +02:00
|
|
|
value.BoolValue = false
|
|
|
|
default:
|
2015-01-08 23:56:03 +01:00
|
|
|
variable := p.scanner.TokenText()
|
|
|
|
assignment, err := p.scope.Get(variable)
|
2015-01-03 00:19:28 +01:00
|
|
|
if err != nil {
|
|
|
|
p.errorf(err.Error())
|
|
|
|
}
|
|
|
|
value = assignment.Value
|
2015-01-08 23:56:03 +01:00
|
|
|
value.Variable = variable
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
2015-01-08 23:56:03 +01:00
|
|
|
value.Pos = p.scanner.Position
|
2015-01-03 00:19:28 +01:00
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
p.accept(scanner.Ident)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) parseStringValue() (value Value) {
|
|
|
|
value.Type = String
|
|
|
|
value.Pos = p.scanner.Position
|
|
|
|
str, err := strconv.Unquote(p.scanner.TokenText())
|
|
|
|
if err != nil {
|
|
|
|
p.errorf("couldn't parse string: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
value.StringValue = str
|
|
|
|
p.accept(scanner.String)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) parseListValue() (value Value) {
|
|
|
|
value.Type = List
|
|
|
|
value.Pos = p.scanner.Position
|
|
|
|
if !p.accept('[') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var elements []Value
|
2015-01-03 00:19:28 +01:00
|
|
|
for p.tok != ']' {
|
2015-01-03 00:47:54 +01:00
|
|
|
element := p.parseExpression()
|
2015-01-03 00:19:28 +01:00
|
|
|
if element.Type != String {
|
|
|
|
p.errorf("Expected string in list, found %s", element.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
elements = append(elements, element)
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
if p.tok != ',' {
|
|
|
|
// There was no comma, so the list is done.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
p.accept(',')
|
|
|
|
}
|
|
|
|
|
|
|
|
value.ListValue = elements
|
2015-01-08 23:56:03 +01:00
|
|
|
value.EndPos = p.scanner.Position
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
p.accept(']')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-26 02:01:20 +02:00
|
|
|
func (p *parser) parseMapValue() (value Value) {
|
|
|
|
value.Type = Map
|
|
|
|
value.Pos = p.scanner.Position
|
|
|
|
if !p.accept('{') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-15 02:04:13 +01:00
|
|
|
properties := p.parsePropertyList(false, false)
|
2014-07-26 02:01:20 +02:00
|
|
|
value.MapValue = properties
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
value.EndPos = p.scanner.Position
|
2014-07-26 02:01:20 +02:00
|
|
|
p.accept('}')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
type Expression struct {
|
|
|
|
Args [2]Value
|
|
|
|
Operator rune
|
|
|
|
Pos scanner.Position
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type ValueType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Bool ValueType = iota
|
|
|
|
String
|
|
|
|
List
|
2014-07-26 02:01:20 +02:00
|
|
|
Map
|
2014-05-28 01:34:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (p ValueType) String() string {
|
|
|
|
switch p {
|
|
|
|
case Bool:
|
|
|
|
return "bool"
|
|
|
|
case String:
|
|
|
|
return "string"
|
|
|
|
case List:
|
|
|
|
return "list"
|
2014-07-26 02:01:20 +02:00
|
|
|
case Map:
|
|
|
|
return "map"
|
2014-05-28 01:34:41 +02:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown value type: %d", p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Definition interface {
|
|
|
|
String() string
|
|
|
|
definitionTag()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Assignment struct {
|
2015-01-08 23:56:03 +01:00
|
|
|
Name Ident
|
2014-05-28 01:34:41 +02:00
|
|
|
Value Value
|
|
|
|
Pos scanner.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Assignment) String() string {
|
|
|
|
return fmt.Sprintf("%s@%d:%s: %s", a.Name, a.Pos.Offset, a.Pos, a.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Assignment) definitionTag() {}
|
|
|
|
|
|
|
|
type Module struct {
|
2015-01-08 23:56:03 +01:00
|
|
|
Type Ident
|
2014-05-28 01:34:41 +02:00
|
|
|
Properties []*Property
|
2015-01-08 23:56:03 +01:00
|
|
|
LbracePos scanner.Position
|
|
|
|
RbracePos scanner.Position
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) String() string {
|
|
|
|
propertyStrings := make([]string, len(m.Properties))
|
|
|
|
for i, property := range m.Properties {
|
|
|
|
propertyStrings[i] = property.String()
|
|
|
|
}
|
2015-01-08 23:56:03 +01:00
|
|
|
return fmt.Sprintf("%s@%d:%s-%d:%s{%s}", m.Type,
|
|
|
|
m.LbracePos.Offset, m.LbracePos,
|
|
|
|
m.RbracePos.Offset, m.RbracePos,
|
2014-05-28 01:34:41 +02:00
|
|
|
strings.Join(propertyStrings, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) definitionTag() {}
|
|
|
|
|
|
|
|
type Property struct {
|
2015-01-08 23:56:03 +01:00
|
|
|
Name Ident
|
2014-05-28 01:34:41 +02:00
|
|
|
Value Value
|
|
|
|
Pos scanner.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Property) String() string {
|
|
|
|
return fmt.Sprintf("%s@%d:%s: %s", p.Name, p.Pos.Offset, p.Pos, p.Value)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
type Ident struct {
|
|
|
|
Name string
|
|
|
|
Pos scanner.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Ident) String() string {
|
|
|
|
return fmt.Sprintf("%s@%d:%s", i.Name, i.Pos.Offset, i.Pos)
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type Value struct {
|
|
|
|
Type ValueType
|
|
|
|
BoolValue bool
|
|
|
|
StringValue string
|
|
|
|
ListValue []Value
|
2014-07-26 02:01:20 +02:00
|
|
|
MapValue []*Property
|
2015-01-08 23:56:03 +01:00
|
|
|
Expression *Expression
|
|
|
|
Variable string
|
2014-05-28 01:34:41 +02:00
|
|
|
Pos scanner.Position
|
2015-01-08 23:56:03 +01:00
|
|
|
EndPos scanner.Position
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p Value) String() string {
|
|
|
|
switch p.Type {
|
|
|
|
case Bool:
|
|
|
|
return fmt.Sprintf("%t@%d:%s", p.BoolValue, p.Pos.Offset, p.Pos)
|
|
|
|
case String:
|
|
|
|
return fmt.Sprintf("%q@%d:%s", p.StringValue, p.Pos.Offset, p.Pos)
|
|
|
|
case List:
|
|
|
|
valueStrings := make([]string, len(p.ListValue))
|
|
|
|
for i, value := range p.ListValue {
|
|
|
|
valueStrings[i] = value.String()
|
|
|
|
}
|
2015-01-08 23:56:03 +01:00
|
|
|
return fmt.Sprintf("@%d:%s-%d:%s[%s]", p.Pos.Offset, p.Pos, p.EndPos.Offset, p.EndPos,
|
2014-05-28 01:34:41 +02:00
|
|
|
strings.Join(valueStrings, ", "))
|
2014-07-26 02:01:20 +02:00
|
|
|
case Map:
|
|
|
|
propertyStrings := make([]string, len(p.MapValue))
|
|
|
|
for i, property := range p.MapValue {
|
|
|
|
propertyStrings[i] = property.String()
|
|
|
|
}
|
2015-01-08 23:56:03 +01:00
|
|
|
return fmt.Sprintf("@%d:%s-%d:%s{%s}", p.Pos.Offset, p.Pos, p.EndPos.Offset, p.EndPos,
|
2014-07-26 02:01:20 +02:00
|
|
|
strings.Join(propertyStrings, ", "))
|
2014-05-28 01:34:41 +02:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("bad property type: %d", p.Type))
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 00:19:28 +01:00
|
|
|
|
|
|
|
type Scope struct {
|
|
|
|
vars map[string]*Assignment
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewScope(s *Scope) *Scope {
|
|
|
|
newScope := &Scope{
|
|
|
|
vars: make(map[string]*Assignment),
|
|
|
|
}
|
|
|
|
|
|
|
|
if s != nil {
|
|
|
|
for k, v := range s.vars {
|
|
|
|
newScope.vars[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newScope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scope) Add(assignment *Assignment) error {
|
2015-01-08 23:56:03 +01:00
|
|
|
if old, ok := s.vars[assignment.Name.Name]; ok {
|
2015-01-03 00:19:28 +01:00
|
|
|
return fmt.Errorf("variable already set, previous assignment: %s", old)
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:56:03 +01:00
|
|
|
s.vars[assignment.Name.Name] = assignment
|
2015-01-03 00:19:28 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scope) Remove(name string) {
|
|
|
|
delete(s.vars, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scope) Get(name string) (*Assignment, error) {
|
|
|
|
if a, ok := s.vars[name]; ok {
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("variable %s not set", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scope) String() string {
|
|
|
|
vars := []string{}
|
|
|
|
|
|
|
|
for k := range s.vars {
|
|
|
|
vars = append(vars, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(vars)
|
|
|
|
|
|
|
|
ret := []string{}
|
|
|
|
for _, v := range vars {
|
|
|
|
ret = append(ret, s.vars[v].String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(ret, "\n")
|
|
|
|
}
|
2015-01-08 23:56:03 +01:00
|
|
|
|
|
|
|
type Comment struct {
|
|
|
|
Comment string
|
|
|
|
Pos scanner.Position
|
|
|
|
}
|