feat(ccsexp): support #; for commenting a sexp

This commit is contained in:
Simon Cruanes 2019-11-05 19:02:16 -06:00
parent 2ed821bbe1
commit 2b6d9126c1
2 changed files with 16 additions and 1 deletions

View file

@ -195,6 +195,10 @@ module Decoder = struct
let next (t:t) = let next (t:t) =
let rec expr () = match cur t with let rec expr () = match cur t with
| L.EOI -> raise E_end | 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.ATOM s -> junk t; `Atom s
| L.LIST_OPEN -> | L.LIST_OPEN ->
junk t; junk t;
@ -206,7 +210,7 @@ module Decoder = struct
| L.LIST_CLOSE -> error_ t.buf "expected expression" | L.LIST_CLOSE -> error_ t.buf "expected expression"
and lst acc = match cur t with and lst acc = match cur t with
| L.LIST_CLOSE -> List.rev acc | L.LIST_CLOSE -> List.rev acc
| L.LIST_OPEN | L.ATOM _ -> | L.LIST_OPEN | L.ATOM _ | L.SEXP_COMMENT ->
let sub = expr () in let sub = expr () in
lst (sub::acc) lst (sub::acc)
| L.EOI -> error_ t.buf "unexpected EOI" | 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 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 (*$inject
let sexp_gen = let sexp_gen =
let mkatom a = `Atom a and mklist l = `List l in let mkatom a = `Atom a and mklist l = `List l in

View file

@ -4,6 +4,7 @@
| ATOM of string | ATOM of string
| LIST_OPEN | LIST_OPEN
| LIST_CLOSE | LIST_CLOSE
| SEXP_COMMENT
| EOI | EOI
(* location + message *) (* location + message *)
@ -65,6 +66,7 @@ let string_item =
let string = '"' string_item* '"' let string = '"' string_item* '"'
rule token = parse rule token = parse
| "#;" { SEXP_COMMENT }
| comment_line { token lexbuf } | comment_line { token lexbuf }
| newline { Lexing.new_line lexbuf; token lexbuf } | newline { Lexing.new_line lexbuf; token lexbuf }
| white { token lexbuf } | white { token lexbuf }