blob: 72b7c786f888b44636a05b25af2578fcd7ca290f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package ast
import (
"go.neonxp.ru/conf/internal/parser"
"modernc.org/scanner"
)
func Parse(p *parser.Parser, data []int32) []*Node {
nodes := make([]*Node, 0, 2)
for len(data) != 0 {
next := int32(1)
var node *Node
switch n := data[0]; {
case n < 0:
next = 2 + data[1]
node = &Node{
Symbol: parser.Symbol(-data[0]),
Children: Parse(p, data[2:next]),
}
default:
tok := p.Token(n)
node = &Node{
Token: tok,
Symbol: parser.Symbol(tok.Ch),
Source: tok.Src(),
Col: tok.Position().Column,
Line: tok.Position().Line,
}
}
nodes = append(nodes, node)
data = data[next:]
}
return nodes
}
type Node struct {
Symbol parser.Symbol
Token scanner.Token
Children []*Node
Source string
Col int
Line int
}
|