def parse_me(apm_parse_element, apm_test_data, apm_assert_flag=True, apm_debug=False, apm_verbosity=False): apm_parse_element.setDebug(flag=apm_debug) if apm_verbosity: print('\ntest_strings: %s' % apm_assert_flag) print('Parse Element: ', end='') print(apm_parse_element) print('Test Data: "%s":' % apm_test_data) try: # parseAll=True - raise ParseException if the grammar does not process # the complete input string greeting = apm_parse_element.parseString(apm_test_data, parseAll=True) x = greeting.asList() pp = PrettyPrinter(indent=2, width=66, compact=False) print("Result: ", pp.pprint(x)) if len(greeting) == 0: retsts = None else: retsts = greeting except ParseException as pe: print('ParseException:') print(pe.line) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) ParseException.explain(pe) retsts = None except ParseSyntaxException as pe: print('ParseSyntaxException:') print(apm_test_data) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) # print(parser_element.errmsg) retsts = None # raise InvalidConfiguration(error_msg) status = not not retsts if status == apm_assert_flag: print('assert(True)') return retsts else: errmsg = 'Error(assert=' + str( apm_assert_flag) + '): \"' + apm_test_data + '\".' raise SyntaxError(errmsg)
def _format_exc_msg(exc: ParseException): exc.loc += 2 # 2 because we append `${` at the start of expr below expr = exc.pstr exc.pstr = "${" + exc.pstr + "}" error = ParseException.explain(exc, depth=0) _, pointer, *explains = error.splitlines() pstr = "{brace_open}{expr}{brace_close}".format( brace_open=colorize("${", color="blue"), expr=colorize(expr, color="magenta"), brace_close=colorize("}", color="blue"), ) msg = "\n".join(explains) pointer = colorize(pointer, color="red") return "\n".join([pstr, pointer, colorize(msg, color="red", style="bold")])
def _format_exc_msg(exc: "ParseException"): from pyparsing import ParseException exc.loc += 2 # 2 because we append `${` at the start of expr below expr = exc.pstr exc.pstr = embrace(exc.pstr) error = ParseException.explain(exc, depth=0) _, pointer, *explains = error.splitlines() pstr = "{brace_open}{expr}{brace_close}".format( brace_open=colorize(BRACE_OPEN, color="blue"), expr=colorize(expr, color="magenta"), brace_close=colorize(BRACE_CLOSE, color="blue"), ) msg = "\n".join(explains) pointer = colorize(pointer, color="red") return "\n".join([pstr, pointer, colorize(msg, color="red", style="bold")])
def parse_dot_data(s): """Parse DOT description in (unicode) string `s`. @return: Graphs that result from parsing. @rtype: `list` of `pydot.Dot` """ global top_graphs top_graphs = list() try: graphparser = graph_definition() graphparser.parseWithTabs() tokens = graphparser.parseString(s) return list(tokens) except ParseException as err: if PY3 and (hasattr(ParseException, 'explain') and callable(getattr(ParseException, 'explain'))): print(ParseException.explain(err)) else: print(err.line + "\n" + " " * (err.column - 1) + "^\n" + str(err)) return None
def format_and_raise_parse_error(exc): msg = ParseException.explain(exc, depth=0) raise ParseError(msg)
def test_main(tm_parse_element): """ python_script # defaults to STDIN for input a_file (good for quick test or cut-n-paste) python_script -t # Exercise built-in unit test python_script <filespec> # Read a_file and syntax-check it python_script -v # Increase verbosity level python_script -d # Increase PyParsing debugging :return: """ pgm_basename = os.path.basename(sys.argv[0]) prgm_desc = 'Exercise or test the {} function in {} python script against ParserElement "{}"'.format( tm_parse_element.__class__.__name__, pgm_basename, tm_parse_element) parser = argparse.ArgumentParser(description=prgm_desc) parser.add_argument('-v', '--verbose', action='store_true', help='Run with extra messages') parser.add_argument('-d', '--debug', action='store_true', help='Run with PyParsing debugging enabled; outputs "Match/Matched" a_debug lines') if unix_pipe_support: default_arg1 = '-' else: default_arg1 = None # nagrs='?' is zero or one argument exactly parser.add_argument('filespec', type=argparse.FileType('r'), default=default_arg1, nargs='?', help='Input a_file to read and parse') args = parser.parse_args() if args.verbose: print('"Number of arguments: ', len(sys.argv)) print('The arguments are: ', str(sys.argv)) print('argparse.args:', args) retsts = 0 test_data = None if not unix_pipe_support: # test if file exist if args.filespec is None: retsts = errno.ENOTTY # Most people don't want UNIX pipe support elif not os.access(args.filespec.name, os.R_OK): print("Cannot read {} file. Exiting...".format(args.filespec.name)) # If no a_file given, we default to STDIN as a a_file to be opened # Naturally, you'll have to press Ctrl-D to close the a_file. if args.verbose: print('parser_element:', tm_parse_element) try: test_data = args.filespec.read() args.filespec.close() except Exception as pe: print('Exception:') print(pe) retsts = errno.EBADFD if test_data: if args.debug: tm_parse_element.setDebug() try: tm_parse_element.ignore(cppStyleComment) tm_parse_element.ignore(pythonStyleComment) result = tm_parse_element.parseString(test_data, parseAll=True) result_text = result.asList() print("Result: ", result_text) if len(result) == 0: retsts = errno.EBADE else: retsts = 0 except ParseException as pe: print('ParseException:') print(pe.line) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) ParseException.explain(pe) retsts = errno.ELIBSCN except ParseSyntaxException as pe: print('ParseSyntaxException:') print(test_data) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) retsts = errno.ELIBBAD else: retsts = errno.ENODATA if args.verbose: print("test_data is empty") # Build return values return_list = {} return_list['verbosity'] = args.verbose return_list['a_debug'] = args.debug return_list['filespec'] = args.filespec return_list['errcode'] = retsts return return_list
def assertParserResultDict(parser_element, test_strings, expected_results, assert_flag=True, message=''): """ A nice unit test tool which provides an assert()-like function that takes an string, parse the string, takes its computed Pythonized list/dict and compares the result against its expected Pythonized result. :param parser_element: ParserElement class to exercise :param test_strings: A string in which to be parsed by parser_element. Or it can be a list of strings ['a', 'b']. :param expected_results: A Python list in which to expect If a_test_data is a list, then this argument shall also be a list of expected result :param assert_flag: If True, then expected result must match or an exception gets raised. If False, then parse MUST fail or expected result does not match, else an exception gets raised :return: Always returns True (exception handles the False, like an assert() class would do) """ retsts = None def incr_pos(fn): def _inner(*args): global pos pos += 1 print("\t" * pos, end="") return fn(*args) return _inner def decr_pos(fn): def _inner(*args): global pos print("\t" * pos, end="") pos -= 1 return fn(*args) return _inner import pyparsing pyparsing._defaultStartDebugAction = incr_pos(pyparsing._defaultStartDebugAction) pyparsing._defaultSuccessDebugAction = decr_pos(pyparsing._defaultSuccessDebugAction) pyparsing._defaultExceptionDebugAction = decr_pos(pyparsing._defaultExceptionDebugAction) try: parser_element = parser_element.setDebug(True) result = parser_element.parseString(test_strings, parseAll=True) from pprint import PrettyPrinter pp = PrettyPrinter(indent=2, width=66, compact=False) if result.asDict() == {}: print('Dict() empty; BAD result:', end='') pp.pprint(result) retsts = False else: print('Good result:') pp.pprint(result.asDict()) # Convert ParserElement into Python List[] retsts = (result.asDict() == expected_results) print('expecting: ') pp.pprint(expected_results) except ParseException as pe: print('ParseException:') print(pe.line) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) ParseException.explain(pe) retsts = False except ParseSyntaxException as pe: print('ParseSyntaxException:') print(test_strings) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) # print(parser_element.errmsg) retsts = False # raise InvalidConfiguration(error_msg) if retsts == assert_flag: print('assert(True)') return True else: errmsg = 'Error(assert=' + str(False) + '): ' + message + '\"' + test_strings + '\".' raise SyntaxError(errmsg)
def assertParseElement(a_parse_element, a_test_data, a_expected_result, a_assert_flag=True): """ A nice unit test tool which provides an assert()-like function that takes an string, parse the string, takes its computed Pythonized list/dict and compares the result against its expected Pythonized result. :param a_parse_element: ParserElement class to exercise :param a_test_data: A string in which to be parsed by a_parse_element :param a_expected_result: A Python list in which to expect :param a_assert_flag: If True, then expected result must match or an exception gets raised. If False, then parse MUST fail or expected result does not match, else an exception gets raised :return: Always returns True (exception handles the False, like an assert() class would do) """ retsts = None try: a_parse_element = a_parse_element.setDebug(True) result = a_parse_element.parseString(a_test_data, parseAll=True) pp = PrettyPrinter(indent=2, width=66, compact=False) if result.asDict() == {}: print('***BAD***-Python-Dict result:', end='') pp.pprint(result) else: print('Good-Python-Dict result:') pp.pprint(result.asDict()) print('expecting: ') pp.pprint(a_expected_result) # Convert ParserElement into Python List[] and compare retsts = (result.asDict() == a_expected_result) except ParseException as pe: print('ParseException:') print(pe.line) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) ParseException.explain(pe) retsts = False except ParseBaseException as pe: print('ParseBaseException:') print(a_test_data) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) retsts = False except ParseSyntaxException as pe: print('ParseSyntaxException:') print(a_test_data) # affected data content print(' ' * (pe.column - 1) + '^') # Show where the error occurred print(pe) retsts = False if retsts == a_assert_flag: print('assert(True)') return True else: print('assert(***FALSE***)') errmsg = 'Error(assert=' + str(False) + '): \"' + a_test_data + '\".' raise SyntaxError(errmsg)
def parse(self, filepath: str) -> None: with open(filepath, "r") as filehandle: try: self.__grammar.parseFile(filehandle) except (ParseException, ParseFatalException) as e: raise ParserError("\n" + ParseException.explain(e, 0))