mako icon indicating copy to clipboard operation
mako copied to clipboard

Difficult to identify the location of an error of type: SyntaxException: Expected: \|,} at line: 3680 char: 2

Open rdan opened this issue 4 months ago • 1 comments

Hello, I am using mako to create a generator of files (.c, .h, ...). While implementing the templates, I faced the error: SyntaxException: Expected: \|,} at line: 3680 char: 2. It took me some time to identify where the issue was.

I finally identified its location: 99: #define EVADC_u32CFG${Cfg_Adc["INDEX"]}_${CfgrdGroup}_CFGD_FADCI_KHz ${CfgrdGroup["FADCI_KHz"] (there is a missing '}' at the end).

After searching in the code who is reporting this error, I found that "Lexer.parse_until_text()" is. The difficulty with the exception generated by this function is that it points to a location far far from the source of the problem. So, I did the following modifications in the function: 1/ I added the following lines at the start of the function:

        startlineno = self.matched_lineno
        startcharpos = self.matched_charpos

2/ I replaced the following code:

            raise exceptions.SyntaxException(
                "Expected: %s" % ",".join(text), **self.exception_kwargs
            )

by:

            self.matched_lineno = startlineno
            self.matched_charpos = startcharpos
            raise exceptions.SyntaxException(
                "Expected '%s' while parsing text starting" % ",".join(text), **self.exception_kwargs
            )

And now, the reported error message is: SyntaxException: Expected '\|,}' while parsing text starting at line: 99 char: 69

This way, at least I am able to identify the python block or expression that triggered the exception.

I don't know if this is the proper way to reach this goal, but at least I think it would be beneficial for every user of mako to be able to identify the python block/expression in which an error has been detected.

Ronan

rdan avatar Sep 17 '25 07:09 rdan

we accept pull requests for simple fixes like this.

zzzeek avatar Sep 19 '25 17:09 zzzeek