Comments (10)
Hi,
Transformers, generally speaking, only return a value instead of changing the tree in-place.
So I suspect that
tree = HaxeTransformer().transform(tree)
Should resolve this issue.
If you only want to make in-place changes, you can use Visitor, which performs slightly better.
from lark.
That seems to do it. Thanks for this.
Are there any examples demonstrating how to use Visitor?
from lark.
Lark uses it to process the grammar: https://github.com/erezsh/lark/blob/master/lark/load_grammar.py#L166
You can also read the implementation, it's only a dozen lines: https://github.com/erezsh/lark/blob/master/lark/tree.py#L149
from lark.
Thanks for the visitor example. It looks like there's no real requirement per-se, just a visit
method you can call however you like. (When I used to write Java code, visitor pattern
meant something very specific.)
This works for me.
from lark.
I looked through the visitor source code and created a simple version that looks something like this:
from lark.tree import Visitor
class HaxeVisitor(Visitor):
def process(self, tree):
return self.visit(tree)
def import_stmt(self, data):
print("IMPORT {}".format(data))
def compound_stmt(self, data):
print("COMP".format(data))
This code looks exactly identical to the Transformer
that I wrote above, down to the same method names (and probably implementations -- tree nodes look identical).
What exactly is the difference between a transformer and a visitor, and how do I choose which one to use? They look the same to me (create a subclass, write method names that correspond to the grammar rules, return tranformed output).
from lark.
The difference is between an in-place change (imperative approach) and returning a new copy (functional approach).
It's like the difference between list.sort
and sorted(list)
, both of which are orthogonal Python constructs.
from lark.
That makes sense. I thought the differences would be bigger.
Thanks, I'll go with a visitor approach instead (seems more pythonic to me).
from lark.
Just wanted to say thanks again for providing this library (and the examples). In basically a week, I got a simple Python file (hand-converted from a Haxe template) compiling and transpiling fully into virtually identical Haxe code. There were some bumps, but it wasn't as bad as I thought overall.
from lark.
Happy to hear it!
I aim to make Lark as user-friendly as possible. So if the bumps are related to Lark, I'd like to hear about them.
from lark.
With Lark specifically, I didn't find documentation on what I wanted to know: the .value
and .children
parameters are really the main ones that I used to get the data I want and tranform.
The bigger hurdle for me was that I have a fully-formed Python grammar and a partial transformer; I ended up in situations where I needed to change a bunch of nodes at the same time, quickly (prototype style) or end up with runtime failures (can't convert tree/node into string in ",".join(...)
from lark.
Related Issues (20)
- Transforming tree after standalone parser results in different AST HOT 4
- Making a comment by using regular expression HOT 5
- earley very, very slow HOT 24
- Cant read `meta` from Tree or Token? HOT 5
- How to define lark grammar for best parsing performance HOT 8
- Unable to parse Arabic text HOT 3
- Incorrect start_pos / end_pos in the tree HOT 8
- Add `outlines` in the list of projects using Lark HOT 2
- Lark.open_from_package() does not support namespace packages HOT 2
- Stand-alone program cannot be run HOT 4
- Issue of installing lark in Python HOT 1
- Pipe in terminal regex not working as expected HOT 1
- Transformer Not Applying Expected Transformations in Lark Parser HOT 3
- Deprecation Warning HOT 6
- accepts() vs choices() in InteractiveParser HOT 10
- No such file or directory: 'COMMON.lark' HOT 4
- Grammar Syntax For Unordered Groups HOT 1
- Is it possible to parse parts of the input? HOT 12
- Forgiving syntax HOT 3
- Post 1388 changes HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lark.