z_mirror/luapls
git clone https://git.raiguard.me/z_mirror/luapls
582fe74
main
1package main 2 3import ( 4"encoding/json" 5"fmt" 6"os" 7"path/filepath" 8"strconv" 9"time" 10 11"github.com/raiguard/luapls/lsp" 12"github.com/raiguard/luapls/lua/ast" 13"github.com/raiguard/luapls/lua/lexer" 14"github.com/raiguard/luapls/lua/parser" 15"github.com/raiguard/luapls/lua/token" 16"github.com/raiguard/luapls/lua/types" 17"github.com/raiguard/luapls/repl" 18"github.com/tliron/kutil/util" 19) 20 21func main () { 22args := os .Args 23task := "lsp" 24if len (args )> 1 { 25task = args [1 ] 26 } 27 28switch task { 29case "lex" : 30if len (args )< 3 { 31fmt .Fprintln (os .Stderr ,"Did not provide a filename" ) 32os .Exit (1 ) 33 } 34lexFile (args [2 ]) 35case "lsp" : 36level := int64 (0 ) 37if len (args )> 2 { 38level ,_ = strconv .ParseInt (args [2 ],0 ,8 ) 39 } 40lsp .Run (int (level )) 41case "parse" : 42if len (args )< 3 { 43fmt .Fprintln (os .Stderr ,"Did not provide a filename" ) 44os .Exit (1 ) 45 } 46parseFile (args [2 ]) 47case "make-test" : 48if len (args )< 4 { 49fmt .Fprintln (os .Stderr ,"Not enough arguments: luapls make-test <suite> <label> <input string>" ) 50os .Exit (1 ) 51 } 52makeTest (args [2 ],args [3 ],args [4 ]) 53case "repl" : 54repl .Run () 55case "check" : 56check () 57default : 58fmt .Fprintf (os .Stderr ,"%s: unrecognized subcommand\n" ,task ) 59 } 60 61util .Exit (0 ) 62} 63 64func lexFile (filename string ) { 65src ,err := os .ReadFile (filename ) 66if err != nil { 67panic (err ) 68 } 69l := lexer .New (string (src )) 70for { 71tok := l .Next () 72fmt .Println (tok .String ()) 73if tok .Type == token .EOF || tok .Type == token .INVALID { 74break 75 } 76 } 77fmt .Println (l .GetLineBreaks ()) 78} 79 80func parseFile (filename string ) { 81before := time .Now () 82src ,err := os .ReadFile (filename ) 83if err != nil { 84fmt .Fprintln (os .Stderr ,err ) 85os .Exit (1 ) 86 } 87// units, _ := parser.Run(string(src)) 88// bytes, err := json.MarshalIndent(units, "", " ") 89// if err != nil { 90// panic(err) 91// } 92// fmt.Println(string(bytes)) 93p := parser .New (string (src )) 94file := p .ParseFile () 95duration := time .Since (before ) 96bytes ,err := json .MarshalIndent (struct { 97Duration string 98File ast.File 99 }{ 100Duration :duration .String (), 101File :file , 102 },"" ," " ) 103if err != nil { 104fmt .Fprintln (os .Stderr ,err ) 105os .Exit (1 ) 106 } 107fmt .Println (string (bytes )) 108} 109 110type testSpec struct { 111Label string 112Input string 113AST json.RawMessage 114Errors json.RawMessage `json:",omitempty"` 115} 116 117func makeTest (suite string ,label string ,input string ) { 118p := parser .New (input ) 119file := p .ParseFile () 120ast ,_ := json .Marshal (& file .Block ) 121errors ,_ := json .Marshal (p .Errors ()) 122newSpec := testSpec {label ,input ,ast ,errors } 123path := filepath .Join ("lua/parser/test_specs" ,suite + ".json" ) 124specs := readOrMakeSpecs (path ) 125specs = append (specs ,newSpec ) 126output ,_ := json .MarshalIndent (specs ,"" ," " ) 127os .WriteFile (path ,output ,0644 ) 128} 129 130func readOrMakeSpecs (path string ) []testSpec { 131specs := []testSpec {} 132file ,err := os .ReadFile (path ) 133if err == nil { 134json .Unmarshal (file ,& specs ) 135 } 136return specs 137} 138 139func check () { 140env := types .NewEnvironment () 141if env == nil { 142panic ("Failed to initialize environment" ) 143 } 144env .RootPath = "." 145env .Init () 146fmt .Println ("TYPES:" ) 147for typName := range env .Types { 148fmt .Printf (" %s\n" ,typName ) 149 } 150fmt .Println ("DIAGNOSTICS:" ) 151for uri ,file := range env .Files { 152if len (file .Diagnostics )> 0 { 153fmt .Printf (" %s\n" ,uri ) 154for _ ,diag := range file .Diagnostics { 155fmt .Printf (" %s: %s\n" ,diag .Range .String (),diag .Message ) 156 } 157 } 158 } 159}