diff options
| author | Marcos Magueta <maguetamarcos@gmail.com> | 2026-07-12 18:27:44 -0300 |
|---|---|---|
| committer | Marcos Magueta <maguetamarcos@gmail.com> | 2026-07-12 18:27:44 -0300 |
| commit | 27e9ddee53b8fa43725f951434404691d8012190 (patch) | |
| tree | ea78559da973d4628cf135096488d55c7b7cd35e /lib/front.ml | |
| parent | 257f5288a486740187e44b6ef4cadaad7166930e (diff) | |
| download | SOL-27e9ddee53b8fa43725f951434404691d8012190.tar.gz SOL-27e9ddee53b8fa43725f951434404691d8012190.zip | |
language server over stdio, emacs mode with repl and eglot wiring
Diffstat (limited to '')
| -rw-r--r-- | lib/front.ml | 29 | ||||
| -rw-r--r-- | lib/front.mli | 15 |
2 files changed, 34 insertions, 10 deletions
diff --git a/lib/front.ml b/lib/front.ml index 7cacc20..b728825 100644 --- a/lib/front.ml +++ b/lib/front.ml @@ -1,14 +1,27 @@ -let parse_string source = +type error = { line : int; column : int; message : string } + +let position lexbuf = + let p = lexbuf.Lexing.lex_curr_p in + (p.Lexing.pos_lnum, p.Lexing.pos_cnum - p.Lexing.pos_bol) + +let parse source = let lexbuf = Lexing.from_string source in match Parser.program Lexer.token lexbuf with | statements -> Ok statements | exception Lexer.Error message -> - let position = lexbuf.Lexing.lex_curr_p in - Error - (Printf.sprintf "lexical error at line %d: %s" - position.Lexing.pos_lnum message) + let line, column = position lexbuf in + Error { line; column; message } | exception Parser.Error -> - let position = lexbuf.Lexing.lex_curr_p in + let line, column = position lexbuf in Error - (Printf.sprintf "syntax error at line %d, before %S" - position.Lexing.pos_lnum (Lexing.lexeme lexbuf)) + { + line; + column; + message = Printf.sprintf "syntax error before %S" (Lexing.lexeme lexbuf); + } + +let parse_string source = + match parse source with + | Ok statements -> Ok statements + | Error { line; message; _ } -> + Error (Printf.sprintf "line %d: %s" line message) diff --git a/lib/front.mli b/lib/front.mli index 00fa65f..a4e91e6 100644 --- a/lib/front.mli +++ b/lib/front.mli @@ -1,7 +1,18 @@ (** Front door: text to statements. Wraps the generated lexer and parser so callers see one total - function from source text to syntax or a positioned message. - The REPL, the file runner, and the tests all enter here. *) + function from source text to syntax or a positioned error. The + REPL, the file runner, the LSP server, and the tests all enter + here. *) + +type error = { + line : int; (** 1-based, as editors count *) + column : int; (** 0-based character offset in the line *) + message : string; +} + +val parse : string -> (Ast.statement list, error) result val parse_string : string -> (Ast.statement list, string) result +(** [parse] with the position folded into the message, for callers + that only print. *) |
