kaby76 / Domemtech.StringTemplate4

C# StringTemplate, v4
BSD 3-Clause "New" or "Revised" License
8 stars 3 forks source link

StringTemplate4 does not handle embedded open and close curly braces. #2

Closed kaby76 closed 1 year ago

kaby76 commented 1 year ago

I want to do something like this:

const fs = require('fs');

// Read package.json
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));

// Extract the antlr4 version
const antlrVersion = packageJson.dependencies.antlr4.replace('^', '');

// Run the command with the antlr4 version
const { exec } = require('child_process');
<tool_grammar_tuples:{x |

exec("antlr4 -v antlrVersion -encoding <antlr_encoding> -Dlanguage=JavaScript <x.AntlrArgs> <antlr_tool_args:{y | <y> } > <x.GrammarFileName>", (err, stdout, stderr) => \{
    if (err) \{
        // node couldn't execute the command
        return;
    \}

    console.log("stdout: $\{stdout\}");
    console.log("stderr: $\{stderr\}");
} >
});

In this template file, I want to instantiate some Javascript code that expands the call to "antlr4" for all .g4 files. But, StringTemplate won't allow open and close curlies in the template expansion for Anonymous subtemplates. If I escape the curlies using \{ and \} as I done above, the code is expanded, but StringTemplate does not remove the backslash of \{ or \} after expansion.

kaby76 commented 1 year ago

Looks like the template grammar is Antlr3, and it's only for the parser side. The lexer for the grammar is hand-written code.

kaby76 commented 1 year ago

Not a bug. Looks like you only need to escape the closing curly brace.