ast.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. package parser
  15. type Pos int
  16. const NoPos Pos = 0
  17. type Node interface {
  18. Dump() string
  19. Pos() Pos
  20. End() Pos
  21. }
  22. type Assignment struct {
  23. Target *MakeString
  24. Name *MakeString
  25. Value *MakeString
  26. Type string
  27. }
  28. func (x *Assignment) Dump() string {
  29. target := ""
  30. if x.Target != nil {
  31. target = x.Target.Dump() + ": "
  32. }
  33. return target + x.Name.Dump() + " " + x.Type + " " + x.Value.Dump()
  34. }
  35. func (x *Assignment) Pos() Pos {
  36. if x.Target != nil {
  37. return x.Target.Pos()
  38. }
  39. return x.Name.Pos()
  40. }
  41. func (x *Assignment) End() Pos { return x.Value.End() }
  42. type Comment struct {
  43. CommentPos Pos
  44. Comment string
  45. }
  46. func (x *Comment) Dump() string {
  47. return "#" + x.Comment
  48. }
  49. func (x *Comment) Pos() Pos { return x.CommentPos }
  50. func (x *Comment) End() Pos { return Pos(int(x.CommentPos) + len(x.Comment)) }
  51. type Directive struct {
  52. NamePos Pos
  53. Name string
  54. Args *MakeString
  55. EndPos Pos
  56. }
  57. func (x *Directive) Dump() string {
  58. return x.Name + " " + x.Args.Dump()
  59. }
  60. func (x *Directive) Pos() Pos { return x.NamePos }
  61. func (x *Directive) End() Pos {
  62. if x.EndPos != NoPos {
  63. return x.EndPos
  64. }
  65. return x.Args.End()
  66. }
  67. type Rule struct {
  68. Target *MakeString
  69. Prerequisites *MakeString
  70. RecipePos Pos
  71. Recipe string
  72. }
  73. func (x *Rule) Dump() string {
  74. recipe := ""
  75. if x.Recipe != "" {
  76. recipe = "\n" + x.Recipe
  77. }
  78. return "rule: " + x.Target.Dump() + ": " + x.Prerequisites.Dump() + recipe
  79. }
  80. func (x *Rule) Pos() Pos { return x.Target.Pos() }
  81. func (x *Rule) End() Pos { return Pos(int(x.RecipePos) + len(x.Recipe)) }
  82. type Variable struct {
  83. Name *MakeString
  84. }
  85. func (x *Variable) Pos() Pos { return x.Name.Pos() }
  86. func (x *Variable) End() Pos { return x.Name.End() }
  87. func (x *Variable) Dump() string {
  88. return "$(" + x.Name.Dump() + ")"
  89. }
  90. // Sort interface for []Node by position
  91. type byPosition []Node
  92. func (s byPosition) Len() int {
  93. return len(s)
  94. }
  95. func (s byPosition) Swap(i, j int) {
  96. s[i], s[j] = s[j], s[i]
  97. }
  98. func (s byPosition) Less(i, j int) bool {
  99. return s[i].Pos() < s[j].Pos()
  100. }