more work on the parser

This commit is contained in:
Moritz Gmeiner 2024-08-12 16:31:28 +02:00
commit 3590a78154
6 changed files with 287 additions and 12 deletions

View file

@ -56,7 +56,7 @@ type state = {
}
module State = struct
type t = state
(* type t = state *)
let is_digit c = match c with '0' .. '9' -> true | _ -> false
let is_alpha c = match c with 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false
@ -95,10 +95,6 @@ module State = struct
| Some c when f c -> advance_while f (snd (advance state))
| _ -> state (* EOF or no match *)
let last_char (state : state) : char =
assert (state.cur_pos > 0);
state.source.[state.cur_pos - 1]
let append_token (pos : code_pos) (token_type : token_type) (state : state) : state =
(* let pos = { line = state.line; col = state.col } in *)
{ state with tokens_rev = { token_type; pos } :: state.tokens_rev }
@ -208,5 +204,6 @@ let tokenize (source : string) : lexer_result =
{ source; start_pos = 0; cur_pos = 0; tokens_rev = []; errors_rev = []; line = 1; col = 0 }
in
(* reverse the reversed tokens/errors *)
if List.length state.errors_rev = 0 then Ok (List.rev state.tokens_rev)
else Error (List.rev state.errors_rev)
(* if List.length state.errors_rev = 0 then Ok (List.rev state.tokens_rev)
else Error (List.rev state.errors_rev) *)
match state.errors_rev with [] -> Ok (List.rev state.tokens_rev) | es -> Error (List.rev es)