javascriptparsinggrammarpegpegjs

Peg.JS: Simple if..then..else implementation


I am trying to implement a grammar for a simple if..then..else statement along with simple statements.

It should be able to parse a statement like:

if things are going fine
then
    things are supposed to be this way
    just go with it
else
    nothing new
How are you?

The document starts with a decision (if.. then.. else) and is followed by a simple statement.

My grammar so far looks like this:

document = decision / simple_statement / !.

decision = i:if t:then e:(else)? document { return { if: { cond: i }, then: t, else: e } }

if = 'if' s:statement nl { return s }
then = 'then' nl actions:indented_statements+ { return actions }
else = 'else' nl actions:indented_statements+ { return actions }
indented_statements = ss:(tab statement nl)+ { return ss.reduce(function(st, el) { return st.concat(el) }, []) }
statement = text:$(char+) { return text.trim() }

simple_statement = s:statement nl document { return { action: s } }

char = [^\n\r]
ws = [ \t]*
tab = [\t]+ { return '' }
nl = [\r\n]+

This returns an output:

{
   "if": {
      "cond": "things are going fine"
   },
   "then": [
      [
         "",
         "things are supposed to be this way",
         [
            "
"
         ],
         "",
         "just go with it",
         [
            "
"
         ]
      ]
   ],
   "else": [
      [
         "",
         "nothing new",
         [
            "
"
         ]
      ]
   ]
}

1. Why are there extra empty strings and arrays in the then and else array? What should I do to remove them?

  1. Why doesn't my grammar read the simple statement(s) after the decision? What should I do to make it read and parse the entire document?

EDIT: I think I figured out why I was getting the arrays. I changed the grammar to remove repetitions inside indented_statements.

document = decision / simple_statement / !.

decision = i:if t:then e:(else)? document { return { if: i, then: t, else: e } }

if = 'if' s:statement nl { return s }
then = 'then' nl actions:indented_statements+ { return actions }
else = 'else' nl actions:indented_statements+ { return actions }
indented_statements = tab s:statement nl { return s }
statement = text:$(char+) { return text.trim() }

simple_statement = s:statement nl document { return { action: s } }

char = [^\n\r]
ws = [ \t]*
tab = [\t]+ { return '' }
nl = [\r\n]+


Solution

  • I figured out the answer. I needed to provide the first statement as repeating:

    document = (decision / simple_statement)*