Exemplo n.º 1
0
def lex():
    """
    return a Lexer
    """
    compiled_tokens = {}
    regexs = []
    import sys
    all_vars = sys._getframe(1).f_locals
    if 'tokens' not in all_vars:
        raise NotImplementedError(
            'Lex need variable `tokens` but not defined'
        )
    tokens = all_vars['tokens']
    if not hasattr(tokens, '__iter__'):
        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__)
Exemplo n.º 2
0
def lex():
    """
    return a Lexer
    """
    compiled_tokens = {}
    regexs = []
    import sys
    all_vars = sys._getframe(1).f_locals
    if 'tokens' not in all_vars:
        raise NotImplementedError('Lex need variable `tokens` but not defined')
    tokens = all_vars['tokens']
    if not hasattr(tokens, '__iter__'):
        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__)
Exemplo n.º 3
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)))
Exemplo n.º 4
0
 def setUp(self):
     """compile a regex"""
     self.__regex__ = yare.compile('a*')
Exemplo n.º 5
0
 def setUp(self):
     """compile a regex"""
     self.__regex__ = yare.compile('(a|b)*a(a|b)(a|b)(a|b)')
Exemplo n.º 6
0
 def setUp(self):
     """compile a regex"""
     self.__regex__ = yare.compile('a(\*b|c)d')
Exemplo n.º 7
0
#!/usr/bin/env python
# coding:utf-8

"""demo for package pyre"""

import yare

regex = yare.compile("(a|b)*a(a|b)")
print regex.match("aa")
# >>> True
print yare.match(regex, "aab")
# >>> True
Exemplo n.º 8
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)))