Neon with AST parser and Format-preserving Printing
- David Grudl
- Nette Core | 8253
NEON has a completely rewritten parser that generates an AST tree. Even the export uses AST trees. This gives the library a few new features.
This already works in version 3.3.0, only the classes listed are marked
as @internal
because the interface may still change.
AST parser
Parsing text into AST nodes can be done using:
Finish processing and return a PHP value can be done with
toValue()
:
Or conversely, to generate the NEON format can be done using
toString()
:
Which returns a string essentially identical to $input
.
You can iterate (traverse) over individual nodes and modify their values. For
example, this is how to add a key of the form key#
to all elements
in the array if they don't have a key:
This would change the original $input
to:
Format-preserving Printing
The new parser gives the possibility to create format-preserving printing as well. This is implemented in the test branch for now.
Source file foo.neon
:
We update its contents while preserving the formatting:
Writes a new foo.neon
: