How to use the tatsu.util.re function in TatSu

To help you get started, we’ve selected a few TatSu 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 neogeny / TatSu / tatsu / parser_semantics.py View on Github external
def regexes(self, ast, *args):
        pattern = ''.join(ast)
        try:
            re.compile(pattern)
        except (TypeError, re.error) as e:
            raise FailedSemantics('regexp error: ' + str(e))
        return ast
github neogeny / TatSu / tatsu / buffering.py View on Github external
def _scanre(self, pattern, ignorecase=None, offset=0):
        if isinstance(pattern, RETYPE):
            re = pattern
        elif pattern in self._re_cache:
            re = self._re_cache[pattern]
        else:
            re = regexp.compile(pattern, regexp.MULTILINE | regexp.UNICODE)
            self._re_cache[pattern] = re
        return re.match(self.text, self.pos + offset)
github neogeny / TatSu / tatsu / buffering.py View on Github external
def build_whitespace_re(whitespace):
        if whitespace is None:
            return WHITESPACE_RE
        elif isinstance(whitespace, RETYPE):
            return whitespace
        elif whitespace:
            if not isinstance(whitespace, str):
                # a list or a set?
                whitespace = ''.join(c for c in whitespace)
            return regexp.compile(
                '[%s]+' % regexp.escape(whitespace),
                regexp.MULTILINE | regexp.UNICODE
            )
        else:
            return None
github neogeny / TatSu / tatsu / walkers.py View on Github external
classid = id(node.__class__)

        if classid in self._walker_cache:
            return self._walker_cache[classid]

        classes = [node.__class__]
        while classes:
            cls = classes.pop(0)

            cammelcase_name = cls.__name__
            walker = getattr(self, prefix + cammelcase_name, None)
            if callable(walker):
                break

            # walk__pythonic_name with double underscore after walk
            pythonic_name = re.sub('[A-Z]+', pythonize_match, cls.__name__)
            if pythonic_name != cammelcase_name:
                walker = getattr(self, prefix + pythonic_name, None)
                if callable(walker):
                    break

            # walk_pythonic_name with single underscore after walk

            # pythonic_name = pythonic_name.lstrip('_')
            # if pythonic_name != cammelcase_name:
            #     walker = getattr(self, prefix + pythonic_name, None)
            #     if callable(walker):
            #         break

            for b in cls.__bases__:
                if b not in classes:
                    classes.append(b)
github neogeny / TatSu / tatsu / parser_semantics.py View on Github external
def regex(self, ast, *args):
        pattern = ast
        try:
            re.compile(pattern)
        except (TypeError, re.error) as e:
            raise FailedSemantics('regexp error: ' + str(e))
        return pattern
github neogeny / TatSu / tatsu / codegen / objectmodel.py View on Github external
def _get_node_class_name(rule):
    if not rule.params:
        return None

    typespec = rule.params[0]
    if not re.match(NODE_NAME_PATTERN, typespec):
        return None
    if not typespec[0].isupper():
        return None
    return typespec
github neogeny / TatSu / tatsu / grammars.py View on Github external
def __postinit__(self, ast):
        super().__postinit__(ast)
        if not isinstance(ast, list):
            ast = [ast]
        self.patterns = ast
        self.regex = re.compile(self.pattern)
github microsoft / TextWorld / textworld / textgen / parser.py View on Github external
def __init__(
        self,
        text,
        whitespace=re.compile('[\\t ]+'),
        nameguard=None,
        comments_re=None,
        eol_comments_re='^(#.*|\\s*)\\n',
        ignorecase=None,
        namechars='',
        **kwargs
    ):
        super(TextGrammarBuffer, self).__init__(
            text,
            whitespace=whitespace,
            nameguard=nameguard,
            comments_re=comments_re,
            eol_comments_re=eol_comments_re,
            ignorecase=ignorecase,
            namechars=namechars,
            **kwargs
github neogeny / TatSu / tatsu / exceptions.py View on Github external
def __str__(self):
        info = self.tokenizer.line_info(self.pos)
        template = "{}({}:{}) {} :\n{}\n{}^\n{}"
        text = info.text.rstrip()
        leading = re.sub(r'[^\t]', ' ', text)[:info.col]

        text = text.expandtabs()
        leading = leading.expandtabs()
        return template.format(info.filename,
                               info.line + 1, info.col + 1,
                               self.message.rstrip(),
                               text,
                               leading,
                               '\n'.join(reversed(self.stack))
                               )