I found several topics about pyparsing. They are dealing with almost the same problem in parsing nested loop, but even with that, i can't find a solution to my errors.
I have the following format :
key value;
header_name "optional_metadata"
{
key value;
sub_header_name
{
key value;
};
};
key value;
- Key is alphanum
- Value may be type of Int, String, with alphanum + "@._"
- key/value may be after a brace block
- key/value may be in the file before the first brace block
- key/value before or after a brace block are optionals
- header may have a name
- Closing brace is followed by a semi-colon
I used the following parser:
VALID_KEY_CHARACTERS = alphanums
VALID_VALUE_CHARACTERS = srange("[a-zA-Z0-9_\"\'\-\.@]")
lbr = Literal( '{' ).suppress()
rbr = Literal( '}' ).suppress() + Literal(";").suppress()
expr = Forward()
atom = Word(VALID_KEY_CHARACTERS) + Optional(Word(VALID_VALUE_CHARACTERS))
pair = atom | lbr + OneOrMore( expr ) + rbr
expr << Group( atom + pair )
When i use it, i got only the "header_name" and "header_metadata", i modified it, and i got only key/value inside a brace, python exception is triggered to show a parsing error (it expects '}' when reaching the sub_header_name.
Anyone can help me to understand why ? Thank you.