-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
184 lines (159 loc) · 3.67 KB
/
parser.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package liquid
// https://github.com/Shopify/liquid/blob/master/lib/liquid/parser.rb
import (
"errors"
"fmt"
)
// Parser errors
var (
ErrIndexOutOfBounds = errors.New("No tokens remaining")
)
// Parser objects parse the contents of liquid tags
type Parser struct {
tokens []Token
index uint64
}
// NewParser generates a parser object for consuming tokens
func NewParser(input string) (*Parser, error) {
tokens, err := Lexer(input)
if err != nil {
return nil, err
}
return &Parser{tokens: tokens}, nil
}
func (p *Parser) consume(tType string) (string, error) {
if int(p.index) >= len(p.tokens) {
return "", ErrIndexOutOfBounds
}
token := p.tokens[p.index]
if tType != "" && token.name != tType {
return "", fmt.Errorf("Expected %v but found %v:%v", tType, token.name, token.value)
}
p.index++
return token.value, nil
}
// analog to `consume?` in the ruby
func (p *Parser) tryConsume(tType string) bool {
if int(p.index) >= len(p.tokens) {
return false
}
token := p.tokens[p.index]
if tType != "" && token.name != tType {
return false
}
p.index++
return true
}
func (p *Parser) jump(count uint64) error {
p.index += count
if int(p.index) >= len(p.tokens) {
return ErrIndexOutOfBounds
}
return nil
}
func (p *Parser) lookahead(tType string, offset uint64) (bool, error) {
index := p.index + offset
if int(index) >= len(p.tokens) {
return false, ErrIndexOutOfBounds
}
token := p.tokens[index]
return token.name == tType, nil
}
func (p *Parser) expression() (string, error) {
token := p.tokens[p.index]
if token.name == tIdentifier {
return p.variableSignature()
} else if token.name == tSingleStringLiteral || token.name == tDoubleStringLiteral || token.name == tNumberLiteral {
return p.consume(token.name)
} else if token.name == tOpenRound {
p.consume(token.name)
first, err := p.expression()
if err != nil {
return "", err
}
p.consume(tDotDot)
last, err := p.expression()
if err != nil {
return "", err
}
_, err = p.consume(tCloseRound)
return fmt.Sprintf("(%v..%v)", first, last), err
}
return "", fmt.Errorf("%v is not a valid expression", token)
}
func (p *Parser) variableSignature() (string, error) {
result, err := p.consume(tIdentifier)
if err != nil {
return "", err
}
inSquare, err := p.lookahead(tOpenSquare, 0)
for inSquare {
open, err := p.consume("")
if err != nil {
return "", err
}
result += open
expr, err := p.expression()
if err != nil {
return "", err
}
result += expr
close, err := p.consume(tCloseSquare)
if err != nil {
fmt.Println("ALMOST, got:", result)
return "", err
}
result += close
inSquare, err = p.lookahead(tOpenSquare, 0)
if err != nil {
return "", err
}
}
dotNext, err := p.lookahead(tDot, 0)
if err != nil {
return "", err
}
if dotNext {
value, err := p.consume(tDot)
if err != nil {
return "", err
}
result += value
vsig, err := p.variableSignature()
if err != nil {
return "", err
}
result += vsig
}
return result, nil
}
func (p *Parser) argument() (string, error) {
var arg string
idNext, err := p.lookahead(tIdentifier, 0)
if err != nil {
return "", err
}
colonAfter, err := p.lookahead(tColon, 1)
if err != nil {
return "", err
}
// check for a keyword argument (identifier: expression)
if idNext && colonAfter {
keyword, _ := p.consume(tIdentifier)
arg += keyword
colon, _ := p.consume(tColon)
arg += colon
arg += " "
}
expr, err := p.expression()
return arg + expr, err
// def argument
// str = ""
// # might be a keyword argument (identifier: expression)
// if look(:id) && look(:colon, 1)
// str << consume << consume << ' '.freeze
// end
// str << expression
// str
// end
}