def grammar(self): interesting_group = textparser.Sequence( 'interesting_group', '{', ZeroOrMore(Sequence('WORD', '=', 'WORD', ';')), '}', ';') return Sequence(AnyUntil('interesting_group'), interesting_group, ZeroOrMore(Any()))
def test_grammar_any(self): grammar = Grammar(Any()) datas = [ ( [('A', r'a')], 'a' ), ( [('B', r'b')], 'b' ) ] self.parse_and_assert_tree(grammar, datas)
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))
def grammar(self): word = choice('WORD', *list(self.KEYWORDS)) version = Sequence('FormatVersion', '=', 'NUMBER', 'COMMENT') title = Sequence('Title', '=', 'STRING') unique_variables = Sequence('UniqueVariables', '=', word) float_decimal_places = Sequence('FloatDecimalPlaces', '=', 'NUMBER') bit_rate_switch = Sequence('BRS', '=', word) enum_value = Sequence('NUMBER', '=', 'STRING') enum = Sequence('Enum', '=', word, '(', Optional(DelimitedList(enum_value)), ')', Optional('COMMENT')) sig_unit = '/u:' sig_factor = Sequence('/f:', 'NUMBER') sig_offset = Sequence('/o:', 'NUMBER') sig_min = Sequence('/min:', 'NUMBER') sig_max = Sequence('/max:', 'NUMBER') sig_default = Sequence('/d:', choice('NUMBER', 'WORD')) sig_long_name = Sequence('/ln:', 'STRING') sig_enum = Sequence('/e:', word) sig_places = Sequence('/p:', 'NUMBER') signal = Sequence( 'Sig', '=', Any(), word, Optional('NUMBER'), Optional(choice('-h', '-b')), Optional('-m'), ZeroOrMore( choice(sig_unit, sig_factor, sig_offset, sig_min, sig_max, sig_default, sig_long_name, sig_enum, sig_places)), Optional('COMMENT')) variable = Sequence( 'Var', '=', Any(), word, 'NUMBER', ',', 'NUMBER', ZeroOrMore(choice('-v', '-m', '-s')), ZeroOrMore( choice(sig_unit, sig_factor, sig_offset, sig_min, sig_max, sig_default, sig_long_name, sig_enum, sig_places)), Optional('COMMENT')) symbol = Sequence( '[', Any(), ']', ZeroOrMoreDict( choice( Sequence('ID', '=', 'NUMBER', word, Optional(Sequence('NUMBER', word)), Optional('COMMENT')), Sequence('Len', '=', 'NUMBER'), Sequence('Mux', '=', Any(), 'NUMBER', ',', 'NUMBER', 'NUMBER', Optional('-t')), Sequence('CycleTime', '=', 'NUMBER'), Sequence('Timeout', '=', 'NUMBER'), Sequence('MinInterval', '=', 'NUMBER'), Sequence('Color', '=', 'NUMBER', 'WORD'), variable, Sequence('Sig', '=', Any(), 'NUMBER')))) enums = Sequence('{ENUMS}', ZeroOrMore(choice(enum, 'COMMENT'))) signals = Sequence('{SIGNALS}', ZeroOrMore(choice(signal, 'COMMENT'))) send = Sequence('{SEND}', ZeroOrMore(choice(symbol, 'COMMENT'))) receive = Sequence('{RECEIVE}', ZeroOrMore(choice(symbol, 'COMMENT'))) sendreceive = Sequence('{SENDRECEIVE}', ZeroOrMore(choice(symbol, 'COMMENT'))) section = choice(enums, signals, send, receive, sendreceive) grammar = Sequence( Optional('COMMENT'), version, ZeroOrMore( choice(unique_variables, float_decimal_places, title, bit_rate_switch)), ZeroOrMore(section)) return grammar