Exemplo n.º 1
0
def test_succeed(parser: ParserElement,
                 text: str,
                 target: str = None,
                 skip_target: bool = False,
                 show_raw: bool = False,
                 verbose: bool = False) -> None:
    log.critical("Testing to succeed: " + text)
    if target is None:
        target = text
    try:
        p = parser.parseString(text, parseAll=True)
        log.debug("Success: {} -> {}", text, text_from_parsed(p))
        if show_raw:
            log.debug("... raw: {}", p)
        if verbose:
            log.debug("... dump:\n{}", p.dump())
    except ParseException as exception:
        log.debug("ParseException on: {}\n... parser: {}", text, parser)
        print(statement_and_failure_marker(text, exception))
        raise
    if not skip_target:
        intended = standardize_for_testing(target)
        raw = text_from_parsed(p)
        actual = standardize_for_testing(raw)
        if intended != actual:
            raise ValueError(f"Failure on: {text}\n"
                             f"-> Raw output:\n"
                             f"{raw!r}\n"
                             f"-> Standardized output:\n"
                             f"{actual!r}\n"
                             f"... should have been:\n"
                             f"{intended!r}\n"
                             f"... parser: {parser}\n"
                             f"... as list: {p.asList()!r}]")
Exemplo n.º 2
0
def string_to_ast(rule: ParserElement, input_: str) -> Dict:
    """
    Convert the input string "input_" into an AST, according to "rule"

    :param rule:
    :param input_:
    :return: a dictionary conforming the AST (the format changes from rule to rule)
    """
    def clean_str(us):
        # "En dash"                 character is replaced by minus (-)
        # "Left/Right double quote" character is replaced by double quote (")
        # "Left/Right single quote" character is replaced by single quote (')
        # "€"                       character is replaced by "eur"
        # "$"                       character is replaced by "usd"
        return us.replace(u'\u2013', '-'). \
            replace(u'\u201d', '"').replace(u'\u201c', '"'). \
            replace(u'\u2018', "'").replace(u'\u2019', "'"). \
            replace('€', 'eur'). \
            replace('$', 'usd')

    res = rule.parseString(clean_str(input_), parseAll=True)
    res = res.asList()[0]
    while isinstance(res, list):
        res = res[0]
    return res
Exemplo n.º 3
0
def test_fail(parser: ParserElement, text: str, verbose: bool = True) -> None:
    if verbose:
        log.critical("Testing to fail: {}", text)
    try:
        p = parser.parseString(text, parseAll=True)
        raise ValueError(f"Succeeded erroneously (no ParseException): {text}\n"
                         f"-> structured format: {p}\n"
                         f"-> Raw text output: {text_from_parsed(p)}\n"
                         f"... parser: {parser}]")
    except ParseException:
        log.debug("Correctly failed: {}", text)
Exemplo n.º 4
0
def test(parser: ParserElement, text: str) -> None:
    print(f"STATEMENT:\n{text}")

    # scanned = parser.scanString(text)
    # for tokens, start, end in scanned:
    #     print(start, end, tokens)

    try:
        tokens = parser.parseString(text, parseAll=True)
        print(tokens.asXML())
        # print(tokens.dump())
        # print(text_from_parsed(tokens))
        print(f"tokens = {tokens}")
        d = tokens.asDict()
        for k, v in d.items():
            print(f"tokens.{k} = {v}")
        for c in tokens.columns:
            print(f"column: {c}")
    except ParseException as err:
        print(statement_and_failure_marker(text, err))
        raise
    print()