def _get_parser(strict):
    """Ensure the module-level peg parser is created and return it."""
    global _strict_parser, _permissive_parser

    # Each branch below only acquires the lock if the global is unset.

    if strict:
        if _strict_parser is None:
            with _parser_create_lock:
                if _strict_parser is None:
                    _strict_parser = ParserPEG(
                        canonical, root_rule_name='version', skipws=False
                    )

        return _strict_parser
    else:
        if _permissive_parser is None:
            with _parser_create_lock:
                if _permissive_parser is None:
                    _permissive_parser = ParserPEG(
                        permissive,
                        root_rule_name='version',
                        skipws=False,
                        ignore_case=True,
                    )

        return _permissive_parser
Beispiel #2
0
def solve(file, verbose):
    simple_parser = ParserPEG(simple_grammar, root_rule_name='expr')
    advanced_parser = ParserPEG(advanced_grammar, root_rule_name='expr')
    simple_sum = 0
    advanced_sum = 0

    for line in file:
        parse_tree = simple_parser.parse(line)
        simple_sum += visit_parse_tree(parse_tree, SimpleVisitor())
        parse_tree = advanced_parser.parse(line)
        advanced_sum += visit_parse_tree(parse_tree, AdvancedVisitor())

    print('Part 1:', simple_sum)
    print('Part 2:', advanced_sum)
Beispiel #3
0
def main(debug=False):

    # 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, "document", debug=debug)

    filename = sys.argv[1]
    with open(filename) as file:
        contents = file.read()

    # An expression we want to evaluate
    input_expr = "<tag>dgdg</tag>"
    input_expr = contents

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

    #result = parser.getASG(sem_actions)

    #print( result )
    print(parse_tree)
    if debug:
        # getASG will start semantic analysis.
        # In this case semantic analysis will evaluate expression and
        # returned value will be evaluated result of the input_expr expression.
        # Semantic actions are supplied to the getASG function.
        print("{} = {}".format(input_expr, result))
Beispiel #4
0
def parse(filename, debug=False):

    
    with open(filename) as file:
        contents = file.read()
        
    parser = ParserPEG(calc_grammar, "start", True)
    
    parse_tree = parser.parse(contents)

    visitor = Visitor(debug=debug)
    
    
    result = visit_parse_tree(parse_tree, visitor )
    
    #Try to get if we found anything or not
    
    if ( visitor.functions or visitor.classes):
        print (visitor.namespace )
    
        return visitor
        
    print ("Empty")
    
    return None
Beispiel #5
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.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("{} = {}".format(input_expr, result))
Beispiel #6
0
def construct( arpeg_node ) :
    
        

with open( "apl_grammar.txt", "rt", encoding='utf8' ) as f_in :
    funpy_grammar = f_in.read()

parser = ParserPEG(funpy_grammar, "program",
                   reduce_tree=True,
                   debug=1)

parser.debug = 1
#def test( parser ) :
    #parser.parse("R")
#result = parser.parse("⌽A") OK
#result = parser.parse("⌽[1]A") # OK
result = parser.parse("⊖⌽A") # OK 

#result = parser.parse("") 
#result = parser.parse("") 
#result = parser.parse("") 
#result = parser.parse("") 
#result = parser.parse("") 
#result = parser.parse("") 
#result = parser.parse("") 
#result = parser.parse("") 
#result = parser.parse("") 

    
result
Beispiel #7
0
def interactive_testing():
    # %%
    parser = ParserPEG(peg_grammar, root_rule_name='search_expr')
    # %%
    parser.parse('fld:4 AND fld1:5')
    # %%
    parser.parse('fld:1 TO 2')
    # %%
    parser.parse('NOT fld:true')
    # %%
    parser.parse('price < 10 AND (category:Book OR NOT category:Ebook)')
    # %%
    parser.parse('country:Colombia OR country:USA')
    # %%
    d = parser.parse('country:Colombia OR country:USA')
    # %%
    parser.parse('NOT country:venezuela')
    # %%
    parser.parse(
        '(category:Book OR category:Ebook) AND NOT author:"JK Rowling"')
    # %%
    parser.parse("f1:v1 OR f2:v2")
    # %%
    parser.parse("NOT(f1:v1 OR f2:v2)")
    # %%
    parser.parse('x AND y OR z AND a OR b')
    # %%
    parser.parse('x OR y AND z OR a AND b')
    # %%
    parser.parse('(x OR y) AND (z OR a) AND b')
Beispiel #8
0
def main(debug=False):

    # 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", True)

    analyseASG( parser, input, Visitor(), True, False )

#    filename = sys.argv[1]
#    with open(filename) as file:
#        contents = file.read()
#    
#    # An expression we want to evaluate
    input_expr = """public function __construct( $rtt = hh ) { }"""
#    input_expr = contents
    input_expr = input

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

    #print( parse_tree )
    #print( result )
    if debug:
        # getASG will start semantic analysis.
        # In this case semantic analysis will evaluate expression and
        # returned value will be evaluated result of the input_expr expression.
        # Semantic actions are supplied to the getASG function.
        print("{} = {}".format(input_expr, result))
Beispiel #9
0
    def __init__(self, grammarPath = None, root: str = 'volpiano',
        strict: bool = False, **kwargs) -> None:
        """Initialize a Cantus Volpiano parser.

        Parameters
        ----------
        grammarPath : str, optional
            Path to the PEG grammar file, defaults to 
            ``grammars/cantus_volpiano.peg``
        root : str, optional
            Root element of the parser, by default 'volpiano'
        strict : bool, optional
            Whether to parse in strict mode or not. In strict mode, exceptions
            are raised whenever volpiano strings deviate from the standard 
            syntax. In non-strict mode, some deviations will be automatically 
            corrected. See :meth:`ParserCantusVolpiano.preprocess` for details.
            By default True
        **kwargs 
            Other keywords are passed to :class:`ParserPeg`.
        """
        if grammarPath == None:
            grammarPath = GRAMMAR_PATH
        if not os.path.exists(grammarPath):
            raise Exception(f'Grammar file ({ grammarPath }) does not exist')
        with open(grammarPath, 'r') as handle:
            grammar = handle.read()
        self.strict = strict
        self.parser = ParserPEG(grammar, root, skipws=False, **kwargs)
Beispiel #10
0
def get_rule_parser():
    grammar = '''
        rule = int ':' (str / seq ('|' seq)*)
        seq = int+
        str = '"' r'[^"]+' '"'
        int = r'[0-9]+'
    '''
    return ParserPEG(grammar, root_rule_name='rule')
Beispiel #11
0
def parse(input_expr, context, debug=False):
    with open(os.path.join(os.path.dirname(__file__), 'matrip.peg'), 'r') as f:
        grammar = f.read()
        parser = ParserPEG(grammar, "matrip", debug=debug)

    parse_tree = parser.parse(input_expr)
    visitor = MatripVisitor(debug=debug)
    visit_parse_tree(parse_tree, visitor)
    return generate_base_table(visitor.measures, visitor.expressions, context)
Beispiel #12
0
def runSmurf(program):
    parser = ParserPEG(grammar, "program", "comment", debug=False)
    parse_tree = parser.parse(program)
    ast = visit_parse_tree(parse_tree, NodeVisitor(debug=False))
    ast.accept(Interpreter())


# runSmurf("""
# let a = 5, b = 4 print(a+b)
# """)
def test_regex_with_empty_successful_match_in_repetition():
    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 #14
0
def runGrammar(program):
    parser = ParserPEG(grammar, "program", "comment", debug=False)
    tree = parser.parse(program)
    ast = visit_parse_tree(tree, VisitorClass(debug=False))
    ast.accept(Interpreter())


# runGrammar("""
# let f = fn (a) { a + 1 }
# print(f(3))
# """)
Beispiel #15
0
def 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 #16
0
def solve(file, verbose):
    parser = ParserPEG(grammar, root_rule_name='rules')
    parse_tree = parser.parse(file.read())
    rules = visit_parse_tree(parse_tree, BagVisitor())

    graph = nx.DiGraph()
    for rule in rules:
        for bag in rule.contains:
            graph.add_edge(rule.bag.name, bag.name, quantity=bag.quantity)

    print('Part 1:', len(nx.ancestors(graph, 'shiny gold')))
    print('Part 2:', count_bags(graph, 'shiny gold'))
Beispiel #17
0
def main(debug=False):

        
    filename = sys.argv[1]
    #debug = bool(sys.argv[2])
    
    with open(filename) as file:
        contents = file.read()
    #print (contents)
#    # An expression we want to evaluate
    input_expr = """class Application extends Container implements HttpKernelInterface, TerminableInterface, ResponsePreparerInterface {
        """
     
     
#    input_expr = input
    input_expr = contents
    
    if not input_expr:
        return None
    
    # 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, "start", True)


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

    visitor = Visitor(debug=debug)
    result = visit_parse_tree(parse_tree, visitor )
    
    print (visitor.namespace )
    #print (visitor.classes )
    #print (visitor.functions )

#    analyseASG( parser, input, Visitor(), True, False )


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

    #print( parse_tree )
    #print( result )
    if debug:
        # getASG will start semantic analysis.
        # In this case semantic analysis will evaluate expression and
        # returned value will be evaluated result of the input_expr expression.
        # Semantic actions are supplied to the getASG function.
        print("{} = {}".format(input_expr, result))
Beispiel #18
0
def parse(input_expr):
    """
       @param input_expr type str
       @return calcVisitor type CalcVisitor
     """

    parser = ParserPEG(calc_grammar, "calc")

    parse_tree = parser.parse(input_expr)

    calcVisitor = CalcVisitor()

    visit_parse_tree(parse_tree, calcVisitor)

    return calcVisitor
Beispiel #19
0
def solve(file, verbose):
    parser = ParserPEG(grammar, root_rule_name='passports', skipws=False)
    parse_tree = parser.parse(file.read())
    required = {'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'}

    valid_keys = 0
    valid_strict = 0
    schema = PassportSchema()

    for passport in visit_parse_tree(parse_tree, PassportVisitor()):
        if required <= passport.keys():
            valid_keys += 1

        if not schema.validate(passport):
            valid_strict += 1

    print('Part 1:', valid_keys)
    print('Part 2:', valid_strict)
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 #21
0
def main(debug=False):

    # 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", True)

    # 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)

    result = parser.getASG(sem_actions)

    if debug:
        # getASG will start semantic analysis.
        # In this case semantic analysis will evaluate expression and
        # returned value will be evaluated result of the input_expr expression.
        # Semantic actions are supplied to the getASG function.
        print("{} = {}".format(input_expr, result))
Beispiel #22
0
    def __init__(self,
                 grammarPath: str = GRAMMAR_PATH,
                 root: str = 'text',
                 **kwargs) -> None:
        """Initialize a Cantus Text parser.

        Parameters
        ----------
        grammarPath : str, optional
            Path to the PEG grammar file, defaults to 
            ``cantus/cantus_text.peg``
        root : str, optional
            Root element of the parser, by default 'text'
        **kwargs 
            Other keywords are passed to :class:`ParserPeg`.
        """
        if not os.path.exists(grammarPath):
            raise Exception(f'Grammar file ({ grammarPath }) does not exist')
        with open(grammarPath, 'r') as handle:
            grammar = handle.read()
        self.parser = ParserPEG(grammar, root, skipws=False, **kwargs)
Beispiel #23
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 #24
0
    def __init__(self,
                 grammarPath: str = GRAMMAR_PATH,
                 root: str = 'file',
                 **kwargs) -> None:
        """
        Args:
            grammar_path (:obj:`str`, optional): path to the grammar file 
                (default is gabc/gabc.peg)
            root (:obj:`str`, optional): the root element of the parser 
                (default is 'file')
        """
        if not os.path.exists(grammarPath):
            raise Exception(f'Grammar file ({ grammarPath }) does not exist')

        with open(grammarPath, 'r') as handle:
            grammar = handle.read()

        self.parser = ParserPEG(grammar,
                                root,
                                skipws=False,
                                memoization=True,
                                **kwargs)
Beispiel #25
0
def main(debug=False):
    # First we will make a parser - an instance of the calc parser model.
    # Parser model is given in the form of python constructs therefore we
    # are using ParserPython class.
    with open('calc.peg', 'r') as file:
        data = file.read()
    parser = ParserPEG(data, "calc")

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

    # We create a parse tree out of textual input_expr
    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("{} = {}".format(input_expr, result))
Beispiel #26
0
def main():
    with open('java2.peg', 'r') as file:
        data = file.read()
    parser = ParserPEG(data, "java")

    #    ok_samples = [
    #        "class Baz extends Korv implements Runnable, Comparable {}",
    #        "class Bar implements Runnable, Comparable {}",
    #        "class Korv extends Fisk {}",
    #        "class Knas {}",
    #        "class Foo implements Runnable {}",
    # "class HejA { int foo() {} }",
    #"class HejB { int foo(String apaB) {} }",
    #"class HejC { int foo(String apaC, int korvC) {} }",
    #"class HejD { int foo(String apaD, int korvD, Object raketD) {} }",
    #"class HejE { int foo() {}  void bar(){} }",
    #"class HejF extends Knas { int foo() {}  void bar(){} }",
    #"class HejG extends Knas implements Runnable { int foo() {}  void bar(){} }",
    #    ]
    #    fail_samples = [
    #        "class {}",
    #        "class Knas extends implements Runnable {}",
    #        "class Foo implements {}",
    #    ]
    ok_samples = [
        "sampleinputs/SampleD.java",
        "sampleinputs/SampleB.java",
        "sampleinputs/SampleC.java",
        "sampleinputs/SampleA.java",
    ]
    for input_file in ok_samples:
        with open(input_file, 'r') as file:
            input_expr = file.read()
        parse_tree = parser.parse(input_expr)
        result = visit_parse_tree(parse_tree, CalcVisitor(debug=False))
        print(str(result[0]))
relop   = '==' / '!=' / '>=' / '>' / '<=' / '<'

primary    = integer / function_call / variable_reference / "(" arithmetic_expression ")"

function_call  = variable_reference "(" call_arguments ")" / "print" "(" call_arguments ")"

call_arguments = (expr ("," expr)*)?

function_definition = param_list brace_block

param_list =  "(" identifier ("," identifier)* ")" / "(" ")"

brace_block = "{" code  "}"
"""
with open(argv[1]) as file:
    filePath = file.read()

#PARSERPG WENT ALONG BEST WITH MY SYTAX FOR THE GRAMMER

#PARSERS THROUGHT THE WHOLE SYTAX
parser = ParserPEG(grammer, "program", "comment")

#THIS IS THE PARSING FOR THE FILE
parse_tree = parser.parse(filePath)

#THIS IS THE BUILDING OF ASTTREE
ast = visit_parse_tree(parse_tree, Visitor())

#CALLS INTERPRETER
ast.accept(Interpreter())
Beispiel #28
0
    release = int (dot int)*
    pre = sep? pre_tag pre_post_num?
    pre_tag = "c" / "rc" / "alpha" / "a" / "beta" / "b" / "preview" / "pre"
    post = sep? post_tag pre_post_num?
    post_implicit = "-" int
    post_tag = "post" / "rev" / "r"
    pre_post_num = sep? int
    dev = sep? "dev" int?
    local = "+" r'([a-zA-Z0-9]+([-_\.][a-zA-Z0-9]+)*)'
    sep = dot / "-" / "_"
    dot = "."
    int = r'[0-9]+'
    alpha = r'[a-zA-Z0-9]'
'''

_strict_parser = ParserPEG(canonical, root_rule_name='version', skipws=False)
_permissive_parser = ParserPEG(
    permissive, root_rule_name='version', skipws=False, ignore_case=True)


def unwrap_token(value):
    if isinstance(value, Token):
        return value.value
    return value


@attr.s
class Token(object):
    value = attr.ib()

from arpeggio import visit_parse_tree
from arpeggio.cleanpeg import ParserPEG
from nose.tools import assert_equal

from calculette_impots_m_language_parser.scripts import m_to_ast

script_dir_path = os.path.dirname(os.path.abspath(__file__))
distribution_dir_path = pkg_resources.get_distribution(
    'calculette_impots_m_language_parser').location
m_grammar_file_path = os.path.join(distribution_dir_path,
                                   'm_language.cleanpeg')

with open(m_grammar_file_path) as m_grammar_file:
    m_grammar = m_grammar_file.read()
m_parser = m_to_ast.m_parser = ParserPEG(m_grammar,
                                         'm_source_file',
                                         reduce_tree=False)


class args(object):
    debug = False
    no_order = False


m_to_ast.args = args


def test_smoke():
    smoke_m_file_path = os.path.join(script_dir_path, 'valid_formulas.m')

    with open(smoke_m_file_path) as smoke_m_file:
Beispiel #30
0
import os
import sys

from simulator import Simulator
from exceptions import SimulationError

MAX_REG = 15
if len(sys.argv) == 2:
    MAX_REG = int(sys.argv[1])

# Load the parser with the correct grammar
from arpeggio.cleanpeg import ParserPEG, NoMatch, PTNodeVisitor, visit_parse_tree

grammar = open(os.path.join(os.path.dirname(__file__), 'grammar.peg'), 'r').read()
parser = ParserPEG(grammar, "program", "comment", debug=False)


# A little visitor to insert instructions and lines informations in the tree
class SetInstructionVisitor(PTNodeVisitor):
    instruction = -1
    line = -1

    def __init__(self, instruction, line):
        self.instruction = instruction
        self.line = line
        super().__init__(self)

    def visit__default__(self, node, children):
        node.instruction = self.instruction
        node.line = self.line