PEGjs use case

167 views
Skip to first unread message

José Luis Millán

unread,
Dec 3, 2012, 1:29:05 PM12/3/12
to pe...@googlegroups.com
Hi David, and rest of the list

I would like to make you know that I'm using PEGjs to generate a SIP parser which is part of a JavaSript SIP library I'm writting.

This is the project's website: http://jssip.net/


For those who don' t know, SIP is the most used protocol for Voice over IP for establishing media sessions, although it has many more uses. SIP message syntax is very close to HTTP message syntax.

What the parser does is filling an initial empty JavaScript object as the input passes through the difrerent rules. The parser returns such data object instead of the 'result' array.

The parser is briefly modified (you can see in the above link _README.md file_) so it does not throw an exceptiion if fails (This is because I handle the parsing errors in an upper layer) and it returns the filled object instead of the 'result' array.

I'm using PEG.js version 0.7.0

Any comment is welcome.

Regards, and congratulations David for this project.

--
José Luis Millán

David Majda

unread,
Dec 9, 2012, 2:32:35 PM12/9/12
to jmi...@aliax.net, pegjs
2012/12/3 José Luis Millán <jmi...@aliax.net>:
> What the parser does is filling an initial empty JavaScript object as the
> input passes through the difrerent rules. The parser returns such data
> object instead of the 'result' array.
>
> The parser is briefly modified (you can see in the above link _README.md
> file_) so it does not throw an exceptiion if fails (This is because I handle
> the parsing errors in an upper layer) and it returns the filled object
> instead of the 'result' array.

What about adding action with "return data;" inside to rules from
which you start parsing? It seems to me that it would be cleaner than
modifying the generated parser.

In general I think it's better to build the return value as you walk
up the grammar during parsing and avoid a global variable. Is there
any specific reason why you chose to go with it?

--
David Majda
Entropy fighter
http://majda.cz/

José Luis Millán

unread,
Dec 9, 2012, 4:31:58 PM12/9/12
to David Majda, pegjs
Hi,


2012/12/9 David Majda <da...@majda.cz>

2012/12/3 José Luis Millán <jmi...@aliax.net>:
> What the parser does is filling an initial empty JavaScript object as the
> input passes through the difrerent rules. The parser returns such data
> object instead of the 'result' array.
>
> The parser is briefly modified (you can see in the above link _README.md
> file_) so it does not throw an exceptiion if fails (This is because I handle
> the parsing errors in an upper layer) and it returns the filled object
> instead of the 'result' array.

What about adding action with "return data;" inside to rules from
which you start parsing? It seems to me that it would be cleaner than
modifying the generated parser.

I will try that. Seems much cleaner, yes.

 
In general I think it's better to build the return value as you walk
up the grammar during parsing and avoid a global variable. Is there
any specific reason why you chose to go with it?


In fact, I build the return value as I walk up the grammar during parsing. Each rule sets a property to the global variable ( some rules set no property, some others set one or many ). As I can start parsing from any rule, I decided to create a global empty object which is populated by rules during parsing. 

Thanks
 
--
David Majda
Entropy fighter
http://majda.cz/



--
José Luis Millán

David Majda

unread,
Dec 10, 2012, 2:25:58 PM12/10/12
to José Luis Millán, pegjs
2012/12/9 José Luis Millán <jmi...@aliax.net>:
>> In general I think it's better to build the return value as you walk
>> up the grammar during parsing and avoid a global variable. Is there
>> any specific reason why you chose to go with it?
>
> In fact, I build the return value as I walk up the grammar during parsing.
> Each rule sets a property to the global variable ( some rules set no
> property, some others set one or many ). As I can start parsing from any
> rule, I decided to create a global empty object which is populated by rules
> during parsing.

If I were doing this, I'd probably return simple objects from the
rules. For example, instead of

... { data.scheme = uri_scheme; }

I'd do

... { return { scheme: uri_scheme }; }

and merge/compose these objects later as appropriate.

José Luis Millán

unread,
Dec 10, 2012, 3:27:54 PM12/10/12
to David Majda, pegjs
Hi David,


2012/12/10 David Majda <da...@majda.cz>
This seems to be cleaner, yes. Could you please tell me the benefits of doing that way? I guess there are some that I can see right now.

Thanks



--
David Majda
Entropy fighter
http://majda.cz/



--
José Luis Millán

José Luis Millán

unread,
Dec 10, 2012, 3:38:51 PM12/10/12
to David Majda, pegjs



2012/12/10 José Luis Millán <jmi...@aliax.net>
Sorry, s/can/can't/

Thanks



--
David Majda
Entropy fighter
http://majda.cz/



--
José Luis Millán



--
José Luis Millán

David Majda

unread,
Dec 10, 2012, 3:43:37 PM12/10/12
to José Luis Millán, pegjs
2012/12/10 José Luis Millán <jmi...@aliax.net>:
> 2012/12/10 David Majda <da...@majda.cz>
>> If I were doing this, I'd probably return simple objects from the
>> rules. For example, instead of
>>
>> ... { data.scheme = uri_scheme; }
>>
>> I'd do
>>
>> ... { return { scheme: uri_scheme }; }
>>
>> and merge/compose these objects later as appropriate.
>
> This seems to be cleaner, yes. Could you please tell me the benefits of
> doing that way? I guess there are some that I can see right now.

You get all the benefits associated with getting rid of a global
variable. See any text about good programming practices for a list.

Note that your SIP grammar is relatively simple and flat in structure,
which means the benefits may not manifest that much.

José Luis Millán

unread,
Dec 10, 2012, 4:02:10 PM12/10/12
to David Majda, pegjs



2012/12/10 David Majda <da...@majda.cz>

2012/12/10 José Luis Millán <jmi...@aliax.net>:
> 2012/12/10 David Majda <da...@majda.cz>
>> If I were doing this, I'd probably return simple objects from the
>> rules. For example, instead of
>>
>>   ... { data.scheme = uri_scheme; }
>>
>> I'd do
>>
>>   ... { return { scheme: uri_scheme }; }
>>
>> and merge/compose these objects later as appropriate.
>
> This seems to be cleaner, yes. Could you please tell me the benefits of
> doing that way? I guess there are some that I can see right now.

You get all the benefits associated with getting rid of a global
variable. See any text about good programming practices for a list.

Sure, I was thinking about pegjs specific benefits.
 
Note that your SIP grammar is relatively simple and flat in structure,
which means the benefits may not manifest that much.


Anyway I like your proposal and I will think about adopting it.

Thanks again
 
--
David Majda
Entropy fighter
http://majda.cz/



--
José Luis Millán
Reply all
Reply to author
Forward
0 new messages