add custom options to the parser.

47 views
Skip to first unread message

Jairo de morais

unread,
Jan 16, 2013, 4:27:19 PM1/16/13
to pe...@googlegroups.com
Hi guys, 

I have a problem with pegjs and I don't realize how to solve it.

I would like that my parser receives a boolean params (configuration property) to know if it should ignore the "ends of lines". 

Depending of this param it should parse the input using one pegjs rule or another.

For example, suppose that I have my param, ignoreEOL, so

if ignoreEOL I want that my buffer rule be this 

buffer "buffer"
  = b:(!tag !comment c:. {return c})+
  { return ["buffer", b.join('')] }

but if the ignoreEOL is false the buffer rule should be this:

buffer "buffer" 
  =  b:(!tag !comment !eol c:. {return c})+
  { return ["buffer", b.join('')] } 

could you help me to fix this problem.

thanks,Jairo

David Majda

unread,
Mar 12, 2013, 3:21:29 PM3/12/13
to jairod...@gmail.com, pegjs
Hi,

2013/1/16 Jairo de morais <jairod...@gmail.com>:
> I would like that my parser receives a boolean params (configuration
> property) to know if it should ignore the "ends of lines".
>
> Depending of this param it should parse the input using one pegjs rule or
> another.
>
> For example, suppose that I have my param, ignoreEOL, so
>
> if ignoreEOL I want that my buffer rule be this
>
> buffer "buffer"
> = b:(!tag !comment c:. {return c})+
> { return ["buffer", b.join('')] }
>
> but if the ignoreEOL is false the buffer rule should be this:
>
> buffer "buffer"
> = b:(!tag !comment !eol c:. {return c})+
> { return ["buffer", b.join('')] }

This is not currently possible to do directly. You can however use
semantic predicates together with the "options" feature (available
only in master ATM) to do something like this:

buffer "buffer"
= &{ return options.ignoreEOL; } b:(...) // parsed when ignoreEOL === true
/ !{ return options.ignoreEOL; } b:(...) // parsed when ignoreEOL === false

The "options" variable is whatever is passed as the second argument to
your parser's "parse" method.

--
David Majda
Entropy fighter
http://majda.cz/
Reply all
Reply to author
Forward
0 new messages