z_mirror/luapls
git clone https://git.raiguard.me/z_mirror/luapls
b6e515c
main
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 ) { 13tokens ,_ := lexer .Run (src ) 14p := parser {tokens ,- 1 , []ast.Diagnostic {}} 15return p .parse () 16} 17 18type parser struct { 19tokens []token.Token 20pos int 21diagnostics []ast.Diagnostic 22} 23 24func (p * parser )parse () (Annotation , []ast.Diagnostic ) { 25tok := p .next () 26switch tok .Type { 27case token .DOC_CLASS : 28name := p .expect (token .IDENT ) 29return & Class {Name :name .Literal },p .diagnostics 30case token .INVALID : 31p .diagnostics = append (p .diagnostics , ast.Diagnostic { 32Message :"Unknown annotation" , 33Range :tok .Range (), 34Severity :protocol .DiagnosticSeverityWarning , 35 }) 36return nil ,p .diagnostics 37default : 38return nil ,p .diagnostics 39 } 40} 41 42func (p * parser )read ()* token.Token { 43if p .pos < len (p .tokens )- 1 { 44p .pos ++ 45 } 46return & p .tokens [p .pos ] 47} 48 49func (p * parser )next ()* token.Token { 50tok := p .read () 51for tok .Type == token .WHITESPACE { 52tok = p .read () 53 } 54return tok 55} 56 57func (p * parser )expect (typ token.TokenType )* token.Token { 58tok := p .next () 59if tok .Type != typ { 60p .diagnostics = append (p .diagnostics , ast.Diagnostic { 61Message :fmt .Sprintf ("Expected %s" ,token .TokenStr [typ ]), 62Range :tok .Range (), 63Severity :protocol .DiagnosticSeverityWarning , 64 }) 65 } 66return tok 67}