ocaml: pattern parsing

This commit is contained in:
Alain Zscheile 2024-02-11 14:41:04 +01:00
parent 65c82283fc
commit a084d9ceb8
2 changed files with 24 additions and 1 deletions

View file

@ -63,6 +63,15 @@ let next_in_noeof ps (ctx : error_ctx) =
let sp = { start = ps.offset; length = zero_len } in
Yanais_syntax_err (EUnexpectedEof ctx, ps.file, sp) |> raise
(* make an optional parser required *)
let require ctx parse_inner ps = match parse_inner ps with
| Some x -> x
| None ->
let sp = match ps.lt with
| (_, sp)::_ -> sp
| [] -> { start = ps.offset; length = zero_len }
in Yanais_syntax_err (EExpected ctx, ps.file, sp) |> raise
module FieldsReg = Set.Make(String)
let parse_record (type a) parse_inner ps = when_got ps (Brace Open) (fun (_, ps) -> (
@ -107,6 +116,16 @@ let parse_record (type a) parse_inner ps = when_got ps (Brace Open) (fun (_, ps)
inner [] FieldsReg.empty ps
))
let rec parse_pattern ps = match ps.lt with
| (PatOut "", s)::lt ->
Some (PatIgnore (ps.file, s), { ps with lt; offset = loc_span_end s; })
| (PatOut name, s)::lt ->
Some (PatName {ident = name; file = ps.file; span = s; }, { ps with lt; offset = loc_span_end s; })
| (Brace Open, _)::_ ->
let rqp_pattern = require XPattern parse_pattern in
Some (require XRecord (parse_record rqp_pattern) ps |> (fun (x, ps) -> (PatRecord x, ps)))
| _ -> None
(*
let parse_expr ps =
match parse_one ps with

View file

@ -10,7 +10,11 @@ type loc_span_full = string * loc_span
let loc_span_end ls = ls.start + Unsigned.UInt32.to_int ls.length
type tagged_ident = TaggedIdent of loc_span_full * string
type tagged_ident =
{ ident: string
; file : string
; span : loc_span
}
type grp_state = Open | Close