I'm using Pyparsing to parse a log file which has blocks that look like this:
keyName0: foo
keyName1: bar
msgKey [Read]: 21 FA 00 34
msgKey [Read]:
MESSAGE 1 of 2
keyName0: keyValue0
keyName1: keyValue1
Flags1: No Flags Set
Flags1: 0
Flags2: No Flags Set
Flags2: 0
keyName6: $12AB34CD56EF (123456789)
keyName7: 7
keyName8: 7
Data [Read]: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38
msgKey [Read]: 01 02 03 04
msgKey [Read]:
MESSAGE 2 of 2
# same structure as message above
keyName3: keyValue3
keyName4 [IN]: keyValue4 (123 IN)
keyName4 [OUT]: keyValue4 (123 OUT)
I wrote a grammar for the keyName-Value-lines:
key_line = lineEnd + OneOrMore(Word(printables_no_column)).setParseAction(' '.join).setResultsName('keyName') + Suppress(':') \
+ OneOrMore(Word(printables_no_column), stopOn=lineEnd).setParseAction(' '.join).setResultsName('keyValue')
This grammar works fine for the individual lines. Now I tried to use this grammar to describe the grammar of the whole test data:
message = Forward()
key_line = lineEnd + OneOrMore(Word(printables_no_column)).setParseAction(' '.join).setResultsName('keyName') + Suppress(':') \
+ MatchFirst(message, OneOrMore(Word(printables_no_column),stopOn=lineEnd).setParseAction(' '.join).setResultsName('keyValue'))
key_lines = ZeroOrMore(Group(key_line)).setResultsName('keys')
message << Literal('MESSAGE') + number + Literal('of')
+ number.setResultsName('totalMsgs') + key_lines
However, I think that this grammar ends in infinite recursion. I need help to figure out how to use the Forward() recursive grammar properly. Many thanks in advance!