Fork me on GitHub
#instaparse
<
2018-10-25
>
schmee21:10:56

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:

schmee21:10:00

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

schmee21:10:19

if there’s any other way to simplify it I’d love to hear about it 🙂

socksy12:11:14

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?