-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
158 lines (135 loc) · 3.61 KB
/
Copy pathparse.go
File metadata and controls
158 lines (135 loc) · 3.61 KB
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
package build
import (
"bufio"
"errors"
"io"
"log/slog"
"os"
"sync"
"github.com/ProCode-Software/klar/internal/lexer"
"github.com/ProCode-Software/klar/internal/parser"
"github.com/ProCode-Software/klar/internal/util"
)
// Compilation stops after exceeding this number of errors.
const MaxErrors = 10
var errMaxErrors = errors.New("max errors reached")
func (c *Compiler) parseFile(m *Module, file string,
reporterMu, moduleMu *sync.Mutex,
) error {
// Use [StdParser] if [Compiler.Parser] isn't set
if c.Parser == nil {
c.UseStdParser()
}
path := m.FilePath(file)
c.Debug("Parsing file", slog.String("file", path))
shortPath, res, err := c.Parser.Parse(path, c.Logger, m.Stdin)
if err != nil {
return err
}
moduleMu.Lock()
hasErrors, maxErrors := c.sendErrors(res.Errors)
if hasErrors {
m.Failed = true
c.Error("File has syntax errors", slog.String("file", path))
}
m.Programs[file] = res.Program
m.ModTimes[file] = res.ModTime
moduleMu.Unlock()
// Load tokens into error reporter
reporterMu.Lock()
c.Reporter.LoadFile(path, shortPath, res.Tokens)
reporterMu.Unlock()
if maxErrors {
return errMaxErrors
}
return nil
}
// Standard parser implementation
// ========
// StdParser is the default [Parser] implementation for Klar.
type StdParser struct {
*parsePool
cwd string
}
func NewStdParser(cwd string, parseOpts *parser.Options) *StdParser {
return &StdParser{parsePool: newParsePool(parseOpts), cwd: cwd}
}
func (p *StdParser) Reset() {
p.parsePool = nil
p.cwd = ""
}
const stdinName = "standardInput"
func (p *StdParser) Parse(filePath string, l *slog.Logger, stdin bool) (
shortPath string, res *ParseResult, err error,
) {
// Open file
// ==========
var f *os.File
var sizeEst int64
res = &ParseResult{}
if stdin {
// Read from standard input
f = os.Stdin
shortPath = filePath
l.Info("Reading file from stdin")
} else {
f, err = os.Open(filePath)
if err != nil {
l.Error("Error while opening file", slog.Any("error", err))
return "", nil, &FilesystemError{"open", filePath, err}
}
defer f.Close()
// Get file size and last modified time
stat, err := f.Stat()
if err != nil {
l.Error("Error while getting file info", slog.Any("error", err))
return shortPath, nil, &FilesystemError{"stat", filePath, err}
}
res.ModTime = stat.ModTime()
sizeEst = stat.Size() / 10
shortPath = util.RelPath(p.cwd, filePath) // Get relative path
}
// Tokenize
// =========
lex := p.GetLexer(f)
defer p.PutLexer(lex)
res.Tokens = lex.TokenizeAll(sizeEst)
// Parse
// ========
pa := p.GetParser(res.Tokens, filePath)
defer p.PutParser(pa)
res.Program = pa.Parse()
res.Errors = pa.Errors
return shortPath, res, nil
}
// Lexer/parser pool
// ========
// parsePool provides a pool of [lexer.Lexer] and [parser.Parser].
type parsePool struct{ parser, lexer sync.Pool }
// newParsePool creates a new [parsePool] with the provided
// [lexer.Flags] and [parser2.Options] as defaults.
func newParsePool(parseOpts *parser.Options) *parsePool {
return &parsePool{
lexer: sync.Pool{New: func() any { return lexer.NewLexer(nil) }},
parser: sync.Pool{New: func() any { return parser.New(nil, parseOpts) }},
}
}
func (p *parsePool) GetLexer(r io.Reader) *lexer.Lexer {
l := p.lexer.Get().(*lexer.Lexer)
l.Reader = bufio.NewReader(r)
return l
}
func (p *parsePool) PutLexer(l *lexer.Lexer) {
l.Reset()
p.lexer.Put(l)
}
func (p *parsePool) GetParser(tokens []lexer.Token, file string) *parser.Parser {
pa := p.parser.Get().(*parser.Parser)
pa.Tokens = tokens
pa.Options.File = file
return pa
}
func (p *parsePool) PutParser(pa *parser.Parser) {
pa.Reset()
p.parser.Put(pa)
}