def __mul__(self, other): ''' Repeat self other times (num of (min, max)) ''' if isinstance(other, tuple): min, max = other else: min, max = None, other parser = basic.fail if max == inf: if min is None: min = 0 if min == 0: parser = compound.star(self.parser) elif min == 1: parser = compound.plus(self.parser) elif min > 1: parser = compound.seq(compound.rep(self.parser, min), compound.star(self.parser)) else: if min is None: min = max elif min > max: min, max = max, min if min > -1 and max > 0: if min == 0 and max == 1: parser = tools.opt(self.parser) else: parser = compound.rep(self.parser, min, max) return Parser(parser)
def __mul__(self, other): ''' Repeat self other times (num of (min, max)) ''' if isinstance(other, tuple): min, max = other else: min, max = None, other parser = basic.fail if max == inf: if min is None: min = 0 if min == 0: parser = compound.star(self.parser) elif min == 1: parser = compound.plus(self.parser) elif min > 1: parser = compound.seq( compound.rep(self.parser, min), compound.star(self.parser)) else: if min is None: min = max elif min > max: min, max = max, min if min > -1 and max > 0: if min == 0 and max == 1: parser = tools.opt(self.parser) else: parser = compound.rep(self.parser, min, max) return Parser(parser)
def quoted(char=anychar, quot=charclass('\'"'), esc=lit('\\')): charseq = seq(but(state.check), char) if esc: escseq = seq(skip(esc), alter(quot, esc)) charseq = alter(escseq, charseq) return wrap_parser('quoted')( surround(pipe(star(charseq), join), state.push(quot), state.pop) )
def braced(char=anychar, left=lit('('), right=lit(')'), esc=lit('\\')): charseq = seq(but(right), char) if esc: escseq = seq(skip(esc), alter(right, esc)) charseq = alter(escseq, charseq) return wrap_parser('braced')( surround(pipe(star(charseq), join), left, right) )
def word(w, border='\t '): from greencss.lexer.parsers.compound import seq, star border = star(charclass(border)) return wrap_parser('word', w)( seq(border, lit(w), border) )
def indent(line): indent = skip(Indentation(True)) dedent = skip(Indentation(False)) keep = skip(Indentation(None)) return seq(indent, opt(line), star(seq(keep, line)), peek(dedent))
def commalist(parser, comma=lit(','), wsp=charclass('\t ')): delim = seq(star(wsp), comma, star(wsp)) return wrap_parser('commalist')( seq(parser, star(seq(skip(delim), parser))))