Transpile JASS to C# or Lua.
$ dotnet add package War3Net.CodeAnalysis.TranspilersWar3Net.CodeAnalysis.Transpilers is a .NET library for transpiling JASS (the scripting language used by Warcraft III) to C# or Lua. It is part of the War3Net modding library.
using Microsoft.CodeAnalysis.CSharp.Syntax;
using War3Net.CodeAnalysis.Jass;
using War3Net.CodeAnalysis.Transpilers;
// Parse JASS source code
string jassCode = File.ReadAllText("common.j");
var compilationUnit = JassSyntaxFactory.ParseCompilationUnit(jassCode);
// Transpile to C#
var transpiler = new JassToCSharpTranspiler();
IEnumerable<MemberDeclarationSyntax> members = transpiler.Transpile(compilationUnit);
using CSharpLua;
using War3Net.CodeAnalysis.Jass;
using War3Net.CodeAnalysis.Transpilers;
// Parse JASS source code
string jassCode = File.ReadAllText("war3map.j");
var compilationUnit = JassSyntaxFactory.ParseCompilationUnit(jassCode);
// Configure and use the transpiler
var transpiler = new JassToLuaTranspiler
{
IgnoreComments = false,
IgnoreEmptyDeclarations = true,
KeepFunctionsSeparated = true,
};
// Register type information from native/common.j files
transpiler.RegisterJassFile(commonJCompilationUnit);
transpiler.RegisterJassFile(blizzardJCompilationUnit);
// Transpile to Lua
var luaCompilationUnit = transpiler.Transpile(compilationUnit);
// Render to file
using var fileStream = File.Create("war3map.lua");
using var writer = new StreamWriter(fileStream);
var rendererSettings = new LuaSyntaxGenerator.SettingInfo { Indent = 4 };
var renderer = new LuaRenderer(rendererSettings, writer);
renderer.RenderCompilationUnit(luaCompilationUnit);
using CSharpLua;
using War3Net.CodeAnalysis.Transpilers;
// Create the transpiler and renderer
var transpiler = new JassToLuaTranspiler();
var rendererSettings = new LuaSyntaxGenerator.SettingInfo();
using var writer = new StringWriter();
var polyglotTranspiler = new PolyglotJassToLuaTranspiler(
transpiler,
rendererSettings,
writer);
// Transpile JASS containing //! beginusercode and //! endusercode blocks
string polyglotJass = File.ReadAllText("war3map.j");
polyglotTranspiler.Transpile(polyglotJass);
string luaOutput = writer.ToString();
The main types provided by this library are:
War3Net.CodeAnalysis.Transpilers.JassToCSharpTranspiler - Transpiles JASS syntax trees to C# syntax trees using RoslynWar3Net.CodeAnalysis.Transpilers.JassToLuaTranspiler - Transpiles JASS syntax trees to Lua syntax treesWar3Net.CodeAnalysis.Transpilers.PolyglotJassToLuaTranspiler - Handles JASS files with embedded Lua code blocksWar3Net.CodeAnalysis.Transpilers is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.
This README was generated with the assistance of AI and may contain inaccuracies. Please verify the information and consult the source code for authoritative details.