Uploaded image for project: 'UIMA'
  1. UIMA
  2. UIMA-4517

fixing ArrayOutOfBounds exception when parsing invalid Ruta script

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.3.1ruta
    • Fix Version/s: 2.3.1ruta
    • Component/s: Ruta
    • Labels:
      None

      Description

      looking for more informative exception messages when parsing invalid Ruta script

      When using Ruta in a non-Workbench setup (in my case, Maven), I don't manage to catch Ruta script errors in a meaningful way. Here is an example:

      "aaa\." -> MyAnnotation; // fails because of escaped dot

      The thrown error is quite uninformative:

      java.lang.ArrayIndexOutOfBoundsException: -1
      at org.apache.uima.ruta.parser.RutaParser.emitErrorMessage(RutaParser.java:365)
      at org.apache.uima.ruta.parser.RutaParser.reportError(RutaParser.java:386)
      at org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken(BaseRecognizer.java:603)
      at org.antlr.runtime.BaseRecognizer.match(BaseRecognizer.java:115)
      at org.apache.uima.ruta.parser.RutaParser.file_input(RutaParser.java:680)
      at org.apache.uima.ruta.engine.RutaEngine.loadScript(RutaEngine.java:1058)
      at org.apache.uima.ruta.engine.RutaEngine.initializeScript(RutaEngine.java:743)
      ...

      Here is the code to reproduce: https://github.com/renaud/annotate_ruta_example/tree/ruta_error_message

      However, if I paste that script line in the Ruta Workbench, it nicely underlines it in red at the exact location, and even says "Mismatched input". How do I achieve the same programatically (from Java)?

      1. RutaParseRuntimeException.java
        0.4 kB
        Renaud Richardet
      2. RutaParser2.diff
        4 kB
        Renaud Richardet
      3. RutaParser.diff
        2 kB
        Renaud Richardet

        Activity

        Hide
        renaudrichardet Renaud Richardet added a comment -

        Dear Peter,

        Attached a patch to fix the ArrayIndexOutOfBoundsException. It works, but I am not sure it's the best way to fix it – I will let you decide. Also, not sure if 'none' is the best value for stringExpected.

        Now the message thrown is informative (see testEscapedDot):

        Error in Ruta3791116492856817165, line 1, ".": expected none, but found DOT

        But why print it to System.out (instead of throwing an exception)?

        in src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g

            public void emitErrorMessage(String msg) {
                System.out.println(msg);
            }
        

        Thanks, Renaud

        Show
        renaudrichardet Renaud Richardet added a comment - Dear Peter, Attached a patch to fix the ArrayIndexOutOfBoundsException. It works, but I am not sure it's the best way to fix it – I will let you decide. Also, not sure if 'none' is the best value for stringExpected. Now the message thrown is informative (see testEscapedDot): Error in Ruta3791116492856817165, line 1, ".": expected none, but found DOT But why print it to System.out (instead of throwing an exception)? in src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g public void emitErrorMessage( String msg) { System .out.println(msg); } Thanks, Renaud
        Hide
        renaudrichardet Renaud Richardet added a comment -

        Here is another patch that throws a RuntimeException (RutaParseRuntimeException). The advantage I see here is that one can use Java's exception mechanism (instead of System.out).

        Let's discuss...

        Show
        renaudrichardet Renaud Richardet added a comment - Here is another patch that throws a RuntimeException (RutaParseRuntimeException). The advantage I see here is that one can use Java's exception mechanism (instead of System.out). Let's discuss...
        Hide
        pkluegl Peter Klügl added a comment -

        There's nothing to discuss
        The System.out's are just for development and hardly of any use. I haven't found the time yet to introduce more exceptions in ruta.

        Show
        pkluegl Peter Klügl added a comment - There's nothing to discuss The System.out's are just for development and hardly of any use. I haven't found the time yet to introduce more exceptions in ruta.
        Hide
        pkluegl Peter Klügl added a comment -

        Did you introduce a new runtime exception or did you modify the existing one? The patch causes some compile errors because RutaParseRuntimeException is not resolved.

        Show
        pkluegl Peter Klügl added a comment - Did you introduce a new runtime exception or did you modify the existing one? The patch causes some compile errors because RutaParseRuntimeException is not resolved.
        Hide
        renaudrichardet Renaud Richardet added a comment -

        My bad, I forgot to add the file in the diff...

        Show
        renaudrichardet Renaud Richardet added a comment - My bad, I forgot to add the file in the diff...
        Hide
        renaudrichardet Renaud Richardet added a comment - - edited

        Notes:

        • The reason to throw a RuntimeException is that the RutaParser's base class does not throws checked Exceptions for emitErrorMessage() methods.
        • some of the code from RutaParser.g, public void emitErrorMessage(RecognitionException e), could be moved to RutaParseRuntimeException
        Show
        renaudrichardet Renaud Richardet added a comment - - edited Notes: The reason to throw a RuntimeException is that the RutaParser's base class does not throws checked Exceptions for emitErrorMessage() methods. some of the code from RutaParser.g, public void emitErrorMessage(RecognitionException e), could be moved to RutaParseRuntimeException
        Hide
        pkluegl Peter Klügl added a comment -

        I remember now why I didn't throw an exception in the first place: the parser cannot recover from errors, e.g., continuing parsing for the remaining rules.In ruta-core that does not matter, flawed rules should not executed at all. However, in the workbench, it is essential for the editing support and the AST.

        So, should by the analysis engine be robust enough to apply a script with some broken rules? I don't think so. Opinions?

        Show
        pkluegl Peter Klügl added a comment - I remember now why I didn't throw an exception in the first place: the parser cannot recover from errors, e.g., continuing parsing for the remaining rules.In ruta-core that does not matter, flawed rules should not executed at all. However, in the workbench, it is essential for the editing support and the AST. So, should by the analysis engine be robust enough to apply a script with some broken rules? I don't think so. Opinions?
        Hide
        pkluegl Peter Klügl added a comment -

        I've committed the patch.

        Maybe we should only use RuntimeExceptions or only normal Exceptions for parsing errors. I introduced runtime exceptions in the rule inference, e.g., in case a type is not resolved. The interfaces for parsing are, however, rather clear so that we could use normal exceptions here. I do not have a strong opinions yet.

        Show
        pkluegl Peter Klügl added a comment - I've committed the patch. Maybe we should only use RuntimeExceptions or only normal Exceptions for parsing errors. I introduced runtime exceptions in the rule inference, e.g., in case a type is not resolved. The interfaces for parsing are, however, rather clear so that we could use normal exceptions here. I do not have a strong opinions yet.
        Hide
        renaudrichardet Renaud Richardet added a comment -

        I don't think so either.

        Show
        renaudrichardet Renaud Richardet added a comment - I don't think so either.
        Hide
        renaudrichardet Renaud Richardet added a comment -

        Thanks for applying the patch.

        I don't have the whole overview regarding Ruta Exceptions, but using only RE or only checked exceptions for parsing errors would make sense to me. It's not clear to me how one implement it. If you could quickly draft a plan, I could start working on it.

        Show
        renaudrichardet Renaud Richardet added a comment - Thanks for applying the patch. I don't have the whole overview regarding Ruta Exceptions, but using only RE or only checked exceptions for parsing errors would make sense to me. It's not clear to me how one implement it. If you could quickly draft a plan, I could start working on it.
        Hide
        pkluegl Peter Klügl added a comment -

        I have no plan yet and I don't know when I will find the time. I hope, this week, but I cannot promise it. Let's see...

        Show
        pkluegl Peter Klügl added a comment - I have no plan yet and I don't know when I will find the time. I hope, this week, but I cannot promise it. Let's see...
        Hide
        pkluegl Peter Klügl added a comment -

        Can we resolve this issue?

        Show
        pkluegl Peter Klügl added a comment - Can we resolve this issue?
        Hide
        renaudrichardet Renaud Richardet added a comment -

        yes, thanks

        Show
        renaudrichardet Renaud Richardet added a comment - yes, thanks

          People

          • Assignee:
            pkluegl Peter Klügl
            Reporter:
            renaudrichardet Renaud Richardet
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development