Skip to content

Getting the parsed AST. #89

Closed
Closed

Description

@Yzupnick

Is there any way to get the parsed AST from a Lua script?

I am looking into making a visual editor for the lua scripts that are embedded into my app.
I'm still thinking through the idea, but I thought translating back and forth using the AST might be a good way to go.

Activity

  1. davydog187 commented on Aug 22, 2025

    @davydog187
    Contributor

    I've been wanting to expose the AST for a while.

    I'm pretty sure this is possible but I haven't explored it yet.

  2. davydog187 commented on Feb 10, 2026

    @davydog187
    Contributor

    @Yzupnick in the upcoming version of Lua, we have implemented our own parser that returns an AST, with functionality for manipulating the AST. I will let you know when it has been released, or you can test on main. I am still doing a lot of testing and cleanup, so use at your own caution!

  3. added this to the 1.0.0 milestone on May 28, 2026
  4. davydog187 commented on Jun 1, 2026

    @davydog187
    Contributor

    @Yzupnick in 1.0.0 you will be able to do

    iex(1)> Lua.Parser.parse("return 2 + 2")
    {:ok,
     %Lua.AST.Chunk{
       block: %Lua.AST.Block{
         stmts: [
           %Lua.AST.Statement.Return{
             values: [
               %Lua.AST.Expr.BinOp{
                 op: :add,
                 left: %Lua.AST.Expr.Number{
                   value: 2,
                   meta: %Lua.AST.Meta{
                     start: %{line: 1, column: 8, byte_offset: 7},
                     end: nil,
                     metadata: %{}
                   }
                 },
                 right: %Lua.AST.Expr.Number{
                   value: 2,
                   meta: %Lua.AST.Meta{
                     start: %{line: 1, column: 12, byte_offset: 11},
                     end: nil,
                     metadata: %{}
                   }
                 },
                 meta: %Lua.AST.Meta{
                   start: %{line: 1, column: 10, byte_offset: 9},
                   end: nil,
                   metadata: %{}
                 }
               }
             ],
             meta: %Lua.AST.Meta{
               start: %{line: 1, column: 1, byte_offset: 0},
               end: nil,
               metadata: %{}
             }
           }
         ],
         meta: nil
       },
       meta: nil
     }}

    To get the AST, and there are tools such as https://lua.hexdocs.pm/1.0.0-rc.0/Lua.AST.Walker.html for working with the AST. Let me know if you have any feedback

  5. davydog187 commented on Jun 15, 2026

    @davydog187
    Contributor

    Done — Lua.Parser.parse_structured/1 shipped in 1.0.0-rc.3 (#363). It returns {:ok, Chunk.t()} | {:error, [%Lua.Parser.Error{}]}, giving the parsed AST directly (with structured, JSON-serializable errors for tooling) — covering the visual-editor / AST round-trip use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions