aboutsummaryrefslogtreecommitdiffstats
path: root/lib/front.ml
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/front.ml29
-rw-r--r--lib/front.mli15
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. *)