[antlr-interest] string interpolation
Rodrigo B. de Oliveira
rodrigobamboo at hotmail.com
Mon Aug 11 12:49:32 PDT 2003
I'm building a very simple parser that recognizes (and interprets) simple programs such as:
a = 5+3
b = 102-5
print "a: ${a}, b: ${b}, a+b: ${a+b}"
The main thing I'm trying to achieve with this toy is to get the string interpolation thing right before integrating it in a larger parser project I'm working on.
The SI thing works by transforming expressions such as:
"a+b: ${a+b}, b: ${b}"
in:
"a+b: {0}, b: {1}" a + b ESEPARATOR b
before they get to the parser (ESEPARATOR is an imaginary token).
I have successfully put the parser to work by combining two lexers, a TokenStreamSelector along with a custom TokenStream implementation (TokenStreamRecorder).
My problem now is to make sure the lexer rejects invalid sequences such as:
"${ ${}"
"${"
"${
with a SemanticException. Unfortunately the exception is not getting through...
I have already set the defaultErrorHandler to false in the parser and in both lexers.
The grammar for the second lexer (the one parsing the expression inside ${}) follows:
<grammar>
options
{
language="CSharp";
}
class ExpressionLexer extends Lexer;
options
{
importVocab = SI;
defaultErrorHandler=false;
}
{
public override void uponEOF()
{
// should never see EOF
Error();
}
void Error()
{
throw new SemanticException(
"Invalid expression!",
getFilename(),
getLine(),
getColumn()
);
}
}
ID: ID_LETTER (ID_LETTER | DIGIT)*;
INT: (DIGIT)+;
ASSIGN: '=';
SUM_OPERATOR : '+' | '-' ;
WS: (' ' | '\t' | '\r' | '\n' { newline(); }) { $setType(Token.SKIP); };
RBRACE: '}';
DQ: '"' { Error(); };
protected
ID_LETTER : ('_' | 'a'..'z' | 'A'..'Z' );
protected
DIGIT : '0'..'9';
</grammar>
I can send the other 2 files (the main grammar file and the custom TokenStream implementation) if someone is willing to take a look at them also...
BTW, I'll post the whole project to the antlr site upon completion, I think it's a good example of how to combine TokenStreamSelector with custom TokenStream implementations...
Thanks in advance for any help,
Rodrigo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20030811/189f597b/attachment.html
More information about the antlr-interest
mailing list