github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/androidmk/parser/ast.go (about) 1 // Copyright 2017 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package parser 16 17 type Pos int 18 19 const NoPos Pos = 0 20 21 type Node interface { 22 Dump() string 23 Pos() Pos 24 End() Pos 25 } 26 27 type Assignment struct { 28 Target *MakeString 29 Name *MakeString 30 Value *MakeString 31 Type string 32 } 33 34 func (x *Assignment) Dump() string { 35 target := "" 36 if x.Target != nil { 37 target = x.Target.Dump() + ": " 38 } 39 return target + x.Name.Dump() + " " + x.Type + " " + x.Value.Dump() 40 } 41 42 func (x *Assignment) Pos() Pos { 43 if x.Target != nil { 44 return x.Target.Pos() 45 } 46 return x.Name.Pos() 47 } 48 49 func (x *Assignment) End() Pos { return x.Value.End() } 50 51 type Comment struct { 52 CommentPos Pos 53 Comment string 54 } 55 56 func (x *Comment) Dump() string { 57 return "#" + x.Comment 58 } 59 60 func (x *Comment) Pos() Pos { return x.CommentPos } 61 func (x *Comment) End() Pos { return Pos(int(x.CommentPos) + len(x.Comment)) } 62 63 type Directive struct { 64 NamePos Pos 65 Name string 66 Args *MakeString 67 EndPos Pos 68 } 69 70 func (x *Directive) Dump() string { 71 return x.Name + " " + x.Args.Dump() 72 } 73 74 func (x *Directive) Pos() Pos { return x.NamePos } 75 func (x *Directive) End() Pos { 76 if x.EndPos != NoPos { 77 return x.EndPos 78 } 79 return x.Args.End() 80 } 81 82 type Rule struct { 83 Target *MakeString 84 Prerequisites *MakeString 85 RecipePos Pos 86 Recipe string 87 } 88 89 func (x *Rule) Dump() string { 90 recipe := "" 91 if x.Recipe != "" { 92 recipe = "\n" + x.Recipe 93 } 94 return "rule: " + x.Target.Dump() + ": " + x.Prerequisites.Dump() + recipe 95 } 96 97 func (x *Rule) Pos() Pos { return x.Target.Pos() } 98 func (x *Rule) End() Pos { return Pos(int(x.RecipePos) + len(x.Recipe)) } 99 100 type Variable struct { 101 Name *MakeString 102 } 103 104 func (x *Variable) Pos() Pos { return x.Name.Pos() } 105 func (x *Variable) End() Pos { return x.Name.End() } 106 107 func (x *Variable) Dump() string { 108 return "$(" + x.Name.Dump() + ")" 109 } 110 111 // Sort interface for []Node by position 112 type byPosition []Node 113 114 func (s byPosition) Len() int { 115 return len(s) 116 } 117 118 func (s byPosition) Swap(i, j int) { 119 s[i], s[j] = s[j], s[i] 120 } 121 122 func (s byPosition) Less(i, j int) bool { 123 return s[i].Pos() < s[j].Pos() 124 }