parser

Manual:Operators:Binary Operators Between Matchers://

>>> from lepl import * >>> (Literal('a') + Literal('b')).parse('a b c') >>> (Literal('a') // Literal('b')).parse('abc') >>> (Literal('a') // Literal('b')).parse('a b c') ['a', ' ', 'b'] >>> (Literal('a') // Literal('b')).parse('a b c') ['a…

Manual:Operators:Binary Operators Between Matchers:/

>>> from lepl import * >>> (Literal('a') / Literal('b')).parse('abc') ['a', 'b'] >>> (Literal('a') / Literal('b')).parse('a b c') ['a', ' ', 'b'] >>> (Literal('a') / Literal('b')).parse('a b c') ['a', ' ', 'b'] >>> (Literal('a') + Literal(…

Manual:Operators:Binary Operators Between Matchers:+

>>> from lepl import * >>> (Literal('a') + Literal('b')).parse('abc') ['ab']

Manual:Operators:Binary Operators Between Matchers:&

>>> from lepl import * >>> (Literal('a') & Literal('b')).parse('a') >>> (Literal('a') & Literal('b')).parse('ab') ['a', 'b'] >>> (Literal('a') & Literal('b')).parse('abc') ['a', 'b'] >>> And(Literal('a'), Literal('b')).parse('abc') ['a', '…

Manual:Operators:Caveats and Limitations

ほぼ、まんま >>> from lepl import * >>> name = ('Mr' | 'Ms') // Word() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for |: 'str' and 'str' >>> name = (Literal('Mr') | 'Ms') // Word() >>> pr</module></stdin>…

Manual:Matchers:KApply (**)

>>> from lepl import * >>> def show(stream_in, stream_out, results): ... print('stream_in:', stream_in) ... print('stream_out:', stream_out) ... print('results:', results) ... >>> KApply(Any()[:,...], show).parse_string('hello world') stre…

Manual:Matchers:Apply (>, >=, args)

まんま >>> from lepl import * >>> def show(results): ... print('results:', results) ... return results ... >>> Apply(Any()[:,...], show).parse_string('hello world') results: ['hello world'] [['hello world']] >>> (Any()[:,...] > show).parse…

Manual:Matchers:Drop (~)

ほぼ、まんま >>> from lepl import * >>> (Drop('hello') / 'world').parse_string('hello world') [' ', 'world'] >>> (~Literal('hello') / 'world').parse_string('hello world') [' ', 'world'] >>> (Lookahead('hello') / 'world').parse_string('hell…

Manual:Matchers:Lookahead

まんま >>> from lepl import * >>> next(Lookahead(Literal('hello')).match('hello world')) ([], 'hello world') >>> Lookahead(Literal('hello')).parse('hello world') [] >>> Lookahead('hello').parse_string('goodbye cruel world') >>> (~Lookahead…

Manual:Matchers:Repeat ([...])

まんま >>> from lepl import * >>> Repeat(Any(), 3, 3).parse_string('12345') ['1', '2', '3'] >>> Any()[3:3].parse_string('12345') ['1', '2', '3'] >>> Any()[3].parse_string('12345') ['1', '2', '3'] >>> Repeat(Any(), 3).parse_string('12345') …

Manual:Matchers:Or (|)

まんま >>> from lepl import * >>> Or(Any('x'), Any('h'), Any('z')).parse_string('hello world') ['h'] >>> Or(Any('h'), Any()[3]).parse_string('hello world') ['h'] >>> Or(Any()[3], Any('h')).parse_string('hello world') ['h', 'e', 'l'] >>> ma…

Manual:Matchers:And (&)

まんま >>> from lepl import * >>> And(Any('h'), Any()).parse_string('hello world') ['h', 'e'] >>> And(Any('h'), Any('x')).parse_string('hello world') >>> (Any('h') & Any('e')).parse_string('hello world') ['h', 'e'] >>> (Any('h') & Any()).p…

Manual:Matchers:Any

まんま >>> from lepl import * >>> Any().parse_string('hello world') ['h'] >>> Any('abcdefghijklm')[0:].parse_string('hello world') ['h', 'e', 'l', 'l']

Manual:Matchers:Literal

ほぼ、まんま >>> from lepl import * >>> Literal('hello').parse_string('hello world') ['hello'] >>> hello = Empty() & 'hello' >>> hello.parse_string('hello world') ['hello'] Empty() をこういう用途に使って良いかは不明

Manual:Getting Started:Single Dictionary

まんま >>> from lepl import * >>> def combine(results): ... all = {} ... for result in results: ... all[result['name']] = result['phone'] ... return all ... >>> spaces = Space()[0:] >>> name = Word() > 'name' >>> phone = Integer() > 'phone…

Manual:Getting Started:Repetition

まんま >>> from lepl import * >>> spaces = Space()[0:] >>> name = Word() > 'name' >>> phone = Integer() > 'phone' >>> line = name / ',' / phone > make_dict >>> newline = spaces & Newline() & spaces >>> matcher = line[0:,~newline] >>> match…

Manual:Getting Started:More Detail

まんま >>> from lepl import * >>> name = Word() > 'name' >>> phone = Integer() > 'phone' >>> matcher = name / ',' / phone > make_dict >>> matcher.parse_string('andrew, 3333253')[0] {'phone': '3333253', 'name': 'andrew'} >>> matcher.parse_s…

Manual:Getting Started:Matchers

まんま >>> from lepl import * >>> next( Word().match('hello world') ) (['hello'], ' world') >>> next( Integer().match('123 four five') ) (['123'], ' four five') >>> next( And(Word(), Space(), Integer()).match('hello 123') ) (['hello', ' ',…

Manual:Getting Started:First Example

まんま >>> from lepl import * >>> name = Word() > 'name' >>> phone = Integer() > 'phone' >>> matcher = name / ',' / phone > make_dict >>> parser = matcher.string_parser() >>> parser('andrew, 3333253') [{'phone': '3333253', 'name': 'andrew'…

Tutorial 4 Evaluation

まんま >>> from lepl import * >>> from operator import add, sub, mul, truediv >>> class Op(Node): ... def __float__(self): ... return self._op(float(self[0]), float(self[1])) ... >>> class Add(Op): _op = add ... >>> class Sub(Op): _op = su…

Tutorial 4 Subclassing Node

まんま >>> from lepl import * >>> class Add(Node): pass ... >>> class Sub(Node): pass ... >>> class Mul(Node): pass ... >>> class Div(Node): pass ... >>> symbol = Token('[^0-9a-zA-Z \t\r\n]') >>> value = Token(UnsignedFloat()) >>> negfloat…

Tutorial 4 Efficiency

まんま >>> from lepl import * >>> symbol = Token('[^0-9a-zA-Z \t\r\n]') >>> value = Token(UnsignedFloat()) >>> negfloat = lambda x: -float(x) >>> number = Or(value >> float, ~symbol('-') & value >> negfloat) >>> group2, group3 = Delayed(),…

Tutorial 4 Ambiguity and Left Recursion

まんま >>> from lepl import * >>> symbol = Token('[^0-9a-zA-Z \t\r\n]') >>> value = Token(UnsignedFloat()) >>> negfloat = lambda x: -float(x) >>> number = Or(value >> float, ... ~symbol('-') & value >> negfloat) >>> group2, group3 = Delaye…

Tutorial 4 Operator Precedence

まんま >>> from lepl import * >>> symbol = Token('[^0-9a-zA-Z \t\r\n]') >>> value = Token(UnsignedFloat()) >>> negfloat = lambda x: -float(x) >>> number = Or(value >> float, ... ~symbol('-') & value >> negfloat) >>> group2, group3 = Delaye…

Tutorial 3 Node Attributes

まんま >>> from lepl import * >>> letter = Letter() > 'letter' >>> digit = Digit() > 'digit' >>> example = (letter | digit)[:] > Node >>> n = example.parse('abc123d45e')[0] >>> n.letter ['a', 'b', 'c', 'd', 'e'] >>> n.digit ['1', '2', '3',…

Tutorial 3 Nodes

まんま >>> from lepl import * >>> abc = Node('a', 'b', 'c') >>> abc[1] 'b' >>> abc[1:] ['b', 'c'] >>> abc[:-1] ['a', 'b'] >>> fb = Node(('foo', 23), ('bar', 'baz')) >>> fb.foo [23] >>> fb.bar ['baz'] >>> fb = Node(('foo', 23), ('bar', 'baz…

Tutorial 3 Building an AST with Node

まんま >>> from lepl import * >>> symbol = Token('[^0-9a-zA-Z \t\r\n]') >>> value = Token(UnsignedFloat()) >>> negfloat = lambda x: -float(x) >>> number = Or(value >> float, ... ~symbol('-') & value >> negfloat) >>> expr = Delayed() >>> ad…

Tutorial 3 Delayed Matchers

まんま >>> from lepl import * >>> symbol = Token('[^0-9a-zA-Z \t\r\n]') >>> value = Token(UnsignedFloat()) >>> negfloat = lambda x: -float(x) >>> number = Or(value >> float, ... ~symbol('-') & value >> negfloat) >>> expr = Delayed() >>> ad…

Tutorial 3 Recursion

まんま >>> from lepl import * >>> symbol = Token('[^0-9a-zA-Z \t\r\n]') >>> value = Token(UnsignedFloat()) >>> negfloat = lambda x: -float(x) >>> number = Or(value >> float, ... ~symbol('-') & value >> negfloat) >>> add = number & symbol('…

Tutorial 3 Adding Subtraction

まんま >>> from lepl import * >>> symbol = Token('[^0-9a-zA-Z \t\r\n]') >>> value = Token(UnsignedFloat()) >>> negfloat = lambda x: -float(x) >>> number = Or(value >> float, ... ~symbol('-') & value >> negfloat) >>> add = number & symbol('…