From e914af90311b91dead17ea3a83a44220b28291b3 Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Sun, 7 Nov 2010 21:57:24 +0100 Subject: [PATCH] Add extra translation steps to the pipeline to (eventually) produce Java --- CSharpTranslator/antlr3/mkit | 3 + CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs | 328 ++--- .../antlr3/src/cs2j/CSharp/.gitignore | 6 + .../antlr3/src/cs2j/CSharp/CommonWalker.cs | 20 + .../antlr3/src/cs2j/CSharp/JavaMaker.g | 1093 +++++++++++++++++ .../antlr3/src/cs2j/CSharp/JavaPrettyPrint.g | 1046 ++++++++++++++++ .../antlr3/src/cs2j/CSharp/NetMaker.g | 1041 ++++++++++++++++ .../src/cs2j/CSharp/TemplateExtracter.g | 30 +- CSharpTranslator/antlr3/src/cs2j/cs2j.csproj | 8 +- 9 files changed, 3400 insertions(+), 175 deletions(-) create mode 100644 CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g create mode 100644 CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g create mode 100644 CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g diff --git a/CSharpTranslator/antlr3/mkit b/CSharpTranslator/antlr3/mkit index dfd6abe..10100a2 100755 --- a/CSharpTranslator/antlr3/mkit +++ b/CSharpTranslator/antlr3/mkit @@ -5,6 +5,9 @@ set -e cd src/cs2j/CSharp java -Xmx512m -jar ../../../jar/antlr-3.3.jar -make cs.g java -Xmx512m -jar ../../../jar/antlr-3.3.jar -make TemplateExtracter.g +java -Xmx512m -jar ../../../jar/antlr-3.3.jar -make JavaMaker.g +java -Xmx512m -jar ../../../jar/antlr-3.3.jar -make NetMaker.g +java -Xmx512m -jar ../../../jar/antlr-3.3.jar -make JavaPrettyPrint.g cd ../../../ xbuild echo 'All Done' diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs index 20d416f..6800722 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs @@ -18,8 +18,8 @@ namespace RusticiSoftware.Translator.CSharp class CS2J { private const string VERSION = "2009.1.1.x"; - private static DirectoryHT AppEnv { get; set; } - private static CS2JSettings cfg = new CS2JSettings(); + private static DirectoryHT AppEnv { get; set; } + private static CS2JSettings cfg = new CS2JSettings(); public delegate void FileProcessor(string fName); @@ -49,18 +49,18 @@ namespace RusticiSoftware.Translator.CSharp Environment.Exit(0); } - private static void addDirectories(IList strs, string rawStr) { + private static void addDirectories(IList strs, string rawStr) { string[] argDirs = rawStr.Split(';'); for (int i = 0; i < argDirs.Length; i++) strs.Add(Path.GetFullPath(argDirs[i]).ToLower()); - } + } public static void CS2JMain(string[] args) { long startTime = DateTime.Now.Ticks; - IList remArgs = new List(); - XmlTextWriter enumXmlWriter = null; - AppEnv = new DirectoryHT(null); + IList remArgs = new List(); + XmlTextWriter enumXmlWriter = null; + AppEnv = new DirectoryHT(null); // Use a try/catch block for parser exceptions try @@ -71,36 +71,36 @@ namespace RusticiSoftware.Translator.CSharp if (cfg.Verbosity >= 2) Console.Error.WriteLine("Parsing Command Line Arguments..."); - OptionSet p = new OptionSet () - .Add ("v", v => cfg.Verbosity++) - .Add ("debug=", v => cfg.DebugLevel = Int32.Parse(v)) - .Add ("warnings", v => cfg.Warnings = true) - .Add ("version", v => showVersion()) - .Add ("help|h|?", v => showUsage()) - .Add ("dumpcsharp", v => cfg.DumpCSharp = true) - .Add ("dumpjava", v => cfg.DumpJava = true) - .Add ("dumpjavasyntax", v => cfg.DumpJavaSyntax = true) - .Add ("dumptokens", v => cfg.DisplayTokens = true) - .Add ("D=", def => cfg.MacroDefines.Add(def)) - .Add ("dumpenums", v => cfg.DumpEnums = true) - .Add ("enumdir=", dir => cfg.EnumDir = Path.Combine(Directory.GetCurrentDirectory(), dir)) - .Add ("dumpxmls", v => cfg.DumpXmls = true) - .Add ("xmldir=", dir => cfg.XmlDir = Path.Combine(Directory.GetCurrentDirectory(), dir)) - .Add ("odir=", dir => cfg.OutDir = dir) - .Add ("cheatdir=", dir => cfg.CheatDir = dir) - .Add ("netdir=", dirs => addDirectories(cfg.NetRoot, dirs)) - .Add ("exnetdir=", dirs => addDirectories(cfg.ExNetRoot, dirs)) - .Add ("appdir=", dirs => addDirectories(cfg.AppRoot, dirs)) - .Add ("exappdir=", dirs => addDirectories(cfg.ExAppRoot, dirs)) - .Add ("exclude=", dirs => addDirectories(cfg.Exclude, dirs)) - ; + OptionSet p = new OptionSet () + .Add ("v", v => cfg.Verbosity++) + .Add ("debug=", v => cfg.DebugLevel = Int32.Parse(v)) + .Add ("warnings", v => cfg.Warnings = true) + .Add ("version", v => showVersion()) + .Add ("help|h|?", v => showUsage()) + .Add ("dumpcsharp", v => cfg.DumpCSharp = true) + .Add ("dumpjava", v => cfg.DumpJava = true) + .Add ("dumpjavasyntax", v => cfg.DumpJavaSyntax = true) + .Add ("dumptokens", v => cfg.DisplayTokens = true) + .Add ("D=", def => cfg.MacroDefines.Add(def)) + .Add ("dumpenums", v => cfg.DumpEnums = true) + .Add ("enumdir=", dir => cfg.EnumDir = Path.Combine(Directory.GetCurrentDirectory(), dir)) + .Add ("dumpxmls", v => cfg.DumpXmls = true) + .Add ("xmldir=", dir => cfg.XmlDir = Path.Combine(Directory.GetCurrentDirectory(), dir)) + .Add ("odir=", dir => cfg.OutDir = dir) + .Add ("cheatdir=", dir => cfg.CheatDir = dir) + .Add ("netdir=", dirs => addDirectories(cfg.NetRoot, dirs)) + .Add ("exnetdir=", dirs => addDirectories(cfg.ExNetRoot, dirs)) + .Add ("appdir=", dirs => addDirectories(cfg.AppRoot, dirs)) + .Add ("exappdir=", dirs => addDirectories(cfg.ExAppRoot, dirs)) + .Add ("exclude=", dirs => addDirectories(cfg.Exclude, dirs)) + ; - //TODO: fix enum dump - // Final argument is translation target - remArgs = p.Parse (args); + //TODO: fix enum dump + // Final argument is translation target + remArgs = p.Parse (args); - // Load .Net templates + // Load .Net templates foreach (string r in cfg.NetRoot) doFile(r, ".xml", addNetTranslation, cfg.ExNetRoot); @@ -110,16 +110,16 @@ namespace RusticiSoftware.Translator.CSharp cfg.AppRoot.Add(remArgs[0]); foreach (string r in cfg.AppRoot) doFile(r, ".cs", addAppSigTranslation, cfg.ExAppRoot); // parse it - if (cfg.DumpEnums) { - enumXmlWriter = new XmlTextWriter(cfg.EnumDir, System.Text.Encoding.UTF8); - } + if (cfg.DumpEnums) { + enumXmlWriter = new XmlTextWriter(cfg.EnumDir, System.Text.Encoding.UTF8); + } if (cfg.DumpXmls) { // Get package name and convert to directory name foreach (KeyValuePair de in AppEnv) { String xmlFName = Path.Combine(cfg.XmlDir, - ((string)de.Key).Replace('.', Path.DirectorySeparatorChar) + ".xml"); + ((string)de.Key).Replace('.', Path.DirectorySeparatorChar) + ".xml"); String xmlFDir = Path.GetDirectoryName(xmlFName); if (!Directory.Exists(xmlFDir)) { @@ -135,8 +135,8 @@ namespace RusticiSoftware.Translator.CSharp if (cfg.DumpEnums) { enumXmlWriter.WriteEndElement(); - enumXmlWriter.Close(); - } + enumXmlWriter.Close(); + } } else { @@ -161,7 +161,7 @@ namespace RusticiSoftware.Translator.CSharp // Call processFile on all files below f that have the given extension public static void doFile(string root, string ext, FileProcessor processFile, IList excludes) { - string canonicalPath = Path.GetFullPath(root); + string canonicalPath = Path.GetFullPath(root); // If this is a directory, walk each file/dir in that directory if (!excludes.Contains(canonicalPath.ToLower())) { @@ -193,16 +193,16 @@ namespace RusticiSoftware.Translator.CSharp if (cfg.Verbosity > 2) Console.WriteLine("Parsing " + Path.GetFileName(fullName)); - ICharStream input = new ANTLRFileStream(fullName); + ICharStream input = new ANTLRFileStream(fullName); - PreProcessor lex = new PreProcessor(); - lex.AddDefine(cfg.MacroDefines); + PreProcessor lex = new PreProcessor(); + lex.AddDefine(cfg.MacroDefines); lex.CharStream = input; - lex.TraceDestination = Console.Error; + lex.TraceDestination = Console.Error; CommonTokenStream tokens = new CommonTokenStream(lex); csParser p = new csParser(tokens); - p.TraceDestination = Console.Error; + p.TraceDestination = Console.Error; csParser.compilation_unit_return parser_rt = p.compilation_unit(); @@ -220,146 +220,178 @@ namespace RusticiSoftware.Translator.CSharp } CommonTreeNodeStream nodes = new CommonTreeNodeStream(parser_rt.Tree); - nodes.TokenStream = tokens; + nodes.TokenStream = tokens; - return nodes; + return nodes; } - // Here's where we do the real work... + // Here's where we do the real work... public static void addNetTranslation(string fullName) { Stream s = new FileStream(fullName, FileMode.Open, FileAccess.Read); - TypeRepTemplate t = TypeRepTemplate.newInstance(s); + TypeRepTemplate t = TypeRepTemplate.newInstance(s); AppEnv[t.TypeName] = t; } // Here's where we do the real work... public static void addAppSigTranslation(string fullName) { - if (cfg.DebugLevel > 3) Console.Out.WriteLine("Extracting type info from file {0}", fullName); + + if (cfg.DebugLevel > 3) Console.Out.WriteLine("Extracting type info from file {0}", fullName); ITreeNodeStream csTree = parseFile(fullName); if (csTree != null) { - TemplateExtracter templateWalker = new TemplateExtracter(csTree); - templateWalker.Filename = fullName; - templateWalker.TraceDestination = Console.Error; - - templateWalker.compilation_unit(cfg, AppEnv); + TemplateExtracter templateWalker = new TemplateExtracter(csTree); + templateWalker.Filename = fullName; + templateWalker.TraceDestination = Console.Error; + + templateWalker.compilation_unit(cfg, AppEnv); } } // Here's where we do the real work... public static void translateFile(string fullName) { - if (cfg.DebugLevel > 3) Console.Out.WriteLine("Translating file {0}", fullName); - CommonTreeNodeStream csTree = parseFile(fullName); - if (cfg.DumpCSharp && csTree != null) AntlrUtils.AntlrUtils.DumpNodes(csTree); + long startTime = DateTime.Now.Ticks; + if (cfg.DebugLevel > 3) Console.Out.WriteLine("Translating file {0}", fullName); + ITreeNodeStream csTree = parseFile(fullName); + if (cfg.DumpCSharp && csTree != null) AntlrUtils.AntlrUtils.DumpNodes((CommonTreeNodeStream)csTree); + + if (csTree != null) + { + Dictionary cus = new Dictionary(); + JavaMaker javaMaker = new JavaMaker(csTree); + javaMaker.Filename = fullName; + javaMaker.TraceDestination = Console.Error; + + JavaMaker.compilation_unit_return java = javaMaker.compilation_unit(cfg, cus); + foreach (KeyValuePair package in cus) { + + CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(package.Value); + javaSyntaxNodes.TokenStream = csTree.TokenStream; + + NetMaker netMaker = new NetMaker(javaSyntaxNodes); + netMaker.Filename = fullName; + netMaker.TraceDestination = Console.Error; + + NetMaker.type_declaration_return javaCompilationUnit = netMaker.type_declaration(); + + CommonTreeNodeStream javaCompilationUnitNodes = new CommonTreeNodeStream(javaCompilationUnit.Tree); + javaCompilationUnitNodes.TokenStream = csTree.TokenStream; + + JavaPrettyPrint outputMaker = new JavaPrettyPrint(javaCompilationUnitNodes); + outputMaker.Filename = fullName; + outputMaker.TraceDestination = Console.Error; + + outputMaker.type_declaration(); + } + // ITreeNodeStream javaTree = java.Tree; + } - // ASTNode t = parseFile(f, s); - // if (t != null) - // { - // if (showTree) - // { - // ASTNode r = (ASTNode)new ASTNodeFactory().create(0, "AST ROOT"); - // r.setFirstChild(t); - // ASTFrame frame = new ASTFrame("C# AST for file [" + f + "]", r); - // frame.ShowDialog(); - // //frame.Visible = true; - // // System.out.println(t.toStringList()); - // } - // ASTNode r1 = (ASTNode)new ASTNodeFactory().create(0, "AST ROOT"); - // r1.setFirstChild(t); - // ASTFrame frame1 = new ASTFrame("C# AST for file [" + f + "]", r1); - // if (showCSharp) - // frame1.ShowDialog(); + // ASTNode t = parseFile(f, s); + // if (t != null) + // { + // if (showTree) + // { + // ASTNode r = (ASTNode)new ASTNodeFactory().create(0, "AST ROOT"); + // r.setFirstChild(t); + // ASTFrame frame = new ASTFrame("C# AST for file [" + f + "]", r); + // frame.ShowDialog(); + // //frame.Visible = true; + // // System.out.println(t.toStringList()); + // } + // ASTNode r1 = (ASTNode)new ASTNodeFactory().create(0, "AST ROOT"); + // r1.setFirstChild(t); + // ASTFrame frame1 = new ASTFrame("C# AST for file [" + f + "]", r1); + // if (showCSharp) + // frame1.ShowDialog(); - // CSharpTranslator transformer = new CSharpTranslator(); - // transformer.setASTNodeClass(typeof(ASTNode).FullName); - // transformer.setASTFactory(new ASTNodeFactory()); - // CSharpTranslator.initializeASTFactory(transformer.getASTFactory()); + // CSharpTranslator transformer = new CSharpTranslator(); + // transformer.setASTNodeClass(typeof(ASTNode).FullName); + // transformer.setASTFactory(new ASTNodeFactory()); + // CSharpTranslator.initializeASTFactory(transformer.getASTFactory()); - // long startTime = DateTime.Now.Ticks; - // transformer.compilationUnit(t, null); + // long startTime = DateTime.Now.Ticks; + // transformer.compilationUnit(t, null); - // //BaseAST.setVerboseStringConversion(true, tokenNames); - // ASTNode r2 = (ASTNode)new ASTNodeFactory().create(0, "AST ROOT"); - // r2.setFirstChild(transformer.getAST()); - // ASTFrame frame2 = new ASTFrame("Java syntax AST for file [" + f + "]", r2); - // if (showJavaSyntax) - // frame2.ShowDialog(); + // //BaseAST.setVerboseStringConversion(true, tokenNames); + // ASTNode r2 = (ASTNode)new ASTNodeFactory().create(0, "AST ROOT"); + // r2.setFirstChild(transformer.getAST()); + // ASTFrame frame2 = new ASTFrame("Java syntax AST for file [" + f + "]", r2); + // if (showJavaSyntax) + // frame2.ShowDialog(); - // // Take each java compilation unit (each class defn) and write it to the appropriate file - // IEnumerator enumCU = transformer.getAST().findAllPartial((ASTNode)transformer.getASTFactory().create(CSharpParser.COMPILATION_UNIT)); - // while (enumCU.MoveNext()) - // { - // ASTNode javaCU = (ASTNode)enumCU.Current; + // // Take each java compilation unit (each class defn) and write it to the appropriate file + // IEnumerator enumCU = transformer.getAST().findAllPartial((ASTNode)transformer.getASTFactory().create(CSharpParser.COMPILATION_UNIT)); + // while (enumCU.MoveNext()) + // { + // ASTNode javaCU = (ASTNode)enumCU.Current; - // // Extract class/interface name - // String claName = JavaTreeParser.getClassName(javaCU); + // // Extract class/interface name + // String claName = JavaTreeParser.getClassName(javaCU); - // // Get package name and convert to directory name - // String nsDir = ""; - // foreach (String nsc in JavaTreeParser.getPackageName(javaCU)) - // { - // nsDir = Path.Combine(nsDir, nsc); - // } + // // Get package name and convert to directory name + // String nsDir = ""; + // foreach (String nsc in JavaTreeParser.getPackageName(javaCU)) + // { + // nsDir = Path.Combine(nsDir, nsc); + // } - // // Build destination filename for this class - // String fName = Path.Combine(Path.Combine(outDir, nsDir), claName + ".java"); + // // Build destination filename for this class + // String fName = Path.Combine(Path.Combine(outDir, nsDir), claName + ".java"); - // if (cheatDir != "") - // { - // String cheatFile = Path.Combine(cheatDir, Path.Combine(nsDir, claName + ".java")); - // if (File.Exists(cheatFile)) - // { - // // the old switcheroo - // File.Copy(cheatFile, fName, true); - // continue; - // } + // if (cheatDir != "") + // { + // String cheatFile = Path.Combine(cheatDir, Path.Combine(nsDir, claName + ".java")); + // if (File.Exists(cheatFile)) + // { + // // the old switcheroo + // File.Copy(cheatFile, fName, true); + // continue; + // } - // String ignoreMarker = Path.Combine(cheatDir, Path.Combine(nsDir, claName + ".none")); - // if (File.Exists(ignoreMarker)) - // { - // // Don't generate this class - // continue; - // } - // } + // String ignoreMarker = Path.Combine(cheatDir, Path.Combine(nsDir, claName + ".none")); + // if (File.Exists(ignoreMarker)) + // { + // // Don't generate this class + // continue; + // } + // } - // NetTranslator netTx = new NetTranslator(); - // netTx.setASTNodeClass(typeof(ASTNode).FullName); - // netTx.setASTFactory(new ASTNodeFactory()); - // NetTranslator.initializeASTFactory(netTx.getASTFactory()); - // netTx.compilationUnit(javaCU, null, appEnv); + // NetTranslator netTx = new NetTranslator(); + // netTx.setASTNodeClass(typeof(ASTNode).FullName); + // netTx.setASTFactory(new ASTNodeFactory()); + // NetTranslator.initializeASTFactory(netTx.getASTFactory()); + // netTx.compilationUnit(javaCU, null, appEnv); - // //BaseAST.setVerboseStringConversion(true, tokenNames); - // ASTNode r3 = (ASTNode)new ASTNodeFactory().create(0, "AST ROOT"); - // r3.setFirstChild(netTx.getAST()); - // ASTFrame frame3 = new ASTFrame("Java AST for file [" + f + "]", r3); - // if (showJava) - // frame3.ShowDialog(); + // //BaseAST.setVerboseStringConversion(true, tokenNames); + // ASTNode r3 = (ASTNode)new ASTNodeFactory().create(0, "AST ROOT"); + // r3.setFirstChild(netTx.getAST()); + // ASTFrame frame3 = new ASTFrame("Java AST for file [" + f + "]", r3); + // if (showJava) + // frame3.ShowDialog(); - // Console.WriteLine(fName); + // Console.WriteLine(fName); - // String fDir = Path.GetDirectoryName(fName); - // if (!Directory.Exists(fDir)) - // { - // Directory.CreateDirectory(fDir); - // } - // FileInfo outF = new FileInfo(fName); - // StreamWriter w = new StreamWriter(outF.Create()); - // JavaPrettyPrinter writer = new JavaPrettyPrinter(); - // writer.compilationUnit(netTx.getAST(), w, enumXmlWriter, filter); - // w.Close(); + // String fDir = Path.GetDirectoryName(fName); + // if (!Directory.Exists(fDir)) + // { + // Directory.CreateDirectory(fDir); + // } + // FileInfo outF = new FileInfo(fName); + // StreamWriter w = new StreamWriter(outF.Create()); + // JavaPrettyPrinter writer = new JavaPrettyPrinter(); + // writer.compilationUnit(netTx.getAST(), w, enumXmlWriter, filter); + // w.Close(); - // } + // } - // double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0; - // //System.Console.Out.WriteLine(writer.ToString()); - // System.Console.Out.WriteLine(""); - // System.Console.Out.WriteLine(""); - // System.Console.Out.WriteLine("Pretty-printed {0} in: {1} seconds.", f, elapsedTime); - // } + double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0; + //System.Console.Out.WriteLine(writer.ToString()); + System.Console.Out.WriteLine(""); + System.Console.Out.WriteLine(""); + System.Console.Out.WriteLine("Pretty-printed {0} in: {1} seconds.", fullName, elapsedTime); } } } diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/.gitignore b/CSharpTranslator/antlr3/src/cs2j/CSharp/.gitignore index 3f48c4d..5106003 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/.gitignore +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/.gitignore @@ -3,3 +3,9 @@ csLexer.cs csParser.cs TemplateExtracter.cs TemplateExtracter.tokens +NetMaker.cs +NetMaker.tokens +JavaMaker.cs +JavaMaker.tokens +JavaPrettyPrint.cs +JavaPrettyPrint.tokens diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs b/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs index ca9c530..96d50ce 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs @@ -45,5 +45,25 @@ namespace RusticiSoftware.Translator.CSharp Console.Out.WriteLine(s); } } + + // distinguish classes with same name, but differing numbers of type arguments + protected string mkTypeName (string name, List tyargs) { + return name + (tyargs.Count > 0 ? "'" + tyargs.Count.ToString() : ""); + } + + protected string formatTyargs(List tyargs) { + + if (tyargs.Count == 0) { + return ""; + } + StringBuilder buf = new StringBuilder(); + buf.Append("<"); + foreach (string t in tyargs) { + buf.Append(t + ","); + } + buf.Remove(buf.Length-1,1); + buf.Append(">"); + return buf.ToString(); + } } } diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g new file mode 100644 index 0000000..23ec246 --- /dev/null +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g @@ -0,0 +1,1093 @@ + // JavaMaker.g +// +// Convert C# parse tree to a Java parse tree +// +// Kevin Glynn +// kevin.glynn@twigletsoftware.com +// November 2010 +tree grammar JavaMaker; + +options { + tokenVocab=cs; + ASTLabelType=CommonTree; + language=CSharp2; + superClass='RusticiSoftware.Translator.CSharp.CommonWalker'; + output=AST; +} + +@namespace { RusticiSoftware.Translator.CSharp } + +@header +{ + +} + +@members +{ + private IDictionary CUs { get; set; } + +} + +/******************************************************************************************** + Parser section +*********************************************************************************************/ + +/////////////////////////////////////////////////////// + +compilation_unit[CS2JSettings inCfg, IDictionary inCus /*, DirectoryHT inAppEnv*/] +@init { + CUs = inCus; +} +: + namespace_body; + +namespace_declaration: + 'namespace' qualified_identifier namespace_block ';'? ; +namespace_block: + '{' namespace_body '}' ; +namespace_body: + extern_alias_directives? using_directives? global_attributes? namespace_member_declarations? ; +extern_alias_directives: + extern_alias_directive+ ; +extern_alias_directive: + 'extern' 'alias' identifier ';' ; +using_directives: + using_directive+ ; +using_directive: + (using_alias_directive + | using_namespace_directive) ; +using_alias_directive: + 'using' identifier '=' namespace_or_type_name ';' ; +using_namespace_directive: + 'using' namespace_name ';' ; +namespace_member_declarations: + namespace_member_declaration+ ; +namespace_member_declaration: + namespace_declaration + | attributes? modifiers? type_declaration ; +// type_declaration is only called at the top level, so each of the types declared +// here will become a Java compilation unit (and go to its own file) +type_declaration +: + ('partial') => p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } + (pc=class_declaration { CUs.Add($pc.name, $pc.tree); } + | ps=struct_declaration { CUs.Add($ps.name, $ps.tree); } + | pi=interface_declaration { CUs.Add($pi.name, $pi.tree); }) + | c=class_declaration { CUs.Add($c.name, $c.tree); } + | s=struct_declaration { CUs.Add($s.name, $s.tree); } + | i=interface_declaration { CUs.Add($i.name, $i.tree); } + | e=enum_declaration { CUs.Add($e.name, $e.tree); } + | d=delegate_declaration { CUs.Add($d.name, $d.tree); } ; +// Identifiers +qualified_identifier: + identifier ('.' identifier)*; +namespace_name + : namespace_or_type_name ; + +modifiers: + modifier+ ; +modifier: + 'new' | 'public' | 'protected' | 'private' | 'internal' | 'unsafe' | 'abstract' | 'sealed' | 'static' + | 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override'; + +class_member_declaration: + attributes? + m=modifiers? + ( 'const' type constant_declarators ';' + | event_declaration // 'event' + | 'partial' (method_declaration + | interface_declaration + | class_declaration + | struct_declaration) + | interface_declaration // 'interface' + | 'void' method_declaration + | type ( (member_name '(') => method_declaration + | (member_name '{') => property_declaration + | (member_name '.' 'this') => type_name '.' indexer_declaration + | indexer_declaration //this + | field_declaration // qid + | operator_declaration + ) +// common_modifiers// (method_modifiers | field_modifiers) + + | class_declaration // 'class' + | struct_declaration // 'struct' + | enum_declaration // 'enum' + | delegate_declaration // 'delegate' + | conversion_operator_declaration + | constructor_declaration // | static_constructor_declaration + | destructor_declaration + ) + ; + +primary_expression: + ('this' brackets) => 'this' brackets primary_expression_part* + | ('base' brackets) => 'this' brackets primary_expression_part* + | primary_expression_start primary_expression_part* + | 'new' ( (object_creation_expression ('.'|'->'|'[')) => + object_creation_expression primary_expression_part+ // new Foo(arg, arg).Member + // try the simple one first, this has no argS and no expressions + // symantically could be object creation + | (delegate_creation_expression) => delegate_creation_expression// new FooDelegate (MyFunction) + | object_creation_expression + | anonymous_object_creation_expression) // new {int X, string Y} + | sizeof_expression // sizeof (struct) + | checked_expression // checked (... + | unchecked_expression // unchecked {...} + | default_value_expression // default + | anonymous_method_expression // delegate (int foo) {} + ; + +primary_expression_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list + | identifier ('::' identifier)? + | 'this' + | 'base' + | paren_expression + | typeof_expression // typeof(Foo).Name + | literal + ; + +primary_expression_part: + access_identifier + | brackets_or_arguments + | '++' + | '--' ; +access_identifier: + access_operator type_or_generic ; +access_operator: + '.' | '->' ; +brackets_or_arguments: + brackets | arguments ; +brackets: + '[' expression_list? ']' ; +paren_expression: + '(' expression ')' ; +arguments: + '(' argument_list? ')' ; +argument_list: + argument (',' argument)*; +// 4.0 +argument: + argument_name argument_value + | argument_value; +argument_name: + identifier ':'; +argument_value: + expression + | ref_variable_reference + | 'out' variable_reference ; +ref_variable_reference: + 'ref' + (('(' type ')') => '(' type ')' (ref_variable_reference | variable_reference) // SomeFunc(ref (int) ref foo) + // SomeFunc(ref (int) foo) + | variable_reference); // SomeFunc(ref foo) +// lvalue +variable_reference: + expression; +rank_specifiers: + rank_specifier+ ; +rank_specifier: + '[' dim_separators? ']' ; +dim_separators: + ','+ ; + +delegate_creation_expression: + // 'new' + type_name '(' type_name ')' ; +anonymous_object_creation_expression: + // 'new' + anonymous_object_initializer ; +anonymous_object_initializer: + '{' (member_declarator_list ','?)? '}'; +member_declarator_list: + member_declarator (',' member_declarator)* ; +member_declarator: + qid ('=' expression)? ; +primary_or_array_creation_expression: + (array_creation_expression) => array_creation_expression + | primary_expression + ; +// new Type[2] { } +array_creation_expression: + 'new' + (type ('[' expression_list ']' + ( rank_specifiers? array_initializer? // new int[4] + // | invocation_part* + | ( ((arguments ('['|'.'|'->')) => arguments invocation_part)// new object[2].GetEnumerator() + | invocation_part)* arguments + ) // new int[4]() + | array_initializer + ) + | rank_specifier // [,] + (array_initializer // var a = new[] { 1, 10, 100, 1000 }; // int[] + ) + ) ; +array_initializer: + '{' variable_initializer_list? ','? '}' ; +variable_initializer_list: + variable_initializer (',' variable_initializer)* ; +variable_initializer: + expression | array_initializer ; +sizeof_expression: + 'sizeof' '(' unmanaged_type ')'; +checked_expression: + 'checked' '(' expression ')' ; +unchecked_expression: + 'unchecked' '(' expression ')' ; +default_value_expression: + 'default' '(' type ')' ; +anonymous_method_expression: + 'delegate' explicit_anonymous_function_signature? block; +explicit_anonymous_function_signature: + '(' explicit_anonymous_function_parameter_list? ')' ; +explicit_anonymous_function_parameter_list: + explicit_anonymous_function_parameter (',' explicit_anonymous_function_parameter)* ; +explicit_anonymous_function_parameter: + anonymous_function_parameter_modifier? type identifier; +anonymous_function_parameter_modifier: + 'ref' | 'out'; + + +/////////////////////////////////////////////////////// +object_creation_expression: + // 'new' + type + ( '(' argument_list? ')' object_or_collection_initializer? + | object_or_collection_initializer ) + ; +object_or_collection_initializer: + '{' (object_initializer + | collection_initializer) ; +collection_initializer: + element_initializer_list ','? '}' ; +element_initializer_list: + element_initializer (',' element_initializer)* ; +element_initializer: + non_assignment_expression + | '{' expression_list '}' ; +// object-initializer eg's +// Rectangle r = new Rectangle { +// P1 = new Point { X = 0, Y = 1 }, +// P2 = new Point { X = 2, Y = 3 } +// }; +// TODO: comma should only follow a member_initializer_list +object_initializer: + member_initializer_list? ','? '}' ; +member_initializer_list: + member_initializer (',' member_initializer) ; +member_initializer: + identifier '=' initializer_value ; +initializer_value: + expression + | object_or_collection_initializer ; + +/////////////////////////////////////////////////////// + +typeof_expression: + 'typeof' '(' ((unbound_type_name) => unbound_type_name + | type + | 'void') ')' ; +// unbound type examples +//foo>> +//bar::foo<> +//foo1::foo2.foo3<,,> +unbound_type_name: // qualified_identifier v2 +// unbound_type_name_start unbound_type_name_part* ; + unbound_type_name_start + (((generic_dimension_specifier '.') => generic_dimension_specifier unbound_type_name_part) + | unbound_type_name_part)* + generic_dimension_specifier + ; + +unbound_type_name_start: + identifier ('::' identifier)?; +unbound_type_name_part: + '.' identifier; +generic_dimension_specifier: + '<' commas? '>' ; +commas: + ','+ ; + +/////////////////////////////////////////////////////// +// Type Section +/////////////////////////////////////////////////////// + +type_name returns [string thetext]: + namespace_or_type_name { $thetext = $namespace_or_type_name.thetext; }; +namespace_or_type_name returns [string thetext]: + t1=type_or_generic { $thetext=t1.type+formatTyargs($t1.generic_arguments); } ('::' tc=type_or_generic { $thetext+="::"+tc.type+formatTyargs($tc.generic_arguments); })? ('.' tn=type_or_generic { $thetext+="."+tn.type+formatTyargs($tn.generic_arguments); } )* ; +type_or_generic returns [string type, List generic_arguments] +@init { + $generic_arguments = new List(); +} +@after{ + $type = $t.text; +}: + (identifier generic_argument_list) => t=identifier ga=generic_argument_list { $generic_arguments = $ga.tyargs; } + | t=identifier ; +qid: // qualified_identifier v2 + qid_start qid_part* + ; +qid_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list +// | 'this' +// | 'base' + | identifier ('::' identifier)? + | literal + ; // 0.ToString() is legal + + +qid_part: + access_identifier ; + +generic_argument_list returns [List tyargs] +@after { + $tyargs = $ta.tyargs; +} +: + '<' ta=type_arguments '>' ; +type_arguments returns [List tyargs] +@init { + $tyargs = new List(); +} +: + t1=type { $tyargs.Add($t1.thetext); } (',' tn=type { $tyargs.Add($tn.thetext); })* ; + +type returns [string thetext]: + ((predefined_type | type_name) rank_specifiers) => (p1=predefined_type { $thetext = $p1.thetext; } | tn1=type_name { $thetext = $tn1.thetext; }) rs=rank_specifiers { $thetext += $rs.text; } ('*' { $thetext += "*"; })* + | ((predefined_type | type_name) ('*'+ | '?')) => (p2=predefined_type { $thetext = $p2.thetext; } | tn2=type_name { $thetext = $tn2.thetext; }) (('*' { $thetext += "*"; })+ | '?' { $thetext += "?"; }) + | (p3=predefined_type { $thetext = $p3.thetext; } | tn3=type_name { $thetext = $tn3.thetext; }) + | 'void' { $thetext = "System.Void"; } ('*' { $thetext += "*"; })+ + ; + +non_nullable_type: + (predefined_type | type_name) + ( rank_specifiers '*'* + | ('*'+)? + ) + | 'void' '*'+ ; + +non_array_type: + type; +array_type: + type; +unmanaged_type: + type; +class_type: + type; +pointer_type: + type; + + +/////////////////////////////////////////////////////// +// Statement Section +/////////////////////////////////////////////////////// +block: + ';' + | '{' statement_list? '}'; +statement_list: + statement+ ; + +/////////////////////////////////////////////////////// +// Expression Section +/////////////////////////////////////////////////////// +expression: + (unary_expression assignment_operator) => assignment + | non_assignment_expression + ; +expression_list: + expression (',' expression)* ; +assignment: + unary_expression assignment_operator expression ; +unary_expression: + //('(' arguments ')' ('[' | '.' | '(')) => primary_or_array_creation_expression + (cast_expression) => cast_expression + | primary_or_array_creation_expression + | '+' unary_expression + | '-' unary_expression + | '!' unary_expression + | '~' unary_expression + | pre_increment_expression + | pre_decrement_expression + | pointer_indirection_expression + | addressof_expression + ; +cast_expression: + '(' type ')' unary_expression ; +assignment_operator: + '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>' '>=' ; +pre_increment_expression: + '++' unary_expression ; +pre_decrement_expression: + '--' unary_expression ; +pointer_indirection_expression: + '*' unary_expression ; +addressof_expression: + '&' unary_expression ; + +non_assignment_expression: + //'non ASSIGNment' + (anonymous_function_signature '=>') => lambda_expression + | (query_expression) => query_expression + | conditional_expression + ; + +/////////////////////////////////////////////////////// +// Conditional Expression Section +/////////////////////////////////////////////////////// + +multiplicative_expression: + unary_expression ( ('*'|'/'|'%') unary_expression)* ; +additive_expression: + multiplicative_expression (('+'|'-') multiplicative_expression)* ; +// >> check needed (no whitespace) +shift_expression: + additive_expression (('<<'|'>' '>') additive_expression)* ; +relational_expression: + shift_expression + ( (('<'|'>'|'>='|'<=') shift_expression) + | (('is'|'as') non_nullable_type) + )* ; +equality_expression: + relational_expression + (('=='|'!=') relational_expression)* ; +and_expression: + equality_expression ('&' equality_expression)* ; +exclusive_or_expression: + and_expression ('^' and_expression)* ; +inclusive_or_expression: + exclusive_or_expression ('|' exclusive_or_expression)* ; +conditional_and_expression: + inclusive_or_expression ('&&' inclusive_or_expression)* ; +conditional_or_expression: + conditional_and_expression ('||' conditional_and_expression)* ; + +null_coalescing_expression: + conditional_or_expression ('??' conditional_or_expression)* ; +conditional_expression: + null_coalescing_expression ('?' expression ':' expression)? ; + +/////////////////////////////////////////////////////// +// lambda Section +/////////////////////////////////////////////////////// +lambda_expression: + anonymous_function_signature '=>' anonymous_function_body; +anonymous_function_signature: + '(' (explicit_anonymous_function_parameter_list + | implicit_anonymous_function_parameter_list)? ')' + | implicit_anonymous_function_parameter_list + ; +implicit_anonymous_function_parameter_list: + implicit_anonymous_function_parameter (',' implicit_anonymous_function_parameter)* ; +implicit_anonymous_function_parameter: + identifier; +anonymous_function_body: + expression + | block ; + +/////////////////////////////////////////////////////// +// LINQ Section +/////////////////////////////////////////////////////// +query_expression: + from_clause query_body ; +query_body: + // match 'into' to closest query_body + query_body_clauses? select_or_group_clause (('into') => query_continuation)? ; +query_continuation: + 'into' identifier query_body; +query_body_clauses: + query_body_clause+ ; +query_body_clause: + from_clause + | let_clause + | where_clause + | join_clause + | orderby_clause; +from_clause: + 'from' type? identifier 'in' expression ; +join_clause: + 'join' type? identifier 'in' expression 'on' expression 'equals' expression ('into' identifier)? ; +let_clause: + 'let' identifier '=' expression; +orderby_clause: + 'orderby' ordering_list ; +ordering_list: + ordering (',' ordering)* ; +ordering: + expression ordering_direction + ; +ordering_direction: + 'ascending' + | 'descending' ; +select_or_group_clause: + select_clause + | group_clause ; +select_clause: + 'select' expression ; +group_clause: + 'group' expression 'by' expression ; +where_clause: + 'where' boolean_expression ; +boolean_expression: + expression; + +/////////////////////////////////////////////////////// +// B.2.13 Attributes +/////////////////////////////////////////////////////// +global_attributes: + global_attribute+ ; +global_attribute: + '[' global_attribute_target_specifier attribute_list ','? ']' ; +global_attribute_target_specifier: + global_attribute_target ':' ; +global_attribute_target: + 'assembly' | 'module' ; +attributes: + attribute_sections ; +attribute_sections: + attribute_section+ ; +attribute_section: + '[' attribute_target_specifier? attribute_list ','? ']' ; +attribute_target_specifier: + attribute_target ':' ; +attribute_target: + 'field' | 'event' | 'method' | 'param' | 'property' | 'return' | 'type' ; +attribute_list: + attribute (',' attribute)* ; +attribute: + type_name attribute_arguments? ; +// TODO: allows a mix of named/positional arguments in any order +attribute_arguments: + '(' (')' // empty + | (positional_argument ((',' identifier '=') => named_argument + |',' positional_argument)* + ) ')' + ) ; +positional_argument_list: + positional_argument (',' positional_argument)* ; +positional_argument: + attribute_argument_expression ; +named_argument_list: + named_argument (',' named_argument)* ; +named_argument: + identifier '=' attribute_argument_expression ; +attribute_argument_expression: + expression ; + +/////////////////////////////////////////////////////// +// Class Section +/////////////////////////////////////////////////////// + +class_declaration returns [string name]: + 'class' type_or_generic { $name = mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); } class_base? type_parameter_constraints_clauses? class_body ';'? ; +class_base: + // syntactically base class vs interface name is the same + //':' class_type (',' interface_type_list)? ; + ':' interface_type_list ; + +interface_type_list: + type (',' type)* ; + +class_body: + '{' class_member_declarations? '}' ; +class_member_declarations: + class_member_declaration+ ; + +/////////////////////////////////////////////////////// +constant_declaration: + 'const' type constant_declarators ';' ; +constant_declarators: + constant_declarator (',' constant_declarator)* ; +constant_declarator: + identifier ('=' constant_expression)? ; +constant_expression: + expression; + +/////////////////////////////////////////////////////// +field_declaration: + variable_declarators ';' ; +variable_declarators: + variable_declarator (',' variable_declarator)* ; +variable_declarator: + type_name ('=' variable_initializer)? ; // eg. event EventHandler IInterface.VariableName = Foo; + +/////////////////////////////////////////////////////// +method_declaration: + method_header method_body ; +method_header: + member_name '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ; +method_body: + block ; +member_name: + qid ; // IInterface.Method logic added. + +/////////////////////////////////////////////////////// +property_declaration: + member_name '{' accessor_declarations '}' ; +accessor_declarations: + attributes? + (get_accessor_declaration attributes? set_accessor_declaration? + | set_accessor_declaration attributes? get_accessor_declaration?) ; +get_accessor_declaration: + accessor_modifier? 'get' accessor_body ; +set_accessor_declaration: + accessor_modifier? 'set' accessor_body ; +accessor_modifier: + 'public' | 'protected' | 'private' | 'internal' ; +accessor_body: + block ; + +/////////////////////////////////////////////////////// +event_declaration: + 'event' type + ((member_name '{') => member_name '{' event_accessor_declarations '}' + | variable_declarators ';') // typename=foo; + ; +event_modifiers: + modifier+ ; +event_accessor_declarations: + attributes? ((add_accessor_declaration attributes? remove_accessor_declaration) + | (remove_accessor_declaration attributes? add_accessor_declaration)) ; +add_accessor_declaration: + 'add' block ; +remove_accessor_declaration: + 'remove' block ; + +/////////////////////////////////////////////////////// +// enum declaration +/////////////////////////////////////////////////////// +enum_declaration returns [string name]: + 'enum' identifier { $name = $identifier.text; } enum_base? enum_body ';'? ; +enum_base: + ':' integral_type ; +enum_body: + '{' (enum_member_declarations ','?)? '}' ; +enum_member_declarations: + enum_member_declaration (',' enum_member_declaration)* ; +enum_member_declaration: + attributes? identifier ('=' expression)? ; +//enum_modifiers: +// enum_modifier+ ; +//enum_modifier: +// 'new' | 'public' | 'protected' | 'internal' | 'private' ; +integral_type: + 'sbyte' | 'byte' | 'short' | 'ushort' | 'int' | 'uint' | 'long' | 'ulong' | 'char' ; + +// B.2.12 Delegates +delegate_declaration returns [string name]: + 'delegate' return_type identifier { $name = $identifier.text; } variant_generic_parameter_list? + '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ; +delegate_modifiers: + modifier+ ; +// 4.0 +variant_generic_parameter_list: + '<' variant_type_parameters '>' ; +variant_type_parameters: + variant_type_variable_name (',' variant_type_variable_name)* ; +variant_type_variable_name: + attributes? variance_annotation? type_variable_name ; +variance_annotation: + 'in' | 'out' ; + +type_parameter_constraints_clauses: + type_parameter_constraints_clause (',' type_parameter_constraints_clause)* ; +type_parameter_constraints_clause: + 'where' type_variable_name ':' type_parameter_constraint_list ; +// class, Circle, new() +type_parameter_constraint_list: + ('class' | 'struct') (',' secondary_constraint_list)? (',' constructor_constraint)? + | secondary_constraint_list (',' constructor_constraint)? + | constructor_constraint ; +//primary_constraint: +// class_type +// | 'class' +// | 'struct' ; +secondary_constraint_list: + secondary_constraint (',' secondary_constraint)* ; +secondary_constraint: + type_name ; // | type_variable_name) ; +type_variable_name: + identifier ; +constructor_constraint: + 'new' '(' ')' ; +return_type: + type + | 'void'; +formal_parameter_list: + formal_parameter (',' formal_parameter)* ; +formal_parameter: + attributes? (fixed_parameter | parameter_array) + | '__arglist'; // __arglist is undocumented, see google +fixed_parameters: + fixed_parameter (',' fixed_parameter)* ; +// 4.0 +fixed_parameter: + parameter_modifier? type identifier default_argument? ; +// 4.0 +default_argument: + '=' expression; +parameter_modifier: + 'ref' | 'out' | 'this' ; +parameter_array: + 'params' type identifier ; + +/////////////////////////////////////////////////////// +interface_declaration returns [string name]: + 'interface' identifier { $name = $identifier.text; } variant_generic_parameter_list? + interface_base? type_parameter_constraints_clauses? interface_body ';'? ; +interface_modifiers: + modifier+ ; +interface_base: + ':' interface_type_list ; +interface_body: + '{' interface_member_declarations? '}' ; +interface_member_declarations: + interface_member_declaration+ ; +interface_member_declaration: + attributes? modifiers? + ('void' interface_method_declaration + | interface_event_declaration + | type ( (member_name '(') => interface_method_declaration + | (member_name '{') => interface_property_declaration + | interface_indexer_declaration) + ) + ; +interface_property_declaration: + identifier '{' interface_accessor_declarations '}' ; +interface_method_declaration: + identifier generic_argument_list? + '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ; +interface_event_declaration: + //attributes? 'new'? + 'event' type identifier ';' ; +interface_indexer_declaration: + // attributes? 'new'? type + 'this' '[' formal_parameter_list ']' '{' interface_accessor_declarations '}' ; +interface_accessor_declarations: + attributes? + (interface_get_accessor_declaration attributes? interface_set_accessor_declaration? + | interface_set_accessor_declaration attributes? interface_get_accessor_declaration?) ; +interface_get_accessor_declaration: + 'get' ';' ; // no body / modifiers +interface_set_accessor_declaration: + 'set' ';' ; // no body / modifiers +method_modifiers: + modifier+ ; + +/////////////////////////////////////////////////////// +struct_declaration returns [string name]: + 'struct' type_or_generic { $name = mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); } struct_interfaces? type_parameter_constraints_clauses? struct_body ';'? ; +struct_modifiers: + struct_modifier+ ; +struct_modifier: + 'new' | 'public' | 'protected' | 'internal' | 'private' | 'unsafe' ; +struct_interfaces: + ':' interface_type_list; +struct_body: + '{' struct_member_declarations? '}'; +struct_member_declarations: + struct_member_declaration+ ; +struct_member_declaration: + attributes? m=modifiers? + ( 'const' type constant_declarators ';' + | event_declaration // 'event' + | 'partial' (method_declaration + | interface_declaration + | class_declaration + | struct_declaration) + + | interface_declaration // 'interface' + | class_declaration // 'class' + | 'void' method_declaration + | type ( (member_name '(') => method_declaration + | (member_name '{') => property_declaration + | (member_name '.' 'this') => type_name '.' indexer_declaration + | indexer_declaration //this + | field_declaration // qid + | operator_declaration + ) +// common_modifiers// (method_modifiers | field_modifiers) + + | struct_declaration // 'struct' + | enum_declaration // 'enum' + | delegate_declaration // 'delegate' + | conversion_operator_declaration + | constructor_declaration // | static_constructor_declaration + ) + ; + + +/////////////////////////////////////////////////////// +indexer_declaration: + indexer_declarator '{' accessor_declarations '}' ; +indexer_declarator: + //(type_name '.')? + 'this' '[' formal_parameter_list ']' ; + +/////////////////////////////////////////////////////// +operator_declaration: + operator_declarator operator_body ; +operator_declarator: + 'operator' + (('+' | '-') '(' type identifier (binary_operator_declarator | unary_operator_declarator) + | overloadable_unary_operator '(' type identifier unary_operator_declarator + | overloadable_binary_operator '(' type identifier binary_operator_declarator) ; +unary_operator_declarator: + ')' ; +overloadable_unary_operator: + /*'+' | '-' | */ '!' | '~' | '++' | '--' | 'true' | 'false' ; +binary_operator_declarator: + ',' type identifier ')' ; +// >> check needed +overloadable_binary_operator: + /*'+' | '-' | */ '*' | '/' | '%' | '&' | '|' | '^' | '<<' | '>' '>' | '==' | '!=' | '>' | '<' | '>=' | '<=' ; + +conversion_operator_declaration: + conversion_operator_declarator operator_body ; +conversion_operator_declarator: + ('implicit' | 'explicit') 'operator' type '(' type identifier ')' ; +operator_body: + block ; + +/////////////////////////////////////////////////////// +constructor_declaration: + constructor_declarator constructor_body ; +constructor_declarator: + identifier '(' formal_parameter_list? ')' constructor_initializer? ; +constructor_initializer: + ':' ('base' | 'this') '(' argument_list? ')' ; +constructor_body: + block ; + +/////////////////////////////////////////////////////// +//static_constructor_declaration: +// identifier '(' ')' static_constructor_body ; +//static_constructor_body: +// block ; + +/////////////////////////////////////////////////////// +destructor_declaration: + '~' identifier '(' ')' destructor_body ; +destructor_body: + block ; + +/////////////////////////////////////////////////////// +invocation_expression: + invocation_start (((arguments ('['|'.'|'->')) => arguments invocation_part) + | invocation_part)* arguments ; +invocation_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list + | 'this' + | 'base' + | identifier ('::' identifier)? + | typeof_expression // typeof(Foo).Name + ; +invocation_part: + access_identifier + | brackets ; + +/////////////////////////////////////////////////////// + +statement: + (declaration_statement) => declaration_statement + | (identifier ':') => labeled_statement + | embedded_statement + ; +embedded_statement: + block + | selection_statement // if, switch + | iteration_statement // while, do, for, foreach + | jump_statement // break, continue, goto, return, throw + | try_statement + | checked_statement + | unchecked_statement + | lock_statement + | using_statement + | yield_statement + | unsafe_statement + | fixed_statement + | expression_statement // expression! + ; +fixed_statement: + 'fixed' '(' pointer_type fixed_pointer_declarators ')' embedded_statement ; +fixed_pointer_declarators: + fixed_pointer_declarator (',' fixed_pointer_declarator)* ; +fixed_pointer_declarator: + identifier '=' fixed_pointer_initializer ; +fixed_pointer_initializer: + //'&' variable_reference // unary_expression covers this + expression; +unsafe_statement: + 'unsafe' block; +labeled_statement: + identifier ':' statement ; +declaration_statement: + (local_variable_declaration + | local_constant_declaration) ';' ; +local_variable_declaration: + local_variable_type local_variable_declarators ; +local_variable_type: + ('var') => 'var' + | ('dynamic') => 'dynamic' + | type ; +local_variable_declarators: + local_variable_declarator (',' local_variable_declarator)* ; +local_variable_declarator: + identifier ('=' local_variable_initializer)? ; +local_variable_initializer: + expression + | array_initializer + | stackalloc_initializer; +stackalloc_initializer: + 'stackalloc' unmanaged_type '[' expression ']' ; +local_constant_declaration: + 'const' type constant_declarators ; +expression_statement: + expression ';' ; + +// TODO: should be assignment, call, increment, decrement, and new object expressions +statement_expression: + expression + ; +selection_statement: + if_statement + | switch_statement ; +if_statement: + // else goes with closest if + 'if' '(' boolean_expression ')' embedded_statement (('else') => else_statement)? + ; +else_statement: + 'else' embedded_statement ; +switch_statement: + 'switch' '(' expression ')' switch_block ; +switch_block: + '{' switch_sections? '}' ; +switch_sections: + switch_section+ ; +switch_section: + switch_labels statement_list ; +switch_labels: + switch_label+ ; +switch_label: + ('case' constant_expression ':') + | ('default' ':') ; +iteration_statement: + while_statement + | do_statement + | for_statement + | foreach_statement ; +while_statement: + 'while' '(' boolean_expression ')' embedded_statement ; +do_statement: + 'do' embedded_statement 'while' '(' boolean_expression ')' ';' ; +for_statement: + 'for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement ; +for_initializer: + (local_variable_declaration) => local_variable_declaration + | statement_expression_list + ; +for_condition: + boolean_expression ; +for_iterator: + statement_expression_list ; +statement_expression_list: + statement_expression (',' statement_expression)* ; +foreach_statement: + 'foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement ; +jump_statement: + break_statement + | continue_statement + | goto_statement + | return_statement + | throw_statement ; +break_statement: + 'break' ';' ; +continue_statement: + 'continue' ';' ; +goto_statement: + 'goto' ( identifier + | 'case' constant_expression + | 'default') ';' ; +return_statement: + 'return' expression? ';' ; +throw_statement: + 'throw' expression? ';' ; +try_statement: + 'try' block ( catch_clauses finally_clause? + | finally_clause); +//TODO one or both +catch_clauses: + 'catch' (specific_catch_clauses | general_catch_clause) ; +specific_catch_clauses: + specific_catch_clause ('catch' (specific_catch_clause | general_catch_clause))*; +specific_catch_clause: + '(' class_type identifier? ')' block ; +general_catch_clause: + block ; +finally_clause: + 'finally' block ; +checked_statement: + 'checked' block ; +unchecked_statement: + 'unchecked' block ; +lock_statement: + 'lock' '(' expression ')' embedded_statement ; +using_statement: + 'using' '(' resource_acquisition ')' embedded_statement ; +resource_acquisition: + (local_variable_declaration) => local_variable_declaration + | expression ; +yield_statement: + 'yield' ('return' expression ';' + | 'break' ';') ; + +/////////////////////////////////////////////////////// +// Lexar Section +/////////////////////////////////////////////////////// + +predefined_type returns [string thetext]: + 'bool' { $thetext = "System.Boolean"; } + | 'byte' { $thetext = "System.Byte"; } + | 'char' { $thetext = "System.Char"; } + | 'decimal' { $thetext = "System.Decimal"; } + | 'double' { $thetext = "System.Double"; } + | 'float' { $thetext = "System.Single"; } + | 'int' { $thetext = "System.Int32"; } + | 'long' { $thetext = "System.Int64"; } + | 'object' { $thetext = "System.Object"; } + | 'sbyte' { $thetext = "System.SByte"; } + | 'short' { $thetext = "System.Int16"; } + | 'string' { $thetext = "System.String"; } + | 'uint' { $thetext = "System.UInt32"; } + | 'ulong' { $thetext = "System.UInt64"; } + | 'ushort' { $thetext = "System.UInt16"; } + ; + +identifier: + IDENTIFIER | also_keyword; + +keyword: + 'abstract' | 'as' | 'base' | 'bool' | 'break' | 'byte' | 'case' | 'catch' | 'char' | 'checked' | 'class' | 'const' | 'continue' | 'decimal' | 'default' | 'delegate' | 'do' | 'double' | 'else' | 'enum' | 'event' | 'explicit' | 'extern' | 'false' | 'finally' | 'fixed' | 'float' | 'for' | 'foreach' | 'goto' | 'if' | 'implicit' | 'in' | 'int' | 'interface' | 'internal' | 'is' | 'lock' | 'long' | 'namespace' | 'new' | 'null' | 'object' | 'operator' | 'out' | 'override' | 'params' | 'private' | 'protected' | 'public' | 'readonly' | 'ref' | 'return' | 'sbyte' | 'sealed' | 'short' | 'sizeof' | 'stackalloc' | 'static' | 'string' | 'struct' | 'switch' | 'this' | 'throw' | 'true' | 'try' | 'typeof' | 'uint' | 'ulong' | 'unchecked' | 'unsafe' | 'ushort' | 'using' | 'virtual' | 'void' | 'volatile' ; + +also_keyword: + 'add' | 'alias' | 'assembly' | 'module' | 'field' | 'method' | 'param' | 'property' | 'type' | 'yield' + | 'from' | 'into' | 'join' | 'on' | 'where' | 'orderby' | 'group' | 'by' | 'ascending' | 'descending' + | 'equals' | 'select' | 'pragma' | 'let' | 'remove' | 'get' | 'set' | 'var' | '__arglist' | 'dynamic' | 'elif' + | 'endif' | 'define' | 'undef'; + +literal: + Real_literal + | NUMBER + | Hex_number + | Character_literal + | STRINGLITERAL + | Verbatim_string_literal + | TRUE + | FALSE + | NULL + ; + diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g new file mode 100644 index 0000000..e6c8849 --- /dev/null +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g @@ -0,0 +1,1046 @@ +tree grammar JavaPrettyPrint; + +options { + tokenVocab=cs; + ASTLabelType=CommonTree; + language=CSharp2; + superClass='RusticiSoftware.Translator.CSharp.CommonWalker'; + //output=template; +} + +@namespace { RusticiSoftware.Translator.CSharp } + +@header +{ + using System.Collections; +} + +@members +{ + protected bool is_class_modifier() + { + return false; + } +} + +compilation_unit: + namespace_body[true]; + +namespace_declaration: + 'namespace' qualified_identifier namespace_block ';'? ; +namespace_block: + '{' namespace_body[false] '}' ; +namespace_body[bool bGlobal]: + extern_alias_directives? using_directives? global_attributes? namespace_member_declarations? ; +extern_alias_directives: + extern_alias_directive+ ; +extern_alias_directive: + 'extern' 'alias' identifier ';' ; +using_directives: + using_directive+ ; +using_directive: + (using_alias_directive + | using_namespace_directive) ; +using_alias_directive: + 'using' identifier '=' namespace_or_type_name ';' ; +using_namespace_directive: + 'using' namespace_name ';' ; +namespace_member_declarations: + namespace_member_declaration+ ; +namespace_member_declaration: + namespace_declaration + | attributes? modifiers? type_declaration ; +type_declaration: + ('partial') => 'partial' (class_declaration + | struct_declaration + | interface_declaration) + | class_declaration + | struct_declaration + | interface_declaration + | enum_declaration + | delegate_declaration ; +// Identifiers +qualified_identifier: + identifier ('.' identifier)*; +namespace_name + : namespace_or_type_name ; + +modifiers: + modifier+ ; +modifier: + 'new' | 'public' | 'protected' | 'private' | 'internal' | 'unsafe' | 'abstract' | 'sealed' | 'static' + | 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override'; + +class_member_declaration: + attributes? + m=modifiers? + ( 'const' type constant_declarators ';' + | event_declaration // 'event' + | 'partial' (method_declaration + | interface_declaration + | class_declaration + | struct_declaration) + | interface_declaration // 'interface' + | 'void' method_declaration + | type ( (member_name '(') => method_declaration + | (member_name '{') => property_declaration + | (member_name '.' 'this') => type_name '.' indexer_declaration + | indexer_declaration //this + | field_declaration // qid + | operator_declaration + ) +// common_modifiers// (method_modifiers | field_modifiers) + + | class_declaration // 'class' + | struct_declaration // 'struct' + | enum_declaration // 'enum' + | delegate_declaration // 'delegate' + | conversion_operator_declaration + | constructor_declaration // | static_constructor_declaration + | destructor_declaration + ) + ; + +primary_expression: + ('this' brackets) => 'this' brackets primary_expression_part* + | ('base' brackets) => 'this' brackets primary_expression_part* + | primary_expression_start primary_expression_part* + | 'new' ( (object_creation_expression ('.'|'->'|'[')) => + object_creation_expression primary_expression_part+ // new Foo(arg, arg).Member + // try the simple one first, this has no argS and no expressions + // symantically could be object creation + | (delegate_creation_expression) => delegate_creation_expression// new FooDelegate (MyFunction) + | object_creation_expression + | anonymous_object_creation_expression) // new {int X, string Y} + | sizeof_expression // sizeof (struct) + | checked_expression // checked (... + | unchecked_expression // unchecked {...} + | default_value_expression // default + | anonymous_method_expression // delegate (int foo) {} + ; + +primary_expression_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list + | identifier ('::' identifier)? + | 'this' + | 'base' + | paren_expression + | typeof_expression // typeof(Foo).Name + | literal + ; + +primary_expression_part: + access_identifier + | brackets_or_arguments + | '++' + | '--' ; +access_identifier: + access_operator type_or_generic ; +access_operator: + '.' | '->' ; +brackets_or_arguments: + brackets | arguments ; +brackets: + '[' expression_list? ']' ; +paren_expression: + '(' expression ')' ; +arguments: + '(' argument_list? ')' ; +argument_list: + argument (',' argument)*; +// 4.0 +argument: + argument_name argument_value + | argument_value; +argument_name: + identifier ':'; +argument_value: + expression + | ref_variable_reference + | 'out' variable_reference ; +ref_variable_reference: + 'ref' + (('(' type ')') => '(' type ')' (ref_variable_reference | variable_reference) // SomeFunc(ref (int) ref foo) + // SomeFunc(ref (int) foo) + | variable_reference); // SomeFunc(ref foo) +// lvalue +variable_reference: + expression; +rank_specifiers: + rank_specifier+ ; +rank_specifier: + '[' dim_separators? ']' ; +dim_separators: + ','+ ; + +delegate_creation_expression: + // 'new' + type_name '(' type_name ')' ; +anonymous_object_creation_expression: + // 'new' + anonymous_object_initializer ; +anonymous_object_initializer: + '{' (member_declarator_list ','?)? '}'; +member_declarator_list: + member_declarator (',' member_declarator)* ; +member_declarator: + qid ('=' expression)? ; +primary_or_array_creation_expression: + (array_creation_expression) => array_creation_expression + | primary_expression + ; +// new Type[2] { } +array_creation_expression: + 'new' + (type ('[' expression_list ']' + ( rank_specifiers? array_initializer? // new int[4] + // | invocation_part* + | ( ((arguments ('['|'.'|'->')) => arguments invocation_part)// new object[2].GetEnumerator() + | invocation_part)* arguments + ) // new int[4]() + | array_initializer + ) + | rank_specifier // [,] + (array_initializer // var a = new[] { 1, 10, 100, 1000 }; // int[] + ) + ) ; +array_initializer: + '{' variable_initializer_list? ','? '}' ; +variable_initializer_list: + variable_initializer (',' variable_initializer)* ; +variable_initializer: + expression | array_initializer ; +sizeof_expression: + 'sizeof' '(' unmanaged_type ')'; +checked_expression: + 'checked' '(' expression ')' ; +unchecked_expression: + 'unchecked' '(' expression ')' ; +default_value_expression: + 'default' '(' type ')' ; +anonymous_method_expression: + 'delegate' explicit_anonymous_function_signature? block; +explicit_anonymous_function_signature: + '(' explicit_anonymous_function_parameter_list? ')' ; +explicit_anonymous_function_parameter_list: + explicit_anonymous_function_parameter (',' explicit_anonymous_function_parameter)* ; +explicit_anonymous_function_parameter: + anonymous_function_parameter_modifier? type identifier; +anonymous_function_parameter_modifier: + 'ref' | 'out'; + + +/////////////////////////////////////////////////////// +object_creation_expression: + // 'new' + type + ( '(' argument_list? ')' object_or_collection_initializer? + | object_or_collection_initializer ) + ; +object_or_collection_initializer: + '{' (object_initializer + | collection_initializer) ; +collection_initializer: + element_initializer_list ','? '}' ; +element_initializer_list: + element_initializer (',' element_initializer)* ; +element_initializer: + non_assignment_expression + | '{' expression_list '}' ; +// object-initializer eg's +// Rectangle r = new Rectangle { +// P1 = new Point { X = 0, Y = 1 }, +// P2 = new Point { X = 2, Y = 3 } +// }; +// TODO: comma should only follow a member_initializer_list +object_initializer: + member_initializer_list? ','? '}' ; +member_initializer_list: + member_initializer (',' member_initializer) ; +member_initializer: + identifier '=' initializer_value ; +initializer_value: + expression + | object_or_collection_initializer ; + +/////////////////////////////////////////////////////// + +typeof_expression: + 'typeof' '(' ((unbound_type_name) => unbound_type_name + | type + | 'void') ')' ; +// unbound type examples +//foo>> +//bar::foo<> +//foo1::foo2.foo3<,,> +unbound_type_name: // qualified_identifier v2 +// unbound_type_name_start unbound_type_name_part* ; + unbound_type_name_start + (((generic_dimension_specifier '.') => generic_dimension_specifier unbound_type_name_part) + | unbound_type_name_part)* + generic_dimension_specifier + ; + +unbound_type_name_start: + identifier ('::' identifier)?; +unbound_type_name_part: + '.' identifier; +generic_dimension_specifier: + '<' commas? '>' ; +commas: + ','+ ; + +/////////////////////////////////////////////////////// +// Type Section +/////////////////////////////////////////////////////// + +type_name: + namespace_or_type_name ; +namespace_or_type_name: + type_or_generic ('::' type_or_generic)? ('.' type_or_generic)* ; +type_or_generic: + (identifier generic_argument_list) => identifier generic_argument_list + | identifier ; + +qid: // qualified_identifier v2 + qid_start qid_part* + ; +qid_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list +// | 'this' +// | 'base' + | identifier ('::' identifier)? + | literal + ; // 0.ToString() is legal + + +qid_part: + access_identifier ; + +generic_argument_list: + '<' type_arguments '>' ; +type_arguments: + type (',' type)* ; + +type: + ((predefined_type | type_name) rank_specifiers) => (predefined_type | type_name) rank_specifiers '*'* + | ((predefined_type | type_name) ('*'+ | '?')) => (predefined_type | type_name) ('*'+ | '?') + | (predefined_type | type_name) + | 'void' '*'+ + ; +non_nullable_type: + (predefined_type | type_name) + ( rank_specifiers '*'* + | ('*'+)? + ) + | 'void' '*'+ ; + +non_array_type: + type; +array_type: + type; +unmanaged_type: + type; +class_type: + type; +pointer_type: + type; + + +/////////////////////////////////////////////////////// +// Statement Section +/////////////////////////////////////////////////////// +block: + ';' + | '{' statement_list? '}'; +statement_list: + statement+ ; + +/////////////////////////////////////////////////////// +// Expression Section +/////////////////////////////////////////////////////// +expression: + (unary_expression assignment_operator) => assignment + | non_assignment_expression + ; +expression_list: + expression (',' expression)* ; +assignment: + unary_expression assignment_operator expression ; +unary_expression: + //('(' arguments ')' ('[' | '.' | '(')) => primary_or_array_creation_expression + (cast_expression) => cast_expression + | primary_or_array_creation_expression + | '+' unary_expression + | '-' unary_expression + | '!' unary_expression + | '~' unary_expression + | pre_increment_expression + | pre_decrement_expression + | pointer_indirection_expression + | addressof_expression + ; +cast_expression: + '(' type ')' unary_expression ; +assignment_operator: + '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>' '>=' ; +pre_increment_expression: + '++' unary_expression ; +pre_decrement_expression: + '--' unary_expression ; +pointer_indirection_expression: + '*' unary_expression ; +addressof_expression: + '&' unary_expression ; + +non_assignment_expression: + //'non ASSIGNment' + (anonymous_function_signature '=>') => lambda_expression + | (query_expression) => query_expression + | conditional_expression + ; + +/////////////////////////////////////////////////////// +// Conditional Expression Section +/////////////////////////////////////////////////////// + +multiplicative_expression: + unary_expression ( ('*'|'/'|'%') unary_expression)* ; +additive_expression: + multiplicative_expression (('+'|'-') multiplicative_expression)* ; +// >> check needed (no whitespace) +shift_expression: + additive_expression (('<<'|'>' '>') additive_expression)* ; +relational_expression: + shift_expression + ( (('<'|'>'|'>='|'<=') shift_expression) + | (('is'|'as') non_nullable_type) + )* ; +equality_expression: + relational_expression + (('=='|'!=') relational_expression)* ; +and_expression: + equality_expression ('&' equality_expression)* ; +exclusive_or_expression: + and_expression ('^' and_expression)* ; +inclusive_or_expression: + exclusive_or_expression ('|' exclusive_or_expression)* ; +conditional_and_expression: + inclusive_or_expression ('&&' inclusive_or_expression)* ; +conditional_or_expression: + conditional_and_expression ('||' conditional_and_expression)* ; + +null_coalescing_expression: + conditional_or_expression ('??' conditional_or_expression)* ; +conditional_expression: + null_coalescing_expression ('?' expression ':' expression)? ; + +/////////////////////////////////////////////////////// +// lambda Section +/////////////////////////////////////////////////////// +lambda_expression: + anonymous_function_signature '=>' anonymous_function_body; +anonymous_function_signature: + '(' (explicit_anonymous_function_parameter_list + | implicit_anonymous_function_parameter_list)? ')' + | implicit_anonymous_function_parameter_list + ; +implicit_anonymous_function_parameter_list: + implicit_anonymous_function_parameter (',' implicit_anonymous_function_parameter)* ; +implicit_anonymous_function_parameter: + identifier; +anonymous_function_body: + expression + | block ; + +/////////////////////////////////////////////////////// +// LINQ Section +/////////////////////////////////////////////////////// +query_expression: + from_clause query_body ; +query_body: + // match 'into' to closest query_body + query_body_clauses? select_or_group_clause (('into') => query_continuation)? ; +query_continuation: + 'into' identifier query_body; +query_body_clauses: + query_body_clause+ ; +query_body_clause: + from_clause + | let_clause + | where_clause + | join_clause + | orderby_clause; +from_clause: + 'from' type? identifier 'in' expression ; +join_clause: + 'join' type? identifier 'in' expression 'on' expression 'equals' expression ('into' identifier)? ; +let_clause: + 'let' identifier '=' expression; +orderby_clause: + 'orderby' ordering_list ; +ordering_list: + ordering (',' ordering)* ; +ordering: + expression ordering_direction + ; +ordering_direction: + 'ascending' + | 'descending' ; +select_or_group_clause: + select_clause + | group_clause ; +select_clause: + 'select' expression ; +group_clause: + 'group' expression 'by' expression ; +where_clause: + 'where' boolean_expression ; +boolean_expression: + expression; + +/////////////////////////////////////////////////////// +// B.2.13 Attributes +/////////////////////////////////////////////////////// +global_attributes: + global_attribute+ ; +global_attribute: + '[' global_attribute_target_specifier attribute_list ','? ']' ; +global_attribute_target_specifier: + global_attribute_target ':' ; +global_attribute_target: + 'assembly' | 'module' ; +attributes: + attribute_sections ; +attribute_sections: + attribute_section+ ; +attribute_section: + '[' attribute_target_specifier? attribute_list ','? ']' ; +attribute_target_specifier: + attribute_target ':' ; +attribute_target: + 'field' | 'event' | 'method' | 'param' | 'property' | 'return' | 'type' ; +attribute_list: + attribute (',' attribute)* ; +attribute: + type_name attribute_arguments? ; +// TODO: allows a mix of named/positional arguments in any order +attribute_arguments: + '(' (')' // empty + | (positional_argument ((',' identifier '=') => named_argument + |',' positional_argument)* + ) ')' + ) ; +positional_argument_list: + positional_argument (',' positional_argument)* ; +positional_argument: + attribute_argument_expression ; +named_argument_list: + named_argument (',' named_argument)* ; +named_argument: + identifier '=' attribute_argument_expression ; +attribute_argument_expression: + expression ; + +/////////////////////////////////////////////////////// +// Class Section +/////////////////////////////////////////////////////// + +class_declaration: + 'class' type_or_generic class_base? type_parameter_constraints_clauses? class_body ';'? ; +class_base: + // syntactically base class vs interface name is the same + //':' class_type (',' interface_type_list)? ; + ':' interface_type_list ; + +interface_type_list: + type (',' type)* ; + +class_body: + '{' class_member_declarations? '}' ; +class_member_declarations: + class_member_declaration+ ; + +/////////////////////////////////////////////////////// +constant_declaration: + 'const' type constant_declarators ';' ; +constant_declarators: + constant_declarator (',' constant_declarator)* ; +constant_declarator: + identifier ('=' constant_expression)? ; +constant_expression: + expression; + +/////////////////////////////////////////////////////// +field_declaration: + variable_declarators ';' ; +variable_declarators: + variable_declarator (',' variable_declarator)* ; +variable_declarator: + type_name ('=' variable_initializer)? ; // eg. event EventHandler IInterface.VariableName = Foo; + +/////////////////////////////////////////////////////// +method_declaration: + method_header method_body ; +method_header: + member_name '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ; +method_body: + block ; +member_name: + qid ; // IInterface.Method logic added. + +/////////////////////////////////////////////////////// +property_declaration: + member_name '{' accessor_declarations '}' ; +accessor_declarations: + attributes? + (get_accessor_declaration attributes? set_accessor_declaration? + | set_accessor_declaration attributes? get_accessor_declaration?) ; +get_accessor_declaration: + accessor_modifier? 'get' accessor_body ; +set_accessor_declaration: + accessor_modifier? 'set' accessor_body ; +accessor_modifier: + 'public' | 'protected' | 'private' | 'internal' ; +accessor_body: + block ; + +/////////////////////////////////////////////////////// +event_declaration: + 'event' type + ((member_name '{') => member_name '{' event_accessor_declarations '}' + | variable_declarators ';') // typename=foo; + ; +event_modifiers: + modifier+ ; +event_accessor_declarations: + attributes? ((add_accessor_declaration attributes? remove_accessor_declaration) + | (remove_accessor_declaration attributes? add_accessor_declaration)) ; +add_accessor_declaration: + 'add' block ; +remove_accessor_declaration: + 'remove' block ; + +/////////////////////////////////////////////////////// +// enum declaration +/////////////////////////////////////////////////////// +enum_declaration: + 'enum' identifier enum_base? enum_body ';'? ; +enum_base: + ':' integral_type ; +enum_body: + '{' (enum_member_declarations ','?)? '}' ; +enum_member_declarations: + enum_member_declaration (',' enum_member_declaration)* ; +enum_member_declaration: + attributes? identifier ('=' expression)? ; +//enum_modifiers: +// enum_modifier+ ; +//enum_modifier: +// 'new' | 'public' | 'protected' | 'internal' | 'private' ; +integral_type: + 'sbyte' | 'byte' | 'short' | 'ushort' | 'int' | 'uint' | 'long' | 'ulong' | 'char' ; + +// B.2.12 Delegates +delegate_declaration: + 'delegate' return_type identifier variant_generic_parameter_list? + '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ; +delegate_modifiers: + modifier+ ; +// 4.0 +variant_generic_parameter_list: + '<' variant_type_parameters '>' ; +variant_type_parameters: + variant_type_variable_name (',' variant_type_variable_name)* ; +variant_type_variable_name: + attributes? variance_annotation? type_variable_name ; +variance_annotation: + 'in' | 'out' ; + +type_parameter_constraints_clauses: + type_parameter_constraints_clause (',' type_parameter_constraints_clause)* ; +type_parameter_constraints_clause: + 'where' type_variable_name ':' type_parameter_constraint_list ; +// class, Circle, new() +type_parameter_constraint_list: + ('class' | 'struct') (',' secondary_constraint_list)? (',' constructor_constraint)? + | secondary_constraint_list (',' constructor_constraint)? + | constructor_constraint ; +//primary_constraint: +// class_type +// | 'class' +// | 'struct' ; +secondary_constraint_list: + secondary_constraint (',' secondary_constraint)* ; +secondary_constraint: + type_name ; // | type_variable_name) ; +type_variable_name: + identifier ; +constructor_constraint: + 'new' '(' ')' ; +return_type: + type + | 'void'; +formal_parameter_list: + formal_parameter (',' formal_parameter)* ; +formal_parameter: + attributes? (fixed_parameter | parameter_array) + | '__arglist'; // __arglist is undocumented, see google +fixed_parameters: + fixed_parameter (',' fixed_parameter)* ; +// 4.0 +fixed_parameter: + parameter_modifier? type identifier default_argument? ; +// 4.0 +default_argument: + '=' expression; +parameter_modifier: + 'ref' | 'out' | 'this' ; +parameter_array: + 'params' type identifier ; + +/////////////////////////////////////////////////////// +interface_declaration: + 'interface' identifier variant_generic_parameter_list? + interface_base? type_parameter_constraints_clauses? interface_body ';'? ; +interface_modifiers: + modifier+ ; +interface_base: + ':' interface_type_list ; +interface_body: + '{' interface_member_declarations? '}' ; +interface_member_declarations: + interface_member_declaration+ ; +interface_member_declaration: + attributes? modifiers? + ('void' interface_method_declaration + | interface_event_declaration + | type ( (member_name '(') => interface_method_declaration + | (member_name '{') => interface_property_declaration + | interface_indexer_declaration) + ) + ; +interface_property_declaration: + identifier '{' interface_accessor_declarations '}' ; +interface_method_declaration: + identifier generic_argument_list? + '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ; +interface_event_declaration: + //attributes? 'new'? + 'event' type identifier ';' ; +interface_indexer_declaration: + // attributes? 'new'? type + 'this' '[' formal_parameter_list ']' '{' interface_accessor_declarations '}' ; +interface_accessor_declarations: + attributes? + (interface_get_accessor_declaration attributes? interface_set_accessor_declaration? + | interface_set_accessor_declaration attributes? interface_get_accessor_declaration?) ; +interface_get_accessor_declaration: + 'get' ';' ; // no body / modifiers +interface_set_accessor_declaration: + 'set' ';' ; // no body / modifiers +method_modifiers: + modifier+ ; + +/////////////////////////////////////////////////////// +struct_declaration: + 'struct' type_or_generic struct_interfaces? type_parameter_constraints_clauses? struct_body ';'? ; +struct_modifiers: + struct_modifier+ ; +struct_modifier: + 'new' | 'public' | 'protected' | 'internal' | 'private' | 'unsafe' ; +struct_interfaces: + ':' interface_type_list; +struct_body: + '{' struct_member_declarations? '}'; +struct_member_declarations: + struct_member_declaration+ ; +struct_member_declaration: + attributes? m=modifiers? + ( 'const' type constant_declarators ';' + | event_declaration // 'event' + | 'partial' (method_declaration + | interface_declaration + | class_declaration + | struct_declaration) + + | interface_declaration // 'interface' + | class_declaration // 'class' + | 'void' method_declaration + | type ( (member_name '(') => method_declaration + | (member_name '{') => property_declaration + | (member_name '.' 'this') => type_name '.' indexer_declaration + | indexer_declaration //this + | field_declaration // qid + | operator_declaration + ) +// common_modifiers// (method_modifiers | field_modifiers) + + | struct_declaration // 'struct' + | enum_declaration // 'enum' + | delegate_declaration // 'delegate' + | conversion_operator_declaration + | constructor_declaration // | static_constructor_declaration + ) + ; + + +/////////////////////////////////////////////////////// +indexer_declaration: + indexer_declarator '{' accessor_declarations '}' ; +indexer_declarator: + //(type_name '.')? + 'this' '[' formal_parameter_list ']' ; + +/////////////////////////////////////////////////////// +operator_declaration: + operator_declarator operator_body ; +operator_declarator: + 'operator' + (('+' | '-') '(' type identifier (binary_operator_declarator | unary_operator_declarator) + | overloadable_unary_operator '(' type identifier unary_operator_declarator + | overloadable_binary_operator '(' type identifier binary_operator_declarator) ; +unary_operator_declarator: + ')' ; +overloadable_unary_operator: + /*'+' | '-' | */ '!' | '~' | '++' | '--' | 'true' | 'false' ; +binary_operator_declarator: + ',' type identifier ')' ; +// >> check needed +overloadable_binary_operator: + /*'+' | '-' | */ '*' | '/' | '%' | '&' | '|' | '^' | '<<' | '>' '>' | '==' | '!=' | '>' | '<' | '>=' | '<=' ; + +conversion_operator_declaration: + conversion_operator_declarator operator_body ; +conversion_operator_declarator: + ('implicit' | 'explicit') 'operator' type '(' type identifier ')' ; +operator_body: + block ; + +/////////////////////////////////////////////////////// +constructor_declaration: + constructor_declarator constructor_body ; +constructor_declarator: + identifier '(' formal_parameter_list? ')' constructor_initializer? ; +constructor_initializer: + ':' ('base' | 'this') '(' argument_list? ')' ; +constructor_body: + block ; + +/////////////////////////////////////////////////////// +//static_constructor_declaration: +// identifier '(' ')' static_constructor_body ; +//static_constructor_body: +// block ; + +/////////////////////////////////////////////////////// +destructor_declaration: + '~' identifier '(' ')' destructor_body ; +destructor_body: + block ; + +/////////////////////////////////////////////////////// +invocation_expression: + invocation_start (((arguments ('['|'.'|'->')) => arguments invocation_part) + | invocation_part)* arguments ; +invocation_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list + | 'this' + | 'base' + | identifier ('::' identifier)? + | typeof_expression // typeof(Foo).Name + ; +invocation_part: + access_identifier + | brackets ; + +/////////////////////////////////////////////////////// + +statement: + (declaration_statement) => declaration_statement + | (identifier ':') => labeled_statement + | embedded_statement + ; +embedded_statement: + block + | selection_statement // if, switch + | iteration_statement // while, do, for, foreach + | jump_statement // break, continue, goto, return, throw + | try_statement + | checked_statement + | unchecked_statement + | lock_statement + | using_statement + | yield_statement + | unsafe_statement + | fixed_statement + | expression_statement // expression! + ; +fixed_statement: + 'fixed' '(' pointer_type fixed_pointer_declarators ')' embedded_statement ; +fixed_pointer_declarators: + fixed_pointer_declarator (',' fixed_pointer_declarator)* ; +fixed_pointer_declarator: + identifier '=' fixed_pointer_initializer ; +fixed_pointer_initializer: + //'&' variable_reference // unary_expression covers this + expression; +unsafe_statement: + 'unsafe' block; +labeled_statement: + identifier ':' statement ; +declaration_statement: + (local_variable_declaration + | local_constant_declaration) ';' ; +local_variable_declaration: + local_variable_type local_variable_declarators ; +local_variable_type: + ('var') => 'var' + | ('dynamic') => 'dynamic' + | type ; +local_variable_declarators: + local_variable_declarator (',' local_variable_declarator)* ; +local_variable_declarator: + identifier ('=' local_variable_initializer)? ; +local_variable_initializer: + expression + | array_initializer + | stackalloc_initializer; +stackalloc_initializer: + 'stackalloc' unmanaged_type '[' expression ']' ; +local_constant_declaration: + 'const' type constant_declarators ; +expression_statement: + expression ';' ; + +// TODO: should be assignment, call, increment, decrement, and new object expressions +statement_expression: + expression + ; +selection_statement: + if_statement + | switch_statement ; +if_statement: + // else goes with closest if + 'if' '(' boolean_expression ')' embedded_statement (('else') => else_statement)? + ; +else_statement: + 'else' embedded_statement ; +switch_statement: + 'switch' '(' expression ')' switch_block ; +switch_block: + '{' switch_sections? '}' ; +switch_sections: + switch_section+ ; +switch_section: + switch_labels statement_list ; +switch_labels: + switch_label+ ; +switch_label: + ('case' constant_expression ':') + | ('default' ':') ; +iteration_statement: + while_statement + | do_statement + | for_statement + | foreach_statement ; +while_statement: + 'while' '(' boolean_expression ')' embedded_statement ; +do_statement: + 'do' embedded_statement 'while' '(' boolean_expression ')' ';' ; +for_statement: + 'for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement ; +for_initializer: + (local_variable_declaration) => local_variable_declaration + | statement_expression_list + ; +for_condition: + boolean_expression ; +for_iterator: + statement_expression_list ; +statement_expression_list: + statement_expression (',' statement_expression)* ; +foreach_statement: + 'foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement ; +jump_statement: + break_statement + | continue_statement + | goto_statement + | return_statement + | throw_statement ; +break_statement: + 'break' ';' ; +continue_statement: + 'continue' ';' ; +goto_statement: + 'goto' ( identifier + | 'case' constant_expression + | 'default') ';' ; +return_statement: + 'return' expression? ';' ; +throw_statement: + 'throw' expression? ';' ; +try_statement: + 'try' block ( catch_clauses finally_clause? + | finally_clause); +//TODO one or both +catch_clauses: + 'catch' (specific_catch_clauses | general_catch_clause) ; +specific_catch_clauses: + specific_catch_clause ('catch' (specific_catch_clause | general_catch_clause))*; +specific_catch_clause: + '(' class_type identifier? ')' block ; +general_catch_clause: + block ; +finally_clause: + 'finally' block ; +checked_statement: + 'checked' block ; +unchecked_statement: + 'unchecked' block ; +lock_statement: + 'lock' '(' expression ')' embedded_statement ; +using_statement: + 'using' '(' resource_acquisition ')' embedded_statement ; +resource_acquisition: + (local_variable_declaration) => local_variable_declaration + | expression ; +yield_statement: + 'yield' ('return' expression ';' + | 'break' ';') ; + +/////////////////////////////////////////////////////// +// Lexar Section +/////////////////////////////////////////////////////// + +predefined_type: + 'bool' | 'byte' | 'char' | 'decimal' | 'double' | 'float' | 'int' | 'long' | 'object' | 'sbyte' + | 'short' | 'string' | 'uint' | 'ulong' | 'ushort' ; + +identifier: + IDENTIFIER | also_keyword; + +keyword: + 'abstract' | 'as' | 'base' | 'bool' | 'break' | 'byte' | 'case' | 'catch' | 'char' | 'checked' | 'class' | 'const' | 'continue' | 'decimal' | 'default' | 'delegate' | 'do' | 'double' | 'else' | 'enum' | 'event' | 'explicit' | 'extern' | 'false' | 'finally' | 'fixed' | 'float' | 'for' | 'foreach' | 'goto' | 'if' | 'implicit' | 'in' | 'int' | 'interface' | 'internal' | 'is' | 'lock' | 'long' | 'namespace' | 'new' | 'null' | 'object' | 'operator' | 'out' | 'override' | 'params' | 'private' | 'protected' | 'public' | 'readonly' | 'ref' | 'return' | 'sbyte' | 'sealed' | 'short' | 'sizeof' | 'stackalloc' | 'static' | 'string' | 'struct' | 'switch' | 'this' | 'throw' | 'true' | 'try' | 'typeof' | 'uint' | 'ulong' | 'unchecked' | 'unsafe' | 'ushort' | 'using' | 'virtual' | 'void' | 'volatile' ; + +also_keyword: + 'add' | 'alias' | 'assembly' | 'module' | 'field' | 'method' | 'param' | 'property' | 'type' | 'yield' + | 'from' | 'into' | 'join' | 'on' | 'where' | 'orderby' | 'group' | 'by' | 'ascending' | 'descending' + | 'equals' | 'select' | 'pragma' | 'let' | 'remove' | 'get' | 'set' | 'var' | '__arglist' | 'dynamic' | 'elif' + | 'endif' | 'define' | 'undef'; + +literal: + Real_literal + | NUMBER + | Hex_number + | Character_literal + | STRINGLITERAL + | Verbatim_string_literal + | TRUE + | FALSE + | NULL + ; + diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g new file mode 100644 index 0000000..6cec3dc --- /dev/null +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g @@ -0,0 +1,1041 @@ +tree grammar NetMaker; + +options { + tokenVocab=cs; + ASTLabelType=CommonTree; + output=AST; + language=CSharp2; + superClass='RusticiSoftware.Translator.CSharp.CommonWalker'; +} + +@namespace { RusticiSoftware.Translator.CSharp } + +@members +{ + protected bool is_class_modifier() + { + return false; + } +} + +compilation_unit: + namespace_body[true]; + +namespace_declaration: + 'namespace' qualified_identifier namespace_block ';'? ; +namespace_block: + '{' namespace_body[false] '}' ; +namespace_body[bool bGlobal]: + extern_alias_directives? using_directives? global_attributes? namespace_member_declarations? ; +extern_alias_directives: + extern_alias_directive+ ; +extern_alias_directive: + 'extern' 'alias' identifier ';' ; +using_directives: + using_directive+ ; +using_directive: + (using_alias_directive + | using_namespace_directive) ; +using_alias_directive: + 'using' identifier '=' namespace_or_type_name ';' ; +using_namespace_directive: + 'using' namespace_name ';' ; +namespace_member_declarations: + namespace_member_declaration+ ; +namespace_member_declaration: + namespace_declaration + | attributes? modifiers? type_declaration ; +type_declaration: + ('partial') => 'partial' (class_declaration + | struct_declaration + | interface_declaration) + | class_declaration + | struct_declaration + | interface_declaration + | enum_declaration + | delegate_declaration ; +// Identifiers +qualified_identifier: + identifier ('.' identifier)*; +namespace_name + : namespace_or_type_name ; + +modifiers: + modifier+ ; +modifier: + 'new' | 'public' | 'protected' | 'private' | 'internal' | 'unsafe' | 'abstract' | 'sealed' | 'static' + | 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override'; + +class_member_declaration: + attributes? + m=modifiers? + ( 'const' type constant_declarators ';' + | event_declaration // 'event' + | 'partial' (method_declaration + | interface_declaration + | class_declaration + | struct_declaration) + | interface_declaration // 'interface' + | 'void' method_declaration + | type ( (member_name '(') => method_declaration + | (member_name '{') => property_declaration + | (member_name '.' 'this') => type_name '.' indexer_declaration + | indexer_declaration //this + | field_declaration // qid + | operator_declaration + ) +// common_modifiers// (method_modifiers | field_modifiers) + + | class_declaration // 'class' + | struct_declaration // 'struct' + | enum_declaration // 'enum' + | delegate_declaration // 'delegate' + | conversion_operator_declaration + | constructor_declaration // | static_constructor_declaration + | destructor_declaration + ) + ; + +primary_expression: + ('this' brackets) => 'this' brackets primary_expression_part* + | ('base' brackets) => 'this' brackets primary_expression_part* + | primary_expression_start primary_expression_part* + | 'new' ( (object_creation_expression ('.'|'->'|'[')) => + object_creation_expression primary_expression_part+ // new Foo(arg, arg).Member + // try the simple one first, this has no argS and no expressions + // symantically could be object creation + | (delegate_creation_expression) => delegate_creation_expression// new FooDelegate (MyFunction) + | object_creation_expression + | anonymous_object_creation_expression) // new {int X, string Y} + | sizeof_expression // sizeof (struct) + | checked_expression // checked (... + | unchecked_expression // unchecked {...} + | default_value_expression // default + | anonymous_method_expression // delegate (int foo) {} + ; + +primary_expression_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list + | identifier ('::' identifier)? + | 'this' + | 'base' + | paren_expression + | typeof_expression // typeof(Foo).Name + | literal + ; + +primary_expression_part: + access_identifier + | brackets_or_arguments + | '++' + | '--' ; +access_identifier: + access_operator type_or_generic ; +access_operator: + '.' | '->' ; +brackets_or_arguments: + brackets | arguments ; +brackets: + '[' expression_list? ']' ; +paren_expression: + '(' expression ')' ; +arguments: + '(' argument_list? ')' ; +argument_list: + argument (',' argument)*; +// 4.0 +argument: + argument_name argument_value + | argument_value; +argument_name: + identifier ':'; +argument_value: + expression + | ref_variable_reference + | 'out' variable_reference ; +ref_variable_reference: + 'ref' + (('(' type ')') => '(' type ')' (ref_variable_reference | variable_reference) // SomeFunc(ref (int) ref foo) + // SomeFunc(ref (int) foo) + | variable_reference); // SomeFunc(ref foo) +// lvalue +variable_reference: + expression; +rank_specifiers: + rank_specifier+ ; +rank_specifier: + '[' dim_separators? ']' ; +dim_separators: + ','+ ; + +delegate_creation_expression: + // 'new' + type_name '(' type_name ')' ; +anonymous_object_creation_expression: + // 'new' + anonymous_object_initializer ; +anonymous_object_initializer: + '{' (member_declarator_list ','?)? '}'; +member_declarator_list: + member_declarator (',' member_declarator)* ; +member_declarator: + qid ('=' expression)? ; +primary_or_array_creation_expression: + (array_creation_expression) => array_creation_expression + | primary_expression + ; +// new Type[2] { } +array_creation_expression: + 'new' + (type ('[' expression_list ']' + ( rank_specifiers? array_initializer? // new int[4] + // | invocation_part* + | ( ((arguments ('['|'.'|'->')) => arguments invocation_part)// new object[2].GetEnumerator() + | invocation_part)* arguments + ) // new int[4]() + | array_initializer + ) + | rank_specifier // [,] + (array_initializer // var a = new[] { 1, 10, 100, 1000 }; // int[] + ) + ) ; +array_initializer: + '{' variable_initializer_list? ','? '}' ; +variable_initializer_list: + variable_initializer (',' variable_initializer)* ; +variable_initializer: + expression | array_initializer ; +sizeof_expression: + 'sizeof' '(' unmanaged_type ')'; +checked_expression: + 'checked' '(' expression ')' ; +unchecked_expression: + 'unchecked' '(' expression ')' ; +default_value_expression: + 'default' '(' type ')' ; +anonymous_method_expression: + 'delegate' explicit_anonymous_function_signature? block; +explicit_anonymous_function_signature: + '(' explicit_anonymous_function_parameter_list? ')' ; +explicit_anonymous_function_parameter_list: + explicit_anonymous_function_parameter (',' explicit_anonymous_function_parameter)* ; +explicit_anonymous_function_parameter: + anonymous_function_parameter_modifier? type identifier; +anonymous_function_parameter_modifier: + 'ref' | 'out'; + + +/////////////////////////////////////////////////////// +object_creation_expression: + // 'new' + type + ( '(' argument_list? ')' object_or_collection_initializer? + | object_or_collection_initializer ) + ; +object_or_collection_initializer: + '{' (object_initializer + | collection_initializer) ; +collection_initializer: + element_initializer_list ','? '}' ; +element_initializer_list: + element_initializer (',' element_initializer)* ; +element_initializer: + non_assignment_expression + | '{' expression_list '}' ; +// object-initializer eg's +// Rectangle r = new Rectangle { +// P1 = new Point { X = 0, Y = 1 }, +// P2 = new Point { X = 2, Y = 3 } +// }; +// TODO: comma should only follow a member_initializer_list +object_initializer: + member_initializer_list? ','? '}' ; +member_initializer_list: + member_initializer (',' member_initializer) ; +member_initializer: + identifier '=' initializer_value ; +initializer_value: + expression + | object_or_collection_initializer ; + +/////////////////////////////////////////////////////// + +typeof_expression: + 'typeof' '(' ((unbound_type_name) => unbound_type_name + | type + | 'void') ')' ; +// unbound type examples +//foo>> +//bar::foo<> +//foo1::foo2.foo3<,,> +unbound_type_name: // qualified_identifier v2 +// unbound_type_name_start unbound_type_name_part* ; + unbound_type_name_start + (((generic_dimension_specifier '.') => generic_dimension_specifier unbound_type_name_part) + | unbound_type_name_part)* + generic_dimension_specifier + ; + +unbound_type_name_start: + identifier ('::' identifier)?; +unbound_type_name_part: + '.' identifier; +generic_dimension_specifier: + '<' commas? '>' ; +commas: + ','+ ; + +/////////////////////////////////////////////////////// +// Type Section +/////////////////////////////////////////////////////// + +type_name: + namespace_or_type_name ; +namespace_or_type_name: + type_or_generic ('::' type_or_generic)? ('.' type_or_generic)* ; +type_or_generic: + (identifier generic_argument_list) => identifier generic_argument_list + | identifier ; + +qid: // qualified_identifier v2 + qid_start qid_part* + ; +qid_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list +// | 'this' +// | 'base' + | identifier ('::' identifier)? + | literal + ; // 0.ToString() is legal + + +qid_part: + access_identifier ; + +generic_argument_list: + '<' type_arguments '>' ; +type_arguments: + type (',' type)* ; + +type: + ((predefined_type | type_name) rank_specifiers) => (predefined_type | type_name) rank_specifiers '*'* + | ((predefined_type | type_name) ('*'+ | '?')) => (predefined_type | type_name) ('*'+ | '?') + | (predefined_type | type_name) + | 'void' '*'+ + ; +non_nullable_type: + (predefined_type | type_name) + ( rank_specifiers '*'* + | ('*'+)? + ) + | 'void' '*'+ ; + +non_array_type: + type; +array_type: + type; +unmanaged_type: + type; +class_type: + type; +pointer_type: + type; + + +/////////////////////////////////////////////////////// +// Statement Section +/////////////////////////////////////////////////////// +block: + ';' + | '{' statement_list? '}'; +statement_list: + statement+ ; + +/////////////////////////////////////////////////////// +// Expression Section +/////////////////////////////////////////////////////// +expression: + (unary_expression assignment_operator) => assignment + | non_assignment_expression + ; +expression_list: + expression (',' expression)* ; +assignment: + unary_expression assignment_operator expression ; +unary_expression: + //('(' arguments ')' ('[' | '.' | '(')) => primary_or_array_creation_expression + (cast_expression) => cast_expression + | primary_or_array_creation_expression + | '+' unary_expression + | '-' unary_expression + | '!' unary_expression + | '~' unary_expression + | pre_increment_expression + | pre_decrement_expression + | pointer_indirection_expression + | addressof_expression + ; +cast_expression: + '(' type ')' unary_expression ; +assignment_operator: + '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>' '>=' ; +pre_increment_expression: + '++' unary_expression ; +pre_decrement_expression: + '--' unary_expression ; +pointer_indirection_expression: + '*' unary_expression ; +addressof_expression: + '&' unary_expression ; + +non_assignment_expression: + //'non ASSIGNment' + (anonymous_function_signature '=>') => lambda_expression + | (query_expression) => query_expression + | conditional_expression + ; + +/////////////////////////////////////////////////////// +// Conditional Expression Section +/////////////////////////////////////////////////////// + +multiplicative_expression: + unary_expression ( ('*'|'/'|'%') unary_expression)* ; +additive_expression: + multiplicative_expression (('+'|'-') multiplicative_expression)* ; +// >> check needed (no whitespace) +shift_expression: + additive_expression (('<<'|'>' '>') additive_expression)* ; +relational_expression: + shift_expression + ( (('<'|'>'|'>='|'<=') shift_expression) + | (('is'|'as') non_nullable_type) + )* ; +equality_expression: + relational_expression + (('=='|'!=') relational_expression)* ; +and_expression: + equality_expression ('&' equality_expression)* ; +exclusive_or_expression: + and_expression ('^' and_expression)* ; +inclusive_or_expression: + exclusive_or_expression ('|' exclusive_or_expression)* ; +conditional_and_expression: + inclusive_or_expression ('&&' inclusive_or_expression)* ; +conditional_or_expression: + conditional_and_expression ('||' conditional_and_expression)* ; + +null_coalescing_expression: + conditional_or_expression ('??' conditional_or_expression)* ; +conditional_expression: + null_coalescing_expression ('?' expression ':' expression)? ; + +/////////////////////////////////////////////////////// +// lambda Section +/////////////////////////////////////////////////////// +lambda_expression: + anonymous_function_signature '=>' anonymous_function_body; +anonymous_function_signature: + '(' (explicit_anonymous_function_parameter_list + | implicit_anonymous_function_parameter_list)? ')' + | implicit_anonymous_function_parameter_list + ; +implicit_anonymous_function_parameter_list: + implicit_anonymous_function_parameter (',' implicit_anonymous_function_parameter)* ; +implicit_anonymous_function_parameter: + identifier; +anonymous_function_body: + expression + | block ; + +/////////////////////////////////////////////////////// +// LINQ Section +/////////////////////////////////////////////////////// +query_expression: + from_clause query_body ; +query_body: + // match 'into' to closest query_body + query_body_clauses? select_or_group_clause (('into') => query_continuation)? ; +query_continuation: + 'into' identifier query_body; +query_body_clauses: + query_body_clause+ ; +query_body_clause: + from_clause + | let_clause + | where_clause + | join_clause + | orderby_clause; +from_clause: + 'from' type? identifier 'in' expression ; +join_clause: + 'join' type? identifier 'in' expression 'on' expression 'equals' expression ('into' identifier)? ; +let_clause: + 'let' identifier '=' expression; +orderby_clause: + 'orderby' ordering_list ; +ordering_list: + ordering (',' ordering)* ; +ordering: + expression ordering_direction + ; +ordering_direction: + 'ascending' + | 'descending' ; +select_or_group_clause: + select_clause + | group_clause ; +select_clause: + 'select' expression ; +group_clause: + 'group' expression 'by' expression ; +where_clause: + 'where' boolean_expression ; +boolean_expression: + expression; + +/////////////////////////////////////////////////////// +// B.2.13 Attributes +/////////////////////////////////////////////////////// +global_attributes: + global_attribute+ ; +global_attribute: + '[' global_attribute_target_specifier attribute_list ','? ']' ; +global_attribute_target_specifier: + global_attribute_target ':' ; +global_attribute_target: + 'assembly' | 'module' ; +attributes: + attribute_sections ; +attribute_sections: + attribute_section+ ; +attribute_section: + '[' attribute_target_specifier? attribute_list ','? ']' ; +attribute_target_specifier: + attribute_target ':' ; +attribute_target: + 'field' | 'event' | 'method' | 'param' | 'property' | 'return' | 'type' ; +attribute_list: + attribute (',' attribute)* ; +attribute: + type_name attribute_arguments? ; +// TODO: allows a mix of named/positional arguments in any order +attribute_arguments: + '(' (')' // empty + | (positional_argument ((',' identifier '=') => named_argument + |',' positional_argument)* + ) ')' + ) ; +positional_argument_list: + positional_argument (',' positional_argument)* ; +positional_argument: + attribute_argument_expression ; +named_argument_list: + named_argument (',' named_argument)* ; +named_argument: + identifier '=' attribute_argument_expression ; +attribute_argument_expression: + expression ; + +/////////////////////////////////////////////////////// +// Class Section +/////////////////////////////////////////////////////// + +class_declaration: + 'class' type_or_generic class_base? type_parameter_constraints_clauses? class_body ';'? ; +class_base: + // syntactically base class vs interface name is the same + //':' class_type (',' interface_type_list)? ; + ':' interface_type_list ; + +interface_type_list: + type (',' type)* ; + +class_body: + '{' class_member_declarations? '}' ; +class_member_declarations: + class_member_declaration+ ; + +/////////////////////////////////////////////////////// +constant_declaration: + 'const' type constant_declarators ';' ; +constant_declarators: + constant_declarator (',' constant_declarator)* ; +constant_declarator: + identifier ('=' constant_expression)? ; +constant_expression: + expression; + +/////////////////////////////////////////////////////// +field_declaration: + variable_declarators ';' ; +variable_declarators: + variable_declarator (',' variable_declarator)* ; +variable_declarator: + type_name ('=' variable_initializer)? ; // eg. event EventHandler IInterface.VariableName = Foo; + +/////////////////////////////////////////////////////// +method_declaration: + method_header method_body ; +method_header: + member_name '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ; +method_body: + block ; +member_name: + qid ; // IInterface.Method logic added. + +/////////////////////////////////////////////////////// +property_declaration: + member_name '{' accessor_declarations '}' ; +accessor_declarations: + attributes? + (get_accessor_declaration attributes? set_accessor_declaration? + | set_accessor_declaration attributes? get_accessor_declaration?) ; +get_accessor_declaration: + accessor_modifier? 'get' accessor_body ; +set_accessor_declaration: + accessor_modifier? 'set' accessor_body ; +accessor_modifier: + 'public' | 'protected' | 'private' | 'internal' ; +accessor_body: + block ; + +/////////////////////////////////////////////////////// +event_declaration: + 'event' type + ((member_name '{') => member_name '{' event_accessor_declarations '}' + | variable_declarators ';') // typename=foo; + ; +event_modifiers: + modifier+ ; +event_accessor_declarations: + attributes? ((add_accessor_declaration attributes? remove_accessor_declaration) + | (remove_accessor_declaration attributes? add_accessor_declaration)) ; +add_accessor_declaration: + 'add' block ; +remove_accessor_declaration: + 'remove' block ; + +/////////////////////////////////////////////////////// +// enum declaration +/////////////////////////////////////////////////////// +enum_declaration: + 'enum' identifier enum_base? enum_body ';'? ; +enum_base: + ':' integral_type ; +enum_body: + '{' (enum_member_declarations ','?)? '}' ; +enum_member_declarations: + enum_member_declaration (',' enum_member_declaration)* ; +enum_member_declaration: + attributes? identifier ('=' expression)? ; +//enum_modifiers: +// enum_modifier+ ; +//enum_modifier: +// 'new' | 'public' | 'protected' | 'internal' | 'private' ; +integral_type: + 'sbyte' | 'byte' | 'short' | 'ushort' | 'int' | 'uint' | 'long' | 'ulong' | 'char' ; + +// B.2.12 Delegates +delegate_declaration: + 'delegate' return_type identifier variant_generic_parameter_list? + '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ; +delegate_modifiers: + modifier+ ; +// 4.0 +variant_generic_parameter_list: + '<' variant_type_parameters '>' ; +variant_type_parameters: + variant_type_variable_name (',' variant_type_variable_name)* ; +variant_type_variable_name: + attributes? variance_annotation? type_variable_name ; +variance_annotation: + 'in' | 'out' ; + +type_parameter_constraints_clauses: + type_parameter_constraints_clause (',' type_parameter_constraints_clause)* ; +type_parameter_constraints_clause: + 'where' type_variable_name ':' type_parameter_constraint_list ; +// class, Circle, new() +type_parameter_constraint_list: + ('class' | 'struct') (',' secondary_constraint_list)? (',' constructor_constraint)? + | secondary_constraint_list (',' constructor_constraint)? + | constructor_constraint ; +//primary_constraint: +// class_type +// | 'class' +// | 'struct' ; +secondary_constraint_list: + secondary_constraint (',' secondary_constraint)* ; +secondary_constraint: + type_name ; // | type_variable_name) ; +type_variable_name: + identifier ; +constructor_constraint: + 'new' '(' ')' ; +return_type: + type + | 'void'; +formal_parameter_list: + formal_parameter (',' formal_parameter)* ; +formal_parameter: + attributes? (fixed_parameter | parameter_array) + | '__arglist'; // __arglist is undocumented, see google +fixed_parameters: + fixed_parameter (',' fixed_parameter)* ; +// 4.0 +fixed_parameter: + parameter_modifier? type identifier default_argument? ; +// 4.0 +default_argument: + '=' expression; +parameter_modifier: + 'ref' | 'out' | 'this' ; +parameter_array: + 'params' type identifier ; + +/////////////////////////////////////////////////////// +interface_declaration: + 'interface' identifier variant_generic_parameter_list? + interface_base? type_parameter_constraints_clauses? interface_body ';'? ; +interface_modifiers: + modifier+ ; +interface_base: + ':' interface_type_list ; +interface_body: + '{' interface_member_declarations? '}' ; +interface_member_declarations: + interface_member_declaration+ ; +interface_member_declaration: + attributes? modifiers? + ('void' interface_method_declaration + | interface_event_declaration + | type ( (member_name '(') => interface_method_declaration + | (member_name '{') => interface_property_declaration + | interface_indexer_declaration) + ) + ; +interface_property_declaration: + identifier '{' interface_accessor_declarations '}' ; +interface_method_declaration: + identifier generic_argument_list? + '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' ; +interface_event_declaration: + //attributes? 'new'? + 'event' type identifier ';' ; +interface_indexer_declaration: + // attributes? 'new'? type + 'this' '[' formal_parameter_list ']' '{' interface_accessor_declarations '}' ; +interface_accessor_declarations: + attributes? + (interface_get_accessor_declaration attributes? interface_set_accessor_declaration? + | interface_set_accessor_declaration attributes? interface_get_accessor_declaration?) ; +interface_get_accessor_declaration: + 'get' ';' ; // no body / modifiers +interface_set_accessor_declaration: + 'set' ';' ; // no body / modifiers +method_modifiers: + modifier+ ; + +/////////////////////////////////////////////////////// +struct_declaration: + 'struct' type_or_generic struct_interfaces? type_parameter_constraints_clauses? struct_body ';'? ; +struct_modifiers: + struct_modifier+ ; +struct_modifier: + 'new' | 'public' | 'protected' | 'internal' | 'private' | 'unsafe' ; +struct_interfaces: + ':' interface_type_list; +struct_body: + '{' struct_member_declarations? '}'; +struct_member_declarations: + struct_member_declaration+ ; +struct_member_declaration: + attributes? m=modifiers? + ( 'const' type constant_declarators ';' + | event_declaration // 'event' + | 'partial' (method_declaration + | interface_declaration + | class_declaration + | struct_declaration) + + | interface_declaration // 'interface' + | class_declaration // 'class' + | 'void' method_declaration + | type ( (member_name '(') => method_declaration + | (member_name '{') => property_declaration + | (member_name '.' 'this') => type_name '.' indexer_declaration + | indexer_declaration //this + | field_declaration // qid + | operator_declaration + ) +// common_modifiers// (method_modifiers | field_modifiers) + + | struct_declaration // 'struct' + | enum_declaration // 'enum' + | delegate_declaration // 'delegate' + | conversion_operator_declaration + | constructor_declaration // | static_constructor_declaration + ) + ; + + +/////////////////////////////////////////////////////// +indexer_declaration: + indexer_declarator '{' accessor_declarations '}' ; +indexer_declarator: + //(type_name '.')? + 'this' '[' formal_parameter_list ']' ; + +/////////////////////////////////////////////////////// +operator_declaration: + operator_declarator operator_body ; +operator_declarator: + 'operator' + (('+' | '-') '(' type identifier (binary_operator_declarator | unary_operator_declarator) + | overloadable_unary_operator '(' type identifier unary_operator_declarator + | overloadable_binary_operator '(' type identifier binary_operator_declarator) ; +unary_operator_declarator: + ')' ; +overloadable_unary_operator: + /*'+' | '-' | */ '!' | '~' | '++' | '--' | 'true' | 'false' ; +binary_operator_declarator: + ',' type identifier ')' ; +// >> check needed +overloadable_binary_operator: + /*'+' | '-' | */ '*' | '/' | '%' | '&' | '|' | '^' | '<<' | '>' '>' | '==' | '!=' | '>' | '<' | '>=' | '<=' ; + +conversion_operator_declaration: + conversion_operator_declarator operator_body ; +conversion_operator_declarator: + ('implicit' | 'explicit') 'operator' type '(' type identifier ')' ; +operator_body: + block ; + +/////////////////////////////////////////////////////// +constructor_declaration: + constructor_declarator constructor_body ; +constructor_declarator: + identifier '(' formal_parameter_list? ')' constructor_initializer? ; +constructor_initializer: + ':' ('base' | 'this') '(' argument_list? ')' ; +constructor_body: + block ; + +/////////////////////////////////////////////////////// +//static_constructor_declaration: +// identifier '(' ')' static_constructor_body ; +//static_constructor_body: +// block ; + +/////////////////////////////////////////////////////// +destructor_declaration: + '~' identifier '(' ')' destructor_body ; +destructor_body: + block ; + +/////////////////////////////////////////////////////// +invocation_expression: + invocation_start (((arguments ('['|'.'|'->')) => arguments invocation_part) + | invocation_part)* arguments ; +invocation_start: + predefined_type + | (identifier generic_argument_list) => identifier generic_argument_list + | 'this' + | 'base' + | identifier ('::' identifier)? + | typeof_expression // typeof(Foo).Name + ; +invocation_part: + access_identifier + | brackets ; + +/////////////////////////////////////////////////////// + +statement: + (declaration_statement) => declaration_statement + | (identifier ':') => labeled_statement + | embedded_statement + ; +embedded_statement: + block + | selection_statement // if, switch + | iteration_statement // while, do, for, foreach + | jump_statement // break, continue, goto, return, throw + | try_statement + | checked_statement + | unchecked_statement + | lock_statement + | using_statement + | yield_statement + | unsafe_statement + | fixed_statement + | expression_statement // expression! + ; +fixed_statement: + 'fixed' '(' pointer_type fixed_pointer_declarators ')' embedded_statement ; +fixed_pointer_declarators: + fixed_pointer_declarator (',' fixed_pointer_declarator)* ; +fixed_pointer_declarator: + identifier '=' fixed_pointer_initializer ; +fixed_pointer_initializer: + //'&' variable_reference // unary_expression covers this + expression; +unsafe_statement: + 'unsafe' block; +labeled_statement: + identifier ':' statement ; +declaration_statement: + (local_variable_declaration + | local_constant_declaration) ';' ; +local_variable_declaration: + local_variable_type local_variable_declarators ; +local_variable_type: + ('var') => 'var' + | ('dynamic') => 'dynamic' + | type ; +local_variable_declarators: + local_variable_declarator (',' local_variable_declarator)* ; +local_variable_declarator: + identifier ('=' local_variable_initializer)? ; +local_variable_initializer: + expression + | array_initializer + | stackalloc_initializer; +stackalloc_initializer: + 'stackalloc' unmanaged_type '[' expression ']' ; +local_constant_declaration: + 'const' type constant_declarators ; +expression_statement: + expression ';' ; + +// TODO: should be assignment, call, increment, decrement, and new object expressions +statement_expression: + expression + ; +selection_statement: + if_statement + | switch_statement ; +if_statement: + // else goes with closest if + 'if' '(' boolean_expression ')' embedded_statement (('else') => else_statement)? + ; +else_statement: + 'else' embedded_statement ; +switch_statement: + 'switch' '(' expression ')' switch_block ; +switch_block: + '{' switch_sections? '}' ; +switch_sections: + switch_section+ ; +switch_section: + switch_labels statement_list ; +switch_labels: + switch_label+ ; +switch_label: + ('case' constant_expression ':') + | ('default' ':') ; +iteration_statement: + while_statement + | do_statement + | for_statement + | foreach_statement ; +while_statement: + 'while' '(' boolean_expression ')' embedded_statement ; +do_statement: + 'do' embedded_statement 'while' '(' boolean_expression ')' ';' ; +for_statement: + 'for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement ; +for_initializer: + (local_variable_declaration) => local_variable_declaration + | statement_expression_list + ; +for_condition: + boolean_expression ; +for_iterator: + statement_expression_list ; +statement_expression_list: + statement_expression (',' statement_expression)* ; +foreach_statement: + 'foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement ; +jump_statement: + break_statement + | continue_statement + | goto_statement + | return_statement + | throw_statement ; +break_statement: + 'break' ';' ; +continue_statement: + 'continue' ';' ; +goto_statement: + 'goto' ( identifier + | 'case' constant_expression + | 'default') ';' ; +return_statement: + 'return' expression? ';' ; +throw_statement: + 'throw' expression? ';' ; +try_statement: + 'try' block ( catch_clauses finally_clause? + | finally_clause); +//TODO one or both +catch_clauses: + 'catch' (specific_catch_clauses | general_catch_clause) ; +specific_catch_clauses: + specific_catch_clause ('catch' (specific_catch_clause | general_catch_clause))*; +specific_catch_clause: + '(' class_type identifier? ')' block ; +general_catch_clause: + block ; +finally_clause: + 'finally' block ; +checked_statement: + 'checked' block ; +unchecked_statement: + 'unchecked' block ; +lock_statement: + 'lock' '(' expression ')' embedded_statement ; +using_statement: + 'using' '(' resource_acquisition ')' embedded_statement ; +resource_acquisition: + (local_variable_declaration) => local_variable_declaration + | expression ; +yield_statement: + 'yield' ('return' expression ';' + | 'break' ';') ; + +/////////////////////////////////////////////////////// +// Lexar Section +/////////////////////////////////////////////////////// + +predefined_type: + 'bool' | 'byte' | 'char' | 'decimal' | 'double' | 'float' | 'int' | 'long' | 'object' | 'sbyte' + | 'short' | 'string' | 'uint' | 'ulong' | 'ushort' ; + +identifier: + IDENTIFIER | also_keyword; + +keyword: + 'abstract' | 'as' | 'base' | 'bool' | 'break' | 'byte' | 'case' | 'catch' | 'char' | 'checked' | 'class' | 'const' | 'continue' | 'decimal' | 'default' | 'delegate' | 'do' | 'double' | 'else' | 'enum' | 'event' | 'explicit' | 'extern' | 'false' | 'finally' | 'fixed' | 'float' | 'for' | 'foreach' | 'goto' | 'if' | 'implicit' | 'in' | 'int' | 'interface' | 'internal' | 'is' | 'lock' | 'long' | 'namespace' | 'new' | 'null' | 'object' | 'operator' | 'out' | 'override' | 'params' | 'private' | 'protected' | 'public' | 'readonly' | 'ref' | 'return' | 'sbyte' | 'sealed' | 'short' | 'sizeof' | 'stackalloc' | 'static' | 'string' | 'struct' | 'switch' | 'this' | 'throw' | 'true' | 'try' | 'typeof' | 'uint' | 'ulong' | 'unchecked' | 'unsafe' | 'ushort' | 'using' | 'virtual' | 'void' | 'volatile' ; + +also_keyword: + 'add' | 'alias' | 'assembly' | 'module' | 'field' | 'method' | 'param' | 'property' | 'type' | 'yield' + | 'from' | 'into' | 'join' | 'on' | 'where' | 'orderby' | 'group' | 'by' | 'ascending' | 'descending' + | 'equals' | 'select' | 'pragma' | 'let' | 'remove' | 'get' | 'set' | 'var' | '__arglist' | 'dynamic' | 'elif' + | 'endif' | 'define' | 'undef'; + +literal: + Real_literal + | NUMBER + | Hex_number + | Character_literal + | STRINGLITERAL + | Verbatim_string_literal + | TRUE + | FALSE + | NULL + ; + diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g index 1a075a3..132013f 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g @@ -69,20 +69,6 @@ scope NSContext { } } - protected string formatTyargs(List tyargs) { - - if (tyargs.Count == 0) { - return ""; - } - StringBuilder buf = new StringBuilder(); - buf.Append("<"); - foreach (string t in tyargs) { - buf.Append(t + ","); - } - buf.Remove(buf.Length-1,1); - buf.Append(">"); - return buf.ToString(); - } } /******************************************************************************************** @@ -672,10 +658,8 @@ scope NSContext; { Debug("Processing class: " + $type_or_generic.type); klass.Uses = this.NameSpaceContext; - klass.TypeName = this.ParentNameSpace + "." + $type_or_generic.type; + klass.TypeName = this.ParentNameSpace + "." + mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); if ($type_or_generic.generic_arguments.Count > 0) { - // distinguish classes with same name, but differing numbers of type arguments - klass.TypeName+= "'" + $type_or_generic.generic_arguments.Count.ToString(); klass.TypeParams = $type_or_generic.generic_arguments.ToArray(); } // Nested types can see things in this space @@ -848,10 +832,8 @@ scope NSContext; { Debug("Processing delegate: " + $identifier.text); dlegate.Uses = this.NameSpaceContext; - dlegate.TypeName = this.ParentNameSpace + "." + $identifier.text; + dlegate.TypeName = this.ParentNameSpace + "." + mkTypeName($identifier.text, $variant_generic_parameter_list.tyargs); if ($variant_generic_parameter_list.tyargs != null && $variant_generic_parameter_list.tyargs.Count > 0) { - // distinguish classes with same name, but differing numbers of type arguments - dlegate.TypeName+= "'" + $variant_generic_parameter_list.tyargs.Count.ToString(); dlegate.TypeParams = $variant_generic_parameter_list.tyargs.ToArray(); } dlegate.Return=$return_type.thetext; @@ -938,10 +920,8 @@ scope NSContext; { Debug("Processing interface: " + $identifier.text); iface.Uses = this.NameSpaceContext; - iface.TypeName = this.ParentNameSpace + "." + $identifier.text; + iface.TypeName = this.ParentNameSpace + "." + mkTypeName($identifier.text, $variant_generic_parameter_list.tyargs); if ($variant_generic_parameter_list.tyargs != null && $variant_generic_parameter_list.tyargs.Count > 0) { - // distinguish classes with same name, but differing numbers of type arguments - iface.TypeName+= "'" + $variant_generic_parameter_list.tyargs.Count.ToString(); iface.TypeParams = $variant_generic_parameter_list.tyargs.ToArray(); } // Nested types can see things in this space @@ -1024,10 +1004,8 @@ scope NSContext; { Debug("Processing struct: " + $type_or_generic.type); strukt.Uses = this.NameSpaceContext; - strukt.TypeName = this.ParentNameSpace + "." + $type_or_generic.type; + strukt.TypeName = this.ParentNameSpace + "." + mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); if ($type_or_generic.generic_arguments.Count > 0) { - // distinguish structs with same name, but differing numbers of type arguments - strukt.TypeName+= "'" + $type_or_generic.generic_arguments.Count.ToString(); strukt.TypeParams = $type_or_generic.generic_arguments.ToArray(); } // Nested types can see things in this space diff --git a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj index 43ce5d8..3a453b7 100644 --- a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj +++ b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj @@ -1,4 +1,4 @@ - + Debug @@ -66,11 +66,17 @@ + + + + + +