Ejemplo n.º 1
0
 def test_select(self):
     """test : select"""
     cases = [
         (['a'], '(a)'),
         (['a', 'b'], '(a|b)'),
         (['ab', '\\e'], '(ab|\\e)'),
         (['a|b'], '(a|b)'),
         (['a|b', 'c'], '(a|b|c)'),
         (['a|b', 'c|a'], '(a|b|c|a)'),
     ]
     for case in cases:
         self.assertEqual(yare.select(case[0]), case[1])
Ejemplo n.º 2
0
# Literals
def t_INTCON(t):
    t.value = int(t.value)
    return t
t_INTCON.__doc__ = yare.concat([
    yare.loop_(yare.DIGIT),
])

__STRINGCON__ = ' !#$%&\'()*+,-./0123456789:;<=>?@' + \
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'
t_STRINGCON = yare.concat([
    '"',
    yare.loop(
        yare.select([
            yare.select(list(__STRINGCON__)),
            yare.concat(['\\', yare.select(['n', '\\', '"'])]),
        ])
    ),
    '"',
])

__CHARCON__ = ' !#$%&()*+,-./0123456789:;<=>?@' + \
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'
t_CHARCON = yare.concat([
    '\'',
    yare.select([
        yare.select(list(__CHARCON__)),
        yare.concat(['\\', yare.select(['n', '\\', '\'', '0'])]),
    ]),
    '\'',
])
Ejemplo n.º 3
0
def t_INTCON(t):
    t.value = int(t.value)
    return t


t_INTCON.__doc__ = yare.concat([
    yare.loop_(yare.DIGIT),
])

__STRINGCON__ = ' !#$%&\'()*+,-./0123456789:;<=>?@' + \
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'
t_STRINGCON = yare.concat([
    '"',
    yare.loop(
        yare.select([
            yare.select(list(__STRINGCON__)),
            yare.concat(['\\', yare.select(['n', '\\', '"'])]),
        ])),
    '"',
])

__CHARCON__ = ' !#$%&()*+,-./0123456789:;<=>?@' + \
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'
t_CHARCON = yare.concat([
    '\'',
    yare.select([
        yare.select(list(__CHARCON__)),
        yare.concat(['\\', yare.select(['n', '\\', '\'', '0'])]),
    ]),
    '\'',
])
Ejemplo n.º 4
0
        if not token.isupper():
            raise SyntaxError(
                'token `%s` is not uppercase' % token
            )
        if tokens.count(token) > 1:
            raise SyntaxWarning(
                'declared token `%s` %d times' % \
                (token, tokens.count(token))
            )
        func_name = 't_' + token
        if func_name not in all_vars:
            raise NotImplementedError(
                'declared token `%s` but not define `%s`' % \
                (token, func_name)
            )
        func = all_vars[func_name]
        if type(func) is str:
            all_vars[func_name] = lambda t : t
            all_vars[func_name].__doc__ = func
            func = all_vars[func_name]
        import yare
        try:
            compiled_tokens[token] = (yare.compile(func.__doc__), func)
        except SyntaxError, e:
            raise SyntaxError(
                'regular expression `%s` specified' % func.__doc__ + \
                'in function `%s` not valid. Detail: %s' % (func_name, e)
            )
        regexs.append(func.__doc__)
    return Lexer(compiled_tokens, tokens, yare.compile(yare.select(regexs)))
Ejemplo n.º 5
0
        raise TypeError('Lex expected variable `tokens` to be iterable')
    for token in tokens:
        if not token.isupper():
            raise SyntaxError('token `%s` is not uppercase' % token)
        if tokens.count(token) > 1:
            raise SyntaxWarning(
                'declared token `%s` %d times' % \
                (token, tokens.count(token))
            )
        func_name = 't_' + token
        if func_name not in all_vars:
            raise NotImplementedError(
                'declared token `%s` but not define `%s`' % \
                (token, func_name)
            )
        func = all_vars[func_name]
        if type(func) is str:
            all_vars[func_name] = lambda t: t
            all_vars[func_name].__doc__ = func
            func = all_vars[func_name]
        import yare
        try:
            compiled_tokens[token] = (yare.compile(func.__doc__), func)
        except SyntaxError, e:
            raise SyntaxError(
                'regular expression `%s` specified' % func.__doc__ + \
                'in function `%s` not valid. Detail: %s' % (func_name, e)
            )
        regexs.append(func.__doc__)
    return Lexer(compiled_tokens, tokens, yare.compile(yare.select(regexs)))