Skip to content

Can I get ast from preprocessor? #104

Answered by edubart
codehz asked this question in Q&A
Discussion options

You must be logged in to vote

I want to transform ast before injecting

This functionality is not officially supported yet, but you can "produce" an AST from a block of statements with some meta programming tricks:

##[[
-- Utility to help getting a ast from a block of statements.
local function produce_ast(f)
  -- lets hook inject_statement to capture the node
  local inject_statement = ppcontext.inject_statement
  local node
  function ppcontext.inject_statement(_, statnode)
    if node then error 'please use multiple statements inside a `do` block' end
    node = statnode
  end
  f()
  ppcontext.inject_statement = inject_statement
  return node
end
]]

## local myast = produce_ast(function()
  print("hello")
## end)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by edubart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants