2

I've been working on a PEGjs grammar. Most of it works, but I'm having trouble with one last thing. My parser takes input that looks like this: First:[content]; and returns the type (First or Second) and each of the elements inside content ("c", "o"...). I also need it to be able to take ] as input so it should be able to parse the following: First:[co]nt];.

Does anyone have any ideas of how I can go about this? This is what I have so far:

prod = unit+
unit = u:(form / size)
form = key:(kind)  ":" value:(clas)  ";"  {return {type:key, value:value} }
clas = "[" v:(charact+) "]" { return {type:"class",value:v} }
kind = "First" / "Second"
charact = letters / symbols
symbols = "(" / ")" / "!" / "?"  /  "%"  / "{" /  "}" / "[" // "]"
letters = [A-Za-z]`
Sara M
  • 23
  • 2

1 Answers1

1

One way to solve this would be to look for a ] not followed by a semicolon, ie. "]" (!";"). In the example below, I also use $() to combine the symbols into a single string.

prod = unit+
unit = u:(form / size)
form = key:(kind)  ":" value:(clas)  ";"  {return {type:key, value:value} }
clas = "[" v:(charact+) "]" { return {type:"class",value:v} }
kind = "First" / "Second"
charact = letters / symbols
symbols = $("(" / ")" / "!" / "?"  /  "%"  / "{" /  "}" / "[" / "]" (!";"))
letters = [A-Za-z]
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43