def test_recognizer_explicit_get_collector_recognizer_for_unexisting_terminal(
):  # noqa
    """
    Test for situation when `get_collector` has a recognizer for un-existing
    terminal.
    """

    recognizer = get_collector()

    @recognizer
    def INT(input, pos):
        return re.compile(r'\d+').match(input[pos:])

    @recognizer
    def STRING(input, pos):
        return re.compile(r'\d+').match(input[pos:])

    @recognizer
    def STRING2(input, pos):
        return re.compile(r'\d+').match(input[pos:])

    grammar = Grammar.from_file(os.path.join(THIS_FOLDER, 'grammar.pg'),
                                recognizers=recognizer.all)
    parser = Parser(grammar)
    assert parser
Пример #2
0
def test_collector_can_use_unicode_in_python_2():

    action = get_collector()

    def f(context, node):
        return node

    action('f_action')(f)
Пример #3
0
def test_action_explicit_get_collector_missing_action():
    """
    Test when `get_collector` has a terminal without defined action nothing
    happens as the default implicit action will be used.
    """

    action = get_collector()

    @action
    def INT(context, value):
        return int(value)

    grammar = Grammar.from_file(os.path.join(THIS_FOLDER, 'grammar.pg'))
    Parser(grammar, actions=action.all)
def test_recognizer_explicit_get_collector_missing_recognizer():
    """
    Test when `get_collector` has a terminal without defined recognizer an
    exception is raised.
    """

    recognizer = get_collector()

    @recognizer
    def INT(input, pos):
        return re.compile(r'\d+').match(input[pos:])

    with pytest.raises(GrammarError,
                       match=r'Terminal "STRING" has no recognizer defined.'):
        Grammar.from_file(os.path.join(THIS_FOLDER, 'grammar.pg'),
                          recognizers=recognizer.all)
Пример #5
0
def test_action_explicit_get_collector():
    """
    Test the basic usage of `get_collector` API where we don't provide
    actions in a separate python module.
    """

    action = get_collector()

    @action
    def INT(context, value):
        return int(value)

    @action
    def STRING(context, value):
        return "#{}#".format(value)

    grammar = Grammar.from_file(os.path.join(THIS_FOLDER, 'grammar.pg'))
    Parser(grammar, actions=action.all)
def test_recognizer_explicit_get_collector():
    """
    Test the basic usage of `get_collector` API where we don't provide
    recognizers in a separate python module.
    """

    recognizer = get_collector()

    @recognizer
    def INT(input, pos):
        return re.compile(r'\d+').match(input[pos:])

    @recognizer
    def STRING(input, pos):
        return re.compile(r'\d+').match(input[pos:])

    grammar = Grammar.from_file(os.path.join(THIS_FOLDER, 'grammar.pg'),
                                recognizers=recognizer.all)
    parser = Parser(grammar)
    assert parser
Пример #7
0
def test_action_decorator():
    """
    Test collecting actions using action decorator.
    """

    action = get_collector()

    @action
    def number(_, value):
        return float(value)

    @action('E')
    def sum_act(_, nodes):
        return nodes[0] + nodes[2]

    @action('E')
    def pass_act_E(_, nodes):
        return nodes[0]

    @action
    def T(_, nodes):
        if len(nodes) == 3:
            return nodes[0] * nodes[2]
        else:
            return nodes[0]

    @action('F')
    def parenthesses_act(_, nodes):
        return nodes[1]

    @action('F')
    def pass_act_F(_, nodes):
        return nodes[0]

    grammar = get_grammar()
    p = Parser(grammar, actions=action.all)

    result = p.parse("""34.7+78*34 +89+
    12.223*4""")

    assert result == 34.7 + 78 * 34 + 89 + 12.223 * 4
Пример #8
0
def test_actions_explicit_get_collector_action_for_unexisting_terminal():
    """
    Test for situation when `get_collector` has an action for un-existing
    terminal.
    """

    action = get_collector()

    @action
    def INT(context, value):
        return int(value)

    @action
    def STRING(context, value):
        return "#{}#".format(value)

    @action
    def STRING2(context, value):
        return "#{}#".format(value)

    grammar = Grammar.from_file(os.path.join(THIS_FOLDER, 'grammar.pg'))
    Parser(grammar, actions=action.all)
Пример #9
0
from parglare import get_collector

recognizer = get_collector()


@recognizer('base.NUMERIC_ID')
def number(input, pos):
    '''Check override'''
    pass


@recognizer('base.COMMA')
def comma_recognizer(input, pos):
    if input[pos] == ',':
        return input[pos:pos + 1]
Пример #10
0
from parglare import get_collector

action = get_collector()


@action
def number(_, value):
    return float(value)
Пример #11
0
import parglare

action = parglare.get_collector()


@action
def program(_, nodes):
    return nodes[0]


@action
def paren(_, nodes):
    return nodes[1]


@action
def add(_, nodes):
    return nodes[0] + nodes[2]


@action
def mult(_, nodes):
    return nodes[0] * nodes[2]


@action
def num(_, nodes):
    return int(nodes[0])