Beispiel #1
0
def main(debug=False):

    # Grammar is defined using textual specification based on PEG language.
    # Load grammar form file.
    calc_grammar = open(os.path.join(os.path.dirname(__file__),
                                     'calc_clean.peg'), 'r').read()

    # First we will make a parser - an instance of the calc parser model.
    # Parser model is given in the form of PEG notation therefore we
    # are using ParserPEG class. Root rule name (parsing expression) is "calc".
    parser = ParserPEG(calc_grammar, "calc", debug=debug)

    # An expression we want to evaluate
    input_expr = "-(4-1)*5+(2+4.67)+5.89/(.2+7)"

    # Then parse tree is created out of the input_expr expression.
    parse_tree = parser.parse(input_expr)

    # The result is obtained by semantic evaluation using visitor class.
    # visit_parse_tree will start semantic analysis.
    # In this case semantic analysis will evaluate expression and
    # returned value will be evaluated result of the input_expr expression.
    result = visit_parse_tree(parse_tree, CalcVisitor(debug=debug))

    # Check that result is valid
    assert (result - -7.51194444444) < 0.0001

    print(f"{input_expr} = {result}")
Beispiel #2
0
def test_regex_with_empty_successful_match_in_repetition() -> None:
    grammar = \
        """
        rule = (subexpression)+
        subexpression = r'^.*$'
        """
    parser = ParserPEG(grammar, "rule")
    parsed = parser.parse("something simple")

    assert parsed.rule_name == "rule"
    assert parsed.subexpression.rule_name == "subexpression"
Beispiel #3
0
def test_issue_16() -> None:
    """
    >>> test_issue_16()

    """

    parser = ParserPEG(grammar, "calc", skipws=False)

    input_expr = """public function __construct( )"""
    parse_tree = parser.parse(input_expr)

    # Do semantic analysis. Do not use default actions.
    asg = parser.getASG(sem_actions=sem_actions, defaults=False)
    assert asg
Beispiel #4
0
def main(debug=False):
    # First we will make a parser - an instance of the CVS parser model.
    # Parser model is given in the form of clean PEG description therefore we
    # are using ParserPEG class from arpeggio.clenapeg.  Grammar is loaded from
    # csv.peg file Skipping of whitespace will be done only for tabs and
    # spaces. Newlines have semantics in csv files. They are used to separate
    # records.
    current_dir = os.path.dirname(__file__)
    csv_grammar = open(os.path.join(current_dir, 'csv.peg'), 'r').read()
    parser = ParserPEG(csv_grammar, 'csvfile', ws='\t ', debug=debug)

    # Creating parse tree out of textual input
    test_data = open(os.path.join(current_dir, 'test_data.csv'), 'r').read()
    parse_tree = parser.parse(test_data)

    # Create list of lists using visitor
    csv_content = visit_parse_tree(parse_tree, CSVVisitor())
    print("This is a list of lists with the data from CSV file.")
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(csv_content)
Beispiel #5
0
def main(debug=False):

    current_dir = os.path.dirname(__file__)

    # Load grammar
    robot_grammar = open(os.path.join(current_dir, 'robot.peg')).read()

    # First we will make a parser - an instance of the robot parser model.
    # Parser model is given in the form of PEG specification therefore we
    # are using ParserPEG class.
    parser = ParserPEG(robot_grammar, 'robot', debug=debug)

    # Load program code
    robot_program = open(os.path.join(current_dir, 'program.rbt')).read()

    # We create a parse tree out of textual input
    parse_tree = parser.parse(robot_program)

    # visit_parse_tree will start semantic analysis.
    # In this case semantic analysis will evaluate expression and
    # returned value will be the final position of the robot.
    return visit_parse_tree(parse_tree, RobotVisitor(debug=debug))
Beispiel #6
0
def test_issue_22() -> None:
    """
    Infinite recursion during resolving of a grammar given in a clean PEG
    notation.
    """
    current_dir = os.path.dirname(__file__)

    grammar1 = open(os.path.join(current_dir, 'grammar1.peg')).read()
    parser1 = ParserPEG(grammar1, 'belang')
    parser1.parse('a [0]')
    parser1.parse('a (0)')

    grammar2 = open(os.path.join(current_dir, 'grammar2.peg')).read()
    parser2 = ParserPEG(grammar2, 'belang', debug=True)
    parser2.parse('a [0](1)[2]')
Beispiel #7
0
def try_grammar(peg: str) -> None:
    p = ParserPEG(peg, 'letters', debug=False)
    p.parse(""" { a b } """)
    p.parse(""" { b a } """)