increparse-lua 0.1.1

Define an increparse language server in pure Lua: passes, diagnostics, and symbols in one Lua file.
Documentation
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
//! Define an increparse language server in **pure Lua**.
//!
//! A language is a single Lua file returning a table:
//!
//! ```lua
//! -- minilang.lua
//! return {
//!   name = "minilang",
//!   root_ctx = { File = true },
//!   -- round r of a run calls passes[r]
//!   passes = {
//!     function(source, span, ctx)
//!       -- span = { start = , end = , rev = }
//!       -- return { expand = { { start = , end = , ctx = ... }, ... } }
//!       --     or "done" / "failed" / nil
//!       return "done"
//!     end,
//!   },
//!   -- optional: how a failing region becomes a diagnostic
//!   diagnostic = function(source, node) return { message = "oops" } end,
//!   -- optional: outline symbols from a snapshot of the tree
//!   symbols = function(nodes) return {} end,
//! }
//! ```
//!
//! Hand the file to the bundled binary and point any LSP client at it:
//!
//! ```text
//! increparse-lua-server ~/.config/minilang/lang.lua
//! ```
//!
//! # Semantics
//!
//! * Passes receive `(source, span, ctx)` where child ranges in the outcome
//!   are **absolute** byte ranges into `source` — no rebasing traps.
//! * Outcomes: a table with an `expand` array expands the region; `"done"`
//!   accepts it; `"failed"` or `nil` marks it failed (later passes retry).
//!   A Lua error thrown inside a pass is caught and treated as `Failed` —
//!   a broken pass never takes the server down.
//! * Contexts are arbitrary Lua values. Region reuse after an edit matches
//!   contexts by **deep equality** (tables compared recursively), so edits
//!   re-parse only the touched chain — the same incremental behaviour the
//!   Rust side gets.
//! * `diagnostic` receives `(source, node)` with
//!   `node = { ctx, status, start, end }` (`status` is `"failed"` or
//!   `"unparsed"`); returning a table with a `message` publishes a
//!   diagnostic at the node's range (optionally set `start`/`end` there).
//! * `symbols` receives an array of `{ start, end, status, ctx }` snapshots
//!   and returns `{ { name = , detail = , start = , end = } }`; ranges are
//!   byte ranges, converted to editor positions by the skeleton.

#![forbid(unsafe_code)]
#![deny(missing_docs)]

use std::error::Error;
use std::path::Path;

use increparse::{Engine, Outcome, Pass, Schedule, Span};
use increparse_lsp::{Document, FailedNode, Language};
use lsp_types::{Diagnostic, DiagnosticSeverity, DocumentSymbol, SymbolKind};
use mlua::{Function, Lua, Table, Value};

/// A context value from Lua: any [`Value`], compared by deep equality.
///
/// Deep equality is what lets the incremental tree reuse regions across
/// edits: two contexts match only if they are structurally identical
/// (tables recursively; `1` and `1.0` compare equal; functions and other
/// opaque values never compare equal — contexts should be data).
#[derive(Debug, Clone)]
pub struct LuaCtx(pub Value);

impl PartialEq for LuaCtx {
    fn eq(&self, other: &Self) -> bool {
        deep_eq(&self.0, &other.0)
    }
}

impl Eq for LuaCtx {}

fn deep_eq(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Nil, Value::Nil) => true,
        (Value::Boolean(x), Value::Boolean(y)) => x == y,
        (Value::Integer(x), Value::Integer(y)) => x == y,
        (Value::Number(x), Value::Number(y)) => x == y,
        (Value::Integer(x), Value::Number(y)) | (Value::Number(y), Value::Integer(x)) => {
            (*x as f64) == *y
        }
        (Value::String(x), Value::String(y)) => x.as_bytes() == y.as_bytes(),
        (Value::Table(x), Value::Table(y)) => {
            let a: Vec<(Value, Value)> = x.pairs().filter_map(|p| p.ok()).collect();
            let b: Vec<(Value, Value)> = y.pairs().filter_map(|p| p.ok()).collect();
            if a.len() != b.len() {
                return false;
            }
            a.iter()
                .all(|(k, v)| b.iter().any(|(k2, v2)| deep_eq(k, k2) && deep_eq(v, v2)))
        }
        _ => false,
    }
}

fn status_name(status: increparse::Status) -> &'static str {
    match status {
        increparse::Status::Unparsed => "unparsed",
        increparse::Status::Expanded => "expanded",
        increparse::Status::Done => "done",
        increparse::Status::Failed => "failed",
    }
}

/// A [`Pass`] backed by a Lua function.
#[derive(Clone)]
pub(crate) struct LuaPass {
    lua: Lua,
    function: Function,
    index: usize,
}

impl LuaPass {
    fn try_parse(
        &self,
        source: &str,
        span: Span,
        ctx: &LuaCtx,
    ) -> Result<Outcome<LuaCtx>, Box<dyn Error + Send + Sync>> {
        let lua = &self.lua;
        let span_table = lua.create_table()?;
        span_table.set("start", span.start)?;
        span_table.set("end", span.end)?;
        span_table.set("rev", span.rev)?;

        let result: Value = self.function.call((source, span_table, ctx.0.clone()))?;

        match result {
            Value::Nil => Ok(Outcome::Failed),
            Value::String(s) if s.as_bytes() == b"done" => Ok(Outcome::Done),
            Value::String(s) if s.as_bytes() == b"failed" => Ok(Outcome::Failed),
            Value::Table(outcome) => {
                let expand: Value = outcome.get("expand")?;
                let Value::Table(array) = expand else {
                    eprintln!(
                        "increparse-lua: pass #{} returned a table without `expand`; treating as failed",
                        self.index
                    );
                    return Ok(Outcome::Failed);
                };
                let mut children = Vec::new();
                for item in array.sequence_values::<Table>() {
                    let item = item?;
                    let start: usize = item.get("start")?;
                    let end: usize = item.get("end")?;
                    let child_ctx = LuaCtx(item.get::<Value>("ctx")?);
                    if start > end {
                        return Err(format!(
                            "pass #{} produced a child with start {start} > end {end}",
                            self.index
                        )
                        .into());
                    }
                    children.push((Span::new(start, end, span.rev), child_ctx));
                }
                Ok(Outcome::Expand(children))
            }
            other => {
                eprintln!(
                    "increparse-lua: pass #{} returned {}; treating as failed",
                    self.index,
                    other.type_name()
                );
                Ok(Outcome::Failed)
            }
        }
    }
}

impl Pass for LuaPass {
    type Ctx = LuaCtx;

    fn parse(&self, source: &str, span: Span, ctx: &LuaCtx) -> Outcome<LuaCtx> {
        match self.try_parse(source, span, ctx) {
            Ok(outcome) => outcome,
            Err(err) => {
                eprintln!("increparse-lua: pass #{} error: {err}", self.index);
                Outcome::Failed
            }
        }
    }

    fn name(&self) -> &'static str {
        "LuaPass"
    }
}

/// A language defined by a Lua configuration file.
///
/// Build with [`LuaLanguage::from_path`] and hand it to
/// [`increparse_lsp::serve`](increparse_lsp::serve):
///
/// ```no_run
/// fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
///     let language = increparse_lua::LuaLanguage::from_path("minilang.lua")?;
///     increparse_lsp::serve(language)
/// }
/// ```
pub struct LuaLanguage {
    lua: Lua,
    engine: Engine<LuaCtx>,
    root_ctx: LuaCtx,
    name: String,
    diagnostic_fn: Option<Function>,
    symbols_fn: Option<Function>,
}

impl LuaLanguage {
    /// Loads a language definition from a Lua file.
    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, Box<dyn Error + Send + Sync>> {
        let lua = Lua::new();
        let script = std::fs::read_to_string(path)?;
        let config: Value = lua.load(script).eval()?;
        Self::from_config(lua, config)
    }

    /// Builds a language from an already-evaluated configuration table.
    pub fn from_config(lua: Lua, config: Value) -> Result<Self, Box<dyn Error + Send + Sync>> {
        let Value::Table(table) = &config else {
            return Err("config must be a table".into());
        };

        let name: String = table.get("name")?;

        let root_ctx = LuaCtx(table.get("root_ctx")?);

        let passes_table: Table = table.get("passes")?;
        let mut functions = Vec::new();
        for f in passes_table.sequence_values::<Function>() {
            functions.push(f?);
        }
        if functions.is_empty() {
            return Err("config.passes must list at least one function".into());
        }

        let mut schedule = Schedule::new();
        for (index, function) in functions.into_iter().enumerate() {
            schedule.push(LuaPass {
                lua: lua.clone(),
                function,
                index,
            });
        }
        let engine = Engine::new(schedule);

        let diagnostic_fn = match table.get::<Value>("diagnostic")? {
            Value::Nil => None,
            Value::Function(f) => Some(f),
            other => {
                return Err(format!(
                    "config.diagnostic must be a function, got {}",
                    other.type_name()
                )
                .into())
            }
        };
        let symbols_fn = match table.get::<Value>("symbols")? {
            Value::Nil => None,
            Value::Function(f) => Some(f),
            other => {
                return Err(format!(
                    "config.symbols must be a function, got {}",
                    other.type_name()
                )
                .into())
            }
        };

        Ok(Self {
            lua,
            engine,
            root_ctx,
            name,
            diagnostic_fn,
            symbols_fn,
        })
    }

    /// The language's `name` from the config.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The embedded Lua state (contexts live here).
    pub fn lua(&self) -> &Lua {
        &self.lua
    }
}

impl Language<LuaCtx> for LuaLanguage {
    fn supports_symbols(&self) -> bool {
        self.symbols_fn.is_some()
    }

    fn engine(&self) -> &Engine<LuaCtx> {
        &self.engine
    }

    fn root_ctx(&self) -> LuaCtx {
        self.root_ctx.clone()
    }

    fn diagnostic(
        &self,
        doc: &Document<LuaCtx>,
        node: FailedNode<'_, LuaCtx>,
    ) -> Option<Diagnostic> {
        let function = self.diagnostic_fn.as_ref()?;
        let span = doc.session().tree().span(node.id);
        let node_table = self.lua.create_table().ok()?;
        node_table.set("ctx", node.ctx.0.clone()).ok()?;
        node_table.set("status", status_name(node.status)).ok()?;
        node_table.set("start", span.start).ok()?;
        node_table.set("end", span.end).ok()?;

        let result: Value = function.call((doc.text().to_string(), node_table)).ok()?;
        let Value::Table(diag) = result else {
            return None;
        };
        let message: String = diag.get("message").ok()?;
        let severity = diag
            .get::<i64>("severity")
            .ok()
            .and_then(|s| match s {
                1 => Some(DiagnosticSeverity::ERROR),
                2 => Some(DiagnosticSeverity::WARNING),
                3 => Some(DiagnosticSeverity::INFORMATION),
                4 => Some(DiagnosticSeverity::HINT),
                _ => None,
            })
            .or(Some(DiagnosticSeverity::ERROR));

        Some(Diagnostic {
            message,
            severity,
            ..Diagnostic::default()
        })
    }

    fn symbols(&self, doc: &Document<LuaCtx>) -> Vec<DocumentSymbol> {
        let Some(function) = &self.symbols_fn else {
            return Vec::new();
        };
        let Ok(nodes) = self.lua.create_table() else {
            return Vec::new();
        };
        let tree = doc.session().tree();
        for id in tree.nodes() {
            let Ok(entry) = self.lua.create_table() else {
                return Vec::new();
            };
            let span = tree.span(id);
            if entry.set("start", span.start).is_err()
                || entry.set("end", span.end).is_err()
                || entry.set("status", status_name(tree.status(id))).is_err()
                || entry.set("ctx", tree.ctx(id).0.clone()).is_err()
            {
                return Vec::new();
            }
            if nodes.push(entry).is_err() {
                return Vec::new();
            }
        }

        let result: Value = match function.call(nodes) {
            Ok(v) => v,
            Err(err) => {
                eprintln!("increparse-lua: symbols error: {err}");
                return Vec::new();
            }
        };
        let Value::Table(array) = result else {
            return Vec::new();
        };

        let mut symbols = Vec::new();
        for item in array.sequence_values::<Table>() {
            let Ok(item) = item else { break };
            let Ok(name) = item.get::<String>("name") else {
                continue;
            };
            let (Ok(start), Ok(end)) = (item.get::<usize>("start"), item.get::<usize>("end"))
            else {
                continue;
            };
            let detail: Option<String> = item.get("detail").unwrap_or(None);
            let span = Span::new(start, end, doc.revision());
            let range = doc.range(span);
            symbols.push(DocumentSymbol {
                name,
                detail,
                kind: SymbolKind::FUNCTION,
                range,
                selection_range: range,
                children: None,
                tags: None,
                #[allow(deprecated)]
                deprecated: None,
            });
        }
        symbols
    }
}