Ejemplo n.º 1
0
    def format_field(self, value, spec):
        if ':' not in spec:
            return super(RenderingFormatter,
                         self).format_field(self.render(value), spec)

        ind, sep, fmt = spec.split(':')
        if sep == '\\n':
            sep = '\n'

        if not ind:
            ind = 0
            mult = 0
        elif '*' in ind:
            ind, mult = ind.split('*')
        else:
            mult = 4
        ind = int(ind)
        mult = int(mult)

        if not fmt:
            fmt = '%s'

        if isiter(value):
            return indent(sep.join(fmt % self.render(v) for v in value), ind,
                          mult)
        else:
            return indent(fmt % self.render(value), ind, mult)
Ejemplo n.º 2
0
    def format_field(self, value, spec):
        if ':' not in spec:
            return super(RenderingFormatter, self).format_field(
                self.render(value),
                spec
            )

        ind, sep, fmt = spec.split(':')
        if sep == '\\n':
            sep = '\n'

        if not ind:
            ind = 0
            mult = 0
        elif '*' in ind:
            ind, mult = ind.split('*')
        else:
            mult = 4
        ind = int(ind)
        mult = int(mult)

        if not fmt:
            fmt = '%s'

        if isiter(value):
            return indent(sep.join(fmt % self.render(v) for v in value), ind, mult)
        else:
            return indent(fmt % self.render(value), ind, mult)
Ejemplo n.º 3
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))
Ejemplo n.º 4
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)
                   )
Ejemplo n.º 5
0
    def __str__(self):
        comments = self.comments_str()
        params = ', '.join(
            self.param_repr(p) for p in self.params
        ) if self.params else ''

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

        if params and kwparams:
            params = '(%s, %s)' % (params, kwparams)
        elif kwparams:
                params = '(%s)' % (kwparams)
        elif params:
            params = '(%s)' % params

        base = ' < %s' % ustr(self.base.name) if self.base else ''

        return trim(self.str_template).format(
            name=self.name,
            base=base,
            params=params,
            exp=indent(str(self.exp)),
            comments=comments
        )
Ejemplo n.º 6
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,
        )
Ejemplo n.º 7
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,
                      )
Ejemplo n.º 8
0
    def __str__(self):
        options = [ustr(o) for o in self.options]

        multi = any(len(o.splitlines()) > 1 for o in options)
        single = ' | '.join(o for o in options)

        if multi:
            return '\n|\n'.join(indent(o) for o in options)
        elif len(options) and len(single) > PEP8_LLEN:
            return '  ' + '\n| '.join(o for o in options)
        else:
            return single
Ejemplo n.º 9
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)
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
 def indent(self, item, ind=1, multiplier=4):
     return indent(self.rend(item), indent=ind, multiplier=4)
Ejemplo n.º 12
0
 def indent(self, item, ind=1, multiplier=4):
     return indent(self.rend(item), indent=ind, multiplier=4)
Ejemplo n.º 13
0
 def __str__(self):
     sexp = ustr(self.exp)
     if len(sexp.splitlines()) <= 1:
         return '{%s}' % sexp
     else:
         return '{\n%s\n}' % indent(sexp)
Ejemplo n.º 14
0
 def __str__(self):
     exp = ustr(self.exp)
     if len(exp.splitlines()) > 1:
         return '(\n%s\n)' % indent(exp)
     else:
         return '(%s)' % trim(exp)