[antlr-interest] Reading block of arbitrary text delimited by curly braces

Burton Samograd burton.samograd at markit.com
Wed Jul 18 07:55:59 PDT 2012


Is this what you are suggesting?

// Global
bool inBlockData = false;

// Parser
block
    : BLOCK LCURLY { inBlockData = true; }  BLOCK_DATA RCURLY { inBlockData = false; }
        -> ^(BLOCK BLOCK_DATA)
    ;

// Lexer
BLOCK : 'BLOCK' ;
BLOCK_DATA : { inBlockData }?=> (~'}')* ;

Using this technique gets me a bit further, but I am getting a
recognition exception when I hit the BLOCK_DATA like it is still going
through my original lexer/parser and not collecting the BLOCK_DATA
like I would like it to.

I did some reading on semantic predicates but literature just gave examples
for parser rules so I am not sure if I applied the concept to lexer rules properly.

--
Burton Samograd

-----Original Message-----
From: Stephen Siegel [mailto:siegel at udel.edu]
Sent: Tuesday, July 17, 2012 6:35 PM
To: Burton Samograd
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Reading block of arbitrary text delimited by curly braces

You could use a boolean variable added to the lexer "inBlock".  Initially it is false.  Set it to true just after matching the LCURLY and back to false after matching RCURLY in the block rule.   They you could define the BLOCK_DATA token using inBlock as a guard (I think that's called a "semantic predicate").  BLOCK_DATA should match anything EXCEPT RCURLY (I'm assuming you don't want to allow RCURLY in the block data, or how would you know when the block ends? -- just like a comment in C, for example.)
-Steve

On Jul 17, 2012, at 3:57 PM, Burton Samograd wrote:

> Hello,
>
> We have a requirement where we need to include a block of arbitrary text in a block, like so:
>
> BLOCK { some arbitrary text here }
>
> We initially got around this by making the whole block a token, like:
>
> BLOCK : 'BLOCK (' '|'\t'|'\r'|'\n')* '{' (~'}')*  '}' ;
>
> but this is less than ideal.  I am thinking that we would use something like:
>
> block : BLOCK RCURLY BLOCK_DATA LCURLY
>
> with BLOCK : 'BLOCK' and LCURLY/RCURLY as { and }.
>
> I am stuck on specifying BLOCK_DATA which is basically .* to the lexer.  I have attempted to access the input stream from the parser RECOGNIZER but have not been able to come up with a solution.
>
> I am looking to basically hijack the input stream after seeing a BLOCK token so I can read the arbitrary text; I can parse out the  { } as needed.
>
> Is this possible?
>
> --
> Burton Samograd
>
> ________________________________
> This e-mail, including accompanying communications and attachments, is strictly confidential and only for the intended recipient. Any retention, use or disclosure not expressly authorised by Markit is prohibited. This email is subject to all waivers and other terms at the following link: http://www.markit.com/en/about/legal/email-disclaimer.page
>
> Please visit http://www.markit.com/en/about/contact/contact-us.page? for contact information on our offices worldwide.
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


This e-mail, including accompanying communications and attachments, is strictly confidential and only for the intended recipient. Any retention, use or disclosure not expressly authorised by Markit is prohibited. This email is subject to all waivers and other terms at the following link: http://www.markit.com/en/about/legal/email-disclaimer.page

Please visit http://www.markit.com/en/about/contact/contact-us.page? for contact information on our offices worldwide.


More information about the antlr-interest mailing list