Search code examples
javascriptmacrossweet.js

Match the end of a block with Sweet.js


I'm trying to write a case macro in sweet.js but I don't know how to match the end of a block. In particular I would like to match the code between the macro call and the end of the block that contains it. For example if my macro is myMacro, and the code is as follows

var foo = function() {
    console.log('log1');
    myMacro(someArg);
    console.log('log2');
    console.log('log3');
}        

I would like to be able to match the lines with 'log2' and 'log3'. My guess is that I can't but I can't find a clear definition how matching works in the documentation (pointers would be appreciated). Is it possible ?


Solution

  • The ... pattern will match tokens to the end of a delimiter (or file if there is no delimiter) so you can do something like:

    macro myMacro {
        rule { $x ... } => {
            function bar() {
                $x ...
            }
        }
    }
    

    This will just stick all the tokens following myMacro up to the closing delimiter inside of the bar function.