File: extending.rst

package info (click to toggle)
python-parsley 1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: python: 9,897; makefile: 127
file content (25 lines) | stat: -rw-r--r-- 764 bytes parent folder | download | duplicates (4)
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
==================================
Extending Grammars and Inheritance
==================================

:warning: Unfinished

Another feature taken from OMeta is *grammar inheritance*. We can
write a grammar with rules that override ones in a parent. If we load
the grammar from our calculator tutorial as ``Calc``, we can extend it
with some constants::

    from parsley import makeGrammar
    import math
    import calc
    calcGrammarEx = """
    value = super | constant
    constant = 'pi' -> math.pi
             | 'e' -> math.e
    """
    CalcEx = makeGrammar(calcGrammar, {"math": math}, extends=calc.Calc)


Invoking the rule ``super`` calls the rule ``value`` in Calc. If it
fails to match, our new ``value`` rule attempts to match a constant
name.