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

Stephen Siegel siegel at udel.edu
Tue Jul 17 17:34:49 PDT 2012


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



More information about the antlr-interest mailing list