Debian Codegraph @

README

raw
1 .. -*- mode: rst -*-
2
3 ===============================================================
4 Parsley: A Pattern-Matching Language Based on OMeta and Python
5 ===============================================================
6
7 You can read further docs at: http://parsley.readthedocs.org/en/latest/
8
9 Summary
10 -------
11
12 Parsley is a parsing library for people who find parsers scary or
13 annoying. I wrote it because I wanted to parse a programming language,
14 and tools like PLY or ANTLR or Bison were very hard to understand and
15 integrate into my Python code. Most parser generators are based on LL
16 or LR parsing algorithms that compile to big state machine
17 tables. It was like I had to wake up a different section of my brain
18 to understand or work on grammar rules.
19
20 Parsley, like pyparsing and ZestyParser, uses the PEG algorithm, so
21 each expression in the grammar rules works like a Python
22 expression. In particular, alternatives are evaluated in order, unlike
23 table-driven parsers such as yacc, bison or PLY.
24
25 Parsley is an implementation of OMeta, an object-oriented
26 pattern-matching language developed by Alessandro Warth at
27 http://tinlizzie.org/ometa/ . For further reading, see Warth's PhD
28 thesis, which provides a detailed description of OMeta:
29 http://www.vpri.org/pdf/tr2008003_experimenting.pdf
30
31 How It Works
32 ------------
33
34 Parsley compiles a grammar to a Python class, with the rules as methods. The
35 rules specify parsing expressions, which consume input and return values if
36 they succeed in matching.
37
38 Basic syntax
39 ~~~~~~~~~~~~
40 ``foo = ....``:
41 Define a rule named foo.
42
43 ``expr1 expr2``:
44 Match expr1, and then match expr2 if it succeeds, returning the value of
45 expr2. Like Python's ``and``.
46
47 ``expr1 | expr2``:
48 Try to match ``expr1`` --- if it fails, match ``expr2`` instead. Like Python's
49 ``or``.
50
51 ``expr*``:
52 Match ``expr`` zero or more times, returning a list of matches.
53
54 ``expr+``:
55 Match ``expr`` one or more times, returning a list of matches.
56
57 ``expr?``:
58 Try to match ``expr``. Returns ``None`` if it fails to match.
59
60 ``expr{n, m}``:
61 Match ``expr`` at least ``n`` times, and no more than ``m`` times.
62
63 ``expr{n}``:
64 Match ``expr`` ``n`` times exactly.
65
66 ``~expr``:
67 Negative lookahead. Fails if the next item in the input matches
68 ``expr``. Consumes no input.
69
70 ``~~expr``:
71 Positive lookahead. Fails if the next item in the input does *not*
72 match ``expr``. Consumes no input.
73
74 ``ruleName`` or ``ruleName(arg1 arg2 etc)``:
75 Call the rule ``ruleName``, possibly with args.
76
77 ``'x'``:
78 Match the literal character 'x'.
79
80 ``<expr>``:
81 Returns the string consumed by matching ``expr``. Good for tokenizing rules.
82
83 ``expr:name``:
84 Bind the result of expr to the local variable ``name``.
85
86 ``-> pythonExpression``:
87 Evaluate the given Python expression and return its result. Can be
88 used inside parentheses too!
89
90 ``!(pythonExpression)``:
91 Invoke a Python expression as an action.
92
93 ``?(pythonExpression)``:
94 Fail if the Python expression is false, Returns True otherwise.
95
96 Comments like Python comments are supported as well, starting with #
97 and extending to the end of the line.
98
99 Interface
100 ---------
101
102 The starting point for defining a new grammar is
103 ``parsley.makeGrammar(grammarSource, bindings)``, which takes a grammar
104 definition and a dict of variable bindings for its embedded
105 expressions and produces a Python class. Grammars can be subclassed as
106 usual, and makeGrammar can be called on these classes to override
107 rules and provide new ones. Grammar rules are exposed as methods.
108
109 Example Usage
110 -------------
111
112 ::
113
114 from parsley import makeGrammar
115 exampleGrammar = """
116 ones = '1' '1' -> 1
117 twos = '2' '2' -> 2
118 stuff = (ones | twos)+
119 """
120 Example = makeGrammar(exampleGrammar, {})
121 g = Example("11221111")
122 result = g.stuff()
123 print result
124
125 → ``[1, 2, 1, 1]``