Esempio n. 1
0
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)
                )
Esempio n. 2
0
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)
                )
Esempio n. 3
0
        if combinator:
            parser = combinator(self.parser)
        return Parser(parser)

    def __getitem__(self, index):
        parser = filters.pipe(self.parser, filters.take(index))
        return Parser(parser)

    def __getslice__(self, first, last):
        parser = filters.pipe(self.parser, lambda t: t[first:last])
        return Parser(parser)


# Match string or char range
_ = lambda word: word if isinstance(word, Parser) else Parser(
    literals.lit(word)
    if isinstance(word, basestring) else literals.charrange(*word)
    if isinstance(word, (list, tuple)) else word)

# Match any character
A = Parser(basic.anychar)

# Match word (omit word bounds)
W = lambda word: Parser(literals.word(word))

# True and false
T = Parser(basic.success)
F = Parser(basic.fail)

# End of file parser
EOF = Parser(basic.end_of_inp)
Esempio n. 4
0
def surround(parser, left=lit('('), right=lit(')')):
    return wrap_parser('surround')(
            seq(skip(left), parser, skip(right)))
Esempio n. 5
0
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))))
Esempio n. 6
0
        combinator = getattr(tools, name, None)
        if combinator:
            parser = combinator(self.parser)
        return Parser(parser)

    def __getitem__(self, index):
        parser = filters.pipe(self.parser, filters.take(index))
        return Parser(parser)

    def __getslice__(self, first, last):
        parser = filters.pipe(self.parser, lambda t: t[first:last])
        return Parser(parser)

# Match string or char range
_ = lambda word: word if isinstance(word, Parser) else Parser(
        literals.lit(word) if isinstance(word, basestring) else
        literals.charrange(*word) if isinstance(word, (list, tuple)) else
        word)

# Match any character
A = Parser(basic.anychar)

# Match word (omit word bounds)
W = lambda word: Parser(literals.word(word))

# True and false
T = Parser(basic.success)
F = Parser(basic.fail)

# End of file parser
EOF = Parser(basic.end_of_inp)