christoph2/pyA2L

Parser fails on multidimensional arrays

henkelma opened this issue · 3 comments

DATA='''/begin CHARACTERISTIC TEST[0][0].TEST
         "TEST[FL,0].TEST"
         VALUE
         0x0003237C
         _HELLO
         30
         _TEST
         -18
         12
         DISPLAY_IDENTIFIER TEST[FL_0].TEST
         FORMAT "%3.1"
      /end CHARACTERISTIC'''

parser = ParserWrapper('a2l', 'characteristic', A2LListener, debug = False)
session = parser.parseFromString(DATA)

Fails with:
line 1:29 mismatched input '[' expecting STRING

Probably caused by:

partialIdentifier:
    i = IDENT (a = arraySpecifier)?
    ;

in a2l.g4

Man, you are really fast. Thanks a lot for caring.

In the meantime I started working on a patch. Your change only fixes it partly, because the additional array dimensions get lost in the identifier. As far as I can tell it additionally needs to record the array identifiers with something like (a += arraySpecifier)* and also

    def exitPartialIdentifier(self, ctx):
        text = ctx.i.text if ctx.i else None
        arr = ctx.a.value if ctx.a else None
        if arr is not None:
            ctx.value = "{}[{}]".format(text, arr)
        else:
            ctx.value = text

in a2l_listener.py needs to be updated accordingly.

Do you think my assumptions are correct, would you like me to fix it?

OK, here are my suggested changes:

a2l.g4:

(a += arraySpecifier)*

a2l_listener.py:

    def exitPartialIdentifier(self, ctx):
        text = ctx.i.text if ctx.i else None

        result = ""
        for element in ctx.a:
            result += "[{}]".format(element.value)

        ctx.value = text + result

This works for my a2l file. I'm just unsure about the corner case with empty text. Maybe it is better to assign "" (an empty string) instead of None to text.

Great!
I've just added your changes.