xbc: improve parsing

current
Síle Ekaterin Liszka 2024-01-12 14:46:08 -08:00
parent 91a1434b49
commit be69396f69
Signed by: VulpineAmethyst
SSH Key Fingerprint: SHA256:VcHwQ6SUfi/p0Csfxe3SabX/TImWER0PhoJqkt+GlmE
1 changed files with 12 additions and 13 deletions

View File

@ -112,8 +112,8 @@ key_fragment = Word(alphas + nums + '_-')
key = DelimitedList(key_fragment, delim='.', combine=True) key = DelimitedList(key_fragment, delim='.', combine=True)
bareval = CharsNotIn(' {}#=+:;,\n\'"') bareval = CharsNotIn(' {}#=+:;,\n\'"')
strvals = QuotedString("'", multiline=True, unquote_results=False) strvals = QuotedString("'", multiline=True, unquote_results=True)
strvald = QuotedString('"', multiline=True, unquote_results=False) strvald = QuotedString('"', multiline=True, unquote_results=True)
value = bareval | strvald | strvals value = bareval | strvald | strvals
assign = Literal('=') assign = Literal('=')
@ -123,6 +123,7 @@ op = assign | update | append
semi = Literal(';').suppress() semi = Literal(';').suppress()
lbrace = Literal('{').suppress() lbrace = Literal('{').suppress()
rbrace = Literal('}').suppress() rbrace = Literal('}').suppress()
terminal = Word(';\n').suppress()
NL = Literal('\n').suppress() NL = Literal('\n').suppress()
WS = Word(' \t').suppress() WS = Word(' \t').suppress()
@ -134,23 +135,18 @@ values = Group(value + ZeroOrMore(Literal(',').suppress() + Optional(WS_NL) + va
keyvalue = Group(key + Optional(WS) + op + Optional(WS) + values, aslist=True) keyvalue = Group(key + Optional(WS) + op + Optional(WS) + values, aslist=True)
keyvalue.set_parse_action(lambda x: KeyValue(x)) keyvalue.set_parse_action(lambda x: KeyValue(x))
key_stmt = Optional(WS) + key + Optional(assign).suppress() + Optional(WS) key_stmt = key + Optional(WS) + Optional(assign).suppress() + Optional(WS)
key_stmt.set_parse_action(lambda x: Key(x)) key_stmt.set_parse_action(lambda x: Key(x))
kv_stmt = Optional(WS) + keyvalue + Optional(WS)
block = Forward() block = Forward()
statement = kv_stmt | key_stmt statement = (keyvalue | key_stmt)
term_stmt = kv_stmt | key_stmt + Optional(semi) + Optional(WS) statements = DelimitedList(statement, delim=terminal)
segment = Group(OneOrMore(block | statements), aslist=True)
line_statements = Group(term_stmt + ZeroOrMore(NL + term_stmt)) block << Group(key + Optional(WS) + lbrace + segment + rbrace + Optional(NL))
term_stmt_list = Group(statement + ZeroOrMore(semi + statement))
segment = OneOrMore(block | term_stmt_list | line_statements)
block << Group(key + Optional(WS) + lbrace + Group(segment, aslist=True) + rbrace + Optional(NL))
block.set_parse_action(lambda x: Block(x)) block.set_parse_action(lambda x: Block(x))
data = OneOrMore(block | (term_stmt + Optional(NL))) data = OneOrMore(segment)
XBCParser = data XBCParser = data
@ -214,6 +210,9 @@ def parse_block(key, seq):
if k not in ret: if k not in ret:
ret[k] = False ret[k] = False
if not isinstance(value, list):
value = [value]
ret.update(parse_block(k, value)) ret.update(parse_block(k, value))
return ret return ret