Parsing a TCL-like text

旧巷老猫 提交于 2019-12-05 14:34:30

Here's my progress so far. It doesn't parse raw blobs, but everything else seems right.

LBRA = Literal("{").suppress()
RBRA = Literal("}").suppress()
EOL = lineEnd.suppress()
tmshString = Word(alphanums + '!#$%&()*+,-./:;<=>?@[\]^_`|~')

tmshValue = Combine( tmshString | dblQuotedString.setParseAction( removeQuotes ))
tmshKey = tmshString

def toSet(s, loc, t):
    return set(t[0])

tmshSet = LBRA + Group(ZeroOrMore(tmshValue.setWhitespaceChars(' '))).setParseAction(toSet) + RBRA

def toDict(d, l):
    if not l[0] in d:
        d[l[0]] = {}

    for v in l[1:]:
        if type(v) == list:
            toDict(d[l[0]],v)
        else:
            d[l[0]] = v

def trueDefault(s, loc, t):
    return len(t) and t or True

singleKeyValue = Forward()
singleKeyValue << (
            Group(
                tmshKey +  (
                            # A toggle value (i.e. key without value).
                            EOL.setParseAction(trueDefault) |
                            # A set of values on a single line.
                            tmshSet |
                            # A normal value or another singleKeyValue group.
                            Optional(tmshValue | LBRA + ZeroOrMore(singleKeyValue) + RBRA).setParseAction(trueDefault)
                           )
            )
)

multiKeysOneValue = Forward()
multiKeysOneValue << (
            Group(
                tmshKey + (
                            multiKeysOneValue | 
                            tmshSet  |
                            LBRA + ZeroOrMore(singleKeyValue) + RBRA
                          )
            )
)



toplevel = OneOrMore(multiKeysOneValue)

# now parse data and print results
data = toplevel.parseString(testData)

h = {}
map(lambda x:toDict(h, x), data.asList())
pprint(h)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!