Giter Club home page Giter Club logo

Comments (10)

erezsh avatar erezsh commented on July 30, 2024

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.

nightblade9 avatar nightblade9 commented on July 30, 2024

That seems to do it. Thanks for this.

Are there any examples demonstrating how to use Visitor?

from lark.

erezsh avatar erezsh commented on July 30, 2024

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.

nightblade9 avatar nightblade9 commented on July 30, 2024

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.

nightblade9 avatar nightblade9 commented on July 30, 2024

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.

erezsh avatar erezsh commented on July 30, 2024

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.

nightblade9 avatar nightblade9 commented on July 30, 2024

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.

nightblade9 avatar nightblade9 commented on July 30, 2024

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.

erezsh avatar erezsh commented on July 30, 2024

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.

nightblade9 avatar nightblade9 commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.