Exemplo n.º 1
0
 def param_repr(p):
     if isinstance(p, (int, float)):
         return ustr(p)
     elif isinstance(p, strtype) and p.isalnum():
         return ustr(p)
     else:
         return urepr(p)
Exemplo n.º 2
0
    def render_fields(self, fields):
        self.reset_counter()

        params = kwparams = ''
        if self.node.params:
            params = ', '.join(
                self.param_repr(self.rend(p))
                for p in self.node.params
            )
        if self.node.kwparams:
            kwparams = ', '.join(
                '%s=%s'
                %
                (k, self.param_repr(self.rend(v)))
                for k, v in self.kwparams.items()
            )

        if params and kwparams:
            params = params + ', ' + kwparams
        elif kwparams:
            params = kwparams

        fields.update(params=params)

        defines = compress_seq(self.defines())
        sdefs = [d for d, l in defines if not l]
        ldefs = [d for d, l in defines if l]
        if not (sdefs or ldefs):
            sdefines = ''
        else:
            sdefs = '[%s]' % ', '.join(urepr(d) for d in sdefs)
            ldefs = '[%s]' % ', '.join(urepr(d) for d in ldefs)
            if not ldefs:
                sdefines = '\n\n    self.ast._define(%s, %s)' % (sdefs, ldefs)
            else:
                sdefines = indent('\n\n' + trim('''\
                                                self.ast._define(
                                                    %s,
                                                    %s
                                                )''' % (sdefs, ldefs)
                                                )
                                  )

        fields.update(defines=sdefines)
Exemplo n.º 3
0
    def render_fields(self, fields):
        abstract_template = trim(self.abstract_rule_template)
        abstract_rules = [
            abstract_template.format(name=safe_name(rule.name))
            for rule in self.node.rules
        ]
        abstract_rules = indent('\n'.join(abstract_rules))

        if self.node.whitespace is not None:
            whitespace = urepr(self.node.whitespace)
        elif self.node.directives.get('whitespace') is not None:
            whitespace = 're.compile({0}, RE_FLAGS | re.DOTALL)'.format(
                urepr(self.node.directives.get('whitespace')))
        else:
            whitespace = 'None'

        if self.node.nameguard is not None:
            nameguard = urepr(self.node.nameguard)
        elif self.node.directives.get('nameguard') is not None:
            nameguard = self.node.directives.get('nameguard')
        else:
            nameguard = 'None'

        comments_re = urepr(self.node.directives.get('comments'))
        eol_comments_re = urepr(self.node.directives.get('eol_comments'))
        ignorecase = self.node.directives.get('ignorecase', 'None')
        left_recursion = self.node.directives.get('left_recursion', True)

        rules = '\n'.join(
            [self.get_renderer(rule).render() for rule in self.node.rules])

        version = str(tuple(int(n) for n in str(timestamp()).split('.')))

        fields.update(
            rules=indent(rules),
            abstract_rules=abstract_rules,
            version=version,
            whitespace=whitespace,
            nameguard=nameguard,
            comments_re=comments_re,
            eol_comments_re=eol_comments_re,
            ignorecase=ignorecase,
            left_recursion=left_recursion,
        )
Exemplo n.º 4
0
    def render_fields(self, fields):
        abstract_template = trim(self.abstract_rule_template)
        abstract_rules = [
            abstract_template.format(name=safe_name(rule.name))
            for rule in self.node.rules
        ]
        abstract_rules = indent('\n'.join(abstract_rules))

        if self.node.whitespace is not None:
            whitespace = urepr(self.node.whitespace)
        elif self.node.directives.get('whitespace') is not None:
            whitespace = 're.compile({0}, RE_FLAGS | re.DOTALL)'.format(urepr(self.node.directives.get('whitespace')))
        else:
            whitespace = 'None'

        if self.node.nameguard is not None:
            nameguard = urepr(self.node.nameguard)
        elif self.node.directives.get('nameguard') is not None:
            nameguard = self.node.directives.get('nameguard')
        else:
            nameguard = 'None'

        comments_re = urepr(self.node.directives.get('comments'))
        eol_comments_re = urepr(self.node.directives.get('eol_comments'))
        ignorecase = self.node.directives.get('ignorecase', 'None')
        left_recursion = self.node.directives.get('left_recursion', True)

        rules = '\n'.join([
            self.get_renderer(rule).render() for rule in self.node.rules
        ])

        version = str(tuple(int(n) for n in str(timestamp()).split('.')))

        fields.update(rules=indent(rules),
                      abstract_rules=abstract_rules,
                      version=version,
                      whitespace=whitespace,
                      nameguard=nameguard,
                      comments_re=comments_re,
                      eol_comments_re=eol_comments_re,
                      ignorecase=ignorecase,
                      left_recursion=left_recursion,
                      )
Exemplo n.º 5
0
    def parse(self, ctx):
        with ctx._choice():
            for o in self.options:
                with ctx._option():
                    ctx.last_node = o.parse(ctx)
                    return ctx.last_node

            lookahead = ' '.join(ustr(urepr(f[0])) for f in self.lookahead if f)
            if lookahead:
                ctx._error('expecting one of {%s}' % lookahead)
            ctx._error('no available options')
Exemplo n.º 6
0
    def render_fields(self, fields):
        self.reset_counter()

        params = kwparams = ''
        if self.node.params:
            params = ', '.join(
                self.param_repr(self.rend(p)) for p in self.node.params)
        if self.node.kwparams:
            kwparams = ', '.join('%s=%s' % (k, self.param_repr(self.rend(v)))
                                 for k, v in self.kwparams.items())

        if params and kwparams:
            params = params + ', ' + kwparams
        elif kwparams:
            params = kwparams

        fields.update(params=params)

        defines = compress_seq(self.defines())
        sdefs = [d for d, l in defines if not l]
        ldefs = [d for d, l in defines if l]
        if not (sdefs or ldefs):
            sdefines = ''
        else:
            sdefs = '[%s]' % ', '.join(urepr(d) for d in sdefs)
            ldefs = '[%s]' % ', '.join(urepr(d) for d in ldefs)
            if not ldefs:
                sdefines = '\n\n    self.ast._define(%s, %s)' % (sdefs, ldefs)
            else:
                sdefines = indent('\n\n' + trim('''\
                                                self.ast._define(
                                                    %s,
                                                    %s
                                                )''' % (sdefs, ldefs)))

        fields.update(defines=sdefines)
Exemplo n.º 7
0
 def render_fields(self, fields):
     template = trim(self.option_template)
     options = [
         template.format(option=indent(self.rend(o)))
         for o in self.node.options
     ]
     options = '\n'.join(o for o in options)
     firstset = ' '.join(f[0] for f in sorted(self.node.firstset) if f)
     if firstset:
         error = 'expecting one of: ' + firstset
     else:
         error = 'no available options'
     fields.update(n=self.counter(),
                   options=indent(options),
                   error=urepr(error))
Exemplo n.º 8
0
 def render_fields(self, fields):
     template = trim(self.option_template)
     options = [
         template.format(
             option=indent(self.rend(o))) for o in self.node.options
     ]
     options = '\n'.join(o for o in options)
     firstset = ' '.join(f[0] for f in sorted(self.node.firstset) if f)
     if firstset:
         error = 'expecting one of: ' + firstset
     else:
         error = 'no available options'
     fields.update(n=self.counter(),
                   options=indent(options),
                   error=urepr(error)
                   )
Exemplo n.º 9
0
 def render_fields(self, fields):
     raw_repr = 'r' + urepr(self.node.pattern).replace("\\\\", '\\')
     fields.update(pattern=raw_repr)
Exemplo n.º 10
0
 def render_fields(self, fields):
     fields.update(token=urepr(self.node.token))
Exemplo n.º 11
0
 def param_repr(p):
     if isinstance(p, (int, float)):
         return ustr(p)
     else:
         return urepr(p)
Exemplo n.º 12
0
 def __init__(self, ast=None, **kwargs):
     super(Choice, self).__init__(ast=AST(options=ast))
     assert isinstance(self.options, list), urepr(self.options)
Exemplo n.º 13
0
 def __str__(self):
     return urepr(self.token)
Exemplo n.º 14
0
 def render_fields(self, fields):
     raw_repr = 'r' + urepr(self.node.pattern).replace("\\\\", '\\')
     fields.update(pattern=raw_repr)
Exemplo n.º 15
0
 def render_fields(self, fields):
     fields.update(token=urepr(self.node.token))
Exemplo n.º 16
0
 def param_repr(p):
     if isinstance(p, (int, float)):
         return ustr(p)
     else:
         return urepr(p)