From c32c47938f2a04cd62aed8691add85049c6a6625 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 9 Jun 2016 15:52:30 -0700 Subject: [PATCH] Remove blueprint/parser.Ident It wasn't adding anything useful, and it resulted in Name.Name to get to the identifier. Replace Name Ident with Name string; NamePos scanner.Position. Change-Id: Idf9b18b31dd563a18f27c602c2d14298955af371 --- bpmodify/bpmodify.go | 6 +-- context.go | 11 +++-- parser/ast.go | 27 +++++-------- parser/parser.go | 36 +++++++++-------- parser/parser_test.go | 94 ++++++++++++++++++++++++++----------------- parser/printer.go | 6 +-- unpack.go | 2 +- 7 files changed, 100 insertions(+), 82 deletions(-) diff --git a/bpmodify/bpmodify.go b/bpmodify/bpmodify.go index 6f7a5bf..6a3c803 100644 --- a/bpmodify/bpmodify.go +++ b/bpmodify/bpmodify.go @@ -124,9 +124,9 @@ func findModules(file *parser.File) (modified bool, errs []error) { for _, def := range file.Defs { if module, ok := def.(*parser.Module); ok { for _, prop := range module.Properties { - if prop.Name.Name == "name" && prop.Value.Type() == parser.StringType { + if prop.Name == "name" && prop.Value.Type() == parser.StringType { if targetedModule(prop.Value.Eval().(*parser.String).Value) { - m, newErrs := processModule(module, prop.Name.Name, file) + m, newErrs := processModule(module, prop.Name, file) errs = append(errs, newErrs...) modified = modified || m } @@ -142,7 +142,7 @@ func processModule(module *parser.Module, moduleName string, file *parser.File) (modified bool, errs []error) { for _, prop := range module.Properties { - if prop.Name.Name == *parameter { + if prop.Name == *parameter { modified, errs = processParameter(prop.Value, *parameter, moduleName, file) return } diff --git a/context.go b/context.go index b644ac5..ba7ad90 100644 --- a/context.go +++ b/context.go @@ -1040,8 +1040,7 @@ func (c *Context) prettyPrintVariant(variant variationMap) string { func (c *Context) processModuleDef(moduleDef *parser.Module, relBlueprintsFile string) (*moduleInfo, []error) { - typeName := moduleDef.Type.Name - factory, ok := c.moduleFactories[typeName] + factory, ok := c.moduleFactories[moduleDef.Type] if !ok { if c.ignoreUnknownModuleTypes { return nil, nil @@ -1049,8 +1048,8 @@ func (c *Context) processModuleDef(moduleDef *parser.Module, return nil, []error{ &Error{ - Err: fmt.Errorf("unrecognized module type %q", typeName), - Pos: moduleDef.Type.Pos, + Err: fmt.Errorf("unrecognized module type %q", moduleDef.Type), + Pos: moduleDef.TypePos, }, } } @@ -1059,7 +1058,7 @@ func (c *Context) processModuleDef(moduleDef *parser.Module, module := &moduleInfo{ logicModule: logicModule, - typeName: typeName, + typeName: moduleDef.Type, relBlueprintsFile: relBlueprintsFile, } @@ -1074,7 +1073,7 @@ func (c *Context) processModuleDef(moduleDef *parser.Module, return nil, errs } - module.pos = moduleDef.Type.Pos + module.pos = moduleDef.TypePos module.propertyPos = make(map[string]scanner.Position) for name, propertyDef := range propertyMap { module.propertyPos[name] = propertyDef.Pos diff --git a/parser/ast.go b/parser/ast.go index 387f6d5..8349172 100644 --- a/parser/ast.go +++ b/parser/ast.go @@ -29,7 +29,8 @@ type Definition interface { // An Assignment is a variable assignment at the top level of a Blueprints file, scoped to the // file and and subdirs. type Assignment struct { - Name Ident + Name string + NamePos scanner.Position Value Expression OrigValue Expression Pos scanner.Position @@ -45,7 +46,8 @@ func (a *Assignment) definitionTag() {} // A Module is a module definition at the top level of a Blueprints file type Module struct { - Type Ident + Type string + TypePos scanner.Position Map } @@ -70,11 +72,15 @@ func (m *Module) String() string { func (m *Module) definitionTag() {} +func (m *Module) Pos() scanner.Position { return m.TypePos } +func (m *Module) End() scanner.Position { return m.Map.End() } + // A Property is a name: value pair within a Map, which may be a top level Module. type Property struct { - Name Ident - Value Expression - Pos scanner.Position + Name string + NamePos scanner.Position + Pos scanner.Position + Value Expression } func (p *Property) Copy() *Property { @@ -87,17 +93,6 @@ func (p *Property) String() string { return fmt.Sprintf("%s@%s: %s", p.Name, p.Pos, p.Value) } -// An Ident is a name identifier, the Type of a Module, the Name of a Property, or the Name of a -// Variable. -type Ident struct { - Name string - Pos scanner.Position -} - -func (i Ident) String() string { - return fmt.Sprintf("%s@%s", i.Name, i.Pos) -} - // An Expression is a Value in a Property or Assignment. It can be a literal (String or Bool), a // Map, a List, an Operator that combines two expressions of the same type, or a Variable that // references and Assignment. diff --git a/parser/parser.go b/parser/parser.go index 6909c50..c6b4a6a 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -190,7 +190,8 @@ func (p *parser) parseAssignment(name string, namePos scanner.Position, } value := p.parseExpression() - assignment.Name = Ident{name, namePos} + assignment.Name = name + assignment.NamePos = namePos assignment.Value = value assignment.OrigValue = value assignment.Pos = pos @@ -198,13 +199,12 @@ func (p *parser) parseAssignment(name string, namePos scanner.Position, if p.scope != nil { if assigner == "+=" { - if old, local := p.scope.Get(assignment.Name.Name); old == nil { - p.errorf("modified non-existent variable %q with +=", assignment.Name.Name) + if old, local := p.scope.Get(assignment.Name); old == nil { + p.errorf("modified non-existent variable %q with +=", assignment.Name) } else if !local { - p.errorf("modified non-local variable %q with +=", assignment.Name.Name) + p.errorf("modified non-local variable %q with +=", assignment.Name) } else if old.Referenced { - p.errorf("modified variable %q with += after referencing", - assignment.Name.Name) + p.errorf("modified variable %q with += after referencing", assignment.Name) } else { val, err := p.evaluateOperator(old.Value, assignment.Value, '+', assignment.Pos) if err != nil { @@ -244,7 +244,8 @@ func (p *parser) parseModule(typ string, typPos scanner.Position) *Module { } return &Module{ - Type: Ident{typ, typPos}, + Type: typ, + TypePos: typPos, Map: Map{ Properties: properties, LBracePos: lbracePos, @@ -293,7 +294,8 @@ func (p *parser) parseProperty(isModule, compat bool) (property *Property) { value := p.parseExpression() - property.Name = Ident{name, namePos} + property.Name = name + property.NamePos = namePos property.Value = value property.Pos = pos @@ -362,18 +364,18 @@ func (p *parser) addMaps(map1, map2 []*Property, pos scanner.Position) ([]*Prope inBoth := make(map[string]*Property) for _, prop1 := range map1 { - inMap1[prop1.Name.Name] = prop1 + inMap1[prop1.Name] = prop1 } for _, prop2 := range map2 { - inMap2[prop2.Name.Name] = prop2 - if _, ok := inMap1[prop2.Name.Name]; ok { - inBoth[prop2.Name.Name] = prop2 + inMap2[prop2.Name] = prop2 + if _, ok := inMap1[prop2.Name]; ok { + inBoth[prop2.Name] = prop2 } } for _, prop1 := range map1 { - if prop2, ok := inBoth[prop1.Name.Name]; ok { + if prop2, ok := inBoth[prop1.Name]; ok { var err error newProp := *prop1 newProp.Value, err = p.evaluateOperator(prop1.Value, prop2.Value, '+', pos) @@ -387,7 +389,7 @@ func (p *parser) addMaps(map1, map2 []*Property, pos scanner.Position) ([]*Prope } for _, prop2 := range map2 { - if _, ok := inBoth[prop2.Name.Name]; !ok { + if _, ok := inBoth[prop2.Name]; !ok { ret = append(ret, prop2) } } @@ -550,15 +552,15 @@ func NewScope(s *Scope) *Scope { } func (s *Scope) Add(assignment *Assignment) error { - if old, ok := s.vars[assignment.Name.Name]; ok { + if old, ok := s.vars[assignment.Name]; ok { return fmt.Errorf("variable already set, previous assignment: %s", old) } - if old, ok := s.inheritedVars[assignment.Name.Name]; ok { + if old, ok := s.inheritedVars[assignment.Name]; ok { return fmt.Errorf("variable already set in inherited scope, previous assignment: %s", old) } - s.vars[assignment.Name.Name] = assignment + s.vars[assignment.Name] = assignment return nil } diff --git a/parser/parser_test.go b/parser/parser_test.go index e93bb09..50eed95 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -39,7 +39,8 @@ var validParseTestCases = []struct { `, []Definition{ &Module{ - Type: Ident{"foo", mkpos(3, 2, 3)}, + Type: "foo", + TypePos: mkpos(3, 2, 3), Map: Map{ LBracePos: mkpos(7, 2, 7), RBracePos: mkpos(8, 2, 8), @@ -56,14 +57,16 @@ var validParseTestCases = []struct { `, []Definition{ &Module{ - Type: Ident{"foo", mkpos(3, 2, 3)}, + Type: "foo", + TypePos: mkpos(3, 2, 3), Map: Map{ LBracePos: mkpos(7, 2, 7), RBracePos: mkpos(27, 4, 3), Properties: []*Property{ { - Name: Ident{"name", mkpos(12, 3, 4)}, - Pos: mkpos(16, 3, 8), + Name: "name", + NamePos: mkpos(12, 3, 4), + Pos: mkpos(16, 3, 8), Value: &String{ LiteralPos: mkpos(18, 3, 10), Value: "abc", @@ -83,14 +86,16 @@ var validParseTestCases = []struct { `, []Definition{ &Module{ - Type: Ident{"foo", mkpos(3, 2, 3)}, + Type: "foo", + TypePos: mkpos(3, 2, 3), Map: Map{ LBracePos: mkpos(7, 2, 7), RBracePos: mkpos(28, 4, 3), Properties: []*Property{ { - Name: Ident{"isGood", mkpos(12, 3, 4)}, - Pos: mkpos(18, 3, 10), + Name: "isGood", + NamePos: mkpos(12, 3, 4), + Pos: mkpos(18, 3, 10), Value: &Bool{ LiteralPos: mkpos(20, 3, 12), Value: true, @@ -111,14 +116,16 @@ var validParseTestCases = []struct { `, []Definition{ &Module{ - Type: Ident{"foo", mkpos(3, 2, 3)}, + Type: "foo", + TypePos: mkpos(3, 2, 3), Map: Map{ LBracePos: mkpos(7, 2, 7), RBracePos: mkpos(67, 5, 3), Properties: []*Property{ { - Name: Ident{"stuff", mkpos(12, 3, 4)}, - Pos: mkpos(17, 3, 9), + Name: "stuff", + NamePos: mkpos(12, 3, 4), + Pos: mkpos(17, 3, 9), Value: &List{ LBracePos: mkpos(19, 3, 11), RBracePos: mkpos(63, 4, 19), @@ -163,29 +170,33 @@ var validParseTestCases = []struct { `, []Definition{ &Module{ - Type: Ident{"foo", mkpos(3, 2, 3)}, + Type: "foo", + TypePos: mkpos(3, 2, 3), Map: Map{ LBracePos: mkpos(7, 2, 7), RBracePos: mkpos(62, 7, 3), Properties: []*Property{ { - Name: Ident{"stuff", mkpos(12, 3, 4)}, - Pos: mkpos(17, 3, 9), + Name: "stuff", + NamePos: mkpos(12, 3, 4), + Pos: mkpos(17, 3, 9), Value: &Map{ LBracePos: mkpos(19, 3, 11), RBracePos: mkpos(58, 6, 4), Properties: []*Property{ { - Name: Ident{"isGood", mkpos(25, 4, 5)}, - Pos: mkpos(31, 4, 11), + Name: "isGood", + NamePos: mkpos(25, 4, 5), + Pos: mkpos(31, 4, 11), Value: &Bool{ LiteralPos: mkpos(33, 4, 13), Value: true, }, }, { - Name: Ident{"name", mkpos(43, 5, 5)}, - Pos: mkpos(47, 5, 9), + Name: "name", + NamePos: mkpos(43, 5, 5), + Pos: mkpos(47, 5, 9), Value: &String{ LiteralPos: mkpos(49, 5, 11), Value: "bar", @@ -210,14 +221,16 @@ var validParseTestCases = []struct { `, []Definition{ &Module{ - Type: Ident{"foo", mkpos(17, 3, 3)}, + Type: "foo", + TypePos: mkpos(17, 3, 3), Map: Map{ LBracePos: mkpos(32, 3, 18), RBracePos: mkpos(81, 6, 3), Properties: []*Property{ { - Name: Ident{"isGood", mkpos(52, 5, 4)}, - Pos: mkpos(58, 5, 10), + Name: "isGood", + NamePos: mkpos(52, 5, 4), + Pos: mkpos(58, 5, 10), Value: &Bool{ LiteralPos: mkpos(60, 5, 12), Value: true, @@ -258,14 +271,16 @@ var validParseTestCases = []struct { `, []Definition{ &Module{ - Type: Ident{"foo", mkpos(3, 2, 3)}, + Type: "foo", + TypePos: mkpos(3, 2, 3), Map: Map{ LBracePos: mkpos(7, 2, 7), RBracePos: mkpos(27, 4, 3), Properties: []*Property{ { - Name: Ident{"name", mkpos(12, 3, 4)}, - Pos: mkpos(16, 3, 8), + Name: "name", + NamePos: mkpos(12, 3, 4), + Pos: mkpos(16, 3, 8), Value: &String{ LiteralPos: mkpos(18, 3, 10), Value: "abc", @@ -275,14 +290,16 @@ var validParseTestCases = []struct { }, }, &Module{ - Type: Ident{"bar", mkpos(32, 6, 3)}, + Type: "bar", + TypePos: mkpos(32, 6, 3), Map: Map{ LBracePos: mkpos(36, 6, 7), RBracePos: mkpos(56, 8, 3), Properties: []*Property{ { - Name: Ident{"name", mkpos(41, 7, 4)}, - Pos: mkpos(45, 7, 8), + Name: "name", + NamePos: mkpos(41, 7, 4), + Pos: mkpos(45, 7, 8), Value: &String{ LiteralPos: mkpos(47, 7, 10), Value: "def", @@ -303,8 +320,9 @@ var validParseTestCases = []struct { `, []Definition{ &Assignment{ - Name: Ident{"foo", mkpos(3, 2, 3)}, - Pos: mkpos(7, 2, 7), + Name: "foo", + NamePos: mkpos(3, 2, 3), + Pos: mkpos(7, 2, 7), Value: &String{ LiteralPos: mkpos(9, 2, 9), Value: "stuff", @@ -317,8 +335,9 @@ var validParseTestCases = []struct { Referenced: true, }, &Assignment{ - Name: Ident{"bar", mkpos(19, 3, 3)}, - Pos: mkpos(23, 3, 7), + Name: "bar", + NamePos: mkpos(19, 3, 3), + Pos: mkpos(23, 3, 7), Value: &Variable{ Name: "foo", NamePos: mkpos(25, 3, 9), @@ -339,8 +358,9 @@ var validParseTestCases = []struct { Referenced: true, }, &Assignment{ - Name: Ident{"baz", mkpos(31, 4, 3)}, - Pos: mkpos(35, 4, 7), + Name: "baz", + NamePos: mkpos(31, 4, 3), + Pos: mkpos(35, 4, 7), Value: &Operator{ OperatorPos: mkpos(41, 4, 13), Operator: '+', @@ -405,8 +425,9 @@ var validParseTestCases = []struct { Referenced: true, }, &Assignment{ - Name: Ident{"boo", mkpos(49, 5, 3)}, - Pos: mkpos(53, 5, 7), + Name: "boo", + NamePos: mkpos(49, 5, 3), + Pos: mkpos(53, 5, 7), Value: &Operator{ Args: [2]Expression{ &Variable{ @@ -496,8 +517,9 @@ var validParseTestCases = []struct { Assigner: "=", }, &Assignment{ - Name: Ident{"boo", mkpos(61, 6, 3)}, - Pos: mkpos(66, 6, 8), + Name: "boo", + NamePos: mkpos(61, 6, 3), + Pos: mkpos(66, 6, 8), Value: &Variable{ Name: "foo", NamePos: mkpos(68, 6, 10), diff --git a/parser/printer.go b/parser/printer.go index 1e7bc2a..f786eef 100644 --- a/parser/printer.go +++ b/parser/printer.go @@ -87,7 +87,7 @@ func (p *printer) printDef(def Definition) { } func (p *printer) printAssignment(assignment *Assignment) { - p.printToken(assignment.Name.Name, assignment.Name.Pos) + p.printToken(assignment.Name, assignment.NamePos) p.requestSpace() p.printToken(assignment.Assigner, assignment.Pos) p.requestSpace() @@ -96,7 +96,7 @@ func (p *printer) printAssignment(assignment *Assignment) { } func (p *printer) printModule(module *Module) { - p.printToken(module.Type.Name, module.Type.Pos) + p.printToken(module.Type, module.TypePos) p.printMap(&module.Map) p.requestDoubleNewline() } @@ -175,7 +175,7 @@ func (p *printer) printOperator(operator *Operator) { } func (p *printer) printProperty(property *Property) { - p.printToken(property.Name.Name, property.Name.Pos) + p.printToken(property.Name, property.NamePos) p.printToken(":", property.Pos) p.requestSpace() p.printExpression(property.Value) diff --git a/unpack.go b/unpack.go index 11718b4..62ee061 100644 --- a/unpack.go +++ b/unpack.go @@ -82,7 +82,7 @@ func buildPropertyMap(namePrefix string, propertyDefs []*parser.Property, propertyMap map[string]*packedProperty) (errs []error) { for _, propertyDef := range propertyDefs { - name := namePrefix + propertyDef.Name.Name + name := namePrefix + propertyDef.Name if first, present := propertyMap[name]; present { if first.property == propertyDef { // We've already added this property.