-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
159 lines (148 loc) · 3.31 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"github.com/raiguard/luapls/lsp"
"github.com/raiguard/luapls/lua/ast"
"github.com/raiguard/luapls/lua/lexer"
"github.com/raiguard/luapls/lua/parser"
"github.com/raiguard/luapls/lua/token"
"github.com/raiguard/luapls/lua/types"
"github.com/raiguard/luapls/repl"
"github.com/tliron/kutil/util"
)
func main() {
args := os.Args
task := "lsp"
if len(args) > 1 {
task = args[1]
}
switch task {
case "lex":
if len(args) < 3 {
fmt.Fprintln(os.Stderr, "Did not provide a filename")
os.Exit(1)
}
lexFile(args[2])
case "lsp":
level := int64(0)
if len(args) > 2 {
level, _ = strconv.ParseInt(args[2], 0, 8)
}
lsp.Run(int(level))
case "parse":
if len(args) < 3 {
fmt.Fprintln(os.Stderr, "Did not provide a filename")
os.Exit(1)
}
parseFile(args[2])
case "make-test":
if len(args) < 4 {
fmt.Fprintln(os.Stderr, "Not enough arguments: luapls make-test <suite> <label> <input string>")
os.Exit(1)
}
makeTest(args[2], args[3], args[4])
case "repl":
repl.Run()
case "check":
check()
default:
fmt.Fprintf(os.Stderr, "%s: unrecognized subcommand\n", task)
}
util.Exit(0)
}
func lexFile(filename string) {
src, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
l := lexer.New(string(src))
for {
tok := l.Next()
fmt.Println(tok.String())
if tok.Type == token.EOF || tok.Type == token.INVALID {
break
}
}
fmt.Println(l.GetLineBreaks())
}
func parseFile(filename string) {
before := time.Now()
src, err := os.ReadFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// units, _ := parser.Run(string(src))
// bytes, err := json.MarshalIndent(units, "", " ")
// if err != nil {
// panic(err)
// }
// fmt.Println(string(bytes))
p := parser.New(string(src))
file := p.ParseFile()
duration := time.Since(before)
bytes, err := json.MarshalIndent(struct {
Duration string
File ast.File
}{
Duration: duration.String(),
File: file,
}, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(string(bytes))
}
type testSpec struct {
Label string
Input string
AST json.RawMessage
Errors json.RawMessage `json:",omitempty"`
}
func makeTest(suite string, label string, input string) {
p := parser.New(input)
file := p.ParseFile()
ast, _ := json.Marshal(&file.Block)
errors, _ := json.Marshal(p.Errors())
newSpec := testSpec{label, input, ast, errors}
path := filepath.Join("lua/parser/test_specs", suite+".json")
specs := readOrMakeSpecs(path)
specs = append(specs, newSpec)
output, _ := json.MarshalIndent(specs, "", " ")
os.WriteFile(path, output, 0644)
}
func readOrMakeSpecs(path string) []testSpec {
specs := []testSpec{}
file, err := os.ReadFile(path)
if err == nil {
json.Unmarshal(file, &specs)
}
return specs
}
func check() {
env := types.NewEnvironment()
if env == nil {
panic("Failed to initialize environment")
}
env.RootPath = "."
env.Init()
fmt.Println("TYPES:")
for typName := range env.Types {
fmt.Printf(" %s\n", typName)
}
fmt.Println("DIAGNOSTICS:")
for uri, file := range env.Files {
if len(file.Diagnostics) > 0 {
fmt.Printf(" %s\n", uri)
for _, diag := range file.Diagnostics {
fmt.Printf(" %s: %s\n", diag.Range.String(), diag.Message)
}
}
}
}