forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.go
212 lines (198 loc) · 4.56 KB
/
scanner.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package gno
import (
"fmt"
)
type runestate int
const (
runestateCode runestate = 0
runestateRune runestate = 1
runestateStringQuote runestate = 2
runestateStringBacktick runestate = 3
)
type scanner struct {
str string
rnz []rune
idx int
runestate
curly int
round int
square int
}
// returns a new scanner.
func newScanner(str string) *scanner {
rnz := make([]rune, 0, len(str))
for _, r := range str {
rnz = append(rnz, r)
}
return &scanner{
str: str,
runestate: runestateCode,
rnz: rnz,
}
}
// Peeks the next n runes and returns a string. returns a shorter string if
// there are less than n runes left.
func (ss *scanner) peek(n int) string {
if ss.idx+n > len(ss.rnz) {
return string(ss.rnz[ss.idx:len(ss.rnz)])
}
return string(ss.rnz[ss.idx : ss.idx+n])
}
// Advance a single rune, e.g. by incrementing ss.curly if ss.rnz[ss.idx] is
// '{' before advancing. If ss.runestate is runestateRune or runestateQuote,
// advances escape sequences to completion so ss.idx may increment more than
// one. Returns true if done.
func (ss *scanner) advance() bool {
rn := ss.rnz[ss.idx] // just panic if out of scope, caller error.
switch ss.runestate {
case runestateCode:
switch rn {
case '}':
ss.curly--
if ss.curly < 0 {
panic("mismatched curly: " + ss.str)
}
case ')':
ss.round--
if ss.round < 0 {
panic("mismatched round: " + ss.str)
}
case ']':
ss.square--
if ss.square < 0 {
panic("mismatched square: " + ss.str)
}
case '{':
ss.curly++
case '(':
ss.round++
case '[':
ss.square++
case '\'':
ss.runestate = runestateRune
case '"':
ss.runestate = runestateStringQuote
case '`':
ss.runestate = runestateStringBacktick
}
case runestateRune:
switch rn {
case '\\':
return ss.advanceEscapeSequence()
case '\'':
ss.runestate = runestateCode
}
case runestateStringQuote:
switch rn {
case '\\':
return ss.advanceEscapeSequence()
case '"':
ss.runestate = runestateCode
}
case runestateStringBacktick:
switch rn {
case '`':
ss.runestate = runestateCode
}
}
ss.idx++
return ss.done()
}
// returns true if no runes left to advance.
func (ss *scanner) done() bool {
return ss.idx == len(ss.rnz)
}
// returns true if outside the scope of any
// parentheses, brackets, strings, or rune literals.
func (ss *scanner) out() bool {
return ss.runestate == runestateCode &&
ss.curly == int(0) &&
ss.round == int(0) &&
ss.square == int(0)
}
func isOctal(r rune) bool {
switch r {
case '0', '1', '2', '3', '4', '5', '6', '7':
return true
default:
return false
}
}
func isHex(r rune) bool {
switch r {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f',
'A', 'B', 'C', 'D', 'E', 'F':
return true
default:
return false
}
}
// Advances runes, while checking that each passes `check`. if error, panics
// with info including `back` runes back.
func (ss *scanner) eatRunes(back int, eat int, check func(rune) bool) {
for i := 0; i < eat; i++ {
if ss.idx+i == len(ss.rnz) {
panic(fmt.Sprintf("eof while parsing: %s",
string(ss.rnz[ss.idx-back:])))
}
if !check(ss.rnz[ss.idx+i]) {
panic(fmt.Sprintf("invalid character while parsing: %s",
string(ss.rnz[ss.idx-back:ss.idx+i+1])))
}
ss.idx++
}
}
// increments ss.idx until escape sequence is complete. returns true if done.
func (ss *scanner) advanceEscapeSequence() bool {
rn1 := ss.rnz[ss.idx]
if rn1 != '\\' {
panic("should not happen")
}
if ss.idx == len(ss.rnz)-1 {
panic("eof while parsing escape sequence")
}
rn2 := ss.rnz[ss.idx+1]
switch rn2 {
case 'x':
ss.idx += 2
ss.eatRunes(2, 2, isHex)
return ss.done()
case 'u':
ss.idx += 2
ss.eatRunes(2, 4, isHex)
return ss.done()
case 'U':
ss.idx += 2
ss.eatRunes(2, 8, isHex)
return ss.done()
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', '\'', '"':
ss.idx += 2
return ss.done()
default:
ss.idx += 1
if isOctal(rn2) {
ss.eatRunes(1, 3, isOctal)
} else {
panic("invalid escape sequence")
}
return ss.done()
}
}
// pops the next monoid term.
// The result is a string enclosed in balanced parentheses,
// brackets, or quotes; or what comes before such things.
// scanner doesn't understand operators, so a polynomial
// expression could be a single monoid as far as this scanner
// is concerned. TODO Chop functions should maybe use this.
func (ss *scanner) popMonoid() string {
startOut := ss.out()
start := ss.idx
for !ss.advance() {
if ss.out() != startOut {
end := ss.idx
return string(ss.rnz[start:end])
}
}
panic("no monoid")
}