/
parser.go
314 lines (283 loc) · 8.21 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pi
//go:generate core generate -add-types
import (
"encoding/json"
"fmt"
"log"
"log/slog"
"os"
"time"
"cogentcore.org/core/fi"
"cogentcore.org/core/ki"
"cogentcore.org/core/pi/lex"
"cogentcore.org/core/pi/parse"
)
// Parser is the overall parser for managing the parsing
type Parser struct {
// lexer rules for first pass of lexing file
Lexer lex.Rule
// second pass after lexing -- computes nesting depth and EOS finding
PassTwo lex.PassTwo
// parser rules for parsing lexed tokens
Parser parse.Rule
// file name for overall parser (not file being parsed!)
Filename string
// if true, reports errors after parsing, to stdout
ReportErrs bool
// when loaded from file, this is the modification time of the parser -- re-processes cache if parser is newer than cached files
ModTime time.Time `json:"-" xml:"-"`
}
// Init initializes the parser -- must be called after creation
func (pr *Parser) Init() {
pr.Lexer.InitName(&pr.Lexer, "Lexer")
pr.Parser.InitName(&pr.Parser, "Parser")
}
// NewParser returns a new initialized parser
func NewParser() *Parser {
pr := &Parser{}
pr.Init()
return pr
}
// InitAll initializes everything about the parser -- call this when setting up a new
// parser after it has been loaded etc
func (pr *Parser) InitAll() {
fs := &FileState{} // dummy, for error recording
fs.Init()
pr.Lexer.CompileAll(&fs.LexState)
pr.Lexer.Validate(&fs.LexState)
pr.Parser.CompileAll(&fs.ParseState)
pr.Parser.Validate(&fs.ParseState)
}
// LexInit gets the lexer ready to start lexing
func (pr *Parser) LexInit(fs *FileState) {
fs.LexState.Init()
fs.LexState.Time.Now()
fs.TwoState.Init()
if fs.Src.NLines() > 0 {
fs.LexState.SetLine(fs.Src.Lines[0])
}
}
// LexNext does next step of lexing -- returns lowest-level rule that
// matched, and nil when nomatch err or at end of source input
func (pr *Parser) LexNext(fs *FileState) *lex.Rule {
if fs.LexState.Ln >= fs.Src.NLines() {
return nil
}
for {
if fs.LexState.AtEol() {
fs.Src.SetLine(fs.LexState.Ln, fs.LexState.Lex, fs.LexState.Comments, fs.LexState.Stack)
fs.LexState.Ln++
if fs.LexState.Ln >= fs.Src.NLines() {
return nil
}
fs.LexState.SetLine(fs.Src.Lines[fs.LexState.Ln])
}
mrule := pr.Lexer.LexStart(&fs.LexState)
if mrule != nil {
return mrule
}
if !fs.LexState.AtEol() { // err
break
}
}
return nil
}
// LexNextLine does next line of lexing -- returns lowest-level rule that
// matched at end, and nil when nomatch err or at end of source input
func (pr *Parser) LexNextLine(fs *FileState) *lex.Rule {
if fs.LexState.Ln >= fs.Src.NLines() {
return nil
}
var mrule *lex.Rule
for {
if fs.LexState.AtEol() {
fs.Src.SetLine(fs.LexState.Ln, fs.LexState.Lex, fs.LexState.Comments, fs.LexState.Stack)
fs.LexState.Ln++
if fs.LexState.Ln >= fs.Src.NLines() {
return nil
}
fs.LexState.SetLine(fs.Src.Lines[fs.LexState.Ln])
return mrule
}
mrule = pr.Lexer.LexStart(&fs.LexState)
if mrule == nil {
return nil
}
}
return mrule
}
// LexRun keeps running LextNext until it stops
func (pr *Parser) LexRun(fs *FileState) {
for {
if pr.LexNext(fs) == nil {
break
}
}
}
// LexLine runs lexer for given single line of source, which is updated
// from the given text (if non-nil)
// Returns merged regular and token comment lines, cloned and ready for use.
func (pr *Parser) LexLine(fs *FileState, ln int, txt []rune) lex.Line {
nlines := fs.Src.NLines()
if ln >= nlines || ln < 0 {
return nil
}
fs.Src.SetLineSrc(ln, txt)
fs.LexState.SetLine(fs.Src.Lines[ln])
pst := fs.Src.PrevStack(ln)
fs.LexState.Stack = pst.Clone()
for !fs.LexState.AtEol() {
mrule := pr.Lexer.LexStart(&fs.LexState)
if mrule == nil {
break
}
}
initDepth := fs.Src.PrevDepth(ln)
pr.PassTwo.NestDepthLine(fs.LexState.Lex, initDepth) // important to set this one's depth
fs.Src.SetLine(ln, fs.LexState.Lex, fs.LexState.Comments, fs.LexState.Stack) // before saving here
fs.TwoState.SetSrc(&fs.Src)
fs.Src.EosPos[ln] = nil // reset eos
pr.PassTwo.EosDetectPos(&fs.TwoState, lex.Pos{Ln: ln}, 1)
merge := lex.MergeLines(fs.LexState.Lex, fs.LexState.Comments)
mc := merge.Clone()
if len(fs.LexState.Comments) > 0 {
pr.PassTwo.NestDepthLine(mc, initDepth)
}
return mc
}
// DoPassTwo does the second pass after lexing
func (pr *Parser) DoPassTwo(fs *FileState) {
fs.TwoState.SetSrc(&fs.Src)
pr.PassTwo.NestDepth(&fs.TwoState)
if pr.PassTwo.DoEos {
pr.PassTwo.EosDetect(&fs.TwoState)
}
}
// LexAll runs a complete pass of the lexer and pass two, on current state
func (pr *Parser) LexAll(fs *FileState) {
pr.LexInit(fs)
// lprf := prof.Start("LexRun") // quite fast now..
pr.LexRun(fs)
// fs.LexErrReport()
// lprf.End()
pr.DoPassTwo(fs) // takes virtually no time
}
// ParserInit initializes the parser prior to running
func (pr *Parser) ParserInit(fs *FileState) bool {
fs.AnonCtr = 0
fs.ParseState.Init(&fs.Src, &fs.Ast)
return true
}
// ParseNext does next step of parsing -- returns lowest-level rule that matched
// or nil if no match error or at end
func (pr *Parser) ParseNext(fs *FileState) *parse.Rule {
mrule := pr.Parser.StartParse(&fs.ParseState)
return mrule
}
// ParseRun continues running the parser until the end of the file
func (pr *Parser) ParseRun(fs *FileState) {
for {
pr.Parser.StartParse(&fs.ParseState)
if fs.ParseState.AtEofNext() {
break
}
}
}
// ParseAll does full parsing, including ParseInit and ParseRun, assuming LexAll
// has been done already
func (pr *Parser) ParseAll(fs *FileState) {
if !pr.ParserInit(fs) {
return
}
pr.ParseRun(fs)
if pr.ReportErrs {
if fs.ParseHasErrs() {
fmt.Println(fs.ParseErrReport())
}
}
}
// ParseLine runs parser for given single line of source
// does Parsing in a separate FileState and returns that with
// Ast etc (or nil if nothing). Assumes LexLine has already
// been run on given line.
func (pr *Parser) ParseLine(fs *FileState, ln int) *FileState {
nlines := fs.Src.NLines()
if ln >= nlines || ln < 0 {
return nil
}
lfs := NewFileState()
lfs.Src.InitFromLine(&fs.Src, ln)
lfs.Src.EnsureFinalEos(0)
lfs.ParseState.Init(&lfs.Src, &lfs.Ast)
pr.ParseRun(lfs)
return lfs
}
// ParseString runs lexer and parser on given string of text, returning
// FileState of results (can be nil if string is empty or no lexical tokens).
// Also takes supporting contextual info for file / language that this string
// is associated with (only for reference)
func (pr *Parser) ParseString(str string, fname string, sup fi.Known) *FileState {
if str == "" {
return nil
}
lfs := NewFileState()
lfs.Src.InitFromString(str, fname, sup)
// lfs.ParseState.Trace.FullOn()
// lfs.ParseSTate.Trace.StdOut()
lfs.ParseState.Init(&lfs.Src, &lfs.Ast)
pr.LexAll(lfs)
lxs := lfs.Src.Lexs[0]
if len(lxs) == 0 {
return nil
}
lfs.Src.EnsureFinalEos(0)
pr.ParseAll(lfs)
return lfs
}
// ReadJSON opens lexer and parser rules from Bytes, in a standard JSON-formatted file
func (pr *Parser) ReadJSON(b []byte) error {
err := json.Unmarshal(b, pr)
ki.UnmarshalPost(pr.Lexer.This())
ki.UnmarshalPost(pr.Parser.This())
if err != nil {
slog.Error(err.Error())
}
return err
}
// OpenJSON opens lexer and parser rules to current filename, in a standard JSON-formatted file
func (pr *Parser) OpenJSON(filename string) error {
b, err := os.ReadFile(filename)
if err != nil {
return err
}
return pr.ReadJSON(b)
}
// SaveJSON saves lexer and parser rules, in a standard JSON-formatted file
func (pr *Parser) SaveJSON(filename string) error {
b, err := json.MarshalIndent(pr, "", " ")
if err != nil {
log.Println(err) // unlikely
return err
}
err = os.WriteFile(filename, b, 0644)
if err != nil {
log.Println(err)
}
return err
}
// SaveGrammar saves lexer and parser grammar rules to BNF-like .pig file
func (pr *Parser) SaveGrammar(filename string) error {
ofl, err := os.Create(filename)
if err != nil {
log.Println(err)
return err
}
fmt.Fprintf(ofl, "// %v Lexer\n\n", filename)
pr.Lexer.WriteGrammar(ofl, 0)
fmt.Fprintf(ofl, "\n\n///////////////////////////////////////////////////\n// %v Parser\n\n", filename)
pr.Parser.WriteGrammar(ofl, 0)
return ofl.Close()
}