Skip to content

Commit

Permalink
Include a few examples/clarifications in README.
Browse files Browse the repository at this point in the history
  • Loading branch information
metatoaster committed Jul 17, 2018
1 parent 9755795 commit 4110a41
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,18 @@ immediate access to the parsing feature. It may be used like so:
... console.log(main('world'));
... '''
>>> program = es5(program_source)
>>> program # for a simple repr-like nested view of the ast
>>> # for a simple repr-like nested view of the ast
>>> program # equivalent to repr(program)
<ES5Program @3:1 ?children=[
<VarStatement @3:1 ?children=[
<VarDecl @3:5 identifier=<Identifier ...>, initializer=<FuncExpr ...>>
]>,
<ExprStatement @7:1 expr=<FunctionCall @7:1 args=<Arguments ...>,
identifier=<DotAccessor ...>>>
]>
>>> print(program) # automatic reconstruction of ast into source
>>> # automatic reconstruction of ast into source, without having to
>>> # call something like `.to_ecma()`
>>> print(program) # equivalent to str(program)
var main = function(greet) {
var hello = "hello " + greet;
return hello;
Expand Down Expand Up @@ -503,7 +506,7 @@ Object assignments from a given script file:
.. code:: python
>>> from calmjs.parse import es5
>>> from calmjs.parse.asttypes import Object, VarDecl
>>> from calmjs.parse.asttypes import Object, VarDecl, FunctionCall
>>> from calmjs.parse.walkers import Walker
>>> walker = Walker()
>>> declarations = es5(u'''
Expand All @@ -514,12 +517,17 @@ Object assignments from a given script file:
... v: "value"
... }
... };
... foo({foo: "bar"});
... function bar() {
... var t = {
... foo: "bar",
... }
... };
... return t;
... }
... foo.bar = bar;
... foo.bar();
... ''')
>>> # print out the object nodes that were part of some assignments
>>> for node in walker.filter(declarations, lambda node: (
... isinstance(node, VarDecl) and
... isinstance(node.initializer, Object))):
Expand All @@ -534,6 +542,13 @@ Object assignments from a given script file:
{
foo: "bar"
}
>>> # print out all function calls
>>> for node in walker.filter(declarations, lambda node: (
... isinstance(node, FunctionCall))):
... print(node.identifier)
...
foo
foo.bar
Further details and example usage can be consulted from the various
docstrings found within the module.
Expand Down

0 comments on commit 4110a41

Please sign in to comment.