Exemple #1
0
    def _scanre(self, pattern, ignorecase=None, offset=0):
        ignorecase = ignorecase if ignorecase is not None else self.ignorecase

        if isinstance(pattern, RETYPE):
            re = pattern
        elif pattern in self._re_cache:
            re = self._re_cache[pattern]
        else:
            flags = RE_FLAGS | (regexp.IGNORECASE if ignorecase else 0)
            re = regexp.compile(
                pattern,
                flags
            )
            self._re_cache[pattern] = re
        return re.match(self.text, self.pos + offset)
Exemple #2
0
 def build_whitespace_re(whitespace):
     if whitespace is None:
         return WHITESPACE_RE
     elif isinstance(whitespace, RETYPE):
         return whitespace
     elif whitespace:
         if not isinstance(whitespace, strtype):
             # a list or a set?
             whitespace = ''.join(c for c in whitespace)
         return regexp.compile(
             '[%s]+' % regexp.escape(whitespace),
             RE_FLAGS | regexp.DOTALL
         )
     else:
         return None
Exemple #3
0
    def parse(self,
              text,
              start=None,
              filename=None,
              semantics=None,
              trace=False,
              context=None,
              whitespace=None,
              left_recursion=None,
              comments_re=None,
              eol_comments_re=None,
              **kwargs):
        ctx = context or ModelContext(
            self.rules,
            trace=trace,
            **kwargs)

        if whitespace is None:
            whitespace = self.whitespace
        if whitespace:
            whitespace = re.compile(whitespace)

        if left_recursion is None:
            left_recursion = self.left_recursion

        if comments_re is None:
            comments_re = self.comments_re

        if eol_comments_re is None:
            eol_comments_re = self.eol_comments_re

        return ctx.parse(
            text,
            start or self.rules[0].name,
            filename=filename,
            semantics=semantics,
            trace=trace,
            whitespace=whitespace,
            comments_re=comments_re,
            eol_comments_re=eol_comments_re,
            left_recursion=left_recursion,
            **kwargs
        )
Exemple #4
0
 def __postinit__(self, ast):
     re.compile(ast, RE_FLAGS)
     super(Pattern, self).__postinit__(ast)
     self.pattern = ast
Exemple #5
0
"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import os
from bisect import bisect_left
from collections import namedtuple

from dependencies.grako.util import ustr, strtype, re as regexp, WHITESPACE_RE, RE_FLAGS
from dependencies.grako.exceptions import ParseError

# TODO: There could be a file buffer using random access

__all__ = ['Buffer']

RETYPE = type(regexp.compile('.'))


PosLine = namedtuple('PosLine', ['pos', 'line'])


LineInfo = namedtuple(
    'LineInfo',
    ['filename', 'line', 'col', 'start', 'end', 'text']
)


class Buffer(object):
    def __init__(self,
                 text,
                 filename=None,