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
Exemple #2
0
 def __init__(
     self,
     whitespace=re.compile('[\\t ]+'),
     nameguard=None,
     comments_re=None,
     eol_comments_re='#.*?$',
     ignorecase=None,
     left_recursion=True,
     parseinfo=True,
     keywords=None,
     namechars='',
     buffer_class=ObsidianBuffer,
     **kwargs
 ):
     if keywords is None:
         keywords = KEYWORDS
     super(ObsidianParser, self).__init__(
         whitespace=whitespace,
         nameguard=nameguard,
         comments_re=comments_re,
         eol_comments_re=eol_comments_re,
         ignorecase=ignorecase,
         left_recursion=left_recursion,
         parseinfo=parseinfo,
         keywords=keywords,
         namechars=namechars,
         buffer_class=buffer_class,
         **kwargs
     )
Exemple #3
0
 def __init__(
     self,
     whitespace=re.compile(" "),
     nameguard=None,
     comments_re=None,
     eol_comments_re=None,
     ignorecase=None,
     left_recursion=True,
     parseinfo=True,
     keywords=None,
     namechars="",
     buffer_class=UnknownBuffer,
     **kwargs,
 ):
     if keywords is None:
         keywords = KEYWORDS
     super(UnknownParser, self).__init__(
         whitespace=whitespace,
         nameguard=nameguard,
         comments_re=comments_re,
         eol_comments_re=eol_comments_re,
         ignorecase=ignorecase,
         left_recursion=left_recursion,
         parseinfo=parseinfo,
         keywords=keywords,
         namechars=namechars,
         buffer_class=buffer_class,
         **kwargs,
     )
Exemple #4
0
 def __post_init__(self):
     super().__post_init__()
     ast = self.ast
     if not isinstance(ast, list):
         ast = [ast]
     self.patterns = ast
     self.regex = re.compile(self.pattern)
Exemple #5
0
 def __init__(self,
              whitespace=re.compile('(?s)[ \\t\\r\\f\\v]+'),
              nameguard=None,
              comments_re=None,
              eol_comments_re=None,
              ignorecase=None,
              left_recursion=True,
              parseinfo=True,
              keywords=None,
              namechars='',
              tokenizercls=SASCBuffer,
              **kwargs):
     if keywords is None:
         keywords = KEYWORDS
     super().__init__(whitespace=whitespace,
                      nameguard=nameguard,
                      comments_re=comments_re,
                      eol_comments_re=eol_comments_re,
                      ignorecase=ignorecase,
                      left_recursion=left_recursion,
                      parseinfo=parseinfo,
                      keywords=keywords,
                      namechars=namechars,
                      tokenizercls=tokenizercls,
                      **kwargs)
Exemple #6
0
 def __init__(self,
              whitespace=re.compile('[\\t ]+'),
              nameguard=None,
              comments_re='\\/\\*(.|\\n)*\\*\\/',
              eol_comments_re='(#|\\/\\/).*?\\n',
              ignorecase=None,
              left_recursion=True,
              parseinfo=True,
              keywords=None,
              namechars='',
              buffer_class=CryoLangBuffer,
              **kwargs):
     if keywords is None:
         keywords = KEYWORDS
     super(CryoLangParser, self).__init__(whitespace=whitespace,
                                          nameguard=nameguard,
                                          comments_re=comments_re,
                                          eol_comments_re=eol_comments_re,
                                          ignorecase=ignorecase,
                                          left_recursion=left_recursion,
                                          parseinfo=parseinfo,
                                          keywords=keywords,
                                          namechars=namechars,
                                          buffer_class=buffer_class,
                                          **kwargs)
Exemple #7
0
 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)
 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)
Exemple #9
0
    def parse(self,
              text,
              rule_name=None,
              start=None,
              filename=None,
              semantics=None,
              trace=False,
              context=None,
              whitespace=None,
              left_recursion=None,
              comments_re=None,
              eol_comments_re=None,
              parseinfo=None,
              **kwargs):
        start = start if start is not None else rule_name
        start = start if start is not None else self.rules[0].name

        ctx = context or ModelContext(
            self.rules,
            trace=trace,
            keywords=self.keywords,
            **kwargs)

        if semantics is None:
            semantics = self.semantics

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

        if left_recursion is None:
            left_recursion = self.left_recursion

        if parseinfo is None:
            parseinfo = self._use_parseinfo

        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,
            rule_name=start,
            filename=filename,
            semantics=semantics,
            trace=trace,
            whitespace=whitespace,
            comments_re=comments_re,
            eol_comments_re=eol_comments_re,
            left_recursion=left_recursion,
            parseinfo=parseinfo,
            **kwargs
        )
    def parse(self,
              text,
              rule_name=None,
              start=None,
              filename=None,
              semantics=None,
              trace=False,
              context=None,
              whitespace=None,
              left_recursion=None,
              comments_re=None,
              eol_comments_re=None,
              parseinfo=None,
              **kwargs):
        start = start if start is not None else rule_name
        start = start if start is not None else self.rules[0].name

        ctx = context or ModelContext(
            self.rules,
            trace=trace,
            keywords=self.keywords,
            **kwargs)

        if semantics is None:
            semantics = self.semantics

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

        if left_recursion is None:
            left_recursion = self.left_recursion

        if parseinfo is None:
            parseinfo = self._use_parseinfo

        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,
            rule_name=start,
            filename=filename,
            semantics=semantics,
            trace=trace,
            whitespace=whitespace,
            comments_re=comments_re,
            eol_comments_re=eol_comments_re,
            left_recursion=left_recursion,
            parseinfo=parseinfo,
            **kwargs
        )
Exemple #11
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),
                               regexp.MULTILINE | regexp.UNICODE)
     else:
         return None
Exemple #12
0
    def parse(self,
              text,
              rule_name=None,
              start=None,
              filename=None,
              semantics=None,
              trace=False,
              context=None,
              whitespace=None,
              ignorecase=None,
              left_recursion=None,
              comments_re=None,
              eol_comments_re=None,
              parseinfo=None,
              nameguard=None,
              namechars=None,
              **kwargs):  # pylint: disable=W0221
        start = start if start is not None else rule_name
        start = start if start is not None else self.rules[0].name

        ctx = context or ModelContext(
            self.rules, trace=trace, keywords=self.keywords, **kwargs)

        semantics = notnone(semantics, self.semantics)
        left_recursion = notnone(left_recursion, self.left_recursion)
        parseinfo = notnone(parseinfo, self._use_parseinfo)
        comments_re = notnone(comments_re, self.comments_re)
        eol_comments_re = notnone(eol_comments_re, self.eol_comments_re)
        nameguard = notnone(nameguard, self.nameguard)
        namechars = notnone(namechars, self.namechars)
        whitespace = notnone(whitespace, self.whitespace)
        ignorecase = notnone(ignorecase, self.ignorecase)
        if whitespace:
            whitespace = re.compile(whitespace)

        return ctx.parse(text,
                         rule_name=start,
                         filename=filename,
                         semantics=semantics,
                         trace=trace,
                         whitespace=whitespace,
                         ignorecase=ignorecase,
                         comments_re=comments_re,
                         eol_comments_re=eol_comments_re,
                         left_recursion=left_recursion,
                         parseinfo=parseinfo,
                         nameguard=nameguard,
                         namechars=namechars,
                         **kwargs)
 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),
             regexp.MULTILINE | regexp.UNICODE
         )
     else:
         return None
Exemple #14
0
 def __init__(self,
              text,
              whitespace=re.compile('(?s)[ \\t\\r\\f\\v]+'),
              nameguard=None,
              comments_re=None,
              eol_comments_re='#([^\\n]*?)$',
              ignorecase=None,
              namechars='',
              **kwargs):
     super().__init__(text,
                      whitespace=whitespace,
                      nameguard=nameguard,
                      comments_re=comments_re,
                      eol_comments_re=eol_comments_re,
                      ignorecase=ignorecase,
                      namechars=namechars,
                      **kwargs)
Exemple #15
0
 def __init__(self,
              text,
              whitespace=re.compile('[\\t ]+'),
              nameguard=None,
              comments_re=None,
              eol_comments_re='#.*?$',
              ignorecase=None,
              namechars='',
              **kwargs):
     super().__init__(text,
                      whitespace=whitespace,
                      nameguard=nameguard,
                      comments_re=comments_re,
                      eol_comments_re=eol_comments_re,
                      ignorecase=ignorecase,
                      namechars=namechars,
                      **kwargs)
Exemple #16
0
 def __init__(self,
              text,
              whitespace=re.compile('[\\t ]+'),
              nameguard=None,
              comments_re='\\/\\*(.|\\n)*\\*\\/',
              eol_comments_re='(#|\\/\\/).*?\\n',
              ignorecase=None,
              namechars='',
              **kwargs):
     super(CryoLangBuffer, self).__init__(text,
                                          whitespace=whitespace,
                                          nameguard=nameguard,
                                          comments_re=comments_re,
                                          eol_comments_re=eol_comments_re,
                                          ignorecase=ignorecase,
                                          namechars=namechars,
                                          **kwargs)
Exemple #17
0
 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)
Exemple #18
0
 def __init__(
     self,
     text,
     whitespace=re.compile(" "),
     nameguard=None,
     comments_re=None,
     eol_comments_re=None,
     ignorecase=None,
     namechars="",
     **kwargs,
 ):
     super(UnknownBuffer, self).__init__(
         text,
         whitespace=whitespace,
         nameguard=nameguard,
         comments_re=comments_re,
         eol_comments_re=eol_comments_re,
         ignorecase=ignorecase,
         namechars=namechars,
         **kwargs,
     )
about source lines and content.
"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import os
from itertools import takewhile, repeat

from tatsu.util import identity, imap, ustr, strtype
from tatsu.util import extend_list, contains_sublist
from tatsu.util import re as regexp
from tatsu.util import WHITESPACE_RE
from tatsu.exceptions import ParseError
from tatsu.infos import PosLine, LineIndexInfo, LineInfo, CommentInfo

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

# for backwards compatibility with existing parsers
LineIndexEntry = LineIndexInfo


class Buffer(object):
    def __init__(self,
                 text,
                 filename=None,
                 whitespace=None,
                 comments_re=None,
                 eol_comments_re=None,
                 ignorecase=False,
                 nameguard=None,
                 comment_recovery=False,
Exemple #20
0
 def __postinit__(self, ast):
     super(Pattern, self).__postinit__(ast)
     if not isinstance(ast, list):
         ast = [ast]
     self.patterns = ast
     re.compile(self.pattern)
 def __postinit__(self, ast):
     super(Pattern, self).__postinit__(ast)
     if not isinstance(ast, list):
         ast = [ast]
     self.patterns = ast
     re.compile(self.pattern)
Exemple #22
0
about source lines and content.
"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import os
from itertools import takewhile, repeat

from tatsu.util import identity, imap, ustr, strtype
from tatsu.util import extend_list, contains_sublist
from tatsu.util import re as regexp
from tatsu.util import WHITESPACE_RE
from tatsu.exceptions import ParseError
from tatsu.infos import PosLine, LineIndexInfo, LineInfo, CommentInfo

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

# for backwards compatibility with existing parsers
LineIndexEntry = LineIndexInfo


class Buffer(object):
    def __init__(self,
                 text,
                 filename=None,
                 whitespace=None,
                 comments_re=None,
                 eol_comments_re=None,
                 ignorecase=False,
                 nameguard=None,
                 comment_recovery=False,