Markus Mottl avatar Markus Mottl committed 215e83d

Added syntax for sexp comments

Comments (0)

Files changed (3)

base/sexplib/lib/lexer.mll

   | space+ | ';' [^ '\n' '\r']* { main buf lexbuf }
   | '(' { LPAREN }
   | ')' { RPAREN }
+  | "#;" { SEXP_COMMENT }
   | '"'
       {
         scan_string buf lexbuf;

base/sexplib/lib/parser.mly

 %}
 
 %token <string> STRING
-%token LPAREN RPAREN EOF
+%token LPAREN RPAREN SEXP_COMMENT EOF
 
 %start sexp
 %type <Type.t> sexp
   | LPAREN rev_sexps_aux RPAREN { Type.List (List.rev $2) }
   | error { parse_failure "sexp" }
 
+sexp_comment : SEXP_COMMENT sexp { () }
+
+sexp_comments
+  : sexp_comment { () }
+  | sexp_comments sexp_comment { () }
+
 sexp_opt
   : sexp { Some $1 }
+  | sexp_comments sexp { Some $2 }
   | EOF { None }
 
 rev_sexps_aux
   : sexp { [$1] }
+  | sexp_comment { [] }
   | rev_sexps_aux sexp { $2 :: $1 }
+  | rev_sexps_aux sexp_comment { $1 }
 
 rev_sexps
   : rev_sexps_aux { $1 }

base/sexplib/lib/pre_sexp.ml

     if pos_len > str_len then invalid_arg (loc ^ ": pos + len > str_len"); \
     pos_len - 1 \
   \
-  let mk_cont name cont state = \
-    let ws_only = GET_PSTACK = [] && Buffer.length state.pbuf = 0 in \
+  let mk_cont_ws name cont state ~ws_only = \
     let parse_fun = \
       let used_ref = ref false in \
       fun ~pos ~len str -> \
         end \
     in \
     Cont (ws_only, parse_fun) \
+  let mk_cont name cont state = \
+    mk_cont_ws name cont state \
+      ~ws_only:(GET_PSTACK = [] && Buffer.length state.pbuf = 0) \
   \
   let rec PARSE state str ~max_pos ~pos = \
     if pos > max_pos then mk_cont "parse" PARSE state \
           bump_pos_cont state str ~max_pos ~pos parse_quoted \
       | c -> \
           REGISTER_POS \
-          add_bump_pos state str ~max_pos ~pos c parse_atom \
+          let parse = \
+            if c = '#' then maybe_parse_sexp_comment else parse_atom \
+          in \
+          add_bump_pos state str ~max_pos ~pos c parse \
   \
   and parse_nl state str ~max_pos ~pos = \
     if pos > max_pos then mk_cont "parse_nl" parse_nl state \
       | '\013' -> bump_line_cont state str ~max_pos ~pos parse_nl \
       | _ -> bump_pos_cont state str ~max_pos ~pos parse_comment \
   \
+  and maybe_parse_sexp_comment state str ~max_pos ~pos = \
+    if pos > max_pos then \
+      mk_cont "maybe_parse_sexp_comment" maybe_parse_sexp_comment state \
+    else \
+      match GET_CHAR with \
+      | ';' -> bump_pos_cont state str ~max_pos ~pos parse_sexp_comment \
+      | _ -> parse_atom state str ~max_pos ~pos \
+  \
+  and parse_sexp_comment state str ~max_pos ~pos = \
+    let pbuf_str = "" in \
+    ignore (MK_ATOM); \
+    let old_pstack = GET_PSTACK in \
+    let pstack = [] in \
+    SET_PSTACK; \
+    let rec loop parse state str ~max_pos ~pos = \
+      Buffer.clear state.pbuf; \
+      match parse state str ~max_pos ~pos with \
+      | Done (_sexp, parse_pos) -> \
+          Buffer.clear state.pbuf; \
+          let pstack = old_pstack in \
+          SET_PSTACK; \
+          PARSE state str ~max_pos ~pos:parse_pos.Parse_pos.buf_pos \
+      | Cont (_, cont_parse) -> \
+          let parse _state str ~max_pos ~pos = \
+            let len = max_pos - pos + 1 in \
+            cont_parse ~pos ~len str \
+          in \
+          mk_cont_ws "parse_sexp_comment" (loop parse) state ~ws_only:false \
+    in \
+    loop PARSE state str ~max_pos ~pos \
   and parse_atom state str ~max_pos ~pos = \
     if pos > max_pos then mk_cont "parse_atom" parse_atom state \
     else \
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.