How to use the pyleri.Ref function in pyleri

To help you get started, we’ve selected a few pyleri examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github transceptor-technology / pyleri / test / test_pyleri.py View on Github external
s_seq,
        Sequence(THIS, Keyword('and'), THIS),
        Sequence(THIS, Keyword('or'), THIS))


class _TestGrammar3(Grammar):
    s_tic_tac = Sequence(Keyword('tic'), Keyword('tac'))
    s_tic_tac_toe = Sequence(Keyword('tic'), Keyword('tac'), Keyword('toe'))

    START = Sequence(
        Choice(s_tic_tac, s_tic_tac_toe),
        Choice(s_tic_tac, s_tic_tac_toe, most_greedy=False))


class _TestGrammar4(Grammar):
    START = Ref()
    ni_item = Choice(Keyword('ni'), START)
    START = Sequence('[', List(ni_item), ']')


class TestPyleri(unittest.TestCase):

    def setUp(self):
        gc.collect()

    def test_parse_keyword(self):
        tg = _TestGrammar1()
        self.assertTrue(tg.parse('test ignore_case').is_valid)
        self.assertTrue(tg.parse('test Ignore_Case').is_valid)
        # test is not case insensitive
        self.assertFalse(tg.parse('Test ignore_case').is_valid)
        # - is not _
github transceptor-technology / pyleri / examples / json_grammar.py View on Github external
'''JSON Grammar.'''
from pyleri import (
    Ref,
    Choice,
    Grammar,
    Regex,
    Keyword,
    Sequence,
    List)


class JsonGrammar(Grammar):
    START = Ref()

    # JSON strings should be enclosed in double quotes.
    # A backslash can be used as escape character.
    r_string = Regex(r'(")(?:(?=(\\?))\2.)*?\1')

    # JSON does not support floats or integers prefixed with a + sign
    # and floats must start with a number, for example .5 is not allowed
    # but should be written like 0.5
    r_float = Regex(r'-?[0-9]+\.?[0-9]+')
    r_integer = Regex('-?[0-9]+')

    k_true = Keyword('true')
    k_false = Keyword('false')
    k_null = Keyword('null')

    json_map_item = Sequence(r_string, ':', START)
github alastairreid / mra_tools / bin / asm.py View on Github external
neg_guards = {k: expand_dontcares(v) for k, v in neg_guards.items()}
        pos_sail_guards = ' & '.join(['(' + ' | '.join('{} == 0b{}'.format(k, v) for v in vs) + ')' for k, vs in pos_guards.items()])
        neg_sail_guards = ' & '.join(['(' + ' & '.join('{} != 0b{}'.format(k, v) for v in vs) + ')' for k, vs in neg_guards.items()])

        clause = 'mapping clause assembly = {}{}{} <-> {}'.format(lhs,
                                                                  ' if ' if neg_sail_guards else '',
                                                                  neg_sail_guards,
                                                                  rhs.replace(':', '@'))
        print(clause, file=file)

class ASMTemplateGrammar(Grammar):
    doublespace = Regex('\s\s+')
    space = Regex('\s')
    link = Regex('<[A-Za-z0-9_|()+]+>')
    text = Regex('[A-Za-z0-9_[\]!,#.]+')
    optional = Ref()
    optional = Sequence('{', Repeat(Choice(link, text, optional, space), mi=1), '}')
    bracket_alternative = Sequence('(', Repeat(Choice(link, text, space), mi=1), '|', Repeat(Choice(link, text, space), mi=1), ')')
#    unbracket_alternative = Sequence(Choice(link, text), mi=1), '|', Repeat(Choice(link, text), mi=1))
    optional_alternative = Sequence('{', Repeat(Choice(link, text, space), mi=1), '|', Repeat(Choice(link, text, space), mi=1), '}')
    START = Repeat(Choice(doublespace, space, link, text, optional_alternative, bracket_alternative, optional), mi=1)

    def _walk(self, element, pos, tree, rule, is_required):
        if self._pos != pos:
            self._s = self._string[pos:] #.lstrip() # don't strip whitespace
            self._pos = self._len_string - len(self._s)
        node = Node(element, self._string, self._pos)
        self._expecting.set_mode_required(node.start, is_required)
        return element._get_node_result(self, tree, rule, self._s, node)

asm_grammar = ASMTemplateGrammar()
github SiriDB / siridb-server / grammar / grammar.py View on Github external
k_server,
        k_startup_time,
        k_status,
        k_sync_progress,
        k_tee_pipe_name,
        k_time_precision,
        k_timezone,
        k_uptime,
        k_uuid,
        k_version,
        k_who_am_i,
        most_greedy=False), ',', 0))

    timeit_stmt = Repeat(k_timeit, 1, 1)

    help_stmt = Ref()

    START = Sequence(
        Optional(timeit_stmt),
        Optional(Choice(
            select_stmt,
            list_stmt,
            count_stmt,
            alter_stmt,
            create_stmt,
            drop_stmt,
            grant_stmt,
            revoke_stmt,
            show_stmt,
            calc_stmt,
            help_stmt,
            most_greedy=False)),
github alastairreid / mra_tools / bin / asm.py View on Github external
#    unbracket_alternative = Sequence(Choice(link, text), mi=1), '|', Repeat(Choice(link, text), mi=1))
    optional_alternative = Sequence('{', Repeat(Choice(link, text, space), mi=1), '|', Repeat(Choice(link, text, space), mi=1), '}')
    START = Repeat(Choice(doublespace, space, link, text, optional_alternative, bracket_alternative, optional), mi=1)

    def _walk(self, element, pos, tree, rule, is_required):
        if self._pos != pos:
            self._s = self._string[pos:] #.lstrip() # don't strip whitespace
            self._pos = self._len_string - len(self._s)
        node = Node(element, self._string, self._pos)
        self._expecting.set_mode_required(node.start, is_required)
        return element._get_node_result(self, tree, rule, self._s, node)

asm_grammar = ASMTemplateGrammar()

class BitConcatsGrammar(Grammar):
    START = Ref()
    arg = Regex('[A-Za-z][A-Za-z0-9]*')
    brackets = Sequence('(', START, ')')
    literal = Regex('0b[01]+')
    concat = pyleri.List(Choice(brackets, arg, literal), delimiter='@')
    START = Choice(brackets, arg, literal, concat)

bit_concats_grammar = BitConcatsGrammar()

def fst_by_snd(pairs, target):
    for fst, snd in pairs:
        if target == snd:
            return fst
    raise KeyError(target)

def process_bitconcat_node_get_bits(types, node):
    assert hasattr(node.element, 'name')