-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_parser.go
More file actions
99 lines (91 loc) · 2.55 KB
/
Copy pathstatic_parser.go
File metadata and controls
99 lines (91 loc) · 2.55 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
package build
import (
"bytes"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"github.com/ProCode-Software/klar/internal/ast"
"github.com/ProCode-Software/klar/internal/lexer"
"github.com/ProCode-Software/klar/internal/module"
"github.com/ProCode-Software/klar/internal/parser"
)
// StaticParser is a [Parser] implementation that parses only a set of files.
// A reader, tokens, or an [ast.Program] may be provided for each file.
type StaticParser struct {
Files map[string]*StaticParserFile
Fallback *StdParser
}
type StaticParserFile struct {
Tokens []lexer.Token
Reader io.Reader
ShortPath string
Program *ast.Program
}
// NewStaticParser creates a new [StaticParser] that parses one file.
func NewStaticParser(cwd, path string, f *StaticParserFile) *StaticParser {
return &StaticParser{
Files: map[string]*StaticParserFile{path: f},
Fallback: NewStdParser(cwd, DefaultStdParserOptions),
}
}
func (p *StaticParser) LoadFile(path string, f *StaticParserFile) {
if p.Files == nil {
p.Files = make(map[string]*StaticParserFile)
}
p.Files[path] = f
}
func (p *StaticParser) SetFallbackCwd(cwd string) { p.Fallback.cwd = cwd }
// Parse implements [Parser]. It returns [os.ErrNotExist] if path
// is not found in the StaticParser's file map.
func (p *StaticParser) Parse(path string, l *slog.Logger, stdin bool) (
shortPath string, res *ParseResult, err error,
) {
if stdin {
return p.Fallback.Parse(path, l, true)
}
f, ok := p.Files[path]
if !ok {
// Fallback to [StdParser] for parsing modules in the standard library
if _, err := filepath.Rel(module.SystemDirs.Std, path); err == nil {
return p.Fallback.Parse(path, l, false)
}
return path, nil, fmt.Errorf("load file %s: %w", path, os.ErrNotExist)
}
if f.ShortPath == "" {
f.ShortPath = path
}
switch {
case f.Program != nil:
// Program already provided
if f.Tokens == nil {
panic("both Program and Tokens must be provided")
}
return f.ShortPath, &ParseResult{Tokens: f.Tokens, Program: f.Program}, nil
case f.Tokens == nil:
var size int64
switch r := f.Reader.(type) {
case nil:
panic("Reader must be provided if Tokens == nil")
case *os.File:
if stat, err := r.Stat(); err == nil {
size = stat.Size()
}
case *bytes.Buffer:
size = int64(r.Len())
}
f.Tokens = lexer.NewLexer(f.Reader).TokenizeAll(size / 10)
fallthrough
default:
// Need to parse
pa := parser.New(f.Tokens, nil)
pa.Options.File = path
prog := pa.Parse()
return f.ShortPath, &ParseResult{
Tokens: f.Tokens,
Program: prog,
Errors: pa.Errors,
}, nil
}
}