parser

Manual:Line-Aware Parsing and the Offside Rule:Offside Rule and Blocks

まんま >>> from lepl import * >>> introduce = ~Token(':') >>> word = Token(Word(Lower())) >>> statement = Delayed() >>> simple = BLine(word[:]) >>> empty = Line(Empty()) >>> block = BLine(word[:] & introduce) & Block(statement[:]) >>> stat…

Manual:Line-Aware Parsing and the Offside Rule:Lines and Continuations

まんま >>> from lepl import * >>> words = Token(Word(Lower()))[:] > list >>> line = Line(words) >>> parser = line.string_parser(LineAwareConfiguration(tabsize=4)) >>> parser('\tabc def') [['abc', 'def']] >>> words = Token(Word(Lower()))[:]…

Manual:Line-Aware Parsing and the Offside Rule:Indent and Eol Tokens

まんま >>> from lepl import * >>> words = Token(Word(Lower()))[:] > list >>> line = Indent() & words & Eol() >>> parser = line.string_parser(LineAwareConfiguration(tabsize=4)) >>> parser('\tabc def') [' ', ['abc', 'def'], ''] >>> parser('\…

Manual:Line-Aware Parsing and the Offside Rule:Line Aware Alphabet

LEPL-3.1 だと入っていなかったので、LEPL-3.3.3 にまんま >>> from lepl import * >>> start = SOL() & Space()[:, ...] >>> words = Word()[:,~Space()[:]] > list >>> end = EOL() >>> line = start & words & end >>> parser = line.string_parser(LineA…

Manual:Lexer:Advanced Options

まんま >>> from lepl import * >>> abc = Token('abc') >>> incomplete = abc(Literal('ab')) >>> incomplete.parse('abc') >>> abc = Token('abc') >>> incomplete = abc(Literal('ab'), complete=False) >>> incomplete.parse('abc') ['ab']

Manual:Lexer:Limitations

まんま >>> from lepl import * >>> matchers = (Integer() | Literal('-'))[:] & Eos() >>> list(matchers.match('1-2')) [(['1', '-2'], ''), (['1', '-', '2'], '')] >>> tokens = (Token(Integer()) | Token(r'\-'))[:] & Eos() >>> list(tokens.match('…

Manual:Lexer:Use

まんま >>> from lepl import * >>> name = Token('[A-Z][a-z]*') >>> number = Token(Integer()) >>> params = Token('()') >>> function = Token('[a-z]*') >>> sin = function('sine') >>> cos = function('cosine') >>> call = (sin | cos) & params >>>…

Manual:Debugging:Trace Output

まんまではエラーになるようなので、id:noritsugu:20090802:parser のコードに Trace を入れて >>> from lepl import * >>> from lepl.matchers import * >>> from logging import basicConfig, INFO >>> basicConfig(level=INFO) >>> name = Word() > 'name…

Manual:Debugging:Deepest Matches

まんま >>> from lepl import * >>> from lepl.matchers import * >>> from logging import basicConfig, INFO >>> basicConfig(level=INFO) >>> name = Word() >= 'name' >>> phone = Integer() >= 'phone' >>> line = name / ',' / phone >= make_dict >>>…

Manual:Debugging:Logging

id:noritsugu:20090726:parser のコードを使って >>> 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 >…

Manual:Resource Management

まんま >>> from lepl import * >>> matches = (Literal('*')[:,...][2] & Eos()).match('*' * 4) >>> list(matches) [(['****'], ''), (['***', '*'], ''), (['**', '**'], ''), (['*', '***'], ''), (['****'], '')] >>> config = Configuration(monitors=…

Manual:Error Reporting:Operators, Functions and Classes:make_error

>>> from lepl import * >>> make_error("foo") <function fun at 0xb7acad2c> >>> make_error("foo")() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: fun() takes exactly 3 positional arguments (0 given) >>> make_error("foo")("abc", "bc", ["a"]) Error(.</module></stdin></function>…

Manual:Error Reporting:Operators, Functions and Classes:throw

>>> from lepl import * >>> throw() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: throw() takes exactly 1 positional argument (0 given) >>> throw("foo") 'foo'</module></stdin>

Manual:Error Reporting:Operators, Functions and Classes:Error

使い方が分からない…。引数自体も良く分からないのだけど >>> from lepl import * >>> Error('a', 'b', 'c') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/Python-3.1/lib/python3.1/site-packages/lepl/error.py", line 110,</module></stdin>…

Manual:Error Reporting:Operators, Functions and Classes:raise_error

>>> from lepl import * >>> raise_error("foo") <function fun at 0xb7a5bc6c> >>> raise_error("foo")() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: fun() takes exactly 3 positional arguments (0 given) >>> raise_error("foo")("abc", "bc", ["a"]) Trac</module></stdin></function>…

Manual:Error Reporting:Example

まんま >>> from lepl import * >>> class Term(Node): pass ... >>> class Factor(Node): pass ... >>> class Expression(Node): pass ... >>> expr = Delayed() >>> number = Digit()[1:,...] > 'number' >>> badChar = AnyBut(Space() | Digit() | '(')[1…

Manual:Results:Trees

まんま >>> from lepl import * >>> class Term(Node): pass ... >>> class Factor(Node): pass ... >>> class Expression(Node): pass ... >>> expr = Delayed() >>> number = Digit()[1:,...] > 'number' >>> with Separator(r'\s*'): ... term = number |…

Manual:Results:Nested Lists

まんま >>> from lepl import * >>> expr = Delayed() >>> number = Digit()[1:,...] >>> with Separator(Drop(Regexp(r'\s*'))): ... term = number | (Drop('(') & expr & Drop(')') > list) ... muldiv = Any('*/') ... factor = (term & (muldiv & term)…

Manual:Results:Flat List

まんま >>> from lepl import * >>> expr = Delayed() >>> number = Digit()[1:,...] >>> with Separator(r'\s*'): ... term = number | '(' & expr & ')' ... muldiv = Any('*/') ... factor = term & (muldiv & term)[:] ... addsub = Any('+-') ... expr …

Manual:Operators:Spaces

かなり、まんま >>> from lepl import * >>> with Separator(Literal(' ')[:]): ... parser = Optional('a') & Optional('b') & 'c' & Eos() ... >>> parser.parse("a b c") ['a', ' ', 'b', ' ', 'c'] >>> parser.parse(" b c") [' ', 'b', ' ', 'c'] >>> p…

Manual:Operators:Replacement

まんま >>> from lepl import * >>> with Override(or_=And, and_=Or): ... abcd = (Literal('a') & Literal('b')) | ( Literal('c') & Literal('d')) ... print(abcd.parse_string('ac')) ... print(abcd.parse_string('ab')) ... ['a', 'c'] None >>> prin…

Manual:Operators:Operators That Apply Functions To Results:^

>>> from lepl import * >>> (Literal('a') ^ "foo").parse('abc') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/Python-3.1/lib/python3.1/site-packages/lepl/matchers.py", line 187, in parse return self.null_parser(co</module></stdin>…

Manual:Operators:Operators That Apply Functions To Results:**

>>> from lepl import * >>> def show(stream_in, stream_out, results): ... print('stream_in:', stream_in) ... print('stream_out:', stream_out) ... print('results:', results) ... >>> (Literal('a') ** show).parse('abc') stream_in: abc stream_o…

Manual:Operators:Operators That Apply Functions To Results:>>

>>> from lepl import * >>> def star_show(x): ... print("*", x, "*") ... return x ... >>> (Literal('a') > star_show).parse('abc') * ['a'] * [['a']] >>> (Literal('a') >= star_show).parse('abc') * ['a'] * ['a'] >>> (Literal('a') >> star_show)…

Manual:Operators:Operators That Apply Functions To Results:>

>>> from lepl import * >>> def star_show(x): ... print("*", x, "*") ... return x ... >>> (Literal('a') > star_show).parse('abc') * ['a'] * [['a']] >>> (Literal('a') >= star_show).parse('abc') * ['a'] * ['a'] >>> ((Literal('a') & 'b') > sta…

Manual:Operators:Operators That Apply Functions To Results:>=

>>> from lepl import * >>> (Literal('a') >= lambda x: print(x)).parse('abc') File "<stdin>", line 1 (Literal('a') >= lambda x: print(x)).parse('abc') ^ SyntaxError: invalid syntax >>> Apply(Literal('a'), lambda x: print(x)).parse('abc') ['a'] [No</stdin>…

Manual:Operators:Prefix And Postfix Operators On Matchers:[]

>>> from lepl import * >>> (Literal('a')[]).parse('aaa') File "<stdin>", line 1 (Literal('a')[]).parse('aaa') ^ SyntaxError: invalid syntax >>> (Literal('a')[:]).parse('aaa') ['a', 'a', 'a'] >>> (Literal('a')[2:]).parse('aaa') ['a', 'a', 'a'] >>></stdin>…

Manual:Operators:Prefix And Postfix Operators On Matchers:~

>>> from lepl import * >>> (Literal('a')).parse('abc') ['a'] >>> (~Literal('a')).parse('abc') [] >>> (Literal('a') & ~Literal('b') & Literal('c')).parse('abc') ['a', 'c'] >>> (Literal('a') & Drop(Literal('b')) & Literal('c')).parse('abc') …

Manual:Operators:Binary Operators Between Matchers:%

>>> from lepl import * >>> (Literal('a') % Literal('b')).parse('abc') ['a'] >>> (Literal('a') % Literal('b')).parse('bc') ['b'] 「without backtracking between functions」の箇所不明

Manual:Operators:Binary Operators Between Matchers:|

>>> from lepl import * >>> (Literal('a') | Literal('b')).parse('abc') ['a'] >>> (Literal('a') | Literal('b')).parse('bcd') ['b'] >>> Or(Literal('a'), Literal('b')).parse('abc') ['a']