From 2b6d9126c1b92ad24bedc80ac28a1d3fe189fc6a Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 5 Nov 2019 19:02:16 -0600 Subject: [PATCH] feat(ccsexp): support `#;` for commenting a sexp --- src/sexp/CCSexp.ml | 15 ++++++++++++++- src/sexp/CCSexp_lex.mll | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/sexp/CCSexp.ml b/src/sexp/CCSexp.ml index 7c4a8471..c5486528 100644 --- a/src/sexp/CCSexp.ml +++ b/src/sexp/CCSexp.ml @@ -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 diff --git a/src/sexp/CCSexp_lex.mll b/src/sexp/CCSexp_lex.mll index 3db73cd6..f8216a4b 100644 --- a/src/sexp/CCSexp_lex.mll +++ b/src/sexp/CCSexp_lex.mll @@ -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 }