mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 11:45:31 -05:00
feat(ccsexp): support #; for commenting a sexp
This commit is contained in:
parent
2ed821bbe1
commit
2b6d9126c1
2 changed files with 16 additions and 1 deletions
|
|
@ -195,6 +195,10 @@ module Decoder = struct
|
|||
let next (t:t) =
|
||||
let rec expr () = match cur t with
|
||||
| L.EOI -> raise E_end
|
||||
| L.SEXP_COMMENT ->
|
||||
junk t;
|
||||
let _u = expr() in (* discard next sexp *)
|
||||
expr()
|
||||
| L.ATOM s -> junk t; `Atom s
|
||||
| L.LIST_OPEN ->
|
||||
junk t;
|
||||
|
|
@ -206,7 +210,7 @@ module Decoder = struct
|
|||
| L.LIST_CLOSE -> error_ t.buf "expected expression"
|
||||
and lst acc = match cur t with
|
||||
| L.LIST_CLOSE -> List.rev acc
|
||||
| L.LIST_OPEN | L.ATOM _ ->
|
||||
| L.LIST_OPEN | L.ATOM _ | L.SEXP_COMMENT ->
|
||||
let sub = expr () in
|
||||
lst (sub::acc)
|
||||
| L.EOI -> error_ t.buf "unexpected EOI"
|
||||
|
|
@ -233,6 +237,15 @@ let parse_string s : t or_error =
|
|||
CCResult.to_opt (parse_string "\"\123\bcoucou\"") <> None
|
||||
*)
|
||||
|
||||
(*$= & ~printer:(function Ok x -> to_string x | Error e -> "error " ^ e)
|
||||
(parse_string "(a b)") (Ok (`List [`Atom "a"; `Atom "b"]))
|
||||
(parse_string "(a\n ;coucou\n b)") (Ok (`List [`Atom "a"; `Atom "b"]))
|
||||
(parse_string "(a #; (foo bar\n (1 2 3)) b)") (Ok (`List [`Atom "a"; `Atom "b"]))
|
||||
(parse_string "#; (a b) (c d)") (Ok (`List [`Atom "c"; `Atom "d"]))
|
||||
(parse_string "#; (a b) 1") (Ok (`Atom "1"))
|
||||
*)
|
||||
|
||||
|
||||
(*$inject
|
||||
let sexp_gen =
|
||||
let mkatom a = `Atom a and mklist l = `List l in
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
| ATOM of string
|
||||
| LIST_OPEN
|
||||
| LIST_CLOSE
|
||||
| SEXP_COMMENT
|
||||
| EOI
|
||||
|
||||
(* location + message *)
|
||||
|
|
@ -65,6 +66,7 @@ let string_item =
|
|||
let string = '"' string_item* '"'
|
||||
|
||||
rule token = parse
|
||||
| "#;" { SEXP_COMMENT }
|
||||
| comment_line { token lexbuf }
|
||||
| newline { Lexing.new_line lexbuf; token lexbuf }
|
||||
| white { token lexbuf }
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue