-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
459 lines (384 loc) · 11.3 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// lsp_types.go
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/sasogeek/simple/compiler/lexer"
"github.com/sasogeek/simple/compiler/parser"
"github.com/sourcegraph/jsonrpc2"
"log"
"os"
)
type Position struct {
Line int `json:"line"`
Character int `json:"character"`
}
type Range struct {
Start Position `json:"start"`
End Position `json:"end"`
}
type Location struct {
URI string `json:"uri"`
Range Range `json:"range"`
}
type Diagnostic struct {
Range Range `json:"range"`
Severity int `json:"severity"`
Message string `json:"message"`
}
type TextDocumentItem struct {
URI string `json:"uri"`
LanguageID string `json:"languageId"`
Version int `json:"version"`
Text string `json:"text"`
}
type TextDocumentIdentifier struct {
URI string `json:"uri"`
}
type VersionedTextDocumentIdentifier struct {
URI string `json:"uri"`
Version int `json:"version"`
}
type DidOpenTextDocumentParams struct {
TextDocument TextDocumentItem `json:"textDocument"`
}
type DidChangeTextDocumentParams struct {
TextDocument VersionedTextDocumentIdentifier `json:"textDocument"`
ContentChanges []TextDocumentContentChangeEvent `json:"contentChanges"`
}
type TextDocumentContentChangeEvent struct {
Text string `json:"text"`
}
type CompletionItem struct {
Label string `json:"label"`
Kind int `json:"kind,omitempty"`
}
type CompletionParams struct {
TextDocument TextDocumentIdentifier `json:"textDocument"`
Position Position `json:"position"`
}
type Hover struct {
Contents string `json:"contents"`
Range *Range `json:"range,omitempty"`
}
type HoverParams struct {
TextDocument TextDocumentIdentifier `json:"textDocument"`
Position Position `json:"position"`
}
type DefinitionParams struct {
TextDocument TextDocumentIdentifier `json:"textDocument"`
Position Position `json:"position"`
}
type InitializeParams struct {
ProcessID int `json:"processId"`
RootURI string `json:"rootUri"`
}
type InitializeResult struct {
Capabilities ServerCapabilities `json:"capabilities"`
}
type ServerCapabilities struct {
TextDocumentSync int `json:"textDocumentSync"`
CompletionProvider *CompletionOptions `json:"completionProvider,omitempty"`
DefinitionProvider bool `json:"definitionProvider"`
HoverProvider bool `json:"hoverProvider"`
}
type CompletionOptions struct {
ResolveProvider bool `json:"resolveProvider"`
TriggerCharacters []string `json:"triggerCharacters,omitempty"`
}
type Server struct {
conn *jsonrpc2.Conn
documents map[string]*Document
}
type Document struct {
Text string
Lexer *lexer.Lexer
Parser *parser.Parser
AST *parser.Program
Symbols map[string]*parser.Identifier
}
// server.go
func (s *Server) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) {
switch req.Method {
case "initialize":
return s.handleInitialize(ctx, req)
case "textDocument/didOpen":
return s.handleDidOpen(ctx, req)
case "textDocument/didChange":
return s.handleDidChange(ctx, req)
case "textDocument/completion":
return s.handleCompletion(ctx, req)
case "textDocument/hover":
return s.handleHover(ctx, req)
case "textDocument/definition":
return s.handleDefinition(ctx, req)
default:
// Method not implemented
return nil, nil
}
}
func (s *Server) handleInitialize(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) {
var params InitializeParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
capabilities := ServerCapabilities{
TextDocumentSync: 1, // Full synchronization
CompletionProvider: &CompletionOptions{TriggerCharacters: []string{"."}},
DefinitionProvider: true,
HoverProvider: true,
}
result := InitializeResult{
Capabilities: capabilities,
}
return result, nil
}
func (s *Server) handleDidOpen(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) {
var params DidOpenTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
doc := &Document{
Text: params.TextDocument.Text,
}
doc.Lexer = lexer.NewLexer(doc.Text)
doc.Parser = parser.NewParser(doc.Lexer)
doc.AST = doc.Parser.ParseProgram()
// Populate the symbol table
collectSymbols(doc.AST, doc.Symbols)
s.documents[params.TextDocument.URI] = doc
// Send diagnostics if there are errors
s.sendDiagnostics(params.TextDocument.URI, doc.Parser.Errors())
return nil, nil
}
func (s *Server) handleDidChange(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) {
var params DidChangeTextDocumentParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
// For simplicity, we assume full text synchronization
text := params.ContentChanges[0].Text
doc := &Document{
Text: text,
}
doc.Lexer = lexer.NewLexer(doc.Text)
doc.Parser = parser.NewParser(doc.Lexer)
doc.AST = doc.Parser.ParseProgram()
// Populate the symbol table
collectSymbols(doc.AST, doc.Symbols)
s.documents[params.TextDocument.URI] = doc
// Send diagnostics if there are errors
s.sendDiagnostics(params.TextDocument.URI, doc.Parser.Errors())
return nil, nil
}
func collectSymbols(ast *parser.Program, symbols map[string]*parser.Identifier) {
parser.Inspect(ast, func(node parser.Node) bool {
switch n := node.(type) {
case *parser.FunctionLiteral:
if n.Name != nil {
symbols[n.Name.Value] = n.Name
}
}
return true
})
}
func (s *Server) sendDiagnostics(uri string, errors []string) {
diagnostics := []Diagnostic{}
for _, errMsg := range errors {
// Example error format: "expected X, got Y instead (Line 3, Column 15)"
line, column := parseErrorPosition(errMsg)
if line < 0 || column < 0 {
continue // Skip errors without position information
}
diag := Diagnostic{
Range: Range{
Start: Position{Line: line, Character: column},
End: Position{Line: line, Character: column + 1},
},
Severity: 1, // Error
Message: errMsg,
}
diagnostics = append(diagnostics, diag)
}
params := struct {
URI string `json:"uri"`
Diagnostics []Diagnostic `json:"diagnostics"`
}{
URI: uri,
Diagnostics: diagnostics,
}
s.conn.Notify(context.Background(), "textDocument/publishDiagnostics", params)
}
func parseErrorPosition(errMsg string) (int, int) {
var line, column int
// Example error format: "expected X, got Y instead (Line 3, Column 15)"
n, err := fmt.Sscanf(errMsg, "expected %*s, got %*s instead (Line %d, Column %d)", &line, &column)
if err != nil || n != 2 {
// Handle different error message formats or return invalid positions
return -1, -1
}
return line - 1, column - 1 // Convert to zero-based indexing
}
func (s *Server) handleHover(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) {
var params HoverParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
doc := s.documents[params.TextDocument.URI]
if doc == nil {
return nil, nil
}
// Find the token at the given position
token := findTokenAtPosition(doc.Lexer, params.Position)
if token == nil {
return nil, nil
}
// Retrieve symbol information
symbol, exists := doc.Symbols[token.Literal]
if !exists {
return nil, nil
}
// Provide hover information based on symbol type
content := fmt.Sprintf("**Function** `%s`\n\nDefined at Line %d, Column %d", symbol.Value, symbol.Token.Line, symbol.Token.Column)
hover := Hover{
Contents: content,
}
return hover, nil
}
func findTokenAtPosition(l *lexer.Lexer, position Position) *lexer.Token {
tokens := lexAllTokens(l)
for _, tok := range tokens {
if tok.Line == position.Line+1 {
start := tok.Column - 1
end := start + len(tok.Literal)
if position.Character >= start && position.Character <= end {
return &tok
}
}
}
return nil
}
func lexAllTokens(l *lexer.Lexer) []lexer.Token {
tokens := []lexer.Token{}
for {
tok := l.NextToken()
tokens = append(tokens, tok)
if tok.Type == lexer.TokenEOF {
break
}
}
return tokens
}
func (s *Server) handleDefinition(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) {
var params DefinitionParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
doc := s.documents[params.TextDocument.URI]
if doc == nil {
return []Location{}, nil
}
position := params.Position
// Find the token at the position
token := findTokenAtPosition(doc.Lexer, position)
if token == nil {
return []Location{}, nil
}
// Retrieve symbol information
symbol, exists := doc.Symbols[token.Literal]
if !exists {
return []Location{}, nil
}
location := Location{
URI: params.TextDocument.URI,
Range: Range{
Start: Position{Line: symbol.Token.Line - 1, Character: symbol.Token.Column - 1},
End: Position{Line: symbol.Token.Line - 1, Character: symbol.Token.Column - 1 + len(symbol.Value)},
},
}
return []Location{location}, nil
}
func findSymbolAtPosition(ast *parser.Program, position Position) *parser.Identifier {
var foundSymbol *parser.Identifier
parser.Inspect(ast, func(node parser.Node) bool {
switch n := node.(type) {
case *parser.Identifier:
if n.Token.Line == position.Line+1 && n.Token.Column <= position.Character+1 && n.Token.Column+len(n.Value) >= position.Character+1 {
foundSymbol = n
return false // Stop traversal
}
}
return true
})
return foundSymbol
}
func findDefinitionInAST(ast *parser.Program, name string) *parser.Identifier {
var definition *parser.Identifier
parser.Inspect(ast, func(node parser.Node) bool {
switch n := node.(type) {
case *parser.FunctionLiteral:
if n.Name.Value == name {
definition = n.Name
return false
}
}
return true
})
return definition
}
func (s *Server) handleCompletion(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) {
var params CompletionParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
doc := s.documents[params.TextDocument.URI]
if doc == nil {
return []CompletionItem{}, nil
}
// Find the token at the current position
token := findTokenAtPosition(doc.Lexer, params.Position)
if token == nil {
return []CompletionItem{}, nil
}
// Collect identifiers from the symbol table
identifiers := collectIdentifiers(doc.Symbols)
items := []CompletionItem{}
for name := range identifiers {
item := CompletionItem{
Label: name,
Kind: 6, // Function or Variable
}
items = append(items, item)
}
// Add keywords
keywords := []string{"def", "return", "if", "else", "while", "for", "import", "defer", "go", "True", "False", "None"}
for _, kw := range keywords {
item := CompletionItem{
Label: kw,
Kind: 14, // Keyword
}
items = append(items, item)
}
return items, nil
}
func collectIdentifiers(symbols map[string]*parser.Identifier) map[string]struct{} {
identifiers := make(map[string]struct{})
for name := range symbols {
identifiers[name] = struct{}{}
}
return identifiers
}
func main() {
log.Println("Simple Language Server started.")
server := &Server{
documents: make(map[string]*Document),
}
handler := jsonrpc2.HandlerWithError(server.handle)
stdin := os.Stdin
stream := jsonrpc2.NewBufferedStream(stdin, jsonrpc2.VSCodeObjectCodec{})
conn := jsonrpc2.NewConn(context.Background(), stream, handler)
server.conn = conn
<-conn.DisconnectNotify()
log.Println("Simple Language Server stopped.")
}