/
passtwo.go
278 lines (249 loc) · 7.98 KB
/
passtwo.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2018, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lex
import (
"github.com/goki/pi/token"
)
// PassTwo performs second pass(s) through the lexicalized version of the source,
// computing nesting depth for every token once and for all -- this is essential for
// properly matching tokens and also for colorization in syntax highlighting.
// Optionally, a subsequent pass finds end-of-statement (EOS) tokens, which are essential
// for parsing to first break the source down into statement-sized chunks. A separate
// list of EOS token positions is maintained for very fast access.
type PassTwo struct {
// should we perform EOS detection on this type of file?
DoEos bool `desc:"should we perform EOS detection on this type of file?"`
// use end-of-line as a default EOS, if nesting depth is same as start of line (python) -- see also EolToks
Eol bool `desc:"use end-of-line as a default EOS, if nesting depth is same as start of line (python) -- see also EolToks"`
// replace all semicolons with EOS to keep it consistent (C, Go..)
Semi bool `desc:"replace all semicolons with EOS to keep it consistent (C, Go..)"`
// use backslash as a line continuer (python)
Backslash bool `desc:"use backslash as a line continuer (python)"`
// if a right-brace } is detected anywhere in the line, insert an EOS *before* RBrace AND after it (needed for Go) -- do not include RBrace in EolToks in this case
RBraceEos bool `desc:"if a right-brace } is detected anywhere in the line, insert an EOS *before* RBrace AND after it (needed for Go) -- do not include RBrace in EolToks in this case"`
// specific tokens to recognize at the end of a line that trigger an EOS (Go)
EolToks token.KeyTokenList `desc:"specific tokens to recognize at the end of a line that trigger an EOS (Go)"`
}
// TwoState is the state maintained for the PassTwo process
type TwoState struct {
// position in lex tokens we're on
Pos Pos `desc:"position in lex tokens we're on"`
// file that we're operating on
Src *File `desc:"file that we're operating on"`
// stack of nesting tokens
NestStack []token.Tokens `desc:"stack of nesting tokens"`
// any error messages accumulated during lexing specifically
Errs ErrorList `desc:"any error messages accumulated during lexing specifically"`
}
// Init initializes state for a new pass -- called at start of NestDepth
func (ts *TwoState) Init() {
ts.Pos = PosZero
ts.NestStack = ts.NestStack[0:0]
}
// SetSrc sets the source we're operating on
func (ts *TwoState) SetSrc(src *File) {
ts.Src = src
}
// NextLine advances to next line
func (ts *TwoState) NextLine() {
ts.Pos.Ln++
ts.Pos.Ch = 0
}
// Error adds an passtwo error at current position
func (ts *TwoState) Error(msg string) {
ppos := ts.Pos
ppos.Ch--
clex := ts.Src.LexAtSafe(ppos)
ts.Errs.Add(Pos{ts.Pos.Ln, clex.St}, ts.Src.Filename, "PassTwo: "+msg, ts.Src.SrcLine(ts.Pos.Ln), nil)
}
// NestStackStr returns the token stack as strings
func (ts *TwoState) NestStackStr() string {
str := ""
for _, tok := range ts.NestStack {
switch tok {
case token.PunctGpLParen:
str += "paren ( "
case token.PunctGpLBrack:
str += "bracket [ "
case token.PunctGpLBrace:
str += "brace { "
}
}
return str
}
/////////////////////////////////////////////////////////////////////
// PassTwo
// Error adds an passtwo error at given position
func (pt *PassTwo) Error(ts *TwoState, msg string) {
ts.Error(msg)
}
// HasErrs reports if there are errors in eosing process
func (pt *PassTwo) HasErrs(ts *TwoState) bool {
return len(ts.Errs) > 0
}
// MismatchError reports a mismatch for given type of parentheses / bracket
func (pt *PassTwo) MismatchError(ts *TwoState, tok token.Tokens) {
switch tok {
case token.PunctGpRParen:
pt.Error(ts, "mismatching parentheses -- right paren ')' without matching left paren '('")
case token.PunctGpRBrack:
pt.Error(ts, "mismatching square brackets -- right bracket ']' without matching left bracket '['")
case token.PunctGpRBrace:
pt.Error(ts, "mismatching curly braces -- right brace '}' without matching left bracket '{'")
}
}
// PushNest pushes a nesting left paren / bracket onto stack
func (pt *PassTwo) PushNest(ts *TwoState, tok token.Tokens) {
ts.NestStack = append(ts.NestStack, tok)
}
// PopNest attempts to pop given token off of nesting stack, generating error if it mismatches
func (pt *PassTwo) PopNest(ts *TwoState, tok token.Tokens) {
sz := len(ts.NestStack)
if sz == 0 {
pt.MismatchError(ts, tok)
return
}
cur := ts.NestStack[sz-1]
ts.NestStack = ts.NestStack[:sz-1] // better to clear than keep even if err
if cur != tok.PunctGpMatch() {
pt.MismatchError(ts, tok)
}
}
// Perform nesting depth computation
func (pt *PassTwo) NestDepth(ts *TwoState) {
ts.Init()
nlines := ts.Src.NLines()
if nlines == 0 {
return
}
// if len(ts.Src.Lexs[nlines-1]) > 0 { // last line ends with tokens -- parser needs empty last line..
// ts.Src.Lexs = append(ts.Src.Lexs, Line{})
// *ts.Src.Lines = append(*ts.Src.Lines, []rune{})
// }
for ts.Pos.Ln < nlines {
sz := len(ts.Src.Lexs[ts.Pos.Ln])
if sz == 0 {
ts.NextLine()
continue
}
lx := ts.Src.LexAt(ts.Pos)
tok := lx.Tok.Tok
if tok.IsPunctGpLeft() {
lx.Tok.Depth = len(ts.NestStack) // depth increments AFTER -- this turns out to be ESSENTIAL!
pt.PushNest(ts, tok)
} else if tok.IsPunctGpRight() {
pt.PopNest(ts, tok)
lx.Tok.Depth = len(ts.NestStack) // end has same depth as start, which is same as SURROUND
} else {
lx.Tok.Depth = len(ts.NestStack)
}
ts.Pos.Ch++
if ts.Pos.Ch >= sz {
ts.NextLine()
}
}
stsz := len(ts.NestStack)
if stsz > 0 {
pt.Error(ts, "mismatched grouping -- end of file with these left unmatched: "+ts.NestStackStr())
}
}
// Perform nesting depth computation on only one line, starting at
// given initial depth -- updates the given line
func (pt *PassTwo) NestDepthLine(line Line, initDepth int) {
sz := len(line)
if sz == 0 {
return
}
depth := initDepth
for i := 0; i < sz; i++ {
lx := &line[i]
tok := lx.Tok.Tok
if tok.IsPunctGpLeft() {
lx.Tok.Depth = depth
depth++
} else if tok.IsPunctGpRight() {
depth--
lx.Tok.Depth = depth
} else {
lx.Tok.Depth = depth
}
}
}
// Perform EOS detection
func (pt *PassTwo) EosDetect(ts *TwoState) {
nlines := ts.Src.NLines()
pt.EosDetectPos(ts, PosZero, nlines)
}
// Perform EOS detection at given starting position, for given number of lines
func (pt *PassTwo) EosDetectPos(ts *TwoState, pos Pos, nln int) {
ts.Pos = pos
nlines := ts.Src.NLines()
ok := false
for lc := 0; ts.Pos.Ln < nlines && lc < nln; lc++ {
sz := len(ts.Src.Lexs[ts.Pos.Ln])
if sz == 0 {
ts.NextLine()
continue
}
if pt.Semi {
for ts.Pos.Ch = 0; ts.Pos.Ch < sz; ts.Pos.Ch++ {
lx := ts.Src.LexAt(ts.Pos)
if lx.Tok.Tok == token.PunctSepSemicolon {
ts.Src.ReplaceEos(ts.Pos)
}
}
}
if pt.RBraceEos {
skip := false
for ci := 0; ci < sz; ci++ {
lx := ts.Src.LexAt(Pos{ts.Pos.Ln, ci})
if lx.Tok.Tok == token.PunctGpRBrace {
if ci == 0 {
ip := Pos{ts.Pos.Ln, 0}
ip, ok = ts.Src.PrevTokenPos(ip)
if ok {
ilx := ts.Src.LexAt(ip)
if ilx.Tok.Tok != token.PunctGpLBrace && ilx.Tok.Tok != token.EOS {
ts.Src.InsertEos(ip)
}
}
} else {
ip := Pos{ts.Pos.Ln, ci - 1}
ilx := ts.Src.LexAt(ip)
if ilx.Tok.Tok != token.PunctGpLBrace {
ts.Src.InsertEos(ip)
ci++
sz++
}
}
if ci == sz-1 {
ip := Pos{ts.Pos.Ln, ci}
ts.Src.InsertEos(ip)
sz++
skip = true
}
}
}
if skip {
ts.NextLine()
continue
}
}
ep := Pos{ts.Pos.Ln, sz - 1} // end of line token
elx := ts.Src.LexAt(ep)
if pt.Eol {
sp := Pos{ts.Pos.Ln, 0} // start of line token
slx := ts.Src.LexAt(sp)
if slx.Tok.Depth == elx.Tok.Depth {
ts.Src.InsertEos(ep)
}
}
if len(pt.EolToks) > 0 { // not depth specific
if pt.EolToks.Match(elx.Tok) {
ts.Src.InsertEos(ep)
}
}
ts.NextLine()
}
}