From 0a3fed2902e0222dc6450af0ddf1c2a60beff1c0 Mon Sep 17 00:00:00 2001 From: craff Date: Wed, 5 Apr 2023 00:35:29 +0200 Subject: [PATCH] Replace earley by pacomb in examples. --- bindlib.opam | 2 +- dune-project | 2 +- examples/metavar/dune | 4 ++-- examples/metavar/parser.ml | 44 ++++++++++++++++++++------------------ 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/bindlib.opam b/bindlib.opam index 106753d..9375d10 100644 --- a/bindlib.opam +++ b/bindlib.opam @@ -20,7 +20,7 @@ depends: [ "ocaml" {>= "4.07.0"} "dune" {>= "2.7" & build} "timed" {>= "1.0" & with-test} - "earley" {= "3.0.0" & with-test} + "pacomb" {>= "1.1" & with-test} "odoc" {with-doc} ] build: [ diff --git a/dune-project b/dune-project index d3930f3..9e6d1ea 100644 --- a/dune-project +++ b/dune-project @@ -23,5 +23,5 @@ (ocaml (>= 4.07.0)) (dune :build) (timed (and (>= 1.0) :with-test)) - (earley (and (= 3.0.0) :with-test)) + (pacomb (and (>= 1.1) :with-test)) (odoc :with-doc))) diff --git a/examples/metavar/dune b/examples/metavar/dune index e99cc57..427be87 100644 --- a/examples/metavar/dune +++ b/examples/metavar/dune @@ -1,5 +1,5 @@ (test (name main) - (preprocess (per_module ((action (run pa_ocaml %{input-file})) parser))) - (libraries bindlib earley.core timed) + (preprocess (per_module ((pps pacomb.ppx) parser))) + (libraries bindlib unix pacomb timed) (action (with-stdin-from test.txt (run %{test})))) diff --git a/examples/metavar/parser.ml b/examples/metavar/parser.ml index 01d1368..836e9d9 100644 --- a/examples/metavar/parser.ml +++ b/examples/metavar/parser.ml @@ -1,30 +1,32 @@ -open Earley_core +open Pacomb open Ast -let parser ident = id:{#[a-z0-9][_a-zA-Z0-9]*[']*#}[group.(0)] +let%parser ident = (id::RE"[a-z0-9][_a-zA-Z0-9]*[']*") => id -let parser term_atom = - | id:ident -> PVar id - | '(' t:term_full ')' -> t - | '[' t:term_full ']' -> PNrm t +let%parser rec term_atom = + (id::ident) => PVar id + ; '(' (t::term_full) ')' => t + ; '[' (t::term_full) ']' => PNrm t -and parser term_appl = - | term_atom - | t:term_appl u:term_atom -> PApp(t,u) +and term_appl = + (t::term_atom) => t + ; (t::term_appl) (u::term_atom) => PApp(t,u) -and parser term_full = - | term_appl - | {"λ"|"%"} ids:ident* '.' t:term_full -> many_PLam ids t +and term_full = + (t::term_appl) => t + ; ("λ" => () ; "%" => ()) (ids:: ~* ident) '.' (t::term_full) + => many_PLam ids t -let parser command = - | "$u" -> Undo - | "$g" -> Goal - | "$s" x:ident "=" t:term_full -> Decl(x,t) - | "$p" t:term_full -> Prnt(t) +let%parser command = + "$u" => Undo + ; "$g" => Goal + ; "$s" (x::ident) "=" (t::term_full) => Decl(x,t) + ; "$p" (t::term_full) => Prnt(t) -let parser commands = {command ";"}* +let%parser commands = cs :: ~* ((c::command) ";" => c) => cs + +let blank = Blank.line_comments "//" let parse_channel : in_channel -> cmd list = fun ic -> - let blank = Blanks.line_comments "//" in - let parse = Earley.parse_buffer commands blank in - Earley.handle_exception parse (Input.from_channel ic) + let parse = Grammar.parse_channel commands blank in + Pos.handle_exception parse ic