lua-parser icon indicating copy to clipboard operation
lua-parser copied to clipboard

Optional code context linked with ast node

Open alexeyknyshev opened this issue 8 years ago • 3 comments
trafficstars

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.

alexeyknyshev avatar Apr 23 '17 10:04 alexeyknyshev

Hi! Is there any updates?

alexeyknyshev avatar May 09 '17 09:05 alexeyknyshev

Maybe relevant: https://github.com/fab13n/metalua-parser/issues/8

craigbarnes avatar May 09 '17 13:05 craigbarnes

  1. 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)

@alexeyknyshev I'm sorry for the delay, but I removed these combinations due to issue #12 and now the AST have all relational operators.

andremm avatar Sep 15 '17 19:09 andremm