Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse this with peg grammar?

Tags:

parsing

peg

pegjs

I'm trying to make a parser using pegjs. I need to parse something like:

blah blah START Lorem ipsum 
dolor sit amet, consectetur 
adipiscing elit END foo bar 
etc.

I have trouble writing the rule to catch the text from "START" to "END".

like image 661
John Smith Avatar asked Sep 01 '12 19:09

John Smith


1 Answers

Use negative lookahead predicates:

phrase
  =(!"START" .)* "START" result:(!"END" .)* "END" .* {
    for (var i=0;i<result.length;++i)
      // remove empty element added by predicate matching
      {result[i]=result[i][1];
      }
    return result.join("");
   }

You need to use a negative predicate for END as well as START because repetition in pegjs is greedy.

Alternatively, the action could be written as

{return result.join("").split(',').join("");}

Although this relies on not-necessarily documented behavior of join when dealing with nested arrays (namely that it joins the sub-arrays with commas and then concatenates them).

[UPDATE] A shorter way to deal with the empty elements is

phrase
  =(!"START" .)* "START" result:(t:(!"END" .){return t[1];})* "END" .* {
      return result.join("");
    }
like image 113
ebohlman Avatar answered Nov 16 '22 02:11

ebohlman