Fix blank line translation once and for all

Track the position in the input makefile and output blueprint file
separately.  When parsing a new makefile line, increment the line in the
output position by the number of lines between the end of the last
makefile line and the beginning of this line.  Any extra blank lines
will be discarded by the blueprint printer.

Change-Id: I85c6e006667b5733b15e03b56b91caf29fbc0ea0
This commit is contained in:
Colin Cross
2016-05-25 17:21:09 -07:00
parent ad14e6e227
commit 7fd911f713

View File

@@ -23,8 +23,8 @@ type bpFile struct {
scope mkparser.Scope scope mkparser.Scope
module *bpparser.Module module *bpparser.Module
pos scanner.Position mkPos scanner.Position // Position of the last handled line in the makefile
prevLine, line int bpPos scanner.Position // Position of the last emitted line to the blueprint file
inModule bool inModule bool
} }
@@ -32,36 +32,31 @@ type bpFile struct {
func (f *bpFile) errorf(thing mkparser.MakeThing, s string, args ...interface{}) { func (f *bpFile) errorf(thing mkparser.MakeThing, s string, args ...interface{}) {
orig := thing.Dump() orig := thing.Dump()
s = fmt.Sprintf(s, args...) s = fmt.Sprintf(s, args...)
f.comments = append(f.comments, bpparser.Comment{ c := bpparser.Comment{
Comment: []string{fmt.Sprintf("// ANDROIDMK TRANSLATION ERROR: %s", s)}, Comment: []string{fmt.Sprintf("// ANDROIDMK TRANSLATION ERROR: %s", s)},
Pos: f.pos, Pos: f.bpPos,
}) }
lines := strings.Split(orig, "\n") lines := strings.Split(orig, "\n")
for _, l := range lines { for _, l := range lines {
f.incPos() c.Comment = append(c.Comment, "// "+l)
f.comments = append(f.comments, bpparser.Comment{
Comment: []string{"// " + l},
Pos: f.pos,
})
} }
f.incBpPos(len(lines))
f.comments = append(f.comments, c)
} }
func (f *bpFile) setPos(pos, endPos scanner.Position) { func (f *bpFile) setMkPos(pos, end scanner.Position) {
f.pos = pos if pos.Line < f.mkPos.Line {
panic(fmt.Errorf("out of order lines, %q after %q", pos, f.mkPos))
f.line++
if f.pos.Line > f.prevLine+1 {
f.line++
} }
f.bpPos.Line += (pos.Line - f.mkPos.Line)
f.pos.Line = f.line f.mkPos = end
f.prevLine = endPos.Line
} }
func (f *bpFile) incPos() { // Called when inserting extra lines into the blueprint file
f.pos.Line++ func (f *bpFile) incBpPos(lines int) {
f.line++ f.bpPos.Line += lines
f.prevLine++
} }
type conditional struct { type conditional struct {
@@ -96,12 +91,12 @@ func main() {
var assignmentCond *conditional var assignmentCond *conditional
for _, t := range things { for _, t := range things {
file.setPos(t.Pos(), t.EndPos()) file.setMkPos(t.Pos(), t.EndPos())
if comment, ok := t.AsComment(); ok { if comment, ok := t.AsComment(); ok {
file.comments = append(file.comments, bpparser.Comment{ file.comments = append(file.comments, bpparser.Comment{
Pos: file.pos,
Comment: []string{"//" + comment.Comment}, Comment: []string{"//" + comment.Comment},
Pos: file.bpPos,
}) })
} else if assignment, ok := t.AsAssignment(); ok { } else if assignment, ok := t.AsAssignment(); ok {
handleAssignment(file, assignment, assignmentCond) handleAssignment(file, assignment, assignmentCond)
@@ -275,7 +270,7 @@ func handleModuleConditionals(file *bpFile, directive mkparser.Directive, conds
disabledPrefix := conditionalTranslations[c.cond][!c.eq] disabledPrefix := conditionalTranslations[c.cond][!c.eq]
// Create a fake assignment with enabled = false // Create a fake assignment with enabled = false
val, err := makeVariableToBlueprint(file, mkparser.SimpleMakeString("false", file.pos), bpparser.Bool) val, err := makeVariableToBlueprint(file, mkparser.SimpleMakeString("false", file.bpPos), bpparser.Bool)
if err == nil { if err == nil {
err = setVariable(file, false, disabledPrefix, "enabled", val, true) err = setVariable(file, false, disabledPrefix, "enabled", val, true)
} }
@@ -290,14 +285,14 @@ func makeModule(file *bpFile, t string) {
Name: t, Name: t,
Pos: file.module.LbracePos, Pos: file.module.LbracePos,
} }
file.module.RbracePos = file.pos file.module.RbracePos = file.bpPos
file.defs = append(file.defs, file.module) file.defs = append(file.defs, file.module)
file.inModule = false file.inModule = false
} }
func resetModule(file *bpFile) { func resetModule(file *bpFile) {
file.module = &bpparser.Module{} file.module = &bpparser.Module{}
file.module.LbracePos = file.pos file.module.LbracePos = file.bpPos
file.localAssignments = make(map[string]*bpparser.Property) file.localAssignments = make(map[string]*bpparser.Property)
file.inModule = true file.inModule = true
} }
@@ -331,7 +326,7 @@ func setVariable(file *bpFile, plusequals bool, prefix, name string, value *bppa
name = prefix + "." + name name = prefix + "." + name
} }
pos := file.pos pos := file.bpPos
var oldValue *bpparser.Value var oldValue *bpparser.Value
if local { if local {