desugar-position enforces a traversal strategy topdown(repeat(...)) for the desugar strategy:

desugar-position(desugar|ast):
  position -> position'
  where
    ast'  := <at-position(!<id>{MARKER()}|position)> ast;
    ast'' := <topdown(repeat(preserve-annos({?x; desugar; not(?x)})))> ast';
    position' := <position-of-term({?_{a*}; <one(?MARKER())> a*})> ast''

In my Lua plugin I’m now desugaring a few constructs from one statement into multiple (two) statements.
E.g. local function foo()...end becomes local foo; foo = function()...end.

I do this because:

  1. It’s really documented as syntactic sugar
  2. It makes name analysis easier, as function declarations completely disappear (= reduced to variable declaration + assignment of an anonymous function to the variable)

Now the problem is: I have two (relevant) desugaring rules. One transforms the single statement into a list of two statements, and one that flattens lists of statements.

desugar:
  LocalFunctionDecl(...) -> [LocalVariableDecl(...), Assignment(...)]
  where ...

desugar:
  Chunk(x) -> Chunk(y)
  where
    y := <flatten-list> x;
    not(<equal> (x, y))

As such, I need some traversal strategy that goes from bottom to top; I chose innermost:

desugar-top = innermost(desugar)

That works all fine, except when I want to map a position from raw AST to desugared AST, because then desugar-position will either:

  1. apply the innermost traversal at every node of the tree (if I pass desugar-top as desugar strategy), in topdown manner, resulting in way too much traversals (but correct behaviour) or
  2. apply the desugar strategy in the wrong order (if I pass desugar as desugar strategy), so that lists aren’t flattened, and an incorrect position is returned

I workaround it now using a changed desugar-position (using innermost).

So, the question is: is the approach I’m taking (approximately) correct? If yes, then why is topdown(repeat(…)) enforced by desugar-position? If not, what would be a better approach?

Submitted by Tobi Vollebregt on 4 October 2010 at 01:39

On 5 October 2010 at 14:59 Lennart Kats closed this issue.

On 5 October 2010 at 14:59 Lennart Kats commented:

The approach is fine. An alternative is to apply some strategy like <innermost(desugar)> from your Chunk desugar rule, but I’m not sure if desugar-position would like that either. Do you really need desugar-position though? If you return your desugared AST from editor-analyze you shouldn’t really need it.

Log in to post comments