My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

.. index:: patterns
.. _style:

Patterns
========

Some guidelines for writing good parsers.


.. index:: operators

Operators are Matchers
----------------------

Remember that operators are just shorthand for matchers (``&`` instead of
``And()`` etc). You don't have
to use operators --- see the discussion on :ref:`caveatsandlimitations`.

But remember that ``&`` and ``And()`` *do differ* when using
:ref:`separators`.


.. index:: Or()
.. _complexor:

Use Or() With Complex Alternatives
----------------------------------

Use `Or(..., ..., ...) <api/redirect.html#lepl.matchers.combine.Or>`_ for
alternatives with productions. The ``|`` syntax can lead to errors because it
binds more tightly than ``>``.


.. index:: Apply()
.. _applycase:

``>`` Capitalised; ``>>`` lowercase
-----------------------------------

Nodes are typically made with constructors and invoked with ``>``, while the
processing of results is usually done with mapped functions. So ``>`` is
followed by a Capitalised name, while ``>>`` is followed by a lowercase name.
Noticing and following this convention can help avoid issues with the
behaviour of ``Apply()`` with
``raw=False`` (the default implementation of ``>``), which adds an extra level
of nesting and is usually inappropriate for use with functions.

``throw`` can be used with ``>>`` or ``>``.


.. index:: Separator()
.. _separator:

Define Words Before Using DroppedSpace (or Separator)
-----------------------------------------------------

``DroppedSpace()`` and ``Separator()``
simplify the handling of spaces in the grammar. To avoid confusion, split
your grammar into two. The first part, defining words, should come before
``with``; the second part should come after.

>>> # words defined here
>>> word = Letter()[:,...]
>>> with DroppedSpace():
>>> # sentences defined here
>>> sentence = word[1:]


Explicitly Exclude Spaces
-------------------------

By default, repetition in Lepl is greedy. This means that, no matter what
``Separator()`` is used, `Any()[:]
<api/redirect.html#lepl.match.Any>`_ will swallow the *entire* input.

So handling spaces in a grammar takes two steps:

1. Exclude the spaces from matchers that produce results.

2. Use ``Separator()`` to "mop up" the
input that remains.


.. index:: Delayed()

Use Delayed() For Recursive Definitions
---------------------------------------

Sometimes a grammar needs to refer to matchers before they are defined. The
``Delayed()`` matcher acts as a
placeholder which can be passed to other functions. It can be defined later
using ``+=``::

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


Imports
-------

The most commonly used classes are exposed via the ``lepl`` module, so simple
scripts can use::

from lepl import *

Change log

8f2692346ba5 by andrew on Mar 19, 2011   Diff
docs without api links
Go to: 
Project members, sign in to write a code review

Older revisions

6f04b29ce870 by and...@small.europa2189 on Apr 1, 2010   Diff
more docs (inc config example)
153bce972159 by and...@small.europa2189 on Mar 15, 2010   Diff
more docs; some cleaning of streams,
error messages
bd2d17a00155 by and...@small.europa2189 on Feb 20, 2010   Diff
rearranging docs
All revisions of this file

File info

Size: 3014 bytes, 112 lines
Powered by Google Project Hosting