refactor(lang): rename the fn keyword to function
Expand the function-declaration keyword to the full word across the whole
language and toolchain:
fn name(...) -> T { ... } -> function name(...) -> T { ... }
Done as a self-hosting migration: teach the parser both spellings, reseed,
rewrite every .ludic definition to `function`, then drop `fn`. The compiler
now rejects `fn`. Touches the parser, all selfhost/tools/runtime/example/test
sources, the grammars (TextMate shared+vscode, ludic_syntax.h, JetBrains
LudicTokens.kt), the LSP and formatter, the Python doc/vocab tools
(check-impl, check-docs, validate, palette, test-lsp), and the docs
(fences, prose, kw-fn -> kw-function).
Reseeded; C-free bootstrap fixpoint holds. All suites green (45 regression,
24 self-host, 29 tool); the docs site generates and check.py passes.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
2f19c8d8e2
commit
4c48077d68
86 changed files with 793 additions and 793 deletions
|
|
@ -72,7 +72,7 @@ program LudicLsp {
|
|||
const SC_UNKNOWN: int = 24
|
||||
|
||||
# ---- tiny helpers ----
|
||||
fn read_file(path: ptr) -> ptr {
|
||||
function read_file(path: ptr) -> ptr {
|
||||
let f = file_open(path, "rb")
|
||||
if (f == null) { return null }
|
||||
file_seek(f, 0, 2)
|
||||
|
|
@ -84,16 +84,16 @@ program LudicLsp {
|
|||
file_close(f)
|
||||
return buf
|
||||
}
|
||||
fn cstr_len(s: ptr) -> int { var n = 0; while s[n] != 0 { n = n + 1 }; return n }
|
||||
fn char_is_digit(c: int) -> bool { return c >= 48 and c <= 57 }
|
||||
fn char_is_alpha(c: int) -> bool {
|
||||
function cstr_len(s: ptr) -> int { var n = 0; while s[n] != 0 { n = n + 1 }; return n }
|
||||
function char_is_digit(c: int) -> bool { return c >= 48 and c <= 57 }
|
||||
function char_is_alpha(c: int) -> bool {
|
||||
if c >= 65 and c <= 90 { return true }
|
||||
if c >= 97 and c <= 122 { return true }
|
||||
return c == 95
|
||||
}
|
||||
fn char_is_alnum(c: int) -> bool { return char_is_alpha(c) or char_is_digit(c) }
|
||||
fn char_is_hex(c: int) -> bool { return char_is_digit(c) or (c >= 97 and c <= 102) or (c >= 65 and c <= 70) }
|
||||
fn itoa(v: int) -> ptr {
|
||||
function char_is_alnum(c: int) -> bool { return char_is_alpha(c) or char_is_digit(c) }
|
||||
function char_is_hex(c: int) -> bool { return char_is_digit(c) or (c >= 97 and c <= 102) or (c >= 65 and c <= 70) }
|
||||
function itoa(v: int) -> ptr {
|
||||
if v == 0 { let z = bytes(2); z[0] = 48; z[1] = 0; return z }
|
||||
var neg = false; var x = v
|
||||
if x < 0 { neg = true; x = 0 - x }
|
||||
|
|
@ -108,7 +108,7 @@ program LudicLsp {
|
|||
out[total] = 0
|
||||
return out
|
||||
}
|
||||
fn atoi(s: ptr) -> int {
|
||||
function atoi(s: ptr) -> int {
|
||||
var n = 0; var i = 0; var neg = false
|
||||
while s[i] == 32 or s[i] == 9 { i = i + 1 }
|
||||
if s[i] == 45 { neg = true; i = i + 1 }
|
||||
|
|
@ -119,27 +119,27 @@ program LudicLsp {
|
|||
|
||||
# ---- growable byte buffer ----
|
||||
property Buf { data: ptr = null, len: int = 0, cap: int = 0 }
|
||||
fn buf_new() -> Buf { let b = new Buf; b.cap = 256; b.data = bytes(b.cap); b.len = 0; return b }
|
||||
fn buf_ensure(b: Buf, extra: int) -> void {
|
||||
function buf_new() -> Buf { let b = new Buf; b.cap = 256; b.data = bytes(b.cap); b.len = 0; return b }
|
||||
function buf_ensure(b: Buf, extra: int) -> void {
|
||||
if b.len + extra + 1 <= b.cap { return }
|
||||
while b.len + extra + 1 > b.cap { b.cap = b.cap * 2 }
|
||||
b.data = resize(b.data, b.cap)
|
||||
}
|
||||
fn buf_putc(b: Buf, c: int) -> void { buf_ensure(b, 1); b.data[b.len] = c; b.len = b.len + 1 }
|
||||
fn buf_puts(b: Buf, s: ptr) -> void { var i = 0; while s[i] != 0 { buf_putc(b, s[i]); i = i + 1 } }
|
||||
fn buf_puti(b: Buf, n: int) -> void { buf_puts(b, itoa(n)) }
|
||||
fn buf_addrange(b: Buf, s: ptr, a: int, e: int) -> void { var k = a; while k < e { buf_putc(b, s[k]); k = k + 1 } }
|
||||
fn buf_str(b: Buf) -> ptr { b.data[b.len] = 0; return b.data }
|
||||
function buf_putc(b: Buf, c: int) -> void { buf_ensure(b, 1); b.data[b.len] = c; b.len = b.len + 1 }
|
||||
function buf_puts(b: Buf, s: ptr) -> void { var i = 0; while s[i] != 0 { buf_putc(b, s[i]); i = i + 1 } }
|
||||
function buf_puti(b: Buf, n: int) -> void { buf_puts(b, itoa(n)) }
|
||||
function buf_addrange(b: Buf, s: ptr, a: int, e: int) -> void { var k = a; while k < e { buf_putc(b, s[k]); k = k + 1 } }
|
||||
function buf_str(b: Buf) -> ptr { b.data[b.len] = 0; return b.data }
|
||||
|
||||
# ---- string helpers ----
|
||||
fn streq(a: ptr, b: ptr) -> bool { return (a == b) }
|
||||
fn concat3(a: ptr, b: ptr, c: ptr) -> ptr { return ((a + b) + c) }
|
||||
fn ends_with(s: ptr, suf: ptr) -> bool {
|
||||
function streq(a: ptr, b: ptr) -> bool { return (a == b) }
|
||||
function concat3(a: ptr, b: ptr, c: ptr) -> ptr { return ((a + b) + c) }
|
||||
function ends_with(s: ptr, suf: ptr) -> bool {
|
||||
let n = cstr_len(s); let m = cstr_len(suf)
|
||||
if m > n { return false }
|
||||
return (s[n - m..n] == suf)
|
||||
}
|
||||
fn starts_with(s: ptr, pre: ptr) -> bool {
|
||||
function starts_with(s: ptr, pre: ptr) -> bool {
|
||||
let m = cstr_len(pre)
|
||||
if m > cstr_len(s) { return false }
|
||||
return (s[0..m] == pre)
|
||||
|
|
@ -191,7 +191,7 @@ program LudicLsp {
|
|||
var g_shutdown: int = 0
|
||||
|
||||
# ---- the lexer, over a Doc ----
|
||||
fn is_op2(c0: int, c1: int) -> bool {
|
||||
function is_op2(c0: int, c1: int) -> bool {
|
||||
if c0 == 45 and c1 == 62 { return true }
|
||||
if c1 == 61 and (c0 == 43 or c0 == 45 or c0 == 42 or c0 == 47 or c0 == 61 or c0 == 33 or c0 == 60 or c0 == 62) { return true }
|
||||
if c0 == 38 and c1 == 38 { return true }
|
||||
|
|
@ -200,29 +200,29 @@ program LudicLsp {
|
|||
if c0 == 61 and c1 == 62 { return true }
|
||||
return false
|
||||
}
|
||||
fn is_op1(c: int) -> bool {
|
||||
function is_op1(c: int) -> bool {
|
||||
return c == 43 or c == 45 or c == 42 or c == 47 or c == 37 or c == 60 or c == 62 or c == 61 or c == 40 or c == 41 or c == 123 or c == 125 or c == 91 or c == 93 or c == 44 or c == 58 or c == 46 or c == 33 or c == 64 or c == 59
|
||||
}
|
||||
fn is_type_word(w: ptr) -> bool {
|
||||
function is_type_word(w: ptr) -> bool {
|
||||
return (w == "int") or (w == "long") or (w == "fixed") or (w == "bool") or (w == "entity") or (w == "str") or (w == "ptr") or (w == "byte") or (w == "words") or (w == "fixeds") or (w == "ptrs") or (w == "void")
|
||||
}
|
||||
fn is_phase_word(w: ptr) -> bool {
|
||||
function is_phase_word(w: ptr) -> bool {
|
||||
return (w == "Start") or (w == "Input") or (w == "FixedUpdate") or (w == "Update") or (w == "LateUpdate") or (w == "Render")
|
||||
}
|
||||
fn is_keyword_word(w: ptr) -> bool {
|
||||
function is_keyword_word(w: ptr) -> bool {
|
||||
if (w == "program") or (w == "import") or (w == "property") or (w == "model") or (w == "enum") or (w == "ui") { return true }
|
||||
if (w == "const") or (w == "var") or (w == "fn") or (w == "extern") or (w == "handler") or (w == "entry") or (w == "event") or (w == "scene") { return true }
|
||||
if (w == "const") or (w == "var") or (w == "function") or (w == "extern") or (w == "handler") or (w == "entry") or (w == "event") or (w == "scene") { return true }
|
||||
if (w == "phase") or (w == "query") or (w == "on") or (w == "cancellable") or (w == "public") or (w == "layer") or (w == "start") { return true }
|
||||
if (w == "let") or (w == "return") or (w == "if") or (w == "else") or (w == "while") or (w == "for") or (w == "in") or (w == "spawn") or (w == "despawn") { return true }
|
||||
if (w == "enable") or (w == "disable") or (w == "match") or (w == "machine") or (w == "state") or (w == "become") or (w == "where") { return true }
|
||||
if (w == "and") or (w == "or") or (w == "not") or (w == "break") or (w == "continue") or (w == "new") or (w == "emit") or (w == "cancel") { return true }
|
||||
return false
|
||||
}
|
||||
fn is_widget_word(w: ptr) -> bool {
|
||||
function is_widget_word(w: ptr) -> bool {
|
||||
return (w == "panel") or (w == "col") or (w == "row") or (w == "label") or (w == "button") or (w == "image") or (w == "spacer")
|
||||
}
|
||||
|
||||
fn lex_doc(D: Doc) -> void {
|
||||
function lex_doc(D: Doc) -> void {
|
||||
let s = D.src
|
||||
D.tk_kind = new []int; D.tk_start = new []int; D.tk_end = new []int; D.tk_line = new []int
|
||||
D.linestart = new []int
|
||||
|
|
@ -265,15 +265,15 @@ program LudicLsp {
|
|||
push(D.tk_kind, LT_EOF); push(D.tk_start, i); push(D.tk_end, i); push(D.tk_line, line)
|
||||
}
|
||||
|
||||
fn ntok(D: Doc) -> int { return len(D.tk_kind) }
|
||||
fn tlen(D: Doc, i: int) -> int { return D.tk_end[i] - D.tk_start[i] }
|
||||
fn ttext(D: Doc, i: int) -> ptr { return D.src[D.tk_start[i]..D.tk_end[i]] }
|
||||
fn tis(D: Doc, i: int, s: ptr) -> bool { if i < 0 or i >= ntok(D) { return false }; return (ttext(D, i) == s) }
|
||||
fn next_sig(D: Doc, i: int) -> int { var j = i + 1; while j < ntok(D) { let k = D.tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j + 1 }; return 0 - 1 }
|
||||
fn prev_sig(D: Doc, i: int) -> int { var j = i - 1; while j >= 0 { let k = D.tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j - 1 }; return 0 - 1 }
|
||||
function ntok(D: Doc) -> int { return len(D.tk_kind) }
|
||||
function tlen(D: Doc, i: int) -> int { return D.tk_end[i] - D.tk_start[i] }
|
||||
function ttext(D: Doc, i: int) -> ptr { return D.src[D.tk_start[i]..D.tk_end[i]] }
|
||||
function tis(D: Doc, i: int, s: ptr) -> bool { if i < 0 or i >= ntok(D) { return false }; return (ttext(D, i) == s) }
|
||||
function next_sig(D: Doc, i: int) -> int { var j = i + 1; while j < ntok(D) { let k = D.tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j + 1 }; return 0 - 1 }
|
||||
function prev_sig(D: Doc, i: int) -> int { var j = i - 1; while j >= 0 { let k = D.tk_kind[j]; if k != LT_NL and k != LT_COMMENT { return j }; j = j - 1 }; return 0 - 1 }
|
||||
|
||||
# ---- positions (UTF-16 for LSP) ----
|
||||
fn utf16_len(s: ptr, a: int, nbytes: int) -> int {
|
||||
function utf16_len(s: ptr, a: int, nbytes: int) -> int {
|
||||
var u = 0; var i = 0
|
||||
while i < nbytes {
|
||||
let c = s[a + i]
|
||||
|
|
@ -284,13 +284,13 @@ program LudicLsp {
|
|||
}
|
||||
return u
|
||||
}
|
||||
fn line_of(D: Doc, off: int) -> int {
|
||||
function line_of(D: Doc, off: int) -> int {
|
||||
var lo = 0; var hi = len(D.linestart) - 1
|
||||
while lo < hi { let mid = (lo + hi + 1) / 2; if D.linestart[mid] <= off { lo = mid } else { hi = mid - 1 } }
|
||||
return lo
|
||||
}
|
||||
fn col_of(D: Doc, off: int) -> int { let l = line_of(D, off); return utf16_len(D.text, D.linestart[l], off - D.linestart[l]) }
|
||||
fn offset_of(D: Doc, line: int, ch: int) -> int {
|
||||
function col_of(D: Doc, off: int) -> int { let l = line_of(D, off); return utf16_len(D.text, D.linestart[l], off - D.linestart[l]) }
|
||||
function offset_of(D: Doc, line: int, ch: int) -> int {
|
||||
if line < 0 { return 0 }
|
||||
if line >= len(D.linestart) { return cstr_len(D.text) }
|
||||
var off = D.linestart[line]
|
||||
|
|
@ -306,7 +306,7 @@ program LudicLsp {
|
|||
}
|
||||
return off
|
||||
}
|
||||
fn tok_at(D: Doc, off: int) -> int {
|
||||
function tok_at(D: Doc, off: int) -> int {
|
||||
var touching = 0 - 1
|
||||
var i = 0
|
||||
let N = ntok(D)
|
||||
|
|
@ -323,7 +323,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- brace matching ----
|
||||
fn match_braces(D: Doc) -> void {
|
||||
function match_braces(D: Doc) -> void {
|
||||
let N = ntok(D)
|
||||
D.tmatch = new []int
|
||||
var i = 0
|
||||
|
|
@ -342,7 +342,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- doc comment: the comment block directly above a declaration ----
|
||||
fn collect_doc(D: Doc, decl_tok: int) -> ptr {
|
||||
function collect_doc(D: Doc, decl_tok: int) -> ptr {
|
||||
var line = D.tk_line[decl_tok]
|
||||
var first = 0 - 1
|
||||
var i = decl_tok - 1
|
||||
|
|
@ -379,17 +379,17 @@ program LudicLsp {
|
|||
var pD: Doc = null
|
||||
var pi: int = 0
|
||||
|
||||
fn pk() -> int { if pi < ntok(pD) { return pD.tk_kind[pi] }; return LT_EOF }
|
||||
fn pis(s: ptr) -> bool { return tis(pD, pi, s) }
|
||||
fn pskipnl() -> void { while pi < ntok(pD) and (pk() == LT_NL or pk() == LT_COMMENT) { pi = pi + 1 } }
|
||||
fn padv() -> void { if pi < ntok(pD) { pi = pi + 1 }; pskipnl() }
|
||||
fn pword() -> ptr { if pi < ntok(pD) { return ttext(pD, pi) }; return "" }
|
||||
fn pname() -> int {
|
||||
function pk() -> int { if pi < ntok(pD) { return pD.tk_kind[pi] }; return LT_EOF }
|
||||
function pis(s: ptr) -> bool { return tis(pD, pi, s) }
|
||||
function pskipnl() -> void { while pi < ntok(pD) and (pk() == LT_NL or pk() == LT_COMMENT) { pi = pi + 1 } }
|
||||
function padv() -> void { if pi < ntok(pD) { pi = pi + 1 }; pskipnl() }
|
||||
function pword() -> ptr { if pi < ntok(pD) { return ttext(pD, pi) }; return "" }
|
||||
function pname() -> int {
|
||||
let k = pk()
|
||||
if k == LT_ID or k == LT_TYPE or k == LT_PHASE or k == LT_KW or k == LT_BOOL { let t = pi; padv(); return t }
|
||||
return 0 - 1
|
||||
}
|
||||
fn sym_add(kind: int, nametok: int, parent: int) -> int {
|
||||
function sym_add(kind: int, nametok: int, parent: int) -> int {
|
||||
let s = new Sym
|
||||
s.kind = kind; s.tok = nametok; s.parent = parent
|
||||
s.name = ""; s.ty = ""; s.detail = ""; s.doc = ""
|
||||
|
|
@ -398,11 +398,11 @@ program LudicLsp {
|
|||
push(pD.syms, s)
|
||||
return len(pD.syms) - 1
|
||||
}
|
||||
fn cur_match() -> int { if pi < ntok(pD) { return pD.tmatch[pi] }; return 0 - 1 }
|
||||
fn skip_group() -> void { let m = cur_match(); if m < 0 { padv(); return }; pi = m + 1; pskipnl() }
|
||||
function cur_match() -> int { if pi < ntok(pD) { return pD.tmatch[pi] }; return 0 - 1 }
|
||||
function skip_group() -> void { let m = cur_match(); if m < 0 { padv(); return }; pi = m + 1; pskipnl() }
|
||||
|
||||
# `name: Type` pairs inside ( ) — record params, build the signature into sig
|
||||
fn lp_params(owner: int, kind: int, sig: Buf) -> void {
|
||||
function lp_params(owner: int, kind: int, sig: Buf) -> void {
|
||||
if not pis("(") { return }
|
||||
let close = cur_match()
|
||||
buf_putc(sig, 40) # '('
|
||||
|
|
@ -428,7 +428,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# (a, b) variable list -> token indices into out; returns count
|
||||
fn lp_varlist(out: []int) -> int {
|
||||
function lp_varlist(out: []int) -> int {
|
||||
var n = 0
|
||||
if not pis("(") { return 0 }
|
||||
let close = cur_match()
|
||||
|
|
@ -446,7 +446,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# [Pos, Vel, {Enemy}] — bind listed components to vars in order (skip {Tag})
|
||||
fn lp_query_terms(owner: int, vars: []int, nvars: int, scope_start: int, scope_end: int) -> void {
|
||||
function lp_query_terms(owner: int, vars: []int, nvars: int, scope_start: int, scope_end: int) -> void {
|
||||
if not pis("[") { return }
|
||||
let close = cur_match()
|
||||
padv()
|
||||
|
|
@ -470,7 +470,7 @@ program LudicLsp {
|
|||
if pis("where") { padv() }
|
||||
}
|
||||
|
||||
fn lp_block(owner: int, scope_end: int) -> void {
|
||||
function lp_block(owner: int, scope_end: int) -> void {
|
||||
if not pis("{") { return }
|
||||
let close = cur_match()
|
||||
var endo = scope_end
|
||||
|
|
@ -487,7 +487,7 @@ program LudicLsp {
|
|||
if close >= 0 { pi = close + 1 } else { padv() }
|
||||
pskipnl()
|
||||
}
|
||||
fn lp_stmt(owner: int, scope_end: int) -> void {
|
||||
function lp_stmt(owner: int, scope_end: int) -> void {
|
||||
if pis("let") {
|
||||
padv(); let nt = pname(); if nt < 0 { return }
|
||||
var ty = ""
|
||||
|
|
@ -522,7 +522,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# panel id=Root w=288 { … } — each id= mints a UI_<Name> handle
|
||||
fn lp_widget(owner: int, uisym: int) -> void {
|
||||
function lp_widget(owner: int, uisym: int) -> void {
|
||||
let type_tok = pname()
|
||||
if type_tok < 0 { return }
|
||||
while pi < ntok(pD) and pk() == LT_ID {
|
||||
|
|
@ -565,7 +565,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# one top-level declaration
|
||||
fn lp_decl(parent: int) -> void {
|
||||
function lp_decl(parent: int) -> void {
|
||||
let w = pword()
|
||||
let decl_tok = pi
|
||||
|
||||
|
|
@ -638,11 +638,11 @@ program LudicLsp {
|
|||
return
|
||||
}
|
||||
if w == "extern" {
|
||||
padv(); if pis("fn") { padv() }
|
||||
padv(); if pis("function") { padv() }
|
||||
let nt = pname(); if nt < 0 { return }
|
||||
let sidx = sym_add(LS_EXTERN, nt, parent)
|
||||
pD.syms[sidx].doc = collect_doc(pD, decl_tok)
|
||||
let sig = buf_new(); buf_puts(sig, "extern fn "); buf_puts(sig, pD.syms[sidx].name)
|
||||
let sig = buf_new(); buf_puts(sig, "extern function "); buf_puts(sig, pD.syms[sidx].name)
|
||||
lp_params(sidx, LS_PARAM, sig)
|
||||
if pis("->") { padv(); let tt = pname(); if tt >= 0 { pD.syms[sidx].ty = ttext(pD, tt); buf_puts(sig, " -> "); buf_puts(sig, pD.syms[sidx].ty) } }
|
||||
pD.syms[sidx].detail = buf_str(sig)
|
||||
|
|
@ -650,10 +650,10 @@ program LudicLsp {
|
|||
pskipnl()
|
||||
return
|
||||
}
|
||||
if (w == "fn") or (w == "pure") or (w == "export") {
|
||||
if (w == "function") or (w == "pure") or (w == "export") {
|
||||
let exported = (w == "export")
|
||||
if (w == "pure") or exported { padv(); if pis("pure") { padv() } }
|
||||
if pis("fn") { padv() }
|
||||
if pis("function") { padv() }
|
||||
let nt = pname(); if nt < 0 { return }
|
||||
let sidx = sym_add(LS_FN, nt, parent)
|
||||
if exported { pD.syms[sidx].exported = 1 }
|
||||
|
|
@ -795,7 +795,7 @@ program LudicLsp {
|
|||
padv()
|
||||
}
|
||||
|
||||
fn parse_doc(D: Doc) -> void {
|
||||
function parse_doc(D: Doc) -> void {
|
||||
D.syms = new []Sym
|
||||
D.imports = new []ptr
|
||||
D.is_unit = 0; D.is_module = 0; D.unit = ""
|
||||
|
|
@ -833,22 +833,22 @@ program LudicLsp {
|
|||
# ============================================================================
|
||||
# workspace: many documents, one project
|
||||
# ============================================================================
|
||||
fn is_local_kind(k: int) -> bool { return k == LS_LOCAL or k == LS_PARAM or k == LS_QUERYVAR or k == LS_STATE }
|
||||
fn is_toplevel_kind(k: int) -> bool {
|
||||
function is_local_kind(k: int) -> bool { return k == LS_LOCAL or k == LS_PARAM or k == LS_QUERYVAR or k == LS_STATE }
|
||||
function is_toplevel_kind(k: int) -> bool {
|
||||
return k == LS_COMPONENT or k == LS_ARCHETYPE or k == LS_CONST or k == LS_VAR or k == LS_FN or k == LS_EXTERN or k == LS_SYSTEM or k == LS_UI or k == LS_WIDGET or k == LS_SCENE or k == LS_LAYER or k == LS_UNIT
|
||||
}
|
||||
fn is_outline_kind(k: int) -> bool {
|
||||
function is_outline_kind(k: int) -> bool {
|
||||
return k == LS_UNIT or k == LS_COMPONENT or k == LS_FIELD or k == LS_ARCHETYPE or k == LS_CONST or k == LS_VAR or k == LS_FN or k == LS_EXTERN or k == LS_SYSTEM or k == LS_UI or k == LS_WIDGET or k == LS_SCENE or k == LS_LAYER
|
||||
}
|
||||
|
||||
# ---- paths & URIs ----
|
||||
fn hexval(c: int) -> int {
|
||||
function hexval(c: int) -> int {
|
||||
if c >= 48 and c <= 57 { return c - 48 }
|
||||
let l = c | 32
|
||||
if l >= 97 and l <= 102 { return l - 97 + 10 }
|
||||
return 0 - 1
|
||||
}
|
||||
fn uri_to_path(uri: ptr) -> ptr {
|
||||
function uri_to_path(uri: ptr) -> ptr {
|
||||
var s = uri
|
||||
if starts_with(uri, "file://") { s = uri[7..cstr_len(uri)] }
|
||||
let n = cstr_len(s)
|
||||
|
|
@ -861,12 +861,12 @@ program LudicLsp {
|
|||
out[j] = 0
|
||||
return out
|
||||
}
|
||||
fn uri_safe(c: int) -> bool {
|
||||
function uri_safe(c: int) -> bool {
|
||||
if char_is_alnum(c) { return true }
|
||||
return c == 45 or c == 46 or c == 126 or c == 47 # - . ~ /
|
||||
}
|
||||
fn hexch(n: int) -> int { if n < 10 { return 48 + n }; return 55 + n }
|
||||
fn path_to_uri(path: ptr) -> ptr {
|
||||
function hexch(n: int) -> int { if n < 10 { return 48 + n }; return 55 + n }
|
||||
function path_to_uri(path: ptr) -> ptr {
|
||||
let out = buf_new()
|
||||
buf_puts(out, "file://")
|
||||
var i = 0
|
||||
|
|
@ -878,7 +878,7 @@ program LudicLsp {
|
|||
}
|
||||
return buf_str(out)
|
||||
}
|
||||
fn dirname(p: ptr) -> ptr {
|
||||
function dirname(p: ptr) -> ptr {
|
||||
var last = 0 - 1
|
||||
var i = 0
|
||||
while p[i] != 0 { if p[i] == 47 { last = i }; i = i + 1 }
|
||||
|
|
@ -886,7 +886,7 @@ program LudicLsp {
|
|||
if last == 0 { return "/" }
|
||||
return p[0..last]
|
||||
}
|
||||
fn normpath(p: ptr) -> ptr {
|
||||
function normpath(p: ptr) -> ptr {
|
||||
let n = cstr_len(p)
|
||||
let abs = (n > 0 and p[0] == 47)
|
||||
let list = new []ptr
|
||||
|
|
@ -914,16 +914,16 @@ program LudicLsp {
|
|||
while k < cnt { if k > 0 { buf_putc(out, 47) }; buf_puts(out, list[k]); k = k + 1 }
|
||||
return buf_str(out)
|
||||
}
|
||||
fn join_path(dir: ptr, rel: ptr) -> ptr {
|
||||
function join_path(dir: ptr, rel: ptr) -> ptr {
|
||||
if rel[0] == 47 { return normpath(rel) }
|
||||
return normpath(concat3(dir, "/", rel))
|
||||
}
|
||||
|
||||
# ---- markdown scrub: blank everything outside a ```ludic fence ----
|
||||
fn word_ludic_at(s: ptr, at: int) -> bool {
|
||||
function word_ludic_at(s: ptr, at: int) -> bool {
|
||||
return ((s[at] == 108 or s[at] == 76)) and ((s[at + 1] == 117 or s[at + 1] == 85)) and ((s[at + 2] == 100 or s[at + 2] == 68)) and ((s[at + 3] == 105 or s[at + 3] == 73)) and ((s[at + 4] == 99 or s[at + 4] == 67))
|
||||
}
|
||||
fn scrub_markdown(src: ptr) -> ptr {
|
||||
function scrub_markdown(src: ptr) -> ptr {
|
||||
let n = cstr_len(src)
|
||||
let out = bytes(n + 1)
|
||||
var c0 = 0
|
||||
|
|
@ -974,12 +974,12 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- documents ----
|
||||
fn set_text(D: Doc, raw: ptr) -> void {
|
||||
function set_text(D: Doc, raw: ptr) -> void {
|
||||
D.raw = raw
|
||||
if D.is_md == 1 { D.text = scrub_markdown(raw) } else { D.text = raw }
|
||||
D.src = D.text
|
||||
}
|
||||
fn reindex(D: Doc) -> void {
|
||||
function reindex(D: Doc) -> void {
|
||||
lex_doc(D)
|
||||
match_braces(D)
|
||||
parse_doc(D)
|
||||
|
|
@ -988,7 +988,7 @@ program LudicLsp {
|
|||
let N = ntok(D)
|
||||
while i < N { push(D.cls, SC_NONE); i = i + 1 }
|
||||
}
|
||||
fn new_doc(path: ptr, text: ptr) -> Doc {
|
||||
function new_doc(path: ptr, text: ptr) -> Doc {
|
||||
let D = new Doc
|
||||
D.path = normpath(path)
|
||||
D.uri = path_to_uri(D.path)
|
||||
|
|
@ -999,13 +999,13 @@ program LudicLsp {
|
|||
reindex(D)
|
||||
return D
|
||||
}
|
||||
fn by_path(path: ptr) -> Doc {
|
||||
function by_path(path: ptr) -> Doc {
|
||||
let p = normpath(path)
|
||||
var i = 0
|
||||
while i < len(g_docs) { if (g_docs[i].path == p) { return g_docs[i] }; i = i + 1 }
|
||||
return null
|
||||
}
|
||||
fn by_uri(uri: ptr) -> Doc {
|
||||
function by_uri(uri: ptr) -> Doc {
|
||||
let p = uri_to_path(uri)
|
||||
let D = by_path(p)
|
||||
if (D != null) { return D }
|
||||
|
|
@ -1013,7 +1013,7 @@ program LudicLsp {
|
|||
while i < len(g_docs) { if (g_docs[i].uri == uri) { return g_docs[i] }; i = i + 1 }
|
||||
return null
|
||||
}
|
||||
fn ensure_doc(path: ptr) -> Doc {
|
||||
function ensure_doc(path: ptr) -> Doc {
|
||||
let D = by_path(path)
|
||||
if (D != null) { return D }
|
||||
let text = read_file(path)
|
||||
|
|
@ -1023,7 +1023,7 @@ program LudicLsp {
|
|||
return nd
|
||||
}
|
||||
|
||||
fn scan_root(root: ptr) -> void {
|
||||
function scan_root(root: ptr) -> void {
|
||||
if (root == null) { return }
|
||||
let listfile = "/tmp/ludic_lsp_scan.txt"
|
||||
run(concat3(concat3("find '", root, "' -name '*.ludic' -not -path '*/build/*' -not -path '*/node_modules/*' -not -path '*/.*' > "), listfile, " 2>/dev/null"))
|
||||
|
|
@ -1041,7 +1041,7 @@ program LudicLsp {
|
|||
var g_owner: Doc = null
|
||||
var g_symidx: int = 0
|
||||
|
||||
fn imports_of(D: Doc, out: []Doc) -> void {
|
||||
function imports_of(D: Doc, out: []Doc) -> void {
|
||||
var i = 0
|
||||
while i < len(D.imports) {
|
||||
let full = join_path(dirname(D.path), D.imports[i])
|
||||
|
|
@ -1055,7 +1055,7 @@ program LudicLsp {
|
|||
i = i + 1
|
||||
}
|
||||
}
|
||||
fn related(D: Doc) -> []Doc {
|
||||
function related(D: Doc) -> []Doc {
|
||||
let out = new []Doc
|
||||
if (D == null) { return out }
|
||||
push(out, D)
|
||||
|
|
@ -1088,7 +1088,7 @@ program LudicLsp {
|
|||
}
|
||||
return out
|
||||
}
|
||||
fn unit_root(D: Doc) -> Doc {
|
||||
function unit_root(D: Doc) -> Doc {
|
||||
if (D == null) or D.is_md == 1 { return null }
|
||||
if D.is_unit == 1 { return D }
|
||||
let rel = related(D)
|
||||
|
|
@ -1096,7 +1096,7 @@ program LudicLsp {
|
|||
while i < len(rel) { if rel[i].is_unit == 1 and rel[i].is_md == 0 { return rel[i] }; i = i + 1 }
|
||||
return null
|
||||
}
|
||||
fn find_local(D: Doc, name: ptr, off: int) -> int {
|
||||
function find_local(D: Doc, name: ptr, off: int) -> int {
|
||||
var best = 0 - 1
|
||||
var i = 0
|
||||
while i < len(D.syms) {
|
||||
|
|
@ -1109,7 +1109,7 @@ program LudicLsp {
|
|||
return best
|
||||
}
|
||||
# returns sym index in g_owner (set), or -1
|
||||
fn find_top(D: Doc, name: ptr) -> int {
|
||||
function find_top(D: Doc, name: ptr) -> int {
|
||||
let rel = related(D)
|
||||
var i = 0
|
||||
while i < len(rel) {
|
||||
|
|
@ -1132,7 +1132,7 @@ program LudicLsp {
|
|||
return 0 - 1
|
||||
}
|
||||
# component sym index (in g_owner) for a dotted receiver, or -1
|
||||
fn receiver_component(D: Doc, dot_tok: int) -> int {
|
||||
function receiver_component(D: Doc, dot_tok: int) -> int {
|
||||
let recv = prev_sig(D, dot_tok)
|
||||
if recv < 0 or D.tk_kind[recv] != LT_ID { return 0 - 1 }
|
||||
let name = ttext(D, recv)
|
||||
|
|
@ -1145,7 +1145,7 @@ program LudicLsp {
|
|||
return find_top(D, ty)
|
||||
}
|
||||
# resolve the token: sets g_owner/g_symidx; returns the token index (or -1)
|
||||
fn resolve_tok(D: Doc, tok: int) -> int {
|
||||
function resolve_tok(D: Doc, tok: int) -> int {
|
||||
g_owner = null; g_symidx = 0 - 1
|
||||
if tok < 0 { return 0 - 1 }
|
||||
if D.tk_kind[tok] != LT_ID and D.tk_kind[tok] != LT_STR { return 0 - 1 }
|
||||
|
|
@ -1173,10 +1173,10 @@ program LudicLsp {
|
|||
g_owner = null; g_symidx = 0 - 1
|
||||
return tok
|
||||
}
|
||||
fn resolve_at(D: Doc, off: int) -> int { return resolve_tok(D, tok_at(D, off)) }
|
||||
function resolve_at(D: Doc, off: int) -> int { return resolve_tok(D, tok_at(D, off)) }
|
||||
|
||||
fn first_on_line(D: Doc, tok: int) -> bool { let p = prev_sig(D, tok); return p < 0 or D.tk_line[p] != D.tk_line[tok] }
|
||||
fn in_ui_body(D: Doc, off: int) -> bool {
|
||||
function first_on_line(D: Doc, tok: int) -> bool { let p = prev_sig(D, tok); return p < 0 or D.tk_line[p] != D.tk_line[tok] }
|
||||
function in_ui_body(D: Doc, off: int) -> bool {
|
||||
var i = 0
|
||||
while i < len(D.syms) { if D.syms[i].kind == LS_UI and off > D.syms[i].bstart and off < D.syms[i].bend { return true }; i = i + 1 }
|
||||
return false
|
||||
|
|
@ -1185,16 +1185,16 @@ program LudicLsp {
|
|||
# ---- builtins / intrinsics (name -> signature) ----
|
||||
var g_builtins: []ptr
|
||||
var g_bsigs: []ptr
|
||||
fn badd(name: ptr, sig: ptr) -> void { push(g_builtins, name); push(g_bsigs, sig) }
|
||||
fn is_builtin_name(name: ptr) -> bool { var i = 0; while i < len(g_builtins) { if (g_builtins[i] == name) { return true }; i = i + 1 }; return false }
|
||||
fn builtin_sig(name: ptr) -> ptr { var i = 0; while i < len(g_builtins) { if (g_builtins[i] == name) { return g_bsigs[i] }; i = i + 1 }; return null }
|
||||
function badd(name: ptr, sig: ptr) -> void { push(g_builtins, name); push(g_bsigs, sig) }
|
||||
function is_builtin_name(name: ptr) -> bool { var i = 0; while i < len(g_builtins) { if (g_builtins[i] == name) { return true }; i = i + 1 }; return false }
|
||||
function builtin_sig(name: ptr) -> ptr { var i = 0; while i < len(g_builtins) { if (g_builtins[i] == name) { return g_bsigs[i] }; i = i + 1 }; return null }
|
||||
|
||||
# The namespaced standard-library surface: `Namespace.method`, mirroring the
|
||||
# compiler's emit_ns_call (selfhost/emit_expr.ludic). Screen/Random/Input/Map
|
||||
# methods resolve to the underlying bare builtin's signature; Math/Text/List
|
||||
# are pure fixed-point / string / slice ops with their own signatures. Returns
|
||||
# the signature label for signature help, or null if it is not a known method.
|
||||
fn ns_method_sig(ns: ptr, meth: ptr) -> ptr {
|
||||
function ns_method_sig(ns: ptr, meth: ptr) -> ptr {
|
||||
if (ns == "Screen") {
|
||||
if (meth == "clear") { return builtin_sig("clear") }
|
||||
if (meth == "fill_rectangle") { return builtin_sig("fill_rect") }
|
||||
|
|
@ -1395,7 +1395,7 @@ program LudicLsp {
|
|||
}
|
||||
return null
|
||||
}
|
||||
fn init_builtins() -> void {
|
||||
function init_builtins() -> void {
|
||||
g_builtins = new []ptr; g_bsigs = new []ptr
|
||||
badd("min", "min(a: int, b: int) -> int")
|
||||
badd("max", "max(a: int, b: int) -> int")
|
||||
|
|
@ -1478,7 +1478,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- classification ----
|
||||
fn class_of_symkind(k: int) -> int {
|
||||
function class_of_symkind(k: int) -> int {
|
||||
if k == LS_COMPONENT { return SC_COMPONENT }
|
||||
if k == LS_ARCHETYPE { return SC_ARCHETYPE }
|
||||
if k == LS_FIELD { return SC_FIELD }
|
||||
|
|
@ -1495,7 +1495,7 @@ program LudicLsp {
|
|||
if k == LS_UNIT { return SC_UI }
|
||||
return SC_UNKNOWN
|
||||
}
|
||||
fn classify(D: Doc) -> void {
|
||||
function classify(D: Doc) -> void {
|
||||
let N = ntok(D)
|
||||
if len(D.cls) < N { D.cls = new []int; var z = 0; while z < N { push(D.cls, SC_NONE); z = z + 1 } }
|
||||
var i = 0
|
||||
|
|
@ -1542,7 +1542,7 @@ program LudicLsp {
|
|||
|
||||
# semantic token type index + modifiers (via g_semmods)
|
||||
var g_semmods: int = 0
|
||||
fn sem_type_of(cls: int) -> int {
|
||||
function sem_type_of(cls: int) -> int {
|
||||
g_semmods = 0
|
||||
if cls == SC_KEYWORD { return 0 }
|
||||
if cls == SC_TYPE { g_semmods = 4; return 1 }
|
||||
|
|
@ -1580,23 +1580,23 @@ program LudicLsp {
|
|||
const JARR: int = 4
|
||||
const JOBJ: int = 5
|
||||
property JVal { t: int = 0, num: int = 0, b: int = 0, s: ptr = null, kids: []JVal, keys: []ptr }
|
||||
fn jval(t: int) -> JVal { let v = new JVal; v.t = t; v.kids = new []JVal; v.keys = new []ptr; return v }
|
||||
function jval(t: int) -> JVal { let v = new JVal; v.t = t; v.kids = new []JVal; v.keys = new []ptr; return v }
|
||||
|
||||
var jsrc: ptr = null
|
||||
var jp: int = 0
|
||||
fn jskip() -> void { while jsrc[jp] == 32 or jsrc[jp] == 9 or jsrc[jp] == 10 or jsrc[jp] == 13 { jp = jp + 1 } }
|
||||
fn jhex4(s: ptr, at: int) -> int {
|
||||
function jskip() -> void { while jsrc[jp] == 32 or jsrc[jp] == 9 or jsrc[jp] == 10 or jsrc[jp] == 13 { jp = jp + 1 } }
|
||||
function jhex4(s: ptr, at: int) -> int {
|
||||
var v = 0; var i = 0
|
||||
while i < 4 { let h = hexval(s[at + i]); if h < 0 { return v }; v = v * 16 + h; i = i + 1 }
|
||||
return v
|
||||
}
|
||||
fn utf8_emit(o: Buf, cp: int) -> void {
|
||||
function utf8_emit(o: Buf, cp: int) -> void {
|
||||
if cp < 128 { buf_putc(o, cp) }
|
||||
else { if cp < 2048 { buf_putc(o, 192 | (cp >> 6)); buf_putc(o, 128 | (cp & 63)) }
|
||||
else { if cp < 65536 { buf_putc(o, 224 | (cp >> 12)); buf_putc(o, 128 | ((cp >> 6) & 63)); buf_putc(o, 128 | (cp & 63)) }
|
||||
else { buf_putc(o, 240 | (cp >> 18)); buf_putc(o, 128 | ((cp >> 12) & 63)); buf_putc(o, 128 | ((cp >> 6) & 63)); buf_putc(o, 128 | (cp & 63)) } } }
|
||||
}
|
||||
fn jparse_string() -> ptr {
|
||||
function jparse_string() -> ptr {
|
||||
jp = jp + 1 # skip opening quote
|
||||
let o = buf_new()
|
||||
while jsrc[jp] != 0 and jsrc[jp] != 34 {
|
||||
|
|
@ -1621,7 +1621,7 @@ program LudicLsp {
|
|||
if jsrc[jp] == 34 { jp = jp + 1 }
|
||||
return buf_str(o)
|
||||
}
|
||||
fn jparse() -> JVal {
|
||||
function jparse() -> JVal {
|
||||
jskip()
|
||||
let c = jsrc[jp]
|
||||
if c == 123 { # '{'
|
||||
|
|
@ -1671,18 +1671,18 @@ program LudicLsp {
|
|||
jp = jp + 1
|
||||
return jval(JNULL)
|
||||
}
|
||||
fn jget(v: JVal, key: ptr) -> JVal {
|
||||
function jget(v: JVal, key: ptr) -> JVal {
|
||||
if (v == null) { return null }
|
||||
if v.t != JOBJ { return null }
|
||||
var i = 0
|
||||
while i < len(v.keys) { if (v.keys[i] == key) { return v.kids[i] }; i = i + 1 }
|
||||
return null
|
||||
}
|
||||
fn jat(v: JVal, i: int) -> JVal { if (v == null) { return null }; if i < 0 or i >= len(v.kids) { return null }; return v.kids[i] }
|
||||
fn jlen(v: JVal) -> int { if (v == null) { return 0 }; return len(v.kids) }
|
||||
fn jstr(v: JVal, def: ptr) -> ptr { if (v != null) and v.t == JSTR and (v.s != null) { return v.s }; return def }
|
||||
fn jint(v: JVal, def: int) -> int { if (v != null) and v.t == JNUM { return v.num }; return def }
|
||||
fn jpath(v: JVal, path: ptr) -> JVal {
|
||||
function jat(v: JVal, i: int) -> JVal { if (v == null) { return null }; if i < 0 or i >= len(v.kids) { return null }; return v.kids[i] }
|
||||
function jlen(v: JVal) -> int { if (v == null) { return 0 }; return len(v.kids) }
|
||||
function jstr(v: JVal, def: ptr) -> ptr { if (v != null) and v.t == JSTR and (v.s != null) { return v.s }; return def }
|
||||
function jint(v: JVal, def: int) -> int { if (v != null) and v.t == JNUM { return v.num }; return def }
|
||||
function jpath(v: JVal, path: ptr) -> JVal {
|
||||
var cur = v
|
||||
let n = cstr_len(path)
|
||||
var start = 0; var i = 0
|
||||
|
|
@ -1701,8 +1701,8 @@ program LudicLsp {
|
|||
# ============================================================================
|
||||
# the formatter (Doc-based; reused for LSP formatting) — mirrors fmt.ludic
|
||||
# ============================================================================
|
||||
fn f_name_like(k: int) -> bool { return k == LT_ID or k == LT_KW or k == LT_TYPE or k == LT_PHASE or k == LT_BOOL }
|
||||
fn f_dot_tight(F: Doc, t: int) -> bool {
|
||||
function f_name_like(k: int) -> bool { return k == LT_ID or k == LT_KW or k == LT_TYPE or k == LT_PHASE or k == LT_BOOL }
|
||||
function f_dot_tight(F: Doc, t: int) -> bool {
|
||||
if f_name_like(F.tk_kind[t]) { return true }
|
||||
if F.tk_kind[t] != LT_OP { return false }
|
||||
let n = tlen(F, t); let c0 = F.src[F.tk_start[t]]
|
||||
|
|
@ -1710,7 +1710,7 @@ program LudicLsp {
|
|||
if n == 2 and c0 == 46 and F.src[F.tk_start[t] + 1] == 46 { return true }
|
||||
return false
|
||||
}
|
||||
fn f_is_unary(F: Doc, i: int) -> bool {
|
||||
function f_is_unary(F: Doc, i: int) -> bool {
|
||||
if F.tk_kind[i] != LT_OP { return false }
|
||||
let n = tlen(F, i)
|
||||
if not (n == 1 and (F.src[F.tk_start[i]] == 45 or F.src[F.tk_start[i]] == 33)) { return false }
|
||||
|
|
@ -1721,7 +1721,7 @@ program LudicLsp {
|
|||
if pk2 == LT_OP { let c = F.src[F.tk_start[p]]; return not (tlen(F, p) == 1 and (c == 41 or c == 93 or c == 125)) }
|
||||
return true
|
||||
}
|
||||
fn f_space_before(F: Doc, prev: int, cur: int) -> int {
|
||||
function f_space_before(F: Doc, prev: int, cur: int) -> int {
|
||||
if prev < 0 { return 0 }
|
||||
let pk2 = F.tk_kind[prev]; let ck = F.tk_kind[cur]
|
||||
let plen = tlen(F, prev); let clen = tlen(F, cur)
|
||||
|
|
@ -1741,16 +1741,16 @@ program LudicLsp {
|
|||
}
|
||||
return 1
|
||||
}
|
||||
fn f_is_clause(w: ptr) -> bool {
|
||||
function f_is_clause(w: ptr) -> bool {
|
||||
return (w == "phase") or (w == "query") or (w == "reads") or (w == "writes") or (w == "needs") or (w == "uses") or (w == "requires") or (w == "ensures") or (w == "invariant") or (w == "effects")
|
||||
}
|
||||
fn make_text_doc(text: ptr) -> Doc {
|
||||
function make_text_doc(text: ptr) -> Doc {
|
||||
let F = new Doc
|
||||
F.raw = text; F.text = text; F.src = text; F.is_md = 0
|
||||
lex_doc(F)
|
||||
return F
|
||||
}
|
||||
fn format_doc(F: Doc, iw: int) -> ptr {
|
||||
function format_doc(F: Doc, iw: int) -> ptr {
|
||||
let o = buf_new()
|
||||
let stack = words(600)
|
||||
let hang = words(600)
|
||||
|
|
@ -1855,7 +1855,7 @@ program LudicLsp {
|
|||
var mf_flen: int = 0
|
||||
var mf_info: int = 0
|
||||
var mf_marker: int = 0
|
||||
fn mf_fence_at(s: ptr, i: int) -> bool {
|
||||
function mf_fence_at(s: ptr, i: int) -> bool {
|
||||
var j = i; while s[j] == 32 { j = j + 1 }
|
||||
let m = s[j]
|
||||
if m != 96 and m != 126 { return false }
|
||||
|
|
@ -1864,8 +1864,8 @@ program LudicLsp {
|
|||
mf_flen = n; mf_info = j; mf_marker = m
|
||||
return true
|
||||
}
|
||||
fn f_line_end(s: ptr, i: int) -> int { var e = i; while s[e] != 0 and s[e] != 10 { e = e + 1 }; return e }
|
||||
fn format_md(s: ptr, iw: int) -> ptr {
|
||||
function f_line_end(s: ptr, i: int) -> int { var e = i; while s[e] != 0 and s[e] != 10 { e = e + 1 }; return e }
|
||||
function format_md(s: ptr, iw: int) -> ptr {
|
||||
let o = buf_new()
|
||||
var i = 0
|
||||
while s[i] != 0 {
|
||||
|
|
@ -1925,14 +1925,14 @@ program LudicLsp {
|
|||
}
|
||||
return buf_str(o)
|
||||
}
|
||||
fn skip_ws2(s: ptr, at: int) -> int { var a = at; while s[a] == 32 or s[a] == 9 { a = a + 1 }; return a }
|
||||
function skip_ws2(s: ptr, at: int) -> int { var a = at; while s[a] == 32 or s[a] == 9 { a = a + 1 }; return a }
|
||||
|
||||
# ============================================================================
|
||||
# framing + output
|
||||
# ============================================================================
|
||||
extern fn fflush(f: ptr) -> int = "fflush"
|
||||
extern function fflush(f: ptr) -> int = "fflush"
|
||||
|
||||
fn jstr_out(o: Buf, z: ptr) -> void {
|
||||
function jstr_out(o: Buf, z: ptr) -> void {
|
||||
buf_putc(o, 34)
|
||||
if (z == null) { buf_putc(o, 34); return }
|
||||
var i = 0
|
||||
|
|
@ -1950,7 +1950,7 @@ program LudicLsp {
|
|||
buf_putc(o, 34)
|
||||
}
|
||||
|
||||
fn send_raw(body: ptr) -> void {
|
||||
function send_raw(body: ptr) -> void {
|
||||
let hdr = buf_new()
|
||||
buf_puts(hdr, "Content-Length: "); buf_puti(hdr, cstr_len(body)); buf_putc(hdr, 13); buf_putc(hdr, 10); buf_putc(hdr, 13); buf_putc(hdr, 10)
|
||||
let hs = buf_str(hdr)
|
||||
|
|
@ -1959,7 +1959,7 @@ program LudicLsp {
|
|||
file_write(out, body, cstr_len(body))
|
||||
let z = fflush(out)
|
||||
}
|
||||
fn send_result(idv: JVal, result: ptr) -> void {
|
||||
function send_result(idv: JVal, result: ptr) -> void {
|
||||
let o = buf_new()
|
||||
buf_puts(o, "{\"jsonrpc\":\"2.0\",\"id\":")
|
||||
if (idv == null) { buf_puts(o, "null") }
|
||||
|
|
@ -1969,19 +1969,19 @@ program LudicLsp {
|
|||
buf_putc(o, 125)
|
||||
send_raw(buf_str(o))
|
||||
}
|
||||
fn send_notify(method: ptr, params: ptr) -> void {
|
||||
function send_notify(method: ptr, params: ptr) -> void {
|
||||
let o = buf_new()
|
||||
buf_puts(o, "{\"jsonrpc\":\"2.0\",\"method\":"); jstr_out(o, method); buf_puts(o, ",\"params\":"); buf_puts(o, params); buf_putc(o, 125)
|
||||
send_raw(buf_str(o))
|
||||
}
|
||||
|
||||
fn lower(c: int) -> int { if c >= 65 and c <= 90 { return c + 32 }; return c }
|
||||
fn starts_with_ci(s: ptr, pre: ptr) -> bool {
|
||||
function lower(c: int) -> int { if c >= 65 and c <= 90 { return c + 32 }; return c }
|
||||
function starts_with_ci(s: ptr, pre: ptr) -> bool {
|
||||
var i = 0
|
||||
while pre[i] != 0 { if s[i] == 0 or lower(s[i]) != lower(pre[i]) { return false }; i = i + 1 }
|
||||
return true
|
||||
}
|
||||
fn read_message() -> ptr {
|
||||
function read_message() -> ptr {
|
||||
var length = 0 - 1
|
||||
let line = buf_new()
|
||||
while true {
|
||||
|
|
@ -2004,26 +2004,26 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- position output ----
|
||||
fn put_pos(o: Buf, D: Doc, off: int) -> void { buf_puts(o, "{\"line\":"); buf_puti(o, line_of(D, off)); buf_puts(o, ",\"character\":"); buf_puti(o, col_of(D, off)); buf_putc(o, 125) }
|
||||
fn put_range(o: Buf, D: Doc, a: int, b: int) -> void { buf_puts(o, "{\"start\":"); put_pos(o, D, a); buf_puts(o, ",\"end\":"); put_pos(o, D, b); buf_putc(o, 125) }
|
||||
fn put_location(o: Buf, D: Doc, a: int, b: int) -> void { buf_puts(o, "{\"uri\":"); jstr_out(o, D.uri); buf_puts(o, ",\"range\":"); put_range(o, D, a, b); buf_putc(o, 125) }
|
||||
function put_pos(o: Buf, D: Doc, off: int) -> void { buf_puts(o, "{\"line\":"); buf_puti(o, line_of(D, off)); buf_puts(o, ",\"character\":"); buf_puti(o, col_of(D, off)); buf_putc(o, 125) }
|
||||
function put_range(o: Buf, D: Doc, a: int, b: int) -> void { buf_puts(o, "{\"start\":"); put_pos(o, D, a); buf_puts(o, ",\"end\":"); put_pos(o, D, b); buf_putc(o, 125) }
|
||||
function put_location(o: Buf, D: Doc, a: int, b: int) -> void { buf_puts(o, "{\"uri\":"); jstr_out(o, D.uri); buf_puts(o, ",\"range\":"); put_range(o, D, a, b); buf_putc(o, 125) }
|
||||
|
||||
fn doc_of(msg: JVal) -> Doc {
|
||||
function doc_of(msg: JVal) -> Doc {
|
||||
let uri = jstr(jpath(msg, "params.textDocument.uri"), null)
|
||||
if (uri == null) { return null }
|
||||
let D = by_uri(uri)
|
||||
if (D != null) { return D }
|
||||
return ensure_doc(uri_to_path(uri))
|
||||
}
|
||||
fn off_of(msg: JVal, D: Doc, base: ptr) -> int {
|
||||
function off_of(msg: JVal, D: Doc, base: ptr) -> int {
|
||||
let line = jint(jpath(msg, concat3(base, ".line", "")), 0)
|
||||
let ch = jint(jpath(msg, concat3(base, ".character", "")), 0)
|
||||
return offset_of(D, line, ch)
|
||||
}
|
||||
fn basename(p: ptr) -> ptr { var last = 0 - 1; var i = 0; while p[i] != 0 { if p[i] == 47 { last = i }; i = i + 1 }; return p[last + 1..i] }
|
||||
function basename(p: ptr) -> ptr { var last = 0 - 1; var i = 0; while p[i] != 0 { if p[i] == 47 { last = i }; i = i + 1 }; return p[last + 1..i] }
|
||||
|
||||
# ---- LSP symbol kinds & completion kinds ----
|
||||
fn lsp_symbol_kind(k: int) -> int {
|
||||
function lsp_symbol_kind(k: int) -> int {
|
||||
if k == LS_UNIT { return 2 }
|
||||
if k == LS_COMPONENT { return 23 }
|
||||
if k == LS_FIELD { return 8 }
|
||||
|
|
@ -2038,7 +2038,7 @@ program LudicLsp {
|
|||
if k == LS_LAYER { return 3 }
|
||||
return 13
|
||||
}
|
||||
fn comp_kind_of(k: int) -> int {
|
||||
function comp_kind_of(k: int) -> int {
|
||||
if k == LS_COMPONENT { return 22 }
|
||||
if k == LS_ARCHETYPE { return 7 }
|
||||
if k == LS_CONST { return 21 }
|
||||
|
|
@ -2054,7 +2054,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- diagnostics (structural) ----
|
||||
fn diag_structural(D: Doc, o: Buf) -> void {
|
||||
function diag_structural(D: Doc, o: Buf) -> void {
|
||||
let ds_s = new []int; let ds_e = new []int; let ds_m = new []ptr
|
||||
let stack = words(1200); var top = 0
|
||||
var i = 0; let N = ntok(D)
|
||||
|
|
@ -2081,7 +2081,7 @@ program LudicLsp {
|
|||
j = j + 1
|
||||
}
|
||||
}
|
||||
fn publish_diag(D: Doc) -> void {
|
||||
function publish_diag(D: Doc) -> void {
|
||||
let o = buf_new()
|
||||
buf_puts(o, "{\"uri\":"); jstr_out(o, D.uri); buf_puts(o, ",\"diagnostics\":[")
|
||||
if D.is_md == 0 { diag_structural(D, o) }
|
||||
|
|
@ -2090,7 +2090,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- document symbols ----
|
||||
fn put_doc_symbols(o: Buf, D: Doc, parent: int) -> void {
|
||||
function put_doc_symbols(o: Buf, D: Doc, parent: int) -> void {
|
||||
buf_putc(o, 91)
|
||||
var first = 1
|
||||
var i = 0
|
||||
|
|
@ -2116,13 +2116,13 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- hover ----
|
||||
fn hover_md(o: Buf, code: ptr, docstr: ptr) -> void {
|
||||
function hover_md(o: Buf, code: ptr, docstr: ptr) -> void {
|
||||
let m = buf_new()
|
||||
buf_puts(m, "```ludic\n"); buf_puts(m, code); buf_puts(m, "\n```")
|
||||
if (docstr != null) and (docstr[0] != 0) { buf_puts(m, "\n\n"); buf_puts(m, docstr) }
|
||||
buf_puts(o, "{\"contents\":{\"kind\":\"markdown\",\"value\":"); jstr_out(o, buf_str(m)); buf_puts(o, "}}")
|
||||
}
|
||||
fn on_hover(msg: JVal, id: JVal) -> void {
|
||||
function on_hover(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2147,7 +2147,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- definition ----
|
||||
fn on_definition(msg: JVal, id: JVal) -> void {
|
||||
function on_definition(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2172,7 +2172,7 @@ program LudicLsp {
|
|||
# ---- occurrences (references / rename) ----
|
||||
var g_occ_doc: []Doc
|
||||
var g_occ_tok: []int
|
||||
fn find_occurrences(from: Doc, tdoc: Doc, tsym: int) -> void {
|
||||
function find_occurrences(from: Doc, tdoc: Doc, tsym: int) -> void {
|
||||
g_occ_doc = new []Doc; g_occ_tok = new []int
|
||||
let want = tdoc.syms[tsym].name
|
||||
var docs = new []Doc
|
||||
|
|
@ -2191,7 +2191,7 @@ program LudicLsp {
|
|||
i = i + 1
|
||||
}
|
||||
}
|
||||
fn on_references(msg: JVal, id: JVal) -> void {
|
||||
function on_references(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "[]"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2209,7 +2209,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_putc(o, 93); send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_rename(msg: JVal, id: JVal) -> void {
|
||||
function on_rename(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
let newname = jstr(jpath(msg, "params.newName"), null)
|
||||
if (D == null) or (newname == null) or (newname[0] == 0) { send_result(id, "null"); return }
|
||||
|
|
@ -2250,14 +2250,14 @@ program LudicLsp {
|
|||
|
||||
# ---- completion ----
|
||||
var g_first: int = 1
|
||||
fn comp_item(o: Buf, label: ptr, kind: int, detail: ptr) -> void {
|
||||
function comp_item(o: Buf, label: ptr, kind: int, detail: ptr) -> void {
|
||||
if g_first == 0 { buf_putc(o, 44) }
|
||||
g_first = 0
|
||||
buf_puts(o, "{\"label\":"); jstr_out(o, label); buf_puts(o, ",\"kind\":"); buf_puti(o, kind)
|
||||
if (detail != null) and (detail[0] != 0) { buf_puts(o, ",\"detail\":"); jstr_out(o, detail) }
|
||||
buf_putc(o, 125)
|
||||
}
|
||||
fn comp_scope(o: Buf, D: Doc, off: int, types_only: int) -> void {
|
||||
function comp_scope(o: Buf, D: Doc, off: int, types_only: int) -> void {
|
||||
if types_only == 0 {
|
||||
var i = 0
|
||||
while i < len(D.syms) {
|
||||
|
|
@ -2285,12 +2285,12 @@ program LudicLsp {
|
|||
r = r + 1
|
||||
}
|
||||
}
|
||||
fn comp_types(o: Buf) -> void {
|
||||
function comp_types(o: Buf) -> void {
|
||||
comp_item(o, "int", 14, "built-in type"); comp_item(o, "long", 14, "built-in type"); comp_item(o, "fixed", 14, "built-in type"); comp_item(o, "bool", 14, "built-in type")
|
||||
comp_item(o, "entity", 14, "built-in type"); comp_item(o, "str", 14, "built-in type"); comp_item(o, "ptr", 14, "built-in type"); comp_item(o, "void", 14, "built-in type")
|
||||
}
|
||||
fn comp_builtins(o: Buf) -> void { var i = 0; while i < len(g_builtins) { comp_item(o, g_builtins[i], 3, g_bsigs[i]); i = i + 1 } }
|
||||
fn on_completion(msg: JVal, id: JVal) -> void {
|
||||
function comp_builtins(o: Buf) -> void { var i = 0; while i < len(g_builtins) { comp_item(o, g_builtins[i], 3, g_bsigs[i]); i = i + 1 } }
|
||||
function on_completion(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "{\"isIncomplete\":false,\"items\":[]}"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2350,7 +2350,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- inlay hints, signature help, semantic tokens, folding ----
|
||||
fn on_inlay_hint(msg: JVal, id: JVal) -> void {
|
||||
function on_inlay_hint(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "[]"); return }
|
||||
let o = buf_new(); buf_putc(o, 91)
|
||||
|
|
@ -2367,7 +2367,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_putc(o, 93); send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_signature_help(msg: JVal, id: JVal) -> void {
|
||||
function on_signature_help(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
let off = off_of(msg, D, "params.position")
|
||||
|
|
@ -2412,7 +2412,7 @@ program LudicLsp {
|
|||
buf_puts(o, "}],\"activeSignature\":0,\"activeParameter\":"); buf_puti(o, arg); buf_putc(o, 125)
|
||||
send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_semantic_tokens(msg: JVal, id: JVal) -> void {
|
||||
function on_semantic_tokens(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
classify(D)
|
||||
|
|
@ -2442,7 +2442,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_puts(o, "]}"); send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_folding(msg: JVal, id: JVal) -> void {
|
||||
function on_folding(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "[]"); return }
|
||||
let o = buf_new(); buf_putc(o, 91); var first = 1
|
||||
|
|
@ -2463,7 +2463,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_putc(o, 93); send_result(id, buf_str(o))
|
||||
}
|
||||
fn on_workspace_symbol(msg: JVal, id: JVal) -> void {
|
||||
function on_workspace_symbol(msg: JVal, id: JVal) -> void {
|
||||
let q = jstr(jpath(msg, "params.query"), "")
|
||||
let o = buf_new(); buf_putc(o, 91); var first = 1
|
||||
var d = 0
|
||||
|
|
@ -2487,7 +2487,7 @@ program LudicLsp {
|
|||
}
|
||||
buf_putc(o, 93); send_result(id, buf_str(o))
|
||||
}
|
||||
fn contains_ci(hay: ptr, needle: ptr) -> bool {
|
||||
function contains_ci(hay: ptr, needle: ptr) -> bool {
|
||||
let hn = cstr_len(hay); let nn = cstr_len(needle)
|
||||
if nn == 0 { return true }
|
||||
var i = 0
|
||||
|
|
@ -2500,7 +2500,7 @@ program LudicLsp {
|
|||
}
|
||||
return false
|
||||
}
|
||||
fn on_formatting(msg: JVal, id: JVal) -> void {
|
||||
function on_formatting(msg: JVal, id: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { send_result(id, "null"); return }
|
||||
var tab = jint(jpath(msg, "params.options.tabSize"), 2)
|
||||
|
|
@ -2518,7 +2518,7 @@ program LudicLsp {
|
|||
}
|
||||
|
||||
# ---- document lifecycle ----
|
||||
fn on_did_open(msg: JVal) -> void {
|
||||
function on_did_open(msg: JVal) -> void {
|
||||
let uri = jstr(jpath(msg, "params.textDocument.uri"), null)
|
||||
let text = jstr(jpath(msg, "params.textDocument.text"), "")
|
||||
if (uri == null) { return }
|
||||
|
|
@ -2529,7 +2529,7 @@ program LudicLsp {
|
|||
D.isopen = 1
|
||||
publish_diag(D)
|
||||
}
|
||||
fn on_did_change(msg: JVal) -> void {
|
||||
function on_did_change(msg: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { return }
|
||||
let changes = jpath(msg, "params.contentChanges")
|
||||
|
|
@ -2540,7 +2540,7 @@ program LudicLsp {
|
|||
reindex(D)
|
||||
publish_diag(D)
|
||||
}
|
||||
fn on_did_close(msg: JVal) -> void {
|
||||
function on_did_close(msg: JVal) -> void {
|
||||
let D = doc_of(msg)
|
||||
if (D == null) { return }
|
||||
D.isopen = 0
|
||||
|
|
@ -2548,7 +2548,7 @@ program LudicLsp {
|
|||
if (disk != null) { set_text(D, disk); reindex(D) }
|
||||
}
|
||||
|
||||
fn caps() -> ptr {
|
||||
function caps() -> ptr {
|
||||
let o = buf_new()
|
||||
buf_puts(o, "{\"capabilities\":{")
|
||||
buf_puts(o, "\"positionEncoding\":\"utf-16\"")
|
||||
|
|
@ -2572,14 +2572,14 @@ program LudicLsp {
|
|||
buf_puts(o, "},\"serverInfo\":{\"name\":\"ludic-lsp\",\"version\":\"1.0.0\"}}")
|
||||
return buf_str(o)
|
||||
}
|
||||
fn on_initialize(msg: JVal, id: JVal) -> void {
|
||||
function on_initialize(msg: JVal, id: JVal) -> void {
|
||||
let root = jpath(msg, "params.rootUri")
|
||||
if (root != null) and root.t == JSTR { g_root = uri_to_path(root.s) }
|
||||
if (g_root != null) { scan_root(g_root) }
|
||||
send_result(id, caps())
|
||||
}
|
||||
|
||||
fn handle(msg: JVal) -> void {
|
||||
function handle(msg: JVal) -> void {
|
||||
let method = jstr(jget(msg, "method"), null)
|
||||
let id = jget(msg, "id")
|
||||
if (method == null) { return }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue