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) )
from greencss.lexer.parsers.compound import alter from greencss.lexer.parsers.literals import lit spaces = space * inf delims = delim * inf identifier = (alpha - alnum * inf) / join flag = ('!' - _('not-').opt - alnum * (1, inf)) / join flags = flag - (spaces / 0 - flag) * inf uinteger = digit * (1, inf) / join integer = (_('-').opt - uinteger) / join number = (integer - ('.' - uinteger).opt) / join color_unit = ((digit * (1, 3) / join - _('%').opt) / (lambda c: int(c[0]) * 255 // 100 if len(c) > 1 else int(c[0])) == (lambda c: 0 <= c[0] <= 255)) color_args = color_unit - (W(',') / 0 - color_unit) * 2 color_hunit = (hexdigit * 2 / join / (lambda c: int(c[0], 16)) == (lambda c: 0 <= c[0] <= 255)) color = ((_('#') / 'rgb' - color_hunit * 3) | ((_('rgb') | _('hls') | _('hsv') | _('yiq')) - _('(') / 0 - color_args - _(')') / 0)) pcall = identifier - (A ^ space).braced / join string = A.quoted _units = [ 'em', 'ex', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'deg', 'rad' 'grad', 'ms', 's', 'Hz', 'kHz', '%' ] unit = _(alter(*map(lit, _units)))
identifier = (alpha - alnum * inf) / join flag = ('!' - _('not-').opt - alnum * (1, inf)) / join flags = flag - (spaces/0 - flag) * inf uinteger = digit * (1, inf) / join integer = (_('-').opt - uinteger) / join number = (integer - ('.' - uinteger).opt) / join color_unit = ( (digit * (1, 3) / join - _('%').opt) / (lambda c: int(c[0])*255//100 if len(c) > 1 else int(c[0])) == (lambda c: 0 <= c[0] <= 255) ) color_args = color_unit - (W(',')/0 - color_unit) * 2 color_hunit = ( hexdigit * 2 / join / (lambda c: int(c[0], 16)) == (lambda c: 0 <= c[0] <= 255) ) color = ( (_('#')/'rgb' - color_hunit * 3) | ((_('rgb')|_('hls')|_('hsv')|_('yiq')) - _('(')/0 - color_args - _(')')/0) ) pcall = identifier - (A^space).braced / join string = A.quoted _units = ['em', 'ex', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'deg', 'rad' 'grad', 'ms', 's', 'Hz', 'kHz', '%'] unit = _(alter(*map(lit, _units)))
def __ror__(self, other): parser = compound.alter(_(other).parser, self.parser) return Parser(parser)
def __or__(self, other): ''' Either self or other matches ''' parser = compound.alter(self.parser, _(other).parser) return Parser(parser)