Loading ...
Sorry, an error occurred while loading the content.

Non-Default Exception-Handling - Synching to the end of the rule

Expand Messages
  • Colm McHugh
    This may have come up before, but I am wondering if there is a convenient way to synch to the end of the rule when doing non-default exception handling ? I
    Message 1 of 4 , Dec 10, 2004
    • 0 Attachment
      This may have come up before, but I am wondering if
      there is a convenient way to synch to the end of the
      rule when doing non-default exception handling ? I
      like the way the default exception handler does this,
      and it would be great if there was a simple way to
      apply this with non-default exception-handling.

      The reason I am asking is because with non-default
      exception handling, the lookahead is not advanced, and
      multiple exceptions can be raised for the same syntax
      error.

      I could make my exception-handling code ignore the
      exception if it sees that it has already processed the
      message, but hey I like to do as little work as
      possible.

      I could also take a really good look at the placement
      of my exception-handling blocks in my grammar; I
      suspect that some may be superfluous, and merely just
      rethrowing the same exception message. I am paranoid
      that all exceptions are trapped in the parser, and I
      also want to be able to report as many errors as
      possible to the user.

      Anyhoo, thanks for any opinions/advice!

      Colm.



      __________________________________
      Do you Yahoo!?
      Read only the mail you want - Yahoo! Mail SpamGuard.
      http://promotions.yahoo.com/new_mail
    • Ric Klaren
      On Fri, 10 Dec 2004 17:28:12 -0800 (PST), Colm McHugh ... Just mimick the default errorhandlers. Inside the errorhandling section of a rule you can use the
      Message 2 of 4 , Dec 11, 2004
      • 0 Attachment
        On Fri, 10 Dec 2004 17:28:12 -0800 (PST), Colm McHugh
        <colmmagoo@...> wrote:
        > This may have come up before, but I am wondering if
        > there is a convenient way to synch to the end of the
        > rule when doing non-default exception handling ? I
        > like the way the default exception handler does this,
        > and it would be great if there was a simple way to
        > apply this with non-default exception-handling.

        Just mimick the default errorhandlers. Inside the errorhandling
        section of a rule you can use the macros $FIRST $FOLLOW
        $FIRST(rulename) $FOLLOW(rulename) to get access to the sets used for
        error recovery. Just feed the right one to consumeUntil and you should
        have gone to the 'next-rule'.

        > The reason I am asking is because with non-default
        > exception handling, the lookahead is not advanced, and
        > multiple exceptions can be raised for the same syntax
        > error.

        You could also do things the other way around and leave on the default
        handling and turn it off on a per rule basis depends a bit on what's
        most practical. I'm afraid there's still no way to provide your own
        template for a exception handler.

        Cheers,

        Ric
      • Colm McHugh
        ... Thanks Ric, I wasn t aware of those macros, they seem really useful. However, consumeUntil doesn t work in the case of lexer exceptions; I have an expr
        Message 3 of 4 , Dec 12, 2004
        • 0 Attachment
          >
          > Just mimick the default errorhandlers. Inside the
          > errorhandling
          > section of a rule you can use the macros $FIRST
          > $FOLLOW
          > $FIRST(rulename) $FOLLOW(rulename) to get access to
          > the sets used for
          > error recovery. Just feed the right one to
          > consumeUntil and you should
          > have gone to the 'next-rule'.
          >
          Thanks Ric, I wasn't aware of those macros, they seem
          really useful.

          However, consumeUntil doesn't work in the case of
          lexer exceptions; I have an expr rule and if that gets
          something like 'x @ y' where @ is not a recognizable
          character, I still see the same '@ - bad character'
          error reported, even though my error handling code
          does consumeUntil on each rule.

          [I think] this is because 'consumeUntil' calls LA(1),
          but if the current input is on a bad character, that's
          just going to result in the same bad character
          exception being thrown... I'd like something more
          low-level than LA(1) to advance past the character in
          this case, though I'm wary of dragging too much lexer
          detail into hand-written error managing code. Time to
          poke around the ANTLR header files a bit...

          Thanx for the suggestion,
          Colm.



          __________________________________
          Do you Yahoo!?
          Read only the mail you want - Yahoo! Mail SpamGuard.
          http://promotions.yahoo.com/new_mail
        • Ric Klaren
          On Sun, 12 Dec 2004 16:39:54 -0800 (PST), Colm McHugh ... They re pretty recent additions. I noticed the docs are not that clear on their existence :( ...
          Message 4 of 4 , Dec 13, 2004
          • 0 Attachment
            On Sun, 12 Dec 2004 16:39:54 -0800 (PST), Colm McHugh
            <colmmagoo@...> wrote:
            > > Just mimick the default errorhandlers. Inside the
            > > errorhandling
            > > section of a rule you can use the macros $FIRST
            > > $FOLLOW
            > > $FIRST(rulename) $FOLLOW(rulename) to get access to
            > > the sets used for
            > > error recovery. Just feed the right one to
            > > consumeUntil and you should
            > > have gone to the 'next-rule'.
            > >
            > Thanks Ric, I wasn't aware of those macros, they seem
            > really useful.

            They're pretty recent additions. I noticed the docs are not that clear
            on their existence :(

            > However, consumeUntil doesn't work in the case of
            > lexer exceptions;

            consumeUntil does no exception handling (if this is required you need
            to wrap it or provide a version that is exception resistant...)

            By the way you mean MismatchedCharExceptions ? Or stream exceptions?

            > I have an expr rule and if that gets
            > something like 'x @ y' where @ is not a recognizable
            > character, I still see the same '@ - bad character'
            > error reported, even though my error handling code
            > does consumeUntil on each rule.
            >
            > [I think] this is because 'consumeUntil' calls LA(1),
            > but if the current input is on a bad character, that's
            > just going to result in the same bad character
            > exception being thrown... I'd like something more
            > low-level than LA(1) to advance past the character in
            > this case, though I'm wary of dragging too much lexer
            > detail into hand-written error managing code. Time to
            > poke around the ANTLR header files a bit...

            I think your analysis is pretty much dead on. Which makes me wonder
            what kind of error handing is set up in the lexer? Doesn't it some
            kind of consume() call to advance past such a character. My memory is
            letting me down a bit on how this is handled. Anycase the actual act
            of getting rid of such a character is done via the
            LexerSharedInputState. Get from it the InputBuffer do a fill(1) on it
            and consume() that should get rid of the char (I think)

            Cheers,

            Ric
          Your message has been successfully submitted and would be delivered to recipients shortly.