def _parse_func(text, is_output=False): if is_output: text = text[text.index(FUNCTION_START):text.index(FUNCTION_END)] funcs = {} # syntax we don't want to see in the final parse tree LPAR, RPAR, LBRACE, RBRACE, COMMA, SEMICOLON = map(Suppress, "(){},;") const = Keyword("const") dtype = Word(alphanums + "_*") identifier = Word(alphanums + "_") argDecl = Group( Optional(const("const")) + dtype("dtype") + Optional(identifier("name")) + Optional(COMMA)) args = Group(ZeroOrMore(argDecl)) if not is_output: func = Suppress("DVZ_EXPORT") else: func = Empty() func = func + \ dtype("out") + \ identifier("name") + \ LPAR + args("args") + RPAR + \ Optional(SEMICOLON) for item, start, stop in func.scanString(text): args = [] for i, entry in enumerate(item.args): args.append((entry.const, entry.dtype, entry.name)) funcs[item.name] = (item.out, tuple(args)) return funcs
def parse_line(attribute, string): Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword( '=')) + String('data') + Suppress(Literal(';') + Optional(Comments)) result, i, j = Grammar.scanString(string).next() return [int_else_float_except_string(s) for s in result['data'].asList()]
def parse_table(attribute, string): Line = OneOrMore(Float)('data') + Literal(';') + Optional(Comments, default='')('name') Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=') + Keyword('[') + Optional(Comments)) + OneOrMore(Group(Line)) + Suppress(Keyword(']') + Optional(Comments)) result, i, j = Grammar.scanString(string).next() _list = list() for r in result: _list.append([int_else_float_except_string(s) for s in r['data'].asList()]) return _list
def parse_line(attribute, string): Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=')) + String('data') + Suppress(Literal(';') + Optional(Comments)) result, i, j = Grammar.scanString(string).next() return [int_else_float_except_string(s) for s in result['data'].asList()]
def main(argv): """ """ group = OptionGroup("example", "OptionGroup Example", \ "Shows all example options", option_list = [ make_option("--example", action="store_true", dest="example", help="An example option."), ] ) parser = OptionParser("NAMES ...", description="A simple gobject.option example.", option_list = [ make_option("--file", "-f", type="filename", action="store", dest="file", help="A filename option"), # ... ]) parser.add_option_group(group) parser.parse_args() lineBody = SkipTo(lineEnd).setParseAction(mustBeNonBlank) # now define a line with a trailing lineEnd, to be replaced with a space character textLine = lineBody + Suppress(lineEnd).setParseAction(replaceWith(" ")) # define a paragraph, with a separating lineEnd, to be replaced with a double newline para = OneOrMore(textLine) + Suppress(lineEnd).setParseAction(replaceWith("\n\n")) # run a test test = """ Now is the time for all good men to come to the aid of their country. """ print para.transformString(test) subdir_dir = Word(alphas, alphanums+"_") #macroDef = Suppress("SUBDIRS") + Suppress("=") + restOfLine subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + delimitedList( subdir_dir, " ", combine=True ) macros = list(subdir_dir_list.searchString(subdirs_1line)) print("1: subdirs of subdirs_1line are {}".format(macros)) subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + delimitedList( subdir_dir, " ", combine=True ) macros = list(subdir_dir_list.searchString(subdirs_2line)) print("2: subdirs of subdirs_2line are {}".format(macros)) subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + delimitedList( subdir_dir, " ", combine=False) macros = list(subdir_dir_list.searchString(subdirs_2line)) print("2: subdirs of subdirs_2line are {}".format(macros)) subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + \ delimitedList( subdir_dir, " ", combine=False) + \ Suppress(lineEnd) macros = list(subdir_dir_list.searchString(subdirs_3line)) print("3: subdirs of subdirs_3line are {}".format(macros)) # This returns a list! subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + \ delimitedList( subdir_dir, " ", combine=False) + \ SkipTo(lineEnd) macros = list(subdir_dir_list.searchString(subdirs_3line)) print("3: subdirs of subdirs_3line are {}".format(macros)) # This returns nothing because SUBDIRS is commented out! subdir_dir_list = Suppress(lineStart) + Suppress("SUBDIRS") + \ Suppress("=") + delimitedList( subdir_dir, " ", combine=False) + \ SkipTo(lineEnd) macros = list(subdir_dir_list.searchString(subdirs_4line)) print("4: subdirs of subdirs_4line are {}".format(macros)) # This returns subdir_dir_list = Suppress(lineStart) + Suppress("SUBDIRS") + \ Suppress("=") + delimitedList( subdir_dir, OneOrMore(" "), combine=False) + \ SkipTo(lineEnd) macros = list(subdir_dir_list.searchString(subdirs_5line)) print("5: subdirs of subdirs_5line are {}".format(macros)) print("5a: subdirs of subdirs_5line are {}".\ format(subdir_dir_list.scanString(subdirs_5line))) print "group: example ", group.values.example print "parser: file", parser.values.file res = 1 try: pyparsing() print('{}'.format("pyparsing() done.")) lun_q = r'''Lun:\s*(\d+(?:\s+\d+)*)''' s = '''Lun: 0 1 2 3 295 296 297 298''' r = re.search(lun_q, s) luns = None if r: luns = r.group(1).split() # optionally, also convert luns from strings to integers luns = [int(lun) for lun in luns] print('luns: {}'.format(luns)) else: print('{}'.format("Got no Luns")) print('smthng: {}'.format(smthng())) print(re.findall(r"####(.*?)\s(.*?)\s####", string, re.DOTALL)) # Catches testsuite in [SUBDIRS = testsuite] print('1: {}'.format(re.findall(r'''^SUBDIRS =\s+(.*)''', \ subdirs_1line, re.MULTILINE))) # Catches ['SUBDIRS', 'testsuite', 'src'] in [SUBDIRS = testsuite src] # *** THIS IS THE 1! *** # Works for an print('2: {}'.format(re.findall(r'''=\s*(\w+(?:\s+\w+)*)\s*''', \ subdirs_2line, re.MULTILINE))) for iter in re.finditer(r'''(\w+)\s+''', \ subdirs_2line, re.MULTILINE | re.VERBOSE): print('iter: {}'.format(iter.span())) # Catches ['SUBDIRS', 'testsuite', 'src'] in [SUBDIRS = testsuite src] print('3: {}'.format(re.findall( r'''SUBDIRS\s*=\s*(\w+(?:\s+\w+)*)\s*''', subdirs_3line)) ) for iter in re.finditer(r'''SUBDIRS\s*=\s*(\w+(?:\s+\w+)*)\s*''', \ subdirs_3line, re.MULTILINE | re.VERBOSE): print('iter: {}'.format(iter.group())) # Catches ['SUBDIRS', 'testsuite', 'src'] in [SUBDIRS = testsuite src] print('4: {}'.format(re.findall( r'''^SUBDIRS\s*=\s*(\w+(?:\s+\w+)*)\s*''', subdirs_4line)) ) for iter in re.finditer(r'''^SUBDIRS\s*=\s*(\w+(?:\s+\w+)*)\s*''', \ subdirs_4line, re.MULTILINE | re.VERBOSE): print('iter: {}'.format(iter.group())) """ Reference: <http://stackoverflow.com/questions/8651347/ regex-to-match-a-capturing-group-one-or-more-times> regx = re.compile('(?:(?<= )|(?<=\A)|(?<=\r)|(?<=\n))' '(\d\d)(\d\d)?(\d\d)?' '(?= |\Z|\r|\n)') for s in (' 112233 58975 6677 981 897899\r', '\n123456 4433 789101 41586 56 21365899 362547\n', '0101 456899 1 7895'): print repr(s),'\n',regx.findall(s),'\n' """ res = 0 except Exception, err: sys.stderr.write("{}".format(err))