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)
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)
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)) whitespace = self.node.whitespace or self.node.directives.get( 'whitespace') if not whitespace: whitespace = 'None' elif isinstance(whitespace, RETYPE): whitespace = repr(whitespace) else: whitespace = 're.compile({0})'.format(repr(whitespace)) if self.node.nameguard is not None: nameguard = repr(self.node.nameguard) elif self.node.directives.get('nameguard') is not None: nameguard = self.node.directives.get('nameguard') else: nameguard = 'None' comments_re = repr(self.node.directives.get('comments')) eol_comments_re = repr(self.node.directives.get('eol_comments')) ignorecase = self.node.directives.get('ignorecase', 'None') left_recursion = self.node.directives.get('left_recursion', True) parseinfo = self.node.directives.get('parseinfo', True) namechars = repr(self.node.directives.get('namechars') or '') 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('.'))) keywords = '\n'.join(" %s," % repr(k) for k in sorted(self.keywords)) if keywords: keywords = '\n%s\n' % keywords fields.update( rules=indent(rules), start=self.node.rules[0].name, abstract_rules=abstract_rules, version=version, whitespace=whitespace, nameguard=nameguard, ignorecase=ignorecase, comments_re=comments_re, eol_comments_re=eol_comments_re, left_recursion=left_recursion, parseinfo=parseinfo, keywords=keywords, namechars=namechars, )
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.lookahead()) if f) if firstset: error = 'expecting one of: ' + firstset else: error = 'no available options' fields.update(n=self.counter(), options=indent(options), error=repr(error))
def _to_str(self, lean=False): comments = self.comments_str() if lean: params = '' else: 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: if len(self.params) == 1: params = '::%s' % params else: 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(self.exp._to_str(lean=lean)), comments=comments, is_name='@name\n' if self.is_name else '', )
def render_fields(self, fields): defs = [safe_name(d) for d, l in compress_seq(self.defines())] defs = list(sorted(set(defs))) spec = fields["spec"] kwargs = '\n'.join('%s = None' % d for d in defs) if kwargs: kwargs = indent(kwargs) else: kwargs = indent('pass') fields.update( class_name=spec.class_name, base=spec.base, kwargs=kwargs, )
def render_fields(self, fields): defs = [safe_name(d) for d, l in compress_seq(self.defines())] defs = list(sorted(set(defs))) kwargs = '\n'.join('%s = None' % d for d in defs) if kwargs: kwargs = indent(kwargs) else: kwargs = indent('pass') spec = _typespec(self.node) fields.update( class_name=spec.class_name, base=spec.base, kwargs=kwargs, )
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 = self.node.lookahead_str() if firstset: msglines = textwrap.wrap(firstset, width=40) error = ['expecting one of: '] + msglines else: error = ['no available options'] error = [repr(e) for e in error] fields.update( n=self.counter(), options=indent(options), error=error, )
def _to_str(self, lean=False): options = [ustr(o._to_str(lean=lean)) 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
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()) ldefs = set(safe_name(d) for d, l in defines if l) sdefs = set(safe_name(d) for d, l in defines if not l and d not in ldefs) if not (sdefs or ldefs): sdefines = '' else: sdefs = '[%s]' % ', '.join(urepr(d) for d in sorted(sdefs)) ldefs = '[%s]' % ', '.join(urepr(d) for d in sorted(ldefs)) if not ldefs: sdefines = '\n\n self.ast._define(%s, %s)' % (sdefs, ldefs) else: sdefines = indent( '\n' + trim(self.define_template % (sdefs, ldefs)) ) fields.update(defines=sdefines) fields.update( check_name='\n self._check_name()' if self.is_name else '', ) leftrec = self.node.is_leftrec fields.update(leftrec='\n@leftrec' if leftrec else '') fields.update(nomemo='\n@nomemo' if not self.node.is_memoizable and not leftrec else '')
def make_defines_declaration(self): defines = compress_seq(self.defines()) ldefs = oset(safe_name(d) for d, l in defines if l) sdefs = oset( safe_name(d) for d, l in defines if not l and d not in ldefs) if not (sdefs or ldefs): return '' else: sdefs = '[%s]' % ', '.join(sorted(repr(d) for d in sdefs)) ldefs = '[%s]' % ', '.join(sorted(repr(d) for d in ldefs)) if not ldefs: return '\n\n self._define(%s, %s)' % (sdefs, ldefs) else: return indent('\n' + trim(self.define_template % (sdefs, ldefs)))
def _to_str(self, lean=False): comments = self.comments_str() if lean: params = '' else: 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: if len(self.params) == 1: params = '::%s' % params else: 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(self.exp._to_str(lean=lean)), comments=comments, is_name='@name\n' if self.is_name else '', )
def indent(self, item, ind=1, multiplier=4): return indent(self.rend(item), indent=ind, multiplier=4)
def _to_str(self, lean=False): sexp = ustr(self.exp._to_str(lean=lean)) if len(sexp.splitlines()) <= 1: return '{%s}' % sexp else: return '{\n%s\n}' % indent(sexp)
def _to_str(self, lean=False): exp = self.exp._to_ustr(lean=lean) if len(exp.splitlines()) > 1: return '(\n%s\n)' % indent(exp) else: return '(%s)' % trim(exp)