StringTemplate グループ ファイルは、1 つのファイルに格納されたテンプレートのコレクションです。GitHub の ANTLR プロジェクトには、多くの例が含まれています。たとえば、ANTLR 4 の Java ターゲット用のすべてのコード生成テンプレートを含むJava.stgです。
StringTemplate C# プロジェクト自体のStringTemplateTests.csファイルで、C# で StringTemplate 3 を使用するいくつかの例を見つけることができます。これは最も分かりやすいドキュメントではありませんが、幅広い ST3 機能をカバーする例が含まれています。を使用した一例を次に示しStringTemplateGroup
ます。
string templates =
"group dork;" + newline +
"" + newline +
"test(name) ::= <<" +
"<(name)()>" + newline +
">>" + newline +
"first() ::= \"the first\"" + newline +
"second() ::= \"the second\"" + newline
;
StringTemplateGroup group =
new StringTemplateGroup( new StringReader( templates ) );
StringTemplate f = group.GetInstanceOf( "test" );
f.SetAttribute( "name", "first" );
string expecting = "the first";
Assert.AreEqual( expecting, f.ToString() );
読みやすいように、そのテストへのテンプレート グループ ファイル コードは、エスケープ文字なしで次のようになります。
group dork;
test(name) ::= <<<(name)()>
>>
first() ::= "the first"
second() ::= "the second"