Skip to content

Optional code context linked with ast node #7

Description

@alexeyknyshev

I've faced a problem in reproducing original lua source code from AST. There is a pos field in AST node but is it possible to add optional code context? For example:
I have the following original code:

if a == 3 and b then end

and corresponding AST for it;

{ { { "and", { "eq", { "a",
          pos = 4,
          tag = "Id"
        }, { 3,
          pos = 9,
          tag = "Number"
        },
        pos = 4,
        tag = "Op"
      }, { "b",
        pos = 15,
        tag = "Id"
      },
      pos = 4,
      tag = "Op"
    }, {
      pos = 22,
      tag = "Block"
    },
    pos = 1,
    tag = "If"
  },
  pos = 1,
  tag = "Block"
}

I want to restore orig code for if condition body. On one hand I believe that I can do it via Breadth-first search on the AST, but some info can be missing: some binary comparation operators transformed in non-revertable AST such as >= & <=, etc. Lua comments are dropped and so on.

So as the result there is a bunch of questions:

  1. Is it possible to add some tag and parser for comments to reflect them in AST?
  2. Maybe we should preserve original code format in AST. I mean not combining commutative operators in same tags (comparation operators, constructions like not a == b, etc)
  3. Is it feasible to add original code context to AST node?
  4. Maybe optionally save whilespaces in AST? Can be useful for code structure (indentation based) linting / static analysis.

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions