Building AST (Abstract Syntax Tree): Module node

Using Nodes:

Pyrser provide 2 ways to fill nodes:

  • Capture with :

    In the DSL, CREATE a node and STORE the result of a rule in it:

    R = [
        ThisRuleReturnSomethingIn_ : weCaptureInThisNode
    ]
    
    ThisRuleReturnSomethingIn_ = [
        #putSomethingIn(_)
    ]
    
  • Binding with :>

    Change the content of a previously CREATED node to the result of a rule:

    R = [
        Create : aNodeHere
        #showThingReadByCreate(aNodeHere)
        aThing?:>aNodeHere
        #showAThing(aNodeHere)
    ]
    

    You could use __scope__ to create a node from scratch. It’s common to use it with binding:

    R = [
        __scope__:B  //
        Bla?:>B
        __scope__:bim
        [loop]+
    ]
    
    loop = [
        item:i
        #addBim(bim, i) // bim is a node visible here but attached to the scope of R
    ]
    

    In the previous rule, Bla is optional. So his content is only bind to B when Bla is parsed.

Life Cycle and Visibility:

Nodes lives and is visible thru rules in a logical way:

R = [
    // _ is the return of R
    aRule : A
    [
        // A is visible and live
        anotherRule1 : B
        anotherRule2 : C
        |
        // B die
        // C die
        anotherRule3 : D
    ]
    // D die
]
// A die

anotherRule1 = [
    // A live
    // _ is the return of anotherRule1
    doSomething1 #doOn(A)
]

anotherRule2 = [
    // A is visible and lives
    // B is visible and lives
    // _ is the return of anotherRule2
    doSomething2 #doOn(A, B)
]

anotherRule3 = [
    // A is visible and lives
    // _ is the return of anotherRule3
    doSomething3
]

They are no difference between created nodes or binded nodes for the visibility/live cycle.

Python API: class Node

Basic classes for AST manipulation

class pyrser.parsing.node.Node[source]

Bases: dict

Base class for node manipulation.

__bool__(self)[source]
__repr__(self)[source]
check(self, ndict: dict, info= '') → bool[source]

Debug method, help detect cycle and/or other incoherence in a tree of Node

clean(self)[source]
set(self, othernode)[source]

allow to completly mutate the node into any subclasses of Node

to_yml(self)

Allow to get the YML string representation of a Node.:

from pyrser.passes import to_yml

t = Node()
...
print(str(t.to_yml()))
class pyrser.parsing.node.ListNode(it: []=None)[source]

Bases: object

__init__(self, it= None)[source]
append(self, d)[source]
prepend(self, d)[source]
__str__(self) → str[source]
__repr__(self) → str[source]
__len__(self) → int[source]
__iter__(self) → ListNodeItemIterator[source]
__reversed__(self) → ListNodeItemIterator[source]
_trueindex(self, k) → int[source]
_cache(self) → str[source]
_update(self)[source]
index(self, v) → int[source]
count(self, v) → int[source]
get(self, k) → ListNodeItem[source]
__getitem__(self, k) → object[source]
__setitem__(self, k, d)[source]
__delitem__(self, k)[source]
class pyrser.parsing.node.ListNodeItem(data)[source]

Bases: object

__init__(self, data)[source]
__str__(self) → str[source]
__repr__(self) → str[source]
index(self, v) → int[source]
count(self, v) → int[source]
_get_slice(self, sl)[source]
__getitem__(self, k) → object[source]
__setitem__(self, k, d)[source]
__delitem__(self, k)[source]
popitem(self) → object[source]
append(self, data) → ListNodeItem[source]
prepend(self, data) → ListNodeItem[source]
values(self)[source]
rvalues(self)[source]
_fwd(self)[source]
_bwd(self)[source]
class pyrser.parsing.node.ListNodeItemIterator(current: pyrser.parsing.node.ListNodeItem, reverse=False)[source]

Bases: object

__init__(self, current: ListNodeItem, reverse= False)[source]
__iter__(self)[source]
__next__(self)[source]