z_mirror/luapls

git clone https://git.raiguard.me/z_mirror/luapls

Caleb HeuerStart a proper annotation parserb6e515c

main
1.4 KiB67 linesraw
1package annotation
2
3import (
4	"fmt"
5
6	"github.com/raiguard/luapls/lua/ast"
7	"github.com/raiguard/luapls/lua/lexer"
8	"github.com/raiguard/luapls/lua/token"
9	protocol "github.com/tliron/glsp/protocol_3_16"
10)
11
12func Parse(src string) (Annotation, []ast.Diagnostic) {
13	tokens, _ := lexer.Run(src)
14	p := parser{tokens, -1, []ast.Diagnostic{}}
15	return p.parse()
16}
17
18type parser struct {
19	tokens      []token.Token
20	pos         int
21	diagnostics []ast.Diagnostic
22}
23
24func (p *parser) parse() (Annotation, []ast.Diagnostic) {
25	tok := p.next()
26	switch tok.Type {
27	case token.DOC_CLASS:
28		name := p.expect(token.IDENT)
29		return &Class{Name: name.Literal}, p.diagnostics
30	case token.INVALID:
31		p.diagnostics = append(p.diagnostics, ast.Diagnostic{
32			Message:  "Unknown annotation",
33			Range:    tok.Range(),
34			Severity: protocol.DiagnosticSeverityWarning,
35		})
36		return nil, p.diagnostics
37	default:
38		return nil, p.diagnostics
39	}
40}
41
42func (p *parser) read() *token.Token {
43	if p.pos < len(p.tokens)-1 {
44		p.pos++
45	}
46	return &p.tokens[p.pos]
47}
48
49func (p *parser) next() *token.Token {
50	tok := p.read()
51	for tok.Type == token.WHITESPACE {
52		tok = p.read()
53	}
54	return tok
55}
56
57func (p *parser) expect(typ token.TokenType) *token.Token {
58	tok := p.next()
59	if tok.Type != typ {
60		p.diagnostics = append(p.diagnostics, ast.Diagnostic{
61			Message:  fmt.Sprintf("Expected %s", token.TokenStr[typ]),
62			Range:    tok.Range(),
63			Severity: protocol.DiagnosticSeverityWarning,
64		})
65	}
66	return tok
67}