Improve makefile parser
Improve the androidmk makefile parser based on ideas from go/ast and friends: - Use type switching instead of the As* mess - Don't store endPos for every node, compute it based on the last known position in the node plus the length of the last token - Store positions as only the offset into the file, and then unpack them into Line/Column scanner.Position objects later Change-Id: I87eb6661859951e6c2ea5a85db6229fa5561d615
This commit is contained in:
@@ -2,7 +2,6 @@ package parser
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"text/scanner"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
@@ -18,18 +17,30 @@ import (
|
||||
// of Variables. The raw string list is always one longer than the variable
|
||||
// list.
|
||||
type MakeString struct {
|
||||
Pos scanner.Position
|
||||
StringPos Pos
|
||||
Strings []string
|
||||
Variables []Variable
|
||||
}
|
||||
|
||||
func SimpleMakeString(s string, pos scanner.Position) *MakeString {
|
||||
func SimpleMakeString(s string, pos Pos) *MakeString {
|
||||
return &MakeString{
|
||||
Pos: pos,
|
||||
Strings: []string{s},
|
||||
StringPos: pos,
|
||||
Strings: []string{s},
|
||||
}
|
||||
}
|
||||
|
||||
func (ms *MakeString) Pos() Pos {
|
||||
return ms.StringPos
|
||||
}
|
||||
|
||||
func (ms *MakeString) End() Pos {
|
||||
pos := ms.StringPos
|
||||
if len(ms.Strings) > 1 {
|
||||
pos = ms.Variables[len(ms.Variables)-1].End()
|
||||
}
|
||||
return Pos(int(pos) + len(ms.Strings[len(ms.Strings)-1]))
|
||||
}
|
||||
|
||||
func (ms *MakeString) appendString(s string) {
|
||||
if len(ms.Strings) == 0 {
|
||||
ms.Strings = []string{s}
|
||||
@@ -97,7 +108,7 @@ func (ms *MakeString) Split(sep string) []*MakeString {
|
||||
func (ms *MakeString) SplitN(sep string, n int) []*MakeString {
|
||||
ret := []*MakeString{}
|
||||
|
||||
curMs := SimpleMakeString("", ms.Pos)
|
||||
curMs := SimpleMakeString("", ms.Pos())
|
||||
|
||||
var i int
|
||||
var s string
|
||||
@@ -115,7 +126,7 @@ func (ms *MakeString) SplitN(sep string, n int) []*MakeString {
|
||||
|
||||
for _, r := range split[1:] {
|
||||
ret = append(ret, curMs)
|
||||
curMs = SimpleMakeString(r, ms.Pos)
|
||||
curMs = SimpleMakeString(r, ms.Pos())
|
||||
}
|
||||
} else {
|
||||
curMs.appendString(s)
|
||||
@@ -131,7 +142,9 @@ func (ms *MakeString) SplitN(sep string, n int) []*MakeString {
|
||||
}
|
||||
|
||||
func (ms *MakeString) TrimLeftSpaces() {
|
||||
l := len(ms.Strings[0])
|
||||
ms.Strings[0] = strings.TrimLeftFunc(ms.Strings[0], unicode.IsSpace)
|
||||
ms.StringPos += Pos(len(ms.Strings[0]) - l)
|
||||
}
|
||||
|
||||
func (ms *MakeString) TrimRightSpaces() {
|
||||
|
Reference in New Issue
Block a user