Beispiel #1
0
from typing import Tuple

from pyparsing import Word, Literal, nums, alphanums, OneOrMore, Optional,\
    SkipTo, ParseException, Group, Combine, delimitedList, quotedString,\
    nestedExpr, ParseResults, oneOf, ungroup, Keyword

# define punctuation - reuse of expressions helps packratting work better
LPAR, RPAR, LBRACK, RBRACK, COMMA, EQ = map(Literal, "()[],=")

# Qualifier to go in front of type in the argument list (unsigned const int foo)
qualifier = OneOrMore(
    Keyword('const') ^ Keyword('typename') ^ Keyword('struct')
    ^ Keyword('enum'))
qualifier = ungroup(qualifier.addParseAction(' '.join))


def turn_parseresults_to_list(s, loc, toks):
    return ParseResults(normalise_templates(toks[0].asList()))


def normalise_templates(toks):
    s_list = ['<']
    s_list_append = s_list.append  # lookup append func once, instead of many times
    for tok in toks:
        if isinstance(tok, str):  # See if it's a string
            s_list_append(' ' + tok)
        else:
            # If it's not a string
            s_list_append(normalise_templates(tok))
    s_list_append(' >')
    return ''.join(s_list)
Beispiel #2
0

function.addParseAction(function_parse_action)

#########
# Block #
#########


def block_parse_action(instring, tokensStart, retTokens):
    return types.Block(list(retTokens))


block = OneOrMore(expr + (LineEnd() | Literal(';')).suppress())

block.addParseAction(block_parse_action)

###########
# Comment #
###########
comment = (cppStyleComment | pythonStyleComment).suppress()

expr << (string | number | function | comment)

script = StringStart() + block + StringEnd()


def decode(s: str):
    try:
        result = script.parseString(s)
    except PyParsingParseException as e: