Giter Site home page Giter Site logo

Comments (15)

andremm avatar andremm commented on June 27, 2024

Basically, you need to traverse the AST producing the target code that corresponds to the AST format.

What exactly do you want to do? Is Lua both your target and source code?

from lua-parser.

tst2005 avatar tst2005 commented on June 27, 2024

Hello,

I'm working on sh to ast and ast to sh (see lua-sh-parser).

I planned to use the same util (ast-renderer.lua) to support more languages. My next targeted language is lua.
Let me know if I must release sooner as planned ;)

Regards,

from lua-parser.

andremm avatar andremm commented on June 27, 2024

@tst2005 I'm sorry for the delay, but I could not access the file ast-render.lua. Do you need any help from the lua-parser side or can I close this?

from lua-parser.

stuta avatar stuta commented on June 27, 2024

I'm planning to use this as code changer. First I parse code to ast, then change ast but I need changed code back to Lua code. Are there some existing tools that convert AST back to Lua code (maybe in metalua project)? This could also be used as very good code beautifier.

from lua-parser.

andremm avatar andremm commented on June 27, 2024

I'm planning to use this as code changer. First I parse code to ast, then change ast but I need changed code back to Lua code. Are there some existing tools that convert AST back to Lua code (maybe in metalua project)? This could also be used as very good code beautifier.

@stuta I'm not sure whether there are Lua code generators out there, but perhaps our code generator for Typed Lua can help:

https://github.com/andremm/typedlua/blob/master/typedlua/tlcode.lua

from lua-parser.

stuta avatar stuta commented on June 27, 2024

It seems somehow ok (with few bug fixes).

There is line function code_block():
if block[1] and block[1].l and block[1].l > firstline then
fmt.indent = fmt.indent + 1

Lua-parse AST does not seem to contain .l -tag (I guess it is line number)?

from lua-parser.

stuta avatar stuta commented on June 27, 2024

I tested lua-minify and metalua, none has good ast-to-source tools. I think it is best to use ast pos -tag and string.sub to generate fixed source from original source. If someone has example code of this kind of source generation I would like to see it.

I want to use this tool to change parameter order and delete parameters in function calls.

from lua-parser.

pkulchenko avatar pkulchenko commented on June 27, 2024

@stuta, did you get the code generation working for you? I got tlcode working and since I don't care for preserving the exact syntax/indentation, it should work for me (or at least I didn't come across any issues yet). It would be nice to be able to support a full round trip though.

from lua-parser.

pkulchenko avatar pkulchenko commented on June 27, 2024

Actually, given the discussion in #12, it seems like tlcode needs to be extended to cover ne, gt, and ge operators.

@andremm, any plans to include tlcode logic as one of pp outputs?

from lua-parser.

stuta avatar stuta commented on June 27, 2024

I created a new code generator by modifying typedlua/tlcode.lua heavly. I tried lua-parser, lua-minify and metalua as parser but luacheck was the best. It also gives errors on the same scan.

I used is as a tool to change function parameters (add, remove or delete) and automatically add or remove require's to the top of the file. There are a lot of possibilities.

It has been tested on my project that has 880 lua files, 265000 total lines and 208000 lines of code (not comments or blank).

It still fails on some special cases like this:

value = fn.iter(val):reduce(function(acc, rec)
  if acc == "" then
    return toStr(rec)
  end
  return acc..", "..toStr(rec)
end, "")

==>

value = fn.iter(val):reduce(function(acc, rec) ,
  if acc == "" then
    return toStr(rec)
  end
  return acc..", "..toStr(rec)
end"")

I did it when I was sick and fixing non-working project bit by bit was not nice. Also, it is not well parametrized. So code is not pretty or easy to follow.

I have been too busy to work on this. But if you want it I can give it to you. Maybe you could clean it an publish as open source.

from lua-parser.

pkulchenko avatar pkulchenko commented on June 27, 2024

Does luacheck generate compatible AST? Its comment says "Returns AST (in almost MetaLua format)", but I'm not sure how much "almost" here makes it deviate from Metalua/lua-parser format.

It still fails on some special cases like this:

Wouldn't this be a parser issue? Maybe the generated AST is wrong (and not ast2code conversion)?

I have been too busy to work on this. But if you want it I can give it to you. Maybe you could clean it an publish as open source.

Yes, I'd be interested in taking a look. You can email it to me to the address in the profile. Thank you.

from lua-parser.

stuta avatar stuta commented on June 27, 2024

Does luacheck generate compatible AST? Its comment says "Returns AST (in almost MetaLua format)", but I'm not sure how much "almost" here makes it deviate from Metalua/lua-parser format.

They are very similar, just a few minor differences, I can't remember what.

It still fails on some special cases like this:

Wouldn't this be a parser issue? Maybe the generated AST is wrong (and not ast2code conversion)?

Ast is good, my ast2code logic is not.

I have been too busy to work on this. But if you want it I can give it to you. Maybe you could clean it an publish as open source.

Yes, I'd be interested in taking a look. You can email it to me to the address in the profile. Thank you.

I'll contact you later. I need to create first a standalone code without all possible libraries.

from lua-parser.

stuta avatar stuta commented on June 27, 2024

Loot at https://github.com/Koihik/LuaFormatter/blob/master/docs/Style-Config.md.

This project seems active and good. I tested with my hardest problems and it handles almost all of them. It also has quite a good configuration. I think I'll use it for formatting code.

from lua-parser.

stuta avatar stuta commented on June 27, 2024

Loot at https://github.com/Koihik/LuaFormatter/blob/master/docs/Style-Config.md.

This project seems active and good. I tested with my hardest problems and it handles almost all of them. It also has quite a good configuration. I think I'll use it for formatting code.

I have tested it more and it does not give enough freedom with options. I updated my own code for the latest Luacheck and it's almost complete now. I need some help for the ZeroBrane plugin, it works but it needs ui options and text select fixes.

from lua-parser.

pkulchenko avatar pkulchenko commented on June 27, 2024

@stuta, send me an email or open ZBS ticket on github with details on what fixes are needed.

from lua-parser.

Related Issues (12)

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.