librelist archives

« back to archive

How to do Start Conditions?

How to do Start Conditions?

From:
Philippe Rathe
Date:
2010-11-10 @ 03:01
In the flex documentation: (flex provides a mechanism for  
conditionally activating rules...)
http://dinosaur.compilertools.net/flex/flex_11.html

Does Jison have in some ways a mechanism to achieve something like this?

Let's say you want to parse HTML and JavaScript in an html file.
So the regex that match the JavaScript grammar would be conditional  
not to be mixed with HTML regex.

Just to help you visualize how it could look like:
----------------------------------------------------------------

{BEGIN 'html';}
"<script>"		{BEGIN 'js'; return 'SCRIPT';}
<html>"<div/>"	{return 'DIV';}
<html>"<span/>"	{return 'SPAN';}
...

<js>"var"		{return 'VAR';}
<js>"if"			{return 'IF';}
...
"</script>"		{END 'js';}  // OR {BEGIN 'html';} // OR {BEGIN 'INITIAL';}

--------------------------------------------------------------------------------------------------

Thanks btw!

-- Philippe


Re: [jison] How to do Start Conditions?

From:
Zachary Carter
Date:
2010-11-10 @ 03:17
On Tue, Nov 9, 2010 at 10:01 PM, Philippe Rathe <prathe@gmail.com> wrote:

> In the flex documentation: (flex provides a mechanism for conditionally
> activating rules...)
> http://dinosaur.compilertools.net/flex/flex_11.html
>
> Does Jison have in some ways a mechanism to achieve something like this?
>
> Let's say you want to parse HTML and JavaScript in an html file.
> So the regex that match the JavaScript grammar would be conditional not to
> be mixed with HTML regex.
>
> Just to help you visualize how it could look like:
> ----------------------------------------------------------------
>
> {BEGIN 'html';}
> "<script>" {BEGIN 'js'; return 'SCRIPT';}
> <html>"<div/>" {return 'DIV';}
> <html>"<span/>" {return 'SPAN';}
> ...
>
> <js>"var" {return 'VAR';}
> <js>"if"  {return 'IF';}
> ...
> "</script>" {END 'js';}  // OR {BEGIN 'html';} // OR {BEGIN 'INITIAL';}
>
> 
--------------------------------------------------------------------------------------------------
>
> Thanks btw!
>
> -- Philippe
>
>
Interesting – I hadn't seen that section before. The scanner Jison uses
depends on JavaScript Regexps, so a solution might be hacky and not very
efficient, but possible. I've created a ticket here:
https://github.com/zaach/jison/issues/issue/21

Thanks.

-- 
Zach Carter

Re: [jison] How to do Start Conditions?

From:
Philippe Rathe
Date:
2010-11-10 @ 15:15
Quickly, is there any hack around possible with the actual code?

Is there a way to have a closure on a variable that is visible within  
the execution blocks in the lexer?
I guess not since it procuces a JSON structure.

-- Philippe




On 9-Nov-10, at 10:17 PM, Zachary Carter wrote:

>
>
> On Tue, Nov 9, 2010 at 10:01 PM, Philippe Rathe <prathe@gmail.com>  
> wrote:
> In the flex documentation: (flex provides a mechanism for  
> conditionally activating rules...)
> http://dinosaur.compilertools.net/flex/flex_11.html
>
> Does Jison have in some ways a mechanism to achieve something like  
> this?
>
> Let's say you want to parse HTML and JavaScript in an html file.
> So the regex that match the JavaScript grammar would be conditional  
> not to be mixed with HTML regex.
>
> Just to help you visualize how it could look like:
> ----------------------------------------------------------------
>
> {BEGIN 'html';}
> "<script>"		{BEGIN 'js'; return 'SCRIPT';}
> <html>"<div/>"	{return 'DIV';}
> <html>"<span/>"	{return 'SPAN';}
> ...
>
> <js>"var"		{return 'VAR';}
> <js>"if"			{return 'IF';}
> ...
> "</script>"		{END 'js';}  // OR {BEGIN 'html';} // OR {BEGIN  
> 'INITIAL';}
> 
--------------------------------------------------------------------------------------------------
>
> Thanks btw!
>
> -- Philippe
>
>
> Interesting – I hadn't seen that section before. The scanner Jison  
> uses depends on JavaScript Regexps, so a solution might be hacky and  
> not very efficient, but possible. I've created a ticket here: 
https://github.com/zaach/jison/issues/issue/21
>
> Thanks.
>
> -- 
> Zach Carter

Re: [jison] How to do Start Conditions?

From:
Zachary Carter
Date:
2010-11-11 @ 03:14
On Wed, Nov 10, 2010 at 10:15 AM, Philippe Rathe <prathe@gmail.com> wrote:

> Quickly, is there any hack around possible with the actual code?
>
> Is there a way to have a closure on a variable that is visible within the
> execution blocks in the lexer?
> I guess not since it procuces a JSON structure.
>
> -- Philippe
>

During lexer actions you can set flags on the yy context object and return
different tokens based on the flags.

Roughly:

"<script>" { yy.js = true; return "SCRIPT"; }
"var" { return yy.js ? "VAR" : "TEXT"; }
"</script>" {yy.js = false; return "ENDSCRIPT"; }

 You could also try matching the whole script block as a token and using a
separate parser on it later.



>
>
>
> On 9-Nov-10, at 10:17 PM, Zachary Carter wrote:
>
>
>
> On Tue, Nov 9, 2010 at 10:01 PM, Philippe Rathe <prathe@gmail.com> wrote:
>
>>  In the flex documentation: (flex provides a mechanism for conditionally
>> activating rules...)
>> http://dinosaur.compilertools.net/flex/flex_11.html
>>
>> Does Jison have in some ways a mechanism to achieve something like this?
>>
>> Let's say you want to parse HTML and JavaScript in an html file.
>> So the regex that match the JavaScript grammar would be conditional not to
>> be mixed with HTML regex.
>>
>> Just to help you visualize how it could look like:
>> ----------------------------------------------------------------
>>
>> {BEGIN 'html';}
>> "<script>" {BEGIN 'js'; return 'SCRIPT';}
>> <html>"<div/>" {return 'DIV';}
>> <html>"<span/>" {return 'SPAN';}
>> ...
>>
>> <js>"var" {return 'VAR';}
>> <js>"if"  {return 'IF';}
>> ...
>> "</script>" {END 'js';}  // OR {BEGIN 'html';} // OR {BEGIN 'INITIAL';}
>>
>> 
--------------------------------------------------------------------------------------------------
>>
>> Thanks btw!
>>
>> -- Philippe
>>
>>
> Interesting – I hadn't seen that section before. The scanner Jison uses
> depends on JavaScript Regexps, so a solution might be hacky and not very
> efficient, but possible. I've created a ticket here:
> https://github.com/zaach/jison/issues/issue/21
>
> Thanks.
>
> --
> Zach Carter
>
>
>


-- 
Zach Carter

Re: [jison] How to do Start Conditions?

From:
Philippe Rathe
Date:
2010-11-11 @ 03:30
Perfect, in my case I will pick to "second pass" technique.
Thanks Zach!

-- Philippe




On 10-Nov-10, at 10:14 PM, Zachary Carter wrote:

>
>
> On Wed, Nov 10, 2010 at 10:15 AM, Philippe Rathe <prathe@gmail.com>  
> wrote:
> Quickly, is there any hack around possible with the actual code?
>
> Is there a way to have a closure on a variable that is visible  
> within the execution blocks in the lexer?
> I guess not since it procuces a JSON structure.
>
> -- Philippe
>
> During lexer actions you can set flags on the yy context object and  
> return different tokens based on the flags.
>
> Roughly:
>
> "<script>" { yy.js = true; return "SCRIPT"; }
> "var" { return yy.js ? "VAR" : "TEXT"; }
> "</script>" {yy.js = false; return "ENDSCRIPT"; }
>
>  You could also try matching the whole script block as a token and  
> using a separate parser on it later.
>
>
>
>
>
>
> On 9-Nov-10, at 10:17 PM, Zachary Carter wrote:
>
>>
>>
>> On Tue, Nov 9, 2010 at 10:01 PM, Philippe Rathe <prathe@gmail.com>  
>> wrote:
>> In the flex documentation: (flex provides a mechanism for  
>> conditionally activating rules...)
>> http://dinosaur.compilertools.net/flex/flex_11.html
>>
>> Does Jison have in some ways a mechanism to achieve something like  
>> this?
>>
>> Let's say you want to parse HTML and JavaScript in an html file.
>> So the regex that match the JavaScript grammar would be conditional  
>> not to be mixed with HTML regex.
>>
>> Just to help you visualize how it could look like:
>> ----------------------------------------------------------------
>>
>> {BEGIN 'html';}
>> "<script>"		{BEGIN 'js'; return 'SCRIPT';}
>> <html>"<div/>"	{return 'DIV';}
>> <html>"<span/>"	{return 'SPAN';}
>> ...
>>
>> <js>"var"		{return 'VAR';}
>> <js>"if"			{return 'IF';}
>> ...
>> "</script>"		{END 'js';}  // OR {BEGIN 'html';} // OR {BEGIN  
>> 'INITIAL';}
>> 
--------------------------------------------------------------------------------------------------
>>
>> Thanks btw!
>>
>> -- Philippe
>>
>>
>> Interesting – I hadn't seen that section before. The scanner Jison  
>> uses depends on JavaScript Regexps, so a solution might be hacky  
>> and not very efficient, but possible. I've created a ticket here: 
https://github.com/zaach/jison/issues/issue/21
>>
>> Thanks.
>>
>> -- 
>> Zach Carter
>
>
>
>
> -- 
> Zach Carter