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
15package parser
16
17type Pos int
18
19const NoPos Pos = 0
20
21type Node interface {
22	Dump() string
23	Pos() Pos
24	End() Pos
25}
26
27type Assignment struct {
28	Target *MakeString
29	Name   *MakeString
30	Value  *MakeString
31	Type   string
32}
33
34func (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
42func (x *Assignment) Pos() Pos {
43	if x.Target != nil {
44		return x.Target.Pos()
45	}
46	return x.Name.Pos()
47}
48
49func (x *Assignment) End() Pos { return x.Value.End() }
50
51type Comment struct {
52	CommentPos Pos
53	Comment    string
54}
55
56func (x *Comment) Dump() string {
57	return "#" + x.Comment
58}
59
60func (x *Comment) Pos() Pos { return x.CommentPos }
61func (x *Comment) End() Pos { return Pos(int(x.CommentPos) + len(x.Comment)) }
62
63type Directive struct {
64	NamePos Pos
65	Name    string
66	Args    *MakeString
67	EndPos  Pos
68}
69
70func (x *Directive) Dump() string {
71	return x.Name + " " + x.Args.Dump()
72}
73
74func (x *Directive) Pos() Pos { return x.NamePos }
75func (x *Directive) End() Pos {
76	if x.EndPos != NoPos {
77		return x.EndPos
78	}
79	return x.Args.End()
80}
81
82type Rule struct {
83	Target        *MakeString
84	Prerequisites *MakeString
85	RecipePos     Pos
86	Recipe        string
87}
88
89func (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
97func (x *Rule) Pos() Pos { return x.Target.Pos() }
98func (x *Rule) End() Pos { return Pos(int(x.RecipePos) + len(x.Recipe)) }
99
100type Variable struct {
101	Name *MakeString
102}
103
104func (x *Variable) Pos() Pos { return x.Name.Pos() }
105func (x *Variable) End() Pos { return x.Name.End() }
106
107func (x *Variable) Dump() string {
108	return "$(" + x.Name.Dump() + ")"
109}
110
111// Sort interface for []Node by position
112type byPosition []Node
113
114func (s byPosition) Len() int {
115	return len(s)
116}
117
118func (s byPosition) Swap(i, j int) {
119	s[i], s[j] = s[j], s[i]
120}
121
122func (s byPosition) Less(i, j int) bool {
123	return s[i].Pos() < s[j].Pos()
124}
125