[antlr-interest] How to throw an exception from the lexer to the caller rule
lemoine at multimania.com
lemoine at multimania.com
Wed Oct 17 05:46:56 PDT 2001
Hi,
I had specified the testLiterals option to true for the IDENT rule in
the Lexer to test each token against the literals table.
But when the literal does not match with the lexer IDENT rule, it
generates an Exception and exits of the lexer.
I want to catch the exception in the caller rule of the parser to
specify my own error message.
Any help would be greatly apprecied.
Thanks
Hervé
$ cat infile2
GET /NAME=tot?"select name, type from sysdatabases where type='U'"
HTTP/1.1
$
$ java Main < infile2
unexpected token: NAME
$
If the exception throws to the caller rule (key in the grammar), it
should be :
$ java Main < infile2
Unknown option NAME
$
The grammar file is :
class P extends Parser;
options {
defaultErrorHandler=false ;
}
{
String _http_method = null, _http_version = null ;
public String getToken(String msg) {
return msg.substring(18) ;
}
}
start : http_method SLASH uri http_version
;
http_method : g:HTTP_METHOD_GET
{ _http_method = g.getText() ; }
| p:HTTP_METHOD_POST
{ _http_method = p.getText() ; }
;
exception // for HTTP_METHOD
catch [RecognitionException e] {
String token = getToken(e.getMessage()) ;
throw new RecognitionException("HTTP
method "+token+" not supported");
}
uri : commands
| queries
;
http_version : v1:HTTP_VERSION_10
{ _http_version = v1.getText() ; }
| v2:HTTP_VERSION_11
{ _http_version = v2.getText() ; }
;
exception // for HTTP_VERSION
catch [RecognitionException e] {
String token = getToken(e.getMessage()) ;
throw new RecognitionException("Unknown HTTP
protocol "+token);
}
commands : { String val = null ; }
(
s:STATS
{ val = s.getText() ; }
| d:DSINFOS
{ val = d.getText() ; }
| p:PINFOS
{ val = p.getText() ; }
| h:HELP
{ val = h.getText() ; }
)
{
System.out.println("command is "+val) ;
}
;
exception
catch [RecognitionException e] {
String token = getToken(e.getMessage()) ;
throw new RecognitionException("Unknown
command "+token);
}
queries : query (SEMI query)*
;
query : (optionList QUESTION)? sqlList
;
optionList : keypair (COMMA keypair)*
;
sqlList : sql (COMMA sql)*
;
keypair : { String k = null, v = null ;}
k=key EQUALS v=value
{
System.out.println("key="+k) ;
System.out.println("value="+v) ;
}
;
sql : s:STRING_LITERAL
{
System.out.println("sql="+s.getText()) ;
}
;
key returns[String val = null]
: pn:PNAME
{ val = pn.getText() ; }
| d:DSN
{ val = d.getText() ; }
| u:USER
{ val = u.getText() ; }
| pw:PWD
{ val = pw.getText() ; }
;
exception
catch [RecognitionException e] {
String token = getToken(e.getMessage()) ;
throw new RecognitionException("Unknown
option "+token);
}
value returns[String val = null]
: s:STRING_LITERAL
{ val = s.getText() ; }
| i:IDENT
{ val = i.getText() ; }
;
class L extends Lexer;
options {
k=2 ;
charVocabulary = '\3'..'\377' ;
testLiterals=false ;
}
tokens {
// HTTP Syntax
HTTP_METHOD_GET="GET"; HTTP_METHOD_POST="POST";
// HTTP version
HTTP_VERSION_10="HTTP/1.0"; HTTP_VERSION_11="HTTP/1.1";
// Static options keys
PNAME="PNAME"; DSN="DSN"; USER="USER"; PWD="PWD";
// Keywords
STATS="Statistics"; DSINFOS="DataSourcesInfos";
PINFOS="ProfilesInfos"; HELP="Help";
}
//
// string literals
//
STRING_LITERAL : DQUOTE_LITERAL | SQUOTE_LITERAL
;
protected
DQUOTE_LITERAL : '"'!
( '"' '"'!
| ~('"'|'\n'|'\r')
)*
( '"'!
| // nothing
)
;
protected
SQUOTE_LITERAL : '\''!
( '\'' '\''!
| ~('\''|'\n'|'\r')
)*
( '\''!
| // nothing
)
;
//
// Operators
//
COMMA : ',' ;
DOT : '.' ;
QUESTION : '?' ;
SEMI : ';' ;
SLASH : '/' ;
EQUALS : '=' ;
//
// Whitespace
//
WS : (' '
| '\t'
| '\f'
| ( "\r\n"
| '\r'
| '\n'
)
)
{ $setType(Token.SKIP) ; }
;
IDENT
options { testLiterals=true ;}
: 'a'..'z'|'A'..'Z') 'a'..'z'|'A'..'Z'|'0'..'9'|'/'|'.')*
;
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list