Better syntax errors through catch all statement

Bug #602847 reported by Eike
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Freeode
New
Wishlist
Unassigned

Bug Description

Currently most syntax errors produce an error message like "Expecting unindent ...." or "Expecting end of file ..." which is not very helpful.

The cause is IMHO that Pyparsing's backtracking. When the rules for all statements have failed Pyparsing tries to match rules from the indentation logic. The error message is then generated in/for the last rule (parser in Pyparsing's terminology) that was tried.

Therefore I propose to introduce an additional rule on the level of the statements, that comes last. I matches any word and any punctuation. The parser's parse action then raises a ParseFatalException with the message "Unknown keyword ..." or "Unknown operator ...". This would improve the error messages much.

The issue has been already tried unsuccessfully. See comment #1
The issue has been already tried unsuccessfully.

https://sourceforge.net/apps/trac/freeode/ticket/27

The relation between the "indentedBlock" Parser and Siml's "statement" parser are quite intricate and the issue is not easy to solve. It is probably necessary to implement a special version of "indentedBlock" which works better with Siml.

A parser that matches anything has been inserted as the last possible match for statements. For wrong keywords it works as intended; but for broken expressions, error messages become much worse. Somewhere in "indentedBlock" there is backtracking. The line:
{{{
    h = V/A_bott+
}}}
Now leads to the error message "Unknown syntax: "h" ", it reports the error at the beginning of the statement, instead of the end.
The ideal error would have been: "Unknown syntax: "+" "
The old error was: "Expected end of line ... (line:7, col:21)" which points to the right character.

Here are the snippets that were inserted into Siml's parser:

{{{
class Parser(...):
    ....

    def _action_error_stmt(self, s, loc, toks):
        '''
        Raise a ParseFatalException, because a syntax error was detected.
        '''
        err_word = toks[0]
        msg = 'Unknown syntax: "%s"' % err_word
        raise ParseFatalException(s, loc, msg, elem=None )

    ...
    ...
    def _defineLanguageSyntax():
        ...
        #Generate an error when no statement parser could match the text
        error_stmt = CharsNotIn(whitespace_chars) .setParseAction(self._action_error_stmt)
        ...
        #Statement: one line of code, or a compound (if, class, func) statement
        statement = ( simple_stmt + newline
                    | stmt_list_1 + newline
                    | compound_stmt
                    | error_stmt
                    )
}}}

Tags: parser
Eike (eike-welk)
Changed in freeode:
importance: Undecided → Wishlist
milestone: none → 0.4.1
Eike (eike-welk)
Changed in freeode:
assignee: nobody → Eike (eike-welk)
Revision history for this message
Eike (eike-welk) wrote :

The relation between the "indentedBlock" Parser and Siml's "statement" parser are quite intricate and the issue is not easy to solve. It is probably necessary to implement a special version of "indentedBlock" which works better with Siml.

A parser that matches anything has been inserted as the last possible match for statements. For wrong keywords it works as intended; but for broken expressions, error messages become much worse. Somewhere in "indentedBlock" there is backtracking. The line:
    h = V/A_bott+
Now leads to the error message "Unknown syntax: "h" ", it reports the error at the beginning of the statement, instead of the end.
The ideal error would have been: "Unknown syntax: "+" "
The old error was: "Expected end of line ... (line:7, col:21)" which points to the right character.

Here are the snippets that were inserted into Siml's parser:
...(in classParser)
    def _action_error_stmt(self, s, loc, toks):
        '''
        Raise a ParseFatalException, because a syntax error was detected.
        '''
        err_word = toks[0]
        msg = 'Unknown syntax: "%s"' % err_word
        raise ParseFatalException(s, loc, msg, elem=None )
...
...(in def _defineLanguageSyntax)
        #Generate an error when no statement parser could match the text
        error_stmt = CharsNotIn(whitespace_chars) .setParseAction(self._action_error_stmt)
...
        #Statement: one line of code, or a compound (if, class, func) statement
        statement = ( simple_stmt + newline
                    | stmt_list_1 + newline
                    | compound_stmt
                    | error_stmt
                    )

description: updated
tags: added: parser
Eike (eike-welk)
Changed in freeode:
milestone: 0.4.1 → none
Eike (eike-welk)
description: updated
Eike (eike-welk)
Changed in freeode:
assignee: Eike (eike-welk) → nobody
description: updated
Eike (eike-welk)
Changed in freeode:
milestone: none → 0.4.2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Related blueprints

Remote bug watches

Bug watches keep track of this bug in other bug trackers.