Esempio n. 1
0
    def test_grammar_one_or_more_dict_mismatch(self):
        grammar = Grammar(OneOrMoreDict(Sequence('WORD', 'NUMBER')))

        datas = [
            (
                [('WORD', 'foo', 5)],
                -1
            ),
            (
                [
                    ('WORD', 'foo', 5),
                    ('WORD', 'bar', 6)
                ],
                6
            ),
            (
                [
                    ('WORD', 'foo', 5),
                    ('NUMBER', '4', 6),
                    ('WORD', 'bar', 7),
                    ('WORD', 'fie', 8)
                ],
                8
            )
        ]

        self.parse_and_assert_mismatch(grammar, datas)
Esempio n. 2
0
    def test_grammar_one_or_more_dict(self):
        grammar = Grammar(OneOrMoreDict(Sequence('WORD', 'NUMBER')))

        datas = [([('WORD', 'foo'), ('NUMBER', '1')], {
            'foo': [['foo', '1']]
        }),
                 ([('WORD', 'foo'), ('NUMBER', '1'), ('WORD', 'bar'),
                   ('NUMBER', '2'), ('WORD', 'foo'), ('NUMBER', '3')], {
                       'foo': [['foo', '1'], ['foo', '3']],
                       'bar': [['bar', '2']]
                   })]

        self.parse_and_assert_tree(grammar, datas)
Esempio n. 3
0
    def grammar(self):
        version = Sequence('VERSION', 'STRING')

        ns = Sequence('NS_', ':', AnyUntil(Sequence(Any(), ':')))

        bs = Sequence('BS_', ':')

        nodes = Sequence('BU_', ':', ZeroOrMore('WORD'))

        signal = Sequence('SG_',
                          choice(Sequence('WORD', 'WORD'), Sequence('WORD')),
                          ':', 'NUMBER', '|', 'NUMBER', '@', 'NUMBER', '+/-',
                          '(', 'NUMBER', ',', 'NUMBER', ')', '[', 'NUMBER',
                          '|', 'NUMBER', ']', 'STRING', DelimitedList('WORD'))

        message = Sequence('BO_', 'NUMBER', 'WORD', ':', 'NUMBER', 'WORD',
                           ZeroOrMore(signal))

        environment_variable = Sequence('EV_', 'WORD', ':', 'NUMBER', '[',
                                        'NUMBER', '|', 'NUMBER', ']', 'STRING',
                                        'NUMBER', 'NUMBER', 'WORD', 'WORD',
                                        ';')

        comment = Sequence(
            'CM_',
            choice(Sequence('SG_', 'NUMBER', 'WORD', 'STRING'),
                   Sequence('BO_', 'NUMBER', 'STRING'),
                   Sequence('EV_', 'WORD', 'STRING'),
                   Sequence('BU_', 'WORD', 'STRING'), 'STRING'), ';')

        attribute_definition = Sequence(
            'BA_DEF_', Optional(choice('SG_', 'BO_', 'EV_',
                                       'BU_')), 'STRING', 'WORD',
            Optional(choice(DelimitedList('STRING'), ZeroOrMore('NUMBER'))),
            ';')

        attribute_definition_default = Sequence('BA_DEF_DEF_', 'STRING',
                                                choice('NUMBER', 'STRING'),
                                                ';')

        attribute = Sequence(
            'BA_', 'STRING',
            ZeroOrMore(
                choice(Sequence('BO_', 'NUMBER'),
                       Sequence('SG_', 'NUMBER', 'WORD'),
                       Sequence('BU_', 'WORD'), Sequence('EV_', 'WORD'))),
            choice('NUMBER', 'STRING'), ';')

        attribute_definition_rel = Sequence(
            'BA_DEF_REL_', Optional('BU_SG_REL_'), 'STRING', 'WORD',
            Optional(choice(DelimitedList('STRING'), OneOrMore('NUMBER'))),
            ';')

        attribute_definition_default_rel = Sequence('BA_DEF_DEF_REL_',
                                                    'STRING',
                                                    choice('NUMBER',
                                                           'STRING'), ';')

        attribute_rel = Sequence('BA_REL_', 'STRING', 'BU_SG_REL_', 'WORD',
                                 'SG_', 'NUMBER', 'WORD',
                                 choice('NUMBER', 'STRING'), ';')

        choice_ = Sequence('VAL_', Optional('NUMBER'), 'WORD',
                           ZeroOrMore(Sequence('NUMBER', 'STRING')), ';')

        value_table = Sequence('VAL_TABLE_', 'WORD',
                               ZeroOrMore(Sequence('NUMBER', 'STRING')), ';')

        signal_type = Sequence('SIG_VALTYPE_', 'NUMBER', 'WORD', ':', 'NUMBER',
                               ';')

        signal_multiplexer_values = Sequence(
            'SG_MUL_VAL_', 'NUMBER', 'WORD', 'WORD',
            DelimitedList(Sequence('NUMBER', 'NUMBER')), ';')

        message_add_sender = Sequence('BO_TX_BU_', 'NUMBER', ':',
                                      DelimitedList('WORD'), ';')

        signal_group = Sequence('SIG_GROUP_', 'NUMBER', 'WORD', 'NUMBER', ':',
                                Optional(OneOrMore('WORD')), ';')

        return OneOrMoreDict(
            choice(message, comment, attribute_definition, value_table,
                   choice_, attribute, attribute_rel, attribute_definition_rel,
                   attribute_definition_default,
                   attribute_definition_default_rel, signal_group, signal_type,
                   signal_multiplexer_values, message_add_sender,
                   environment_variable, nodes, ns, bs, version))