Debian Codegraph @

pkg/makefile/parser.go

raw symbols SCIP
1 // Copyright (c) Bartłomiej Płotka @bwplotka
2 // Licensed under the Apache License 2.0.
3
4 package makefile
5
6 import (
7 "io"
8 "os"
9 "path/filepath"
10 "strings"
11
12 "github.com/efficientgo/core/errors"
13 )
14
15 // Copied from https://github.com/tj/mmake/blob/b15229aac1a8ea3f0875f064a0864f7250bd7850/parser & improved.
16
17 // Node interface.
18 type Node interface {
19 Lines() []int
20 }
21
22 type node struct {
23 lines []int
24 }
25
26 func (n node) Lines() []int {
27 return n.lines
28 }
29
30 // Comment node.
31 type Comment struct {
32 node
33
34 Target string
35 Value string
36 Default bool
37 }
38
39 // Include node.
40 type Include struct {
41 node
42
43 Value string
44 }
45
46 // Parser is a quick-n-dirty Makefile "parser", not
47 // really, just comments and a few directives, but
48 // you'll forgive me.
49 type Parser struct {
50 i int
51 lines []string
52 nodeBuf node
53 commentBuf []string
54 target string
55 nodes []Node
56 }
57
58 // Parse the given input reader.
59 // TODO(bwplotka): Streaming version would be nice.
60 func (p *Parser) Parse(r io.Reader) ([]Node, error) {
61 b, err := io.ReadAll(r)
62 if err != nil {
63 return nil, errors.Wrap(err, "reading")
64 }
65
66 p.lines = strings.Split(string(b), "\n")
67
68 if err := p.parse(); err != nil {
69 return nil, errors.Wrap(err, "parsing")
70 }
71
72 return p.nodes, nil
73 }
74
75 // Peek at the next line.
76 func (p *Parser) peek() string {
77 return p.lines[p.i]
78 }
79
80 // Advance the next line.
81 func (p *Parser) advance() string {
82 s := p.lines[p.i]
83 p.i++
84 return s
85 }
86
87 // Buffer comment.
88 func (p *Parser) bufferComment() {
89 s := p.advance()[1:]
90
91 if len(s) > 0 {
92 if s[0] == '-' {
93 return
94 }
95
96 // leading space
97 if s[0] == ' ' {
98 s = s[1:]
99 }
100 }
101 p.commentBuf = append(p.commentBuf, s)
102 }
103
104 // Push comment node.
105 func (p *Parser) pushComment() {
106 if len(p.commentBuf) == 0 {
107 return
108 }
109
110 s := strings.Join(p.commentBuf, "\n")
111
112 p.nodes = append(p.nodes, Comment{
113 node: p.nodeBuf,
114 Target: p.target,
115 Value: strings.Trim(s, "\n"),
116 })
117 p.nodeBuf = node{}
118 p.commentBuf = nil
119 p.target = ""
120 }
121
122 // Push include node.
123 func (p *Parser) pushInclude() {
124 s := strings.Trim(strings.Replace(p.advance(), "include ", "", 1), " ")
125 p.nodeBuf.lines = append(p.nodeBuf.lines, p.i-1)
126 p.nodes = append(p.nodes, Include{
127 node: p.nodeBuf,
128 Value: s,
129 })
130 p.nodeBuf = node{}
131 }
132
133 // Parse the input.
134 func (p *Parser) parse() error {
135 for {
136 switch {
137 case p.i == len(p.lines)-1:
138 return nil
139 case strings.HasPrefix(p.peek(), ".PHONY"):
140 p.advance()
141 case len(p.peek()) == 0:
142 p.pushComment()
143 p.advance()
144 case p.peek()[0] == '#':
145 p.bufferComment()
146 case strings.HasPrefix(p.peek(), "include "):
147 p.pushInclude()
148 case strings.ContainsRune(p.peek(), ':'):
149 p.target = strings.Split(p.advance(), ":")[0]
150 p.nodeBuf.lines = append(p.nodeBuf.lines, p.i-1)
151 p.pushComment()
152 default:
153 p.advance()
154 }
155 }
156 }
157
158 // Parse the given input.
159 func Parse(r io.Reader) ([]Node, error) {
160 return (&Parser{}).Parse(r)
161 }
162
163 // ParseRecursive parses the given input recursively
164 // relative to the given dir such as /usr/local/include.
165 func ParseRecursive(r io.Reader, dir string) ([]Node, error) {
166 nodes, err := parseRecursiveHelper(r, dir)
167
168 for i := range nodes {
169 defaultComment, ok := nodes[i].(Comment)
170 if !ok {
171 continue
172 }
173
174 defaultComment.Default = true
175 nodes[i] = Comment{
176 Target: defaultComment.Target,
177 Value: defaultComment.Value,
178 Default: true,
179 }
180 break
181 }
182
183 return nodes, err
184 }
185
186 func parseRecursiveHelper(r io.Reader, dir string) ([]Node, error) {
187 nodes, err := Parse(r)
188
189 if err != nil {
190 return nil, errors.Wrap(err, "parsing")
191 }
192
193 otherNodes := []Node{}
194 for _, n := range nodes {
195 otherNodes = append(otherNodes, n)
196
197 inc, ok := n.(Include)
198
199 if !ok {
200 continue
201 }
202
203 path := filepath.Join(dir, inc.Value)
204 f, err := os.Open(path)
205 if err != nil {
206 return nil, errors.Wrapf(err, "opening %q", path)
207 }
208
209 more, err := parseRecursiveHelper(f, dir)
210
211 if err != nil {
212 return nil, errors.Wrapf(err, "parsing %q", path)
213 }
214
215 otherNodes = append(otherNodes, more...)
216
217 f.Close()
218 }
219
220 return otherNodes, nil
221 }