Update parser and printer to use new Blueprint format

Updates the standard format from:
module_type {
    property: "value",
}
to:
module_type(
    property = "value",
}

The parser will accept both formats for now, so all files can be
updated to the new format using bpfmt -w .

Change-Id: I9566cf1bd3cd66c6cbf3d4bd9dac1f04b9112927
This commit is contained in:
Colin Cross 2015-01-14 17:04:13 -08:00
parent a434b3fd69
commit 6bb4af9e20
3 changed files with 101 additions and 70 deletions

View file

@ -130,10 +130,10 @@ func (p *parser) parseDefinitions() (defs []Definition) {
switch p.tok {
case '=':
defs = append(defs, p.parseAssignment(ident, pos))
case '{':
case '{', '(':
defs = append(defs, p.parseModule(ident, pos))
default:
p.errorf("expected \"=\" or \"{\", found %s",
p.errorf("expected \"=\" or \"{\" or \"(\", found %s",
scanner.TokenString(p.tok))
}
case scanner.EOF:
@ -172,14 +172,22 @@ func (p *parser) parseModule(typ string,
typPos scanner.Position) (module *Module) {
module = new(Module)
compat := false
lbracePos := p.scanner.Position
if !p.accept('{') {
if p.tok == '{' {
compat = true
}
if !p.accept(p.tok) {
return
}
properties := p.parsePropertyList()
properties := p.parsePropertyList(true, compat)
rbracePos := p.scanner.Position
p.accept('}')
if !compat {
p.accept(')')
} else {
p.accept('}')
}
module.Type = Ident{typ, typPos}
module.Properties = properties
@ -188,9 +196,9 @@ func (p *parser) parseModule(typ string,
return
}
func (p *parser) parsePropertyList() (properties []*Property) {
func (p *parser) parsePropertyList(isModule, compat bool) (properties []*Property) {
for p.tok == scanner.Ident {
property := p.parseProperty()
property := p.parseProperty(isModule, compat)
properties = append(properties, property)
if p.tok != ',' {
@ -204,15 +212,26 @@ func (p *parser) parsePropertyList() (properties []*Property) {
return
}
func (p *parser) parseProperty() (property *Property) {
func (p *parser) parseProperty(isModule, compat bool) (property *Property) {
property = new(Property)
name := p.scanner.TokenText()
namePos := p.scanner.Position
p.accept(scanner.Ident)
pos := p.scanner.Position
if !p.accept(':') {
return
if isModule {
if compat && p.tok == ':' {
p.accept(':')
} else {
if !p.accept('=') {
return
}
}
} else {
if !p.accept(':') {
return
}
}
value := p.parseExpression()
@ -366,7 +385,7 @@ func (p *parser) parseMapValue() (value Value) {
return
}
properties := p.parsePropertyList()
properties := p.parsePropertyList(false, false)
value.MapValue = properties
value.EndPos = p.scanner.Position

View file

@ -79,7 +79,7 @@ func (p *printer) printAssignment(assignment *Assignment) {
func (p *printer) printModule(module *Module) {
p.printToken(module.Type.Name, module.Type.Pos, wsBoth)
p.printMap(module.Properties, module.LbracePos, module.RbracePos)
p.printMap(module.Properties, module.LbracePos, module.RbracePos, true)
p.forceLineBreak = 2
}
@ -103,7 +103,7 @@ func (p *printer) printValue(value Value) {
case List:
p.printList(value.ListValue, value.Pos, value.EndPos)
case Map:
p.printMap(value.MapValue, value.Pos, value.EndPos)
p.printMap(value.MapValue, value.Pos, value.EndPos, false)
default:
panic(fmt.Errorf("bad property type: %d", value.Type))
}
@ -129,19 +129,27 @@ func (p *printer) printList(list []Value, pos, endPos scanner.Position) {
p.printToken("]", endPos, wsAfter)
}
func (p *printer) printMap(list []*Property, pos, endPos scanner.Position) {
p.printToken("{", pos, wsBefore)
func (p *printer) printMap(list []*Property, pos, endPos scanner.Position, isModule bool) {
if isModule {
p.printToken("(", pos, wsNone)
} else {
p.printToken("{", pos, wsBefore)
}
if len(list) > 0 || pos.Line != endPos.Line {
p.forceLineBreak = 1
p.indent(p.curIndent() + 4)
for _, prop := range list {
p.printProperty(prop)
p.printProperty(prop, isModule)
p.printToken(",", noPos, wsAfter)
p.forceLineBreak = 1
}
p.unindent()
}
p.printToken("}", endPos, wsAfter)
if isModule {
p.printToken(")", endPos, wsAfter)
} else {
p.printToken("}", endPos, wsAfter)
}
}
func (p *printer) printExpression(expression Expression) {
@ -150,9 +158,13 @@ func (p *printer) printExpression(expression Expression) {
p.printValue(expression.Args[1])
}
func (p *printer) printProperty(property *Property) {
func (p *printer) printProperty(property *Property, isModule bool) {
p.printToken(property.Name.Name, property.Name.Pos, wsMaybe)
p.printToken(":", property.Pos, wsAfter)
if isModule {
p.printToken("=", property.Pos, wsBoth)
} else {
p.printToken(":", property.Pos, wsAfter)
}
p.printValue(property.Value)
}

View file

@ -11,93 +11,93 @@ var validPrinterTestCases = []struct {
}{
{
input: `
foo {}
foo ()
`,
output: `
foo {}
foo()
`,
},
{
input: `
foo{name: "abc",}
foo(name= "abc",)
`,
output: `
foo {
name: "abc",
}
foo(
name = "abc",
)
`,
},
{
input: `
foo {
stuff: ["asdf", "jkl;", "qwert",
foo(
stuff = ["asdf", "jkl;", "qwert",
"uiop", "bnm,"]
}
)
`,
output: `
foo {
stuff: [
foo(
stuff = [
"asdf",
"bnm,",
"jkl;",
"qwert",
"uiop",
],
}
)
`,
},
{
input: `
foo {
stuff: {
foo(
stuff = {
isGood: true,
name: "bar"
}
}
)
`,
output: `
foo {
stuff: {
foo(
stuff = {
isGood: true,
name: "bar",
},
}
)
`,
},
{
input: `
// comment1
foo {
foo(
// comment2
isGood: true, // comment3
}
isGood = true, // comment3
)
`,
output: `
// comment1
foo {
foo(
// comment2
isGood: true, // comment3
}
isGood = true, // comment3
)
`,
},
{
input: `
foo {
name: "abc",
}
foo(
name = "abc",
)
bar {
name: "def",
}
bar (
name = "def",
)
`,
output: `
foo {
name: "abc",
}
foo(
name = "abc",
)
bar {
name: "def",
}
bar(
name = "def",
)
`,
},
{
@ -115,41 +115,41 @@ baz = foo + bar
{
input: `
//test
test /* test */{
srcs: [
test /* test */(
srcs = [
/*"blueprint/bootstrap/bootstrap.go",
"blueprint/bootstrap/cleanup.go",*/
"blueprint/bootstrap/command.go",
"blueprint/bootstrap/doc.go", //doc.go
"blueprint/bootstrap/config.go", //config.go
],
deps: ["libabc"],
incs: []
} //test
deps = ["libabc"],
incs = []
) //test
//test
test2{
}
test2(
)
//test3
`,
output: `
//test
test /* test */ {
srcs: [
test /* test */(
srcs = [
/*"blueprint/bootstrap/bootstrap.go",
"blueprint/bootstrap/cleanup.go",*/
"blueprint/bootstrap/command.go",
"blueprint/bootstrap/config.go", //config.go
"blueprint/bootstrap/doc.go", //doc.go
],
deps: ["libabc"],
incs: [],
} //test
deps = ["libabc"],
incs = [],
) //test
//test
test2 {
}
test2(
)
//test3
`,