def magic_parse_function(fld_name, source): from pyparsing import Keyword, nestedExpr # define parser parser = Keyword(fld_name).suppress() + nestedExpr('{', '}')("content") # search input string for matching keyword and following braced content matches = parser.searchString(source) # remove quotation marks return [[qs for qs in r[0].asList()] for r in matches]
def generate_commands_template_helper(self, sfile, dfile): open_brac = Optional(White()) + "{" + Optional(White()) close_brac = Optional(White()) + "}" + Optional(White()) actions_format = Keyword('actions') + open_brac + Word(alphas + "_", alphanums+"_")('default_action') \ + ";" + Regex(r'[^\}\{]*') + close_brac reads_format = Keyword('reads') + open_brac + Regex( r'[^\}\{]*') + close_brac table_format = Keyword('table') + Word(alphas+"_", alphanums+"_")('table_name') + open_brac \ + Optional(reads_format) + actions_format + \ Optional(reads_format) + Regex(r'[^\}\{]*') + close_brac sfile_str = sfile.read() res = table_format.searchString(sfile_str) for table in res: dfile.write('table_set_default %s %s\n' % (table.table_name, table.default_action))
def check_if_case_arg(code): statement = Keyword('case') if len(statement.searchString(code)): return True else: return False