[antlr-interest] Scanning Perl-style quoted strings q{foo{bar}quux}?!

David-Sarah Hopwood david-sarah at jacaranda.org
Wed Jul 29 15:36:53 PDT 2009


Ralf S. Engelschall wrote:
> To a small ANTLR-based expression language I would like to add
> Perl-style quoted strings:
> 
>     q{foo{bar}quux}
>     q(foo(bar)quux)
>     q!foo/bar/quux!
> 
> For those who don't know these constructs: it is a variant of
> non-interpolating strings where one doesn't have to quote the quote
> character. And in case of one of the open/close pairs of quote
> characters ("(" + ")", "<" + ">", "[" + "]" and "{" + "}") one can even
> nest them without escaping (as long as there as the nesting is correct,
> i.e., equal number of open and close characters).

(I'm assuming, without knowing Perl very well, that only the delimiters
that appear on the "outside" have to nest, e.g. q{foo[{bar}quux} is valid.)

> Remains the question: what is the best way to implement this in ANTLR 3?

Remember that lexer rules can be recursive, so you don't have to explicitly
keep track of nesting depth. The following approach (untested) is more
declarative, and incidentally avoids the problem you encountered:

QSTRING
  : 'q' ( AngleQS | BraceQS | BrackQS | ParenQS | SlashQS | BangQS ) ;

fragment AngleQS
  : '<' ( AngleQS | ~('<' | '>') )* '>' ;

fragment BraceQS
  : '{' ( BraceQS | ~('{' | '}') )* '}' ;

fragment BrackQS
  : '[' ( BrackQS | ~('[' | ']') )* ']' ;

fragment ParenQS
  : '[' ( ParenQS | ~('[' | ']') )* ']' ;

fragment SlashQS
  : '/' ( SlashQS | ~'/' )* '/' ;

fragment BangQS
  : '!' ( BangQS | ~'!' )* '!' ;

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the antlr-interest mailing list