mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
more work on the parser
This commit is contained in:
parent
686fb195af
commit
3590a78154
6 changed files with 287 additions and 12 deletions
24
lib/error.ml
24
lib/error.ml
|
|
@ -4,15 +4,24 @@ type lexer_error = { pos : code_pos; msg : string }
|
|||
module LexerError = struct
|
||||
type t = lexer_error
|
||||
|
||||
let make (pos : code_pos) (msg : string) : lexer_error =
|
||||
(* let pos = { line; col } in *)
|
||||
{ pos; msg }
|
||||
let make (pos : code_pos) (msg : string) : lexer_error = { pos; msg }
|
||||
|
||||
let print (e : lexer_error) =
|
||||
Printf.printf "LexerError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
|
||||
end
|
||||
|
||||
type lox_error = LexerError of lexer_error list
|
||||
type parser_error = { pos : code_pos; msg : string }
|
||||
|
||||
module ParserError = struct
|
||||
type t = parser_error
|
||||
|
||||
let make (pos : code_pos) (msg : string) : parser_error = { pos; msg }
|
||||
|
||||
let print (e : parser_error) =
|
||||
Printf.printf "ParserError at line %d, column %d: %s\n" e.pos.line e.pos.col e.msg
|
||||
end
|
||||
|
||||
type lox_error = LexerError of lexer_error list | ParserError of parser_error list
|
||||
|
||||
let print_error (e : lox_error) =
|
||||
match e with
|
||||
|
|
@ -22,5 +31,12 @@ let print_error (e : lox_error) =
|
|||
Printf.printf "found %d %s:\n" num_errors
|
||||
(if num_errors = 1 then "LexerError" else "LexerErrors");
|
||||
List.iter LexerError.print es
|
||||
| ParserError es ->
|
||||
let num_errors = List.length es in
|
||||
assert (num_errors != 0);
|
||||
Printf.printf "found %d %s:\n" num_errors
|
||||
(if num_errors = 1 then "ParserError" else "ParserErrors");
|
||||
List.iter ParserError.print es
|
||||
|
||||
let of_lexer_error e = Result.map_error (fun e -> LexerError e) e
|
||||
let of_parser_error e = Result.map_error (fun e -> ParserError e) e
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue