Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions bootstrap/bin/hocc/Parse.hmh
Original file line number Diff line number Diff line change
Expand Up @@ -756,12 +756,8 @@ let hmhi scanner =
| [] -> errs
| _ -> List.fold ~init:errs ~f:(fun errs mal -> Error.init_mal mal :: errs) mals
in
let {status; _} as parser = next token parser in
match status, errs with
| Prefix, _ -> inner scanner errs parser
| Accept (Hmhi hmhi), [] -> scanner, Ok hmhi
| Accept (Hmhi _), _ -> scanner, Error errs
| Reject _, _ ->
match next token parser with
| Error _ -> begin
let msg =
String.Fmt.empty
|> Fmt.fmt "Unexpected token not in "
Expand All @@ -770,7 +766,14 @@ let hmhi scanner =
in
let errs = Error.init_token scan_token msg :: errs in
scanner, Error errs
| _ -> not_reached ()
end
| Ok ({status; _} as parser) -> begin
match status, errs with
| Prefix, _ -> inner scanner errs parser
| Accept (Hmhi hmhi), [] -> scanner, Ok hmhi
| Accept (Hmhi _), _ -> scanner, Error errs
| _ -> not_reached ()
end
end in
let parser = Start.Hmhi.boi in
inner scanner [] parser
Expand All @@ -782,12 +785,8 @@ let hmh scanner =
| [] -> errs
| _ -> List.fold ~init:errs ~f:(fun errs mal -> Error.init_mal mal :: errs) mals
in
let {status; _} as parser = next token parser in
match status, errs with
| Prefix, _ -> inner scanner errs parser
| Accept (Hmh hmh), [] -> scanner, Ok hmh
| Accept (Hmh _), _ -> scanner, Error errs
| Reject _, _ -> begin
match next token parser with
| Error _ -> begin
let msg =
String.Fmt.empty
|> Fmt.fmt "Unexpected token not in "
Expand All @@ -797,7 +796,13 @@ let hmh scanner =
let errs = Error.init_token scan_token msg :: errs in
scanner, Error errs
end
| _ -> not_reached ()
| Ok ({status; _} as parser) -> begin
match status, errs with
| Prefix, _ -> inner scanner errs parser
| Accept (Hmh hmh), [] -> scanner, Ok hmh
| Accept (Hmh _), _ -> scanner, Error errs
| _ -> not_reached ()
end
end in
let parser = Start.Hmh.boi in
inner scanner [] parser
Expand Down
95 changes: 48 additions & 47 deletions bootstrap/bin/hocc/Parse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15116,15 +15116,13 @@ Hmhi {prelude; hocc_; postlude; eoi}
| Reduce of Token.t * Stack.Reduction.t
| Prefix
| Accept of Nonterm.t
| Reject of Token.t

let constructor_index = function
| ShiftPrefix _ -> 0L
| ShiftAccept _ -> 1L
| Reduce _ -> 2L
| Prefix -> 3L
| Accept _ -> 4L
| Reject _ -> 5L

let hash_fold t state =
state
Expand All @@ -15138,7 +15136,6 @@ Hmhi {prelude; hocc_; postlude; eoi}
hash_state |> Stack.Reduction.hash_fold reduction |> Token.hash_fold token
| Prefix -> hash_state
| Accept nonterm -> hash_state |> Nonterm.hash_fold nonterm
| Reject token -> hash_state |> Token.hash_fold token
)

let cmp t0 t1 =
Expand All @@ -15163,7 +15160,6 @@ Hmhi {prelude; hocc_; postlude; eoi}
end
| Prefix, Prefix -> Eq
| Accept nonterm0, Accept nonterm1 -> Nonterm.cmp nonterm0 nonterm1
| Reject token0, Reject token1 -> Token.cmp token0 token1
| _, _ -> not_reached ()
end
| Gt -> Gt
Expand Down Expand Up @@ -15192,7 +15188,6 @@ Hmhi {prelude; hocc_; postlude; eoi}
end
| Prefix -> formatter |> Fmt.fmt "Prefix"
| Accept nonterm -> formatter |> Fmt.fmt "Accept " |> Nonterm.pp nonterm
| Reject token -> formatter |> Fmt.fmt "Reject " |> Token.pp token
)
end
include T
Expand Down Expand Up @@ -15225,29 +15220,29 @@ Hmhi {prelude; hocc_; postlude; eoi}
end
end

let feed token = function
let feed token t = match t with
| {stack={state; _} :: _; status=Prefix} as t -> begin
let token_index = Token.index token in
let Spec.State.{actions; _} = Array.get state Spec.states in
let status = match Map.get token_index actions with
| Some (Spec.Action.ShiftPrefix state') -> Status.ShiftPrefix (token, state')
| Some (Spec.Action.ShiftAccept state') -> Status.ShiftAccept (token, state')
match Map.get token_index actions with
| Some (Spec.Action.ShiftPrefix state') ->
Ok {t with status=Status.ShiftPrefix (token, state')}
| Some (Spec.Action.ShiftAccept state') ->
Ok {t with status=Status.ShiftAccept (token, state')}
| Some (Spec.Action.Reduce prod_index) -> begin
let Spec.Prod.{callback=callback_index; _} = Array.get prod_index Spec.prods in
let reduction = Stack.Reduction.init callback_index in
Status.Reduce (token, reduction)
Ok {t with status=Status.Reduce (token, reduction)}
end
| None -> Status.Reject token
in
{t with status}
| None -> Error (token, t)
end
| _ -> not_reached ()

let step {stack; status} =
let open Status in
match status with
| ShiftPrefix (token, state) ->
{stack=Stack.shift ~symbol:(Token token) ~state stack; status=Prefix}
Ok {stack=Stack.shift ~symbol:(Token token) ~state stack; status=Prefix}
| ShiftAccept (token, state) -> begin
(* Shift, perform the ⊥ reduction, and extract the accepted symbol from the stack. *)
let stack = Stack.shift ~symbol:(Token token) ~state stack in
Expand All @@ -15261,47 +15256,48 @@ Hmhi {prelude; hocc_; postlude; eoi}
match stack with
| [] -> not_reached ()
| {symbol=Token _; _} :: _ -> not_reached ()
| {symbol=Nonterm nonterm; _} :: _ -> {stack=[]; status=Accept nonterm}
| {symbol=Nonterm nonterm; _} :: _ -> Ok {stack=[]; status=Accept nonterm}
end
| _ -> not_reached ()
end
| Reduce (token, reduction) -> begin
feed token {stack=Stack.reduce ~reduction stack; status=Prefix}
end
| _ -> not_reached ()
| _ -> halt "`step` only supports {`ShiftPrefix`, `ShiftAccept`, `Reduce`} status"

(* val walk: t -> t *)
(* val walk: t -> (t, Token.t * t) result *)
let rec walk ({status; _} as t) =
let open Status in
match status with
| ShiftPrefix _
| ShiftAccept _
| Reduce _ -> t |> step |> walk
| Reduce _ -> begin
match step t with
| (Error _) as error -> error
| Ok t' -> walk t'
end
| Prefix
| Accept _
| Reject _ -> t
| Accept _ -> Ok t

let next token ({status; _} as t) =
let open Status in
match status with
| Prefix -> begin
match t |> feed token |> walk with
| {status=Reject _ as status; _} -> {t with status}
| t' -> t'
match feed token t with
| (Error _) as error -> error
| Ok t' -> begin
match walk t' with
| Error _ -> Error (token, t)
| (Ok _) as ok -> ok
end
end
| _ -> halt "`token` only supports `Prefix` status"
| _ -> halt "`next` only supports `Prefix` status"

let expect ({status; _} as t) =
let open Status in
let t = match status with
| Prefix -> t
| Reject _ -> {t with status=Prefix}
| _ -> halt "`expect` only supports `Prefix`/`Reject` status"
in
let expect t =
Array.fold ~init:(Ordset.empty (module Token)) ~f:(fun expect token ->
match next token t with
| {status=Reject _; _} -> expect
| _ -> Ordset.insert token expect
| Error _ -> expect
| Ok _ -> Ordset.insert token expect
) Token.protos
end
#678 "./Parse.hmh"
Expand Down Expand Up @@ -15386,12 +15382,8 @@ let hmhi scanner =
| [] -> errs
| _ -> List.fold ~init:errs ~f:(fun errs mal -> Error.init_mal mal :: errs) mals
in
let {status; _} as parser = next token parser in
match status, errs with
| Prefix, _ -> inner scanner errs parser
| Accept (Hmhi hmhi), [] -> scanner, Ok hmhi
| Accept (Hmhi _), _ -> scanner, Error errs
| Reject _, _ ->
match next token parser with
| Error _ -> begin
let msg =
String.Fmt.empty
|> Fmt.fmt "Unexpected token not in "
Expand All @@ -15400,7 +15392,14 @@ let hmhi scanner =
in
let errs = Error.init_token scan_token msg :: errs in
scanner, Error errs
| _ -> not_reached ()
end
| Ok ({status; _} as parser) -> begin
match status, errs with
| Prefix, _ -> inner scanner errs parser
| Accept (Hmhi hmhi), [] -> scanner, Ok hmhi
| Accept (Hmhi _), _ -> scanner, Error errs
| _ -> not_reached ()
end
end in
let parser = Start.Hmhi.boi in
inner scanner [] parser
Expand All @@ -15412,12 +15411,8 @@ let hmh scanner =
| [] -> errs
| _ -> List.fold ~init:errs ~f:(fun errs mal -> Error.init_mal mal :: errs) mals
in
let {status; _} as parser = next token parser in
match status, errs with
| Prefix, _ -> inner scanner errs parser
| Accept (Hmh hmh), [] -> scanner, Ok hmh
| Accept (Hmh _), _ -> scanner, Error errs
| Reject _, _ -> begin
match next token parser with
| Error _ -> begin
let msg =
String.Fmt.empty
|> Fmt.fmt "Unexpected token not in "
Expand All @@ -15427,7 +15422,13 @@ let hmh scanner =
let errs = Error.init_token scan_token msg :: errs in
scanner, Error errs
end
| _ -> not_reached ()
| Ok ({status; _} as parser) -> begin
match status, errs with
| Prefix, _ -> inner scanner errs parser
| Accept (Hmh hmh), [] -> scanner, Ok hmh
| Accept (Hmh _), _ -> scanner, Error errs
| _ -> not_reached ()
end
end in
let parser = Start.Hmh.boi in
inner scanner [] parser
Expand Down
Loading
Loading