hey folks! I’m trying to write my first parser for a very simple file format. I’ve made it work, but my solution uses negative lookahead. Is there any way to write a grammar that produces the same output without negative lookahead? here a REPL example:
user=> (def s
#_=> "NAME=Thing 1
#_=> ACTIVE=120201-171231
#_=>
#_=> NAME=Thing 2
#_=> ACTIVE=120201-171231")
#_=>
#'user/s
user=> (def grammar
#_=> (insta/parser
#_=> "top = (block <newline?>)+
#_=> block = line+ !line
#_=> line = key <'='> value <newline?>
#_=> key = #'[A-Z]+'
#_=> value = #'[^\n]*'
#_=> newline = '\n'"))
#_=>
#'user/grammar
user=> (clojure.pprint/pprint (insta/parse grammar s))
[:top
[:block
[:line [:key "NAME"] [:value "Thing 1"]]
[:line [:key "ACTIVE"] [:value "120201-171231"]]]
[:block
[:line [:key "NAME"] [:value "Thing 2"]]
[:line [:key "ACTIVE"] [:value "120201-171231"]]]]
nil
if there’s any other way to simplify it I’d love to hear about it 🙂
I don't have a repl to hand, but couldn't you use a newline as a character separator rather than a negative look ahead for another line?