[antlr-interest] C++ target and excpetions
Oliver Kowalke
oliver.kowalke at gmx.de
Thu Feb 23 03:17:55 PST 2012
Hello Gokul,
it's a missunderstanding - I was asking for specializing the catch clauses per rule, not globaly as I provide with @rulecatch.
I'm referring to the book 'The Definitive ANTLR Reference', T. Parr, chap. 10.5, p. 243.
regards,
Oliver
> Hmm.. I am confused. Don't catch the exception in displayRecognitionError.
> throw it from inside. In the my_rule, setup some instance variable of the
> parser to the kind of exception, you want to throw. Inside
> displayRecognitionError, access the instance variable and throw either
> different kinds of exception / same exception with its instance variable
> set to different values. So it should be like this.
>
> @parser::context {
> enum RuleType {
> MY_RULE1
> , MY_RULE2
> ......
> };
> RuleType m_rule_type;
>
> void displayRecognitionError(.... )
> {
> switch( m_rule_type ){
> case MY_RULE1:
> throw ipaddress_error("invalid ipaddress at line
> "
> + get_line( ex) );
> .....
> }
> }
> }
>
> my_rule1
> @init{
> m_rule_type = MY_RULE1;
> }
> : ID '=' IPADDRESS { ... }
> ;
>
> Better catch these exceptions at the level, where you call the parser.
>
> Usually It is a bad idea in C++ to setup a lot of try..catch blocks. But i
> also want to provide the decision making power to the library user. So i
> have created and uploaded the latest jar file which supports rulecatch
> directive correctly.
>
> Adding the group, as it might be useful for everyone.
>
> Thanks,
> Gokul.
>
> On Thu, Feb 23, 2012 at 12:54 AM, Oliver Kowalke
> <oliver.kowalke at gmx.de>wrote:
>
> > Hi Gokul,
> >
> > I've another requirement related to exceptions. As you already told me
> you
> > consider not to implement @rulecatch directive.
> > Could you provide a rule-specific catch directive?
> >
> > my_rule
> > : ID '=' IPADDRESS { ... }
> > ;
> > catch[RecognitionError const& e]
> > { throw ipaddress_error("invalid ipaddress at line " + get_line( ex)
> );
> > }
> >
> > Only rules with the catch-directive would get a try-catch block. If I
> > catch this implementation in displayRecognitionError() I don't know if
> it
> > was an illformed ipaddres or an invalid interface etc.
> >
> > regards,
> > Oliver
> >
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
More information about the antlr-interest
mailing list