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
This commit is contained in:
Colin Cross 2016-06-09 15:52:30 -07:00
parent d4f49b07d1
commit c32c47938f
7 changed files with 100 additions and 82 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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
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.

View file

@ -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
}

View file

@ -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,13 +57,15 @@ 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)},
Name: "name",
NamePos: mkpos(12, 3, 4),
Pos: mkpos(16, 3, 8),
Value: &String{
LiteralPos: mkpos(18, 3, 10),
@ -83,13 +86,15 @@ 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)},
Name: "isGood",
NamePos: mkpos(12, 3, 4),
Pos: mkpos(18, 3, 10),
Value: &Bool{
LiteralPos: mkpos(20, 3, 12),
@ -111,13 +116,15 @@ 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)},
Name: "stuff",
NamePos: mkpos(12, 3, 4),
Pos: mkpos(17, 3, 9),
Value: &List{
LBracePos: mkpos(19, 3, 11),
@ -163,20 +170,23 @@ 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)},
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)},
Name: "isGood",
NamePos: mkpos(25, 4, 5),
Pos: mkpos(31, 4, 11),
Value: &Bool{
LiteralPos: mkpos(33, 4, 13),
@ -184,7 +194,8 @@ var validParseTestCases = []struct {
},
},
{
Name: Ident{"name", mkpos(43, 5, 5)},
Name: "name",
NamePos: mkpos(43, 5, 5),
Pos: mkpos(47, 5, 9),
Value: &String{
LiteralPos: mkpos(49, 5, 11),
@ -210,13 +221,15 @@ 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)},
Name: "isGood",
NamePos: mkpos(52, 5, 4),
Pos: mkpos(58, 5, 10),
Value: &Bool{
LiteralPos: mkpos(60, 5, 12),
@ -258,13 +271,15 @@ 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)},
Name: "name",
NamePos: mkpos(12, 3, 4),
Pos: mkpos(16, 3, 8),
Value: &String{
LiteralPos: mkpos(18, 3, 10),
@ -275,13 +290,15 @@ 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)},
Name: "name",
NamePos: mkpos(41, 7, 4),
Pos: mkpos(45, 7, 8),
Value: &String{
LiteralPos: mkpos(47, 7, 10),
@ -303,7 +320,8 @@ var validParseTestCases = []struct {
`,
[]Definition{
&Assignment{
Name: Ident{"foo", mkpos(3, 2, 3)},
Name: "foo",
NamePos: mkpos(3, 2, 3),
Pos: mkpos(7, 2, 7),
Value: &String{
LiteralPos: mkpos(9, 2, 9),
@ -317,7 +335,8 @@ var validParseTestCases = []struct {
Referenced: true,
},
&Assignment{
Name: Ident{"bar", mkpos(19, 3, 3)},
Name: "bar",
NamePos: mkpos(19, 3, 3),
Pos: mkpos(23, 3, 7),
Value: &Variable{
Name: "foo",
@ -339,7 +358,8 @@ var validParseTestCases = []struct {
Referenced: true,
},
&Assignment{
Name: Ident{"baz", mkpos(31, 4, 3)},
Name: "baz",
NamePos: mkpos(31, 4, 3),
Pos: mkpos(35, 4, 7),
Value: &Operator{
OperatorPos: mkpos(41, 4, 13),
@ -405,7 +425,8 @@ var validParseTestCases = []struct {
Referenced: true,
},
&Assignment{
Name: Ident{"boo", mkpos(49, 5, 3)},
Name: "boo",
NamePos: mkpos(49, 5, 3),
Pos: mkpos(53, 5, 7),
Value: &Operator{
Args: [2]Expression{
@ -496,7 +517,8 @@ var validParseTestCases = []struct {
Assigner: "=",
},
&Assignment{
Name: Ident{"boo", mkpos(61, 6, 3)},
Name: "boo",
NamePos: mkpos(61, 6, 3),
Pos: mkpos(66, 6, 8),
Value: &Variable{
Name: "foo",

View file

@ -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)

View file

@ -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.