diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/CS2JSettings.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/CS2JSettings.cs new file mode 100644 index 0000000..f3afb7f --- /dev/null +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/CS2JSettings.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using RusticiSoftware.Translator.Utils; +using System.IO; +namespace RusticiSoftware.Translator.CSharp +{ + public class CS2JSettings + { + + public bool DisplayTokens { get; set; } + + // dump parse trees to stdout + public bool DumpCSharp { get; set; } + public bool DumpJavaSyntax { get; set; } + public bool DumpJava { get; set; } + + public bool DumpXmls { get; set; } + public bool DumpEnums { get; set; } + public string OutDir { get; set; } + public string CheatDir { get; set; } + public IList NetRoot { get; set; } + public IList ExNetRoot { get; set; } + public IList AppRoot { get; set; } + public IList ExAppRoot { get; set; } + public IList Exclude { get; set; } + public IList MacroDefines { get; set; } + public string XmlDir { get; set; } + public string EnumDir { get; set; } + public int Verbosity { get; set; } + + public int DebugLevel { get; set; } + + public CS2JSettings () + { + + DisplayTokens = false; + + // dump parse trees to stdout + DumpCSharp = false; + DumpJavaSyntax = false; + DumpJava = false; + + DumpXmls = false; + DumpEnums = false; + OutDir = Directory.GetCurrentDirectory(); + CheatDir = ""; + NetRoot = new List(); + ExNetRoot = new List(); + AppRoot = new List(); + ExAppRoot = new List(); + Exclude = new List(); + MacroDefines = new List(); + XmlDir = Path.Combine(Directory.GetCurrentDirectory(), "tmpXMLs"); + EnumDir = Path.Combine(Directory.GetCurrentDirectory(), "enums"); + Verbosity = 0; + DebugLevel = 0; + } + } +} + diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs index c34ae9a..eceb6fb 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs @@ -19,36 +19,11 @@ 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(); + public delegate void FileProcessor(string fName); - // show gui explorer of parse tree - internal static bool showTree = false; - internal static bool showCSharp = false; - internal static bool showJavaSyntax = false; - internal static bool showJava = false; - internal static bool displayTokens = false; - - // dump parse tree to stdout - internal static bool dumpCSharp = false; - internal static bool dumpJavaSyntax = false; - internal static bool dumpJava = false; - - internal static bool dumpXmls = false; - internal static bool dumpEnums = false; - internal static string outDir = Directory.GetCurrentDirectory(); - internal static string cheatDir = ""; - internal static IList netRoot = new List(); - internal static IList exNetRoot = new List(); - internal static IList appRoot = new List(); - internal static IList exAppRoot = new List(); - internal static IList exclude = new List(); - internal static DirectoryHT appEnv = new DirectoryHT(null); - internal static IList macroDefines = new List(); - internal static string xmlDir = Path.Combine(Directory.GetCurrentDirectory(), "tmpXMLs"); - internal static string enumDir = Path.Combine(Directory.GetCurrentDirectory(), "enums"); - internal static int verbosity = 0; - private static void showVersion() { Console.Out.WriteLine(Path.GetFileNameWithoutExtension(System.Environment.GetCommandLineArgs()[0]) + ": " + VERSION); @@ -61,7 +36,6 @@ namespace RusticiSoftware.Translator.CSharp Console.Out.WriteLine(" [-v] (be [somewhat more] verbose, repeat for more verbosity)"); Console.Out.WriteLine(" [-D ] (define , option can be repeated)"); Console.Out.WriteLine(" [-showtokens] (the lexer prints the tokenized input to the console)"); - Console.Out.WriteLine(" [-showtree] [-showcsharp] [-showjavasyntax] [-showjava] (show parse tree at various stages of the translation)"); Console.Out.WriteLine(" [-dumpcsharp] [-dumpjavasyntax] [-dumpjava] (show parse tree at various stages of the translation)"); Console.Out.WriteLine(" [-dumpxml] [-xmldir ] (dump the translation repository as xml files)"); Console.Out.WriteLine(" [-dumpenums ] (create an xml file documenting enums)"); @@ -86,7 +60,9 @@ namespace RusticiSoftware.Translator.CSharp { long startTime = DateTime.Now.Ticks; IList remArgs = new List(); - XmlTextWriter enumXmlWriter = null; + XmlTextWriter enumXmlWriter = null; + AppEnv = new DirectoryHT(null); + // Use a try/catch block for parser exceptions try { @@ -94,32 +70,29 @@ namespace RusticiSoftware.Translator.CSharp if (args.Length > 0) { - if (verbosity >= 2) Console.Error.WriteLine("Parsing Command Line Arguments..."); + if (cfg.Verbosity >= 2) Console.Error.WriteLine("Parsing Command Line Arguments..."); OptionSet p = new OptionSet () - .Add ("v", v => verbosity++) + .Add ("v", v => cfg.Verbosity++) + .Add ("debug=", v => cfg.DebugLevel = Int32.Parse(v)) .Add ("version", v => showVersion()) .Add ("help|h|?", v => showUsage()) - .Add ("showtree", v => showTree = true) - .Add ("showcsharp", v => showCSharp = true) - .Add ("showjava", v => showJava = true) - .Add ("showjavasyntax", v => showJavaSyntax = true) - .Add ("dumpcsharp", v => dumpCSharp = true) - .Add ("dumpjava", v => dumpJava = true) - .Add ("dumpjavasyntax", v => dumpJavaSyntax = true) - .Add ("tokens", v => displayTokens = true) - .Add ("D=", def => macroDefines.Add(def)) - .Add ("dumpenums", v => dumpEnums = true) - .Add ("enumdir=", dir => enumDir = Path.Combine(Directory.GetCurrentDirectory(), dir)) - .Add ("dumpxmls", v => dumpXmls = true) - .Add ("xmldir=", dir => xmlDir = Path.Combine(Directory.GetCurrentDirectory(), dir)) - .Add ("odir=", dir => outDir = dir) - .Add ("cheatdir=", dir => cheatDir = dir) - .Add ("netdir=", dirs => addDirectories(netRoot, dirs)) - .Add ("exnetdir=", dirs => addDirectories(exNetRoot, dirs)) - .Add ("appdir=", dirs => addDirectories(appRoot, dirs)) - .Add ("exappdir=", dirs => addDirectories(exAppRoot, dirs)) - .Add ("exclude=", dirs => addDirectories(exclude, dirs)) + .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 @@ -128,24 +101,24 @@ namespace RusticiSoftware.Translator.CSharp // Load .Net templates - foreach (string r in netRoot) - doFile(r, ".xml", addNetTranslation, exNetRoot); + foreach (string r in cfg.NetRoot) + doFile(r, ".xml", addNetTranslation, cfg.ExNetRoot); // Load Application Class Signatures (i.e. generate templates) - if (appRoot.Count == 0) + if (cfg.AppRoot.Count == 0) // By default translation target is application root - appRoot.Add(remArgs[0]); - foreach (string r in appRoot) - doFile(r, ".cs", addAppSigTranslation, exAppRoot); // parse it - if (dumpEnums) { - enumXmlWriter = new XmlTextWriter(enumDir, System.Text.Encoding.UTF8); + 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 (dumpXmls) + if (cfg.DumpXmls) { // Get package name and convert to directory name - foreach (DictionaryEntry de in appEnv) + foreach (DictionaryEntry de in AppEnv) { - String xmlFName = Path.Combine(xmlDir, + String xmlFName = Path.Combine(cfg.XmlDir, ((string)de.Key).Replace('.', Path.DirectorySeparatorChar) + ".xml"); String xmlFDir = Path.GetDirectoryName(xmlFName); if (!Directory.Exists(xmlFDir)) @@ -158,8 +131,8 @@ namespace RusticiSoftware.Translator.CSharp w.Close(); } } - doFile(remArgs[0], ".cs", translateFile, exclude); // parse it - if (dumpEnums) + doFile(remArgs[0], ".cs", translateFile, cfg.Exclude); // parse it + if (cfg.DumpEnums) { enumXmlWriter.WriteEndElement(); enumXmlWriter.Close(); @@ -176,7 +149,7 @@ namespace RusticiSoftware.Translator.CSharp Console.Error.WriteLine(e.StackTrace); // so we can get stack trace } double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0; - if (verbosity >= 1) + if (cfg.Verbosity >= 1) { System.Console.Out.WriteLine(""); System.Console.Out.WriteLine(""); @@ -200,7 +173,7 @@ namespace RusticiSoftware.Translator.CSharp } else if ((Path.GetFileName(canonicalPath).Length > ext.Length) && canonicalPath.Substring(canonicalPath.Length - ext.Length).Equals(ext)) { - if (verbosity >= 2) Console.WriteLine(" " + canonicalPath); + if (cfg.Verbosity >= 2) Console.WriteLine(" " + canonicalPath); try { @@ -215,15 +188,16 @@ namespace RusticiSoftware.Translator.CSharp } } - public static BufferedTreeNodeStream parseFile(string fullName) + public static CommonTreeNodeStream parseFile(string fullName) { CommonTokenStream tokens = null; - if (verbosity > 2) Console.WriteLine("Parsing " + Path.GetFileName(fullName)); + if (cfg.Verbosity > 2) Console.WriteLine("Parsing " + Path.GetFileName(fullName)); PreProcessor lex = new PreProcessor();; - - ICharStream input = new ANTLRFileStream(fullName); + lex.AddDefine(cfg.MacroDefines); + + ICharStream input = new ANTLRFileStream(fullName); lex.CharStream = input; tokens = new CommonTokenStream(lex); @@ -231,12 +205,9 @@ namespace RusticiSoftware.Translator.CSharp csParser.compilation_unit_return parser_rt; parser_rt = p.compilation_unit(); - ITree parse_tree = (ITree)parser_rt.Tree; - if (verbosity > 2) Console.Out.WriteLine(parse_tree.ToStringTree()); + - BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(parse_tree); - - if (nodes == null) + if (parser_rt == null || parser_rt.Tree == null) { if (tokens.Count > 0) { @@ -248,7 +219,9 @@ namespace RusticiSoftware.Translator.CSharp } } - return nodes; + CommonTreeNodeStream nodes = new CommonTreeNodeStream(parser_rt.Tree); + + return nodes; } @@ -257,27 +230,26 @@ namespace RusticiSoftware.Translator.CSharp { Stream s = new FileStream(fullName, FileMode.Open, FileAccess.Read); TypeRepTemplate t = TypeRepTemplate.newInstance(s); - appEnv[t.TypeName] = t; + AppEnv[t.TypeName] = t; } // Here's where we do the real work... public static void addAppSigTranslation(string fullName) { - BufferedTreeNodeStream nodes = parseFile(fullName); - if (nodes != null) + ITreeNodeStream csTree = parseFile(fullName); + if (csTree != null) { - TemplateExtracter templateWalker = new TemplateExtracter(nodes); - templateWalker.DebugLevel = 10; - templateWalker.compilation_unit(); + TemplateExtracter templateWalker = new TemplateExtracter(csTree); + templateWalker.compilation_unit(cfg); } } // Here's where we do the real work... public static void translateFile(string fullName) { - BufferedTreeNodeStream nodes = parseFile(fullName); - if (dumpCSharp) AntlrUtils.AntlrUtils.DumpNodes(new CommonTreeNodeStream(nodes.TreeSource)); + CommonTreeNodeStream csTree = parseFile(fullName); + if (cfg.DumpCSharp) AntlrUtils.AntlrUtils.DumpNodes(csTree); // ASTNode t = parseFile(f, s); // if (t != null) diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs b/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs index 2fe4122..7ee574a 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs @@ -9,22 +9,12 @@ namespace RusticiSoftware.Translator.CSharp { public class CommonWalker : TreeParser { + protected CS2JSettings Cfg { get; set; } + protected CommonWalker(ITreeNodeStream input, RecognizerSharedState state) : base(input, state) { } - - /// - /// Debug Routines - /// - private int debugLevel = 0; - - public int DebugLevel - { - get { return debugLevel; } - set { debugLevel = value; } - } - protected void Debug(String s) { Debug(1, s); @@ -37,7 +27,7 @@ namespace RusticiSoftware.Translator.CSharp protected void Debug(int level, String s) { - if (level <= DebugLevel) + if (level <= Cfg.DebugLevel) { Console.Out.WriteLine(s); } diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g index 31a97ca..f469a24 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g @@ -11,9 +11,13 @@ tree grammar TemplateExtracter; options { tokenVocab=cs; ASTLabelType=CommonTree; - language=CSharp2; - superClass='RusticiSoftware.Translator.CSharp.CommonWalker'; - //backtrack=true; + language=CSharp2; + superClass='RusticiSoftware.Translator.CSharp.CommonWalker'; + //backtrack=true; +} + +scope UseScope { + IList usePath; } @namespace { RusticiSoftware.Translator.CSharp } @@ -21,20 +25,17 @@ options { @header { using System.Text; - using System.Collections.Generic; using RusticiSoftware.Translator.CLR; } @members { - // As we scan the AST we collect these features until - // we reach the end, then calculate the TypeRep and insert it into - // the TypeEnv - private IList Properties = new List(); - private IList Methods = new List(); - private IList Constructors = new List(); - private IList Fields = new List(); - private IList Casts = new List(); + + // This is a global 'magic' string used to return strings from within complex productions. + // For example a namespace rule will set the string that represents the namespace, saves + // passing a whole load of returns through intermediate rules. + protected string Capture {get; set;} + } /******************************************************************************************** @@ -42,24 +43,125 @@ options { *********************************************************************************************/ /////////////////////////////////////////////////////// -compilation_unit: - { Debug("start"); } using_directives + +compilation_unit[CS2JSettings inCfg] +scope UseScope; +@init { + Cfg = inCfg; + $UseScope::usePath = new List(); +} +: +{ Debug("start template extraction"); } + namespace_body +{ Debug("end template extraction"); } ; +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' namespace_name ';' { Debug("using " + $namespace_name.namespaceText); }) - ; - -namespace_name returns [string namespaceText]: - ^(NAMESPACE_OR_TYPE_NAME nsc=namespace_component { namespaceText = $nsc.idText; } - (nscp=namespace_component { namespaceText = namespaceText + "." + $nscp.idText; } )* ) - ; + 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 ; -namespace_component returns [string idText]: - ^(NSTN identifier { idText=$identifier.idText; } ) + +// ad-hoc + +/////////////////////////////////////////////////////// +// Type Section +/////////////////////////////////////////////////////// + +class_declaration + : 'class' identifier ';' ; +struct_declaration + : 'struct' identifier ';' ; +interface_declaration + : 'interface' identifier ';' ; +enum_declaration + : 'enum' identifier ';' ; +delegate_declaration + : 'delegate' identifier ';' ; + +type_name: + namespace_or_type_name ; +namespace_or_type_name: + type_or_generic ('::' type_or_generic)? ('.' type_or_generic)* ; +type_or_generic: + (identifier '<') => identifier generic_argument_list + | 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' '*'+ ; -identifier returns [string idText]: - ^(ID IDENTIFIER { idText = $IDENTIFIER.Text; Debug("Identifier: " + $IDENTIFIER.Text); } ) - ; +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'; + +rank_specifiers: + rank_specifier+ ; +rank_specifier: + '[' dim_separators? ']' ; +dim_separators: + ','+ ; + +global_attributes: + global_attribute+ ; +global_attribute: + '[' 'fred' ','? ']' ; +attributes: + attribute_sections ; +attribute_sections: + attribute_section+ ; +attribute_section: + '[' 'jim' ','? ']' ; + +predefined_type: + 'bool' | 'byte' | 'char' | 'decimal' | 'double' | 'float' | 'int' | 'long' | 'object' | 'sbyte' + | 'short' | 'string' | 'uint' | 'ulong' | 'ushort' ; + +identifier: + IDENTIFIER { Debug($IDENTIFIER.Text); } ; diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g index 94c46d2..b867b64 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g @@ -1,1401 +1,1355 @@ -// cs.g -// -// CSharp 4.0 Parser -// -// Andrew Bradnan -// andrew@whirlygigventures.com -// 2009 - initial version - -grammar cs; - -options { - backtrack=true; - memoize=true; - output=AST; - language=CSharp2; -} - -tokens -{ - ARGUMENTS; - ASSIGNMENT; - BLOCK; - CAST_EXPRESSION; - CLASS_DECL; - CLASS_INHERITANCE; - DELEGATE_DECL; - ELSE; - EMPTY_BODY; - ENUM_DECL; - EXPRESSION; - FIELD_DECL; - FIXED_PARAMETER; - ID; - IF; - INTERFACE_DECL; - INVOCATION_EXPRESSION; - LOCAL_VAR; - LOCAL_VARIABLE_DECLARATOR; - MEMBER_ACCESS; - METHOD_DECL; - NAMESPACE_DECL; - NAMESPACE_OR_TYPE_NAME; - NON_ASSIGNMENT_EXPRESSION; - NSTN; - PREDEFINED_TYPE; - PRIMARY; - PROPERTY_DECLARATION; - QID_PART; - TYPE; - TYPE_NAME; - UNARY_EXPRESSION; - USING_DIRECTIVE; - VARIABLE_DECLARATOR; - - TELEMENT; - TMEMBER; - TINVOCATION; -} - -@lexer::header { - using System.Diagnostics; - using Debug = System.Diagnostics.Debug; -} - -@lexer::members { - // Preprocessor Data Structures - see lexer section below and PreProcessor.cs - protected Dictionary MacroDefines = new Dictionary(); - protected Stack Processing = new Stack(); - - // Uggh, lexer rules don't return values, so use a stack to return values. - protected Stack Returns = new Stack(); -} - -@namespace { RusticiSoftware.Translator.CSharp } - -@header -{ - using System.Text; -} - -/******************************************************************************************** - Parser section -*********************************************************************************************/ - -/////////////////////////////////////////////////////// -compilation_unit: -// extern_alias_directives? - using_directives? - global_attributes? - namespace_body? // specific namespace or in the global namespace - ; -namespace_declaration: - 'namespace' qualified_identifier namespace_block ';'? - -> ^(NAMESPACE_DECL 'namespace' qualified_identifier namespace_block ';'?) ; -namespace_block: - '{' namespace_body '}' ; -namespace_body: - extern_alias_directives? using_directives? 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_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 - | type_declaration ; -type_declaration: - class_declaration - | struct_declaration - | interface_declaration - | enum_declaration - | delegate_declaration ; -qualified_alias_member: - identifier '::' identifier generic_argument_list? ; - -// Identifiers -qualified_identifier: - identifier ('.' identifier)* ; - -qid: // qualified_identifier v2 - qid_start qid_part* ; -qid_start: - identifier ('::' identifier)? generic_argument_list? - | 'this' - | 'base' - | predefined_type - | literal ; // 0.ToString() is legal -qid_part: - access_operator identifier generic_argument_list? - -> ^(QID_PART access_operator identifier generic_argument_list?) ; - - -// B.2.1 Basic Concepts -namespace_name - : namespace_or_type_name ; -type_name: - namespace_or_type_name -> ^(TYPE_NAME namespace_or_type_name) ; -namespace_or_type_name: - id1 = identifier ga1 = generic_argument_list? ('::' id2 = identifier ga2 = generic_argument_list?)? ('.' id3 += identifier ga3 += generic_argument_list?)* - -> ^(NAMESPACE_OR_TYPE_NAME ^(NSTN $id1 $ga1?) ^(NSTN '::' $id2 $ga2?)? ^(NSTN $id3 $ga3?)*) -// | qualified_alias_member (the :: part) - ; - -// 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: - a += attribute (',' a += attribute)* - -> $a+; -attribute: - type_name attribute_arguments? ; -attribute_arguments: - '(' positional_argument_list? ')' - | '(' positional_argument_list ',' named_argument_list ')' - | '(' named_argument_list ')' ; -positional_argument_list: - pa += positional_argument (',' pa += positional_argument)* - -> $pa+; -positional_argument: - attribute_argument_expression ; -named_argument_list: - na += named_argument (',' na += named_argument)* - -> $na+; -named_argument: - identifier '=' attribute_argument_expression ; -attribute_argument_expression: - expression ; - -// B.2.2 Types - -/* I'm going to ignore the mostly semantic bnf in the C Sharp spec and just do syntax */ -type: - (type_name | predefined_type) rank_specifiers '*'* -> - ^(TYPE type_name? predefined_type? rank_specifiers '*'*) - | type_name '*'+ -> ^(TYPE type_name '*'+) - | type_name '?' -> ^(TYPE type_name '?') - | type_name -> ^(TYPE type_name) - | predefined_type '*'+ -> ^(TYPE predefined_type '*'+) - | predefined_type '?' -> ^(TYPE predefined_type '?') - | predefined_type -> ^(TYPE predefined_type) - | 'void' '*'+ -> ^(TYPE 'void' '*'+) ; -non_nullable_type: - (type_name | predefined_type) rank_specifiers '*'* - | type_name '*'+ - | type_name - | predefined_type '*'+ - | predefined_type - | 'void' '*'+ ; -type_list: - type (',' type)* ; -class_type: - type; -non_array_type: - type; -array_type: - type; -integral_type: - 'sbyte' | 'byte' | 'short' | 'ushort' | 'int' | 'uint' | 'long' | 'ulong' | 'char' ; -unmanaged_type: - type; -pointer_type: - type; -rank_specifiers: - rank_specifier+ ; -rank_specifier: - '[' dim_separators? ']' ; -dim_separators: - ','+ ; -generic_argument_list: - '<' type_arguments '>' ; -type_arguments: - ta += type_argument (',' ta += type_argument)* - -> $ta+; -type_argument: - type ; -type_variable_name: - identifier ; - - -// B.2.3 Expressions -expression: - non_assignment_expression -> ^(EXPRESSION non_assignment_expression) - | assignment -> ^(EXPRESSION assignment); -non_assignment_expression: - conditional_expression - | lambda_expression - | query_expression ; -assignment: - unary_expression assignment_operator expression - -> ^(ASSIGNMENT unary_expression assignment_operator expression) ; -unary_expression: - cast_expression // primary_expression... has parenthesized_expression - | '+' unary_expression - | '-' unary_expression - | '!' unary_expression - | '~' unary_expression - | '*' unary_expression - | pre_increment_expression - | pre_decrement_expression - | primary_or_array_creation_expression '++'? '--'? -> ^(UNARY_EXPRESSION primary_or_array_creation_expression '++'? '--'?) - | pointer_indirection_expression - | addressof_expression ; -assignment_operator: - '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>' '>=' ; -pre_increment_expression: - '++' unary_expression ; -pre_decrement_expression: - '--' unary_expression ; -pointer_indirection_expression: - '*' unary_expression ; -addressof_expression: - '&' unary_expression ; -variable_reference: - expression ; - -argument_list: - a += argument (',' a += argument)* -> $a+; -// 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' '(' - (( namespace_or_type_name | predefined_type ) '*'* rank_specifiers? - | 'void' '*'+ rank_specifiers? - ) ')' ref_variable_reference - | 'ref' variable_reference; - -primary_or_array_creation_expression: - primary_expression - | array_creation_expression ; - -primary_expression: - primary_expression_start primary_expression_part* -> ^(PRIMARY primary_expression_start primary_expression_part*) - | delegate_creation_expression // new FooDelegate (int X, string Y) - | 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 | literal) generic_argument_list? - | 'this' bracket_expression? - | 'base' bracket_expression? - | identifier '::' identifier - | paren_expression brackets_or_arguments? - | object_creation_expression // new Foo().Member - | typeof_expression // typeof(Foo).Name - ; -primary_expression_part: - access_identifier brackets_or_arguments? - | brackets_or_arguments ; - -element_part: - access_identifier bracket_expression+ primary_expression_part? - | bracket_expression ; -invocation_part: - access_identifier arguments primary_expression_start? - | arguments; - -member_part: - access_identifier ; -access_identifier: - access_operator identifier generic_argument_list? ; -paren_expression: - '(' expression ')' ; -brackets_or_arguments: - bracket_expression+ arguments? - | arguments bracket_expression* ; -access_operator: - '.' | '->' ; -arguments: - '(' argument_list? ')' -> ^(ARGUMENTS argument_list*) ; -bracket_expression: - '[' expression_list? ']' ; - -member_access: - identifier ('.' primary_or_array_creation_expression)? // generic_argument_list? - -> ^(MEMBER_ACCESS identifier ('.' primary_or_array_creation_expression)? ) ; -predefined_type: - 'bool' -> ^(PREDEFINED_TYPE 'bool' ) - | 'byte' -> ^(PREDEFINED_TYPE 'byte' ) - | 'char' -> ^(PREDEFINED_TYPE 'char' ) - | 'decimal'-> ^(PREDEFINED_TYPE 'decimal') - | 'double' -> ^(PREDEFINED_TYPE 'double' ) - | 'float' -> ^(PREDEFINED_TYPE 'float' ) - | 'int' -> ^(PREDEFINED_TYPE 'int' ) - | 'long' -> ^(PREDEFINED_TYPE 'long' ) - | 'object' -> ^(PREDEFINED_TYPE 'object' ) - | 'sbyte' -> ^(PREDEFINED_TYPE 'sbyte' ) - | 'short' -> ^(PREDEFINED_TYPE 'short' ) - | 'string' -> ^(PREDEFINED_TYPE 'string' ) - | 'uint' -> ^(PREDEFINED_TYPE 'uint' ) - | 'ulong' -> ^(PREDEFINED_TYPE 'ulong' ) - | 'ushort' -> ^(PREDEFINED_TYPE 'ushort' ); - -invocation_expression: - access arguments -> ^(INVOCATION_EXPRESSION access arguments); -access: - '(' expression ')' ( access // cast expression - | access_part) // paren expression - | qid_start access_part? ; -// the recursive part -access_part: - qid_part access_part? // member access -- '.' identifier - | bracket_expression access_part? // element access; use rank_specifiers? - | arguments access_part ; // invocation followed by more invocation -expression_list: - e += expression (',' e += expression)* - -> $e+ ; -object_creation_expression: - 'new' type - ( '(' argument_list? ')' object_or_collection_initializer? - | object_or_collection_initializer ) - ; -object_or_collection_initializer: - object_initializer - | collection_initializer ; - -// object-initializer -// Rectangle r = new Rectangle { -// P1 = new Point { X = 0, Y = 1 }, -// P2 = new Point { X = 2, Y = 3 } -// }; -object_initializer: - '{' member_initializer_list? '}' - | '{' member_initializer_list ',' '}' ; -member_initializer_list: - mi += member_initializer (',' mi += member_initializer) - -> $mi+ ; -member_initializer: - identifier '=' initializer_value ; -initializer_value: - expression - | object_or_collection_initializer ; -collection_initializer: - '{' element_initializer_list ','? '}' ; -element_initializer_list: - ei += element_initializer (',' ei += element_initializer)* - -> $ei+ ; -element_initializer: - non_assignment_expression - | '{' expression_list '}' ; -array_creation_expression: - 'new' (non_array_type '[' expression_list ']' rank_specifiers? array_initializer? (access_operator primary_expression)* - | array_type array_initializer - | rank_specifier array_initializer) ; -delegate_creation_expression: - 'new' type_name '(' expression ')' ; -anonymous_object_creation_expression: - 'new' anonymous_object_initializer ; -anonymous_object_initializer: - '{' member_declarator_list? '}' - | '{' member_declarator_list ',' '}'; -member_declarator_list: - md += member_declarator (',' md += member_declarator) - -> $md+ ; -member_declarator: - identifier generic_argument_list? - | member_access - | identifier '=' expression ; -sizeof_expression: - 'sizeof' '(' unmanaged_type ')'; -typeof_expression: - 'typeof' '(' type ')' - | 'typeof' '(' unbound_type_name ')' - | 'typeof' '(' '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: - identifier ('::' identifier)? generic_dimension_specifier?; -unbound_type_name_part: - '.' identifier generic_dimension_specifier? ; -generic_dimension_specifier: - '<' commas? '>' ; -commas: - ','+ ; -checked_expression: - 'checked' '(' expression ')' ; -unchecked_expression: - 'unchecked' '(' expression ')' ; -default_value_expression: - 'default' '(' type ')' ; -constant_expression: - expression; -boolean_expression: - expression; -anonymous_method_expression: - 'delegate' explicit_anonymous_function_signature? block; -explicit_anonymous_function_signature: - '(' explicit_anonymous_function_parameter_list? ')' ; -explicit_anonymous_function_parameter_list: - e += explicit_anonymous_function_parameter (',' e += explicit_anonymous_function_parameter)* - -> $e+ ; -explicit_anonymous_function_parameter: - anonymous_function_parameter_modifier? type identifier; -anonymous_function_parameter_modifier: - 'ref' | 'out'; -// 4.0 -variant_generic_parameter_list: - '<' variant_type_parameters '>' ; -variant_type_parameters: - at += variant_type_variable_name (',' at += variant_type_variable_name)* - -> $at+ ; -variant_type_variable_name: - attributes? variance_annotation? type_variable_name ; -variance_annotation: - 'in' | 'out' ; - -generic_parameter_list: - '<' type_parameters '>' ; -type_parameters: - at += attributed_type_variable_name (',' at += attributed_type_variable_name)* - -> $at+ ; -attributed_type_variable_name: - attributes? type_variable_name ; -cast_expression: - '(' - ( - ( namespace_or_type_name | predefined_type ) '*'+ rank_specifiers? - | ( namespace_or_type_name | predefined_type ) '?' rank_specifiers? - | ( namespace_or_type_name | predefined_type ) rank_specifiers? - | 'void' '*'* rank_specifiers? // for some reason you can cast to (void) - ) - ')' bracket_expression* unary_expression - -> ^(CAST_EXPRESSION '(' namespace_or_type_name? predefined_type? 'void'? '?'? '*'* rank_specifiers? ')' bracket_expression* unary_expression) ; -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 ('??' null_coalescing_expression)*; -conditional_expression: - null_coalescing_expression ('?' expression ':' expression)? ; - - -array_initializer: - '{' variable_initializer_list? ','? '}' ; -variable_initializer_list: - variable_initializer (',' variable_initializer)* ; -// >>= check needed (no whitespace) -lambda_expression: - anonymous_function_signature '=>' anonymous_function_body; -anonymous_function_signature: - explicit_anonymous_function_signature - | implicit_anonymous_function_signature; -implicit_anonymous_function_signature: - '(' implicit_anonymous_function_parameter_list? ')' - | implicit_anonymous_function_parameter_list (',' implicit_anonymous_function_parameter)?; -implicit_anonymous_function_parameter_list: - implicit_anonymous_function_parameter+ ; -implicit_anonymous_function_parameter: - identifier; -anonymous_function_body: - expression - | block ; - -// B.2.12 Delegates -delegate_declaration: - attributes? delegate_modifiers? 'delegate' return_type identifier variant_generic_parameter_list? - '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ';' - -> ^(DELEGATE_DECL attributes? delegate_modifiers? return_type identifier variant_generic_parameter_list? - formal_parameter_list? type_parameter_constraints_clauses?); -delegate_modifiers: - delegate_modifier (delegate_modifier)* ; -delegate_modifier: - 'new' | 'public' | 'protected' | 'internal' | 'private' | 'unsafe' ; -query_expression: - from_clause query_body ; -from_clause: - 'from' type? identifier 'in' expression ; -query_body: - query_body_clauses? select_or_group_clause 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; -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: - 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 ; - - -// Classes B.2.7 -class_declaration -: - attributes? class_modifiers? 'partial'? 'class' identifier generic_parameter_list? - class_base? type_parameter_constraints_clauses? class_body ';'? -> - ^(CLASS_DECL attributes? class_modifiers? 'partial'? identifier generic_parameter_list? - class_base? type_parameter_constraints_clauses? class_body); -class_modifiers: - class_modifier+ ; -class_modifier: - 'new' | 'public' | 'protected' | 'internal' | 'private' | 'abstract' | 'sealed' | 'static' | 'unsafe'; -class_base: - ':' class_type (',' interface_type_list)? -> ^(CLASS_INHERITANCE class_type interface_type_list?) - | ':' interface_type_list -> ^(CLASS_INHERITANCE interface_type_list) ; -interface_type_list: - t += type_name (',' t += type_name)* - -> $t+; -type_parameter_constraints_clauses: - type_parameter_constraints_clause+ ; -type_parameter_constraints_clause: - 'where' type_variable_name ':' type_parameter_constraint_list ; -type_parameter_constraint_list: - (primary_constraint ',' secondary_constraint_list ',' constructor_constraint) - | (primary_constraint ',' secondary_constraint_list) - | (primary_constraint ',' constructor_constraint) - | (secondary_constraint_list ',' constructor_constraint) - | primary_constraint - | secondary_constraint_list - | constructor_constraint ; - -primary_constraint: - class_type - | 'class' - | 'struct' ; -secondary_constraint_list: - sc += secondary_constraint (',' sc += secondary_constraint)* - -> $sc+ ; -secondary_constraint: - (type_name | type_variable_name) ; -constructor_constraint: - 'new' '(' ')' ; -class_body: - '{' class_member_declarations? '}' ; -class_member_declarations: - class_member_declaration+ ; -class_member_declaration: - constant_declaration - | field_declaration - | method_declaration - | property_declaration - | event_declaration - | indexer_declaration - | operator_declaration - | constructor_declaration - | destructor_declaration - | static_constructor_declaration - | type_declaration - | class_declaration ; -constant_declaration: - attributes? constant_modifiers? 'const' type constant_declarators ';' ; -constant_modifiers: - constant_modifier+ ; -constant_modifier: - 'new' | 'public' | 'protected' | 'internal' | 'private' ; -constant_declarators: - constant_declarator (',' constant_declarator)* ; -constant_declarator: - identifier ('=' constant_expression)? ; - -field_declaration: - attributes? field_modifiers? type variable_declarators ';' - -> ^(FIELD_DECL attributes? field_modifiers? type variable_declarators) - ; -field_modifiers: - field_modifier+ ; -field_modifier: - 'new' | 'public' | 'protected' | 'internal' | 'private' | 'static' | 'readonly' | 'volatile' | 'unsafe' ; -variable_declarators: - vd += variable_declarator (',' vd += variable_declarator)* -> $vd+; -variable_declarator: -// identifier ('=' variable_initializer)? ; - type_name ('=' variable_initializer)? -> ^(VARIABLE_DECLARATOR type_name ('=' variable_initializer)?) ; // eg. event EventHandler IInterface.VariableName; -variable_initializer: - expression | array_initializer ; -// | literal ; -method_declarations: - method_declaration+ ; -method_declaration: - method_header method_body - -> ^(METHOD_DECL method_header method_body?); -method_header: - attributes? method_modifiers? 'partial'? return_type member_name generic_parameter_list? - '(' formal_parameter_list? ')' type_parameter_constraints_clauses? ; -method_modifiers: - method_modifier+ ; -method_modifier: - 'new' | 'public' | 'protected' | 'internal' | 'private' | 'static' | 'virtual' | 'sealed' | 'override' - | 'abstract' | 'extern' | 'unsafe' ; -return_type: - type - | 'void' '*'* -> ^(TYPE ^(PREDEFINED_TYPE 'void' '*'*)); -method_body: - block ; -formal_parameter_list: - fp += formal_parameter (',' fp += formal_parameter)* - -> $fp+ ; -formal_parameter: - fixed_parameter - | parameter_array - | '__arglist'; // __arglist is undocumented, see google -fixed_parameters: - fixed_parameter+ ; -// 4.0 -fixed_parameter: - attributes? parameter_modifier? type identifier default_argument? - -> ^(FIXED_PARAMETER attributes? parameter_modifier? type identifier default_argument?) ; -// 4.0 -default_argument: - '=' expression; -parameter_modifier: - 'ref' | 'out' | 'this' ; -parameter_array: - attributes? 'params' type identifier ; -property_declaration: - attributes? property_modifiers? type member_name '{' accessor_declarations '}' - -> ^(PROPERTY_DECLARATION attributes? property_modifiers? type member_name '{' accessor_declarations '}') ; -property_modifiers: - property_modifier+ ; -property_modifier - : - 'new' | 'public' | 'protected' | 'internal' | 'private' | 'static' | 'virtual' | 'sealed' | 'override' | 'abstract' | 'extern' | 'unsafe' ; -member_name: - qid (generic_parameter_list qid_part)? ; // IInterface.Method logic added. -accessor_declarations: - (get_accessor_declaration set_accessor_declaration?) - | (set_accessor_declaration get_accessor_declaration?) ; -get_accessor_declaration: - attributes? accessor_modifier? 'get' accessor_body ; -set_accessor_declaration: - attributes? accessor_modifier? 'set' accessor_body ; -accessor_modifier: - ('public' | 'protected' | 'internal' | 'private' | ('protected' 'internal') | ('internal' 'protected')) ; -accessor_body: - block - ; -event_declaration: - ( attributes? event_modifiers? 'event' type variable_declarators ';') - | ( attributes? event_modifiers? 'event' type member_name '{' event_accessor_declarations '}') ; -event_modifiers: - event_modifier+ ; -event_modifier: - 'new' | 'public' | 'protected' | 'internal' | 'private' | 'static' | 'virtual' | 'sealed' | 'override' - | 'abstract' | 'extern' | 'unsafe' ; -event_accessor_declarations: - (add_accessor_declaration remove_accessor_declaration) - | (remove_accessor_declaration add_accessor_declaration) ; -add_accessor_declaration: - attributes? 'add' block ; -remove_accessor_declaration: - attributes? 'remove' block ; -indexer_declaration: - attributes? indexer_modifiers? indexer_declarator '{' accessor_declarations '}' ; -indexer_modifiers: - indexer_modifier+ ; -indexer_modifier: - 'new' | 'public' | 'protected' | 'internal' | 'private' | 'virtual' | 'sealed' | 'override' | 'abstract' | 'extern' | 'unsafe' ; -indexer_declarator: - type (type_name '.')? 'this' '[' formal_parameter_list ']' ; -operator_declaration: - attributes? operator_modifiers operator_declarator operator_body ; -operator_modifiers: - operator_modifier+ ; -operator_modifier: - 'public' | 'static' | 'extern' | 'unsafe' ; -operator_declarator: - unary_operator_declarator - | binary_operator_declarator - | conversion_operator_declarator; -unary_operator_declarator: - type 'operator' overloadable_unary_operator '(' type identifier ')' ; -overloadable_unary_operator: - '+' | '-' | '!' | '~' | '++' | '--' | 'true' | 'false' ; -binary_operator_declarator: - type 'operator' overloadable_binary_operator '(' type identifier ',' type identifier ')' ; -// >> check needed -overloadable_binary_operator: - '+' | '-' | '*' | '/' | '%' | '&' | '|' | '^' | '<<' | '>' '>' | '==' | '!=' | '>' | '<' | '>=' | '<=' ; -conversion_operator_declarator: - ('implicit' | 'explicit') 'operator' type '(' type identifier ')' ; -operator_body: - block ; -constructor_declaration: - attributes? constructor_modifiers? constructor_declarator constructor_body ; -constructor_modifiers: - constructor_modifier+ ; -constructor_modifier: - 'public' | 'protected' | 'internal' | 'private' | 'extern' | 'unsafe' ; -constructor_declarator: - identifier '(' formal_parameter_list? ')' constructor_initializer? ; -constructor_initializer: - ':' ('base' | 'this') '(' argument_list? ')' ; -constructor_body: - block ; -static_constructor_declaration: - attributes? static_constructor_modifiers identifier '(' ')' static_constructor_body ; -static_constructor_modifiers: - 'extern' 'unsafe' 'static' - | 'extern' 'static' 'unsafe'? - | 'unsafe' 'extern' 'static' - | 'unsafe' 'static' 'extern'? - | 'static' 'extern' 'unsafe'? - | 'static' 'unsafe' 'extern'? - | 'extern' - | 'unsafe' - | 'static'; -static_constructor_body: - block ; -destructor_declaration: - attributes? destructor_modifiers? '~' identifier '(' ')' destructor_body ; -destructor_modifiers: - ('extern'? 'unsafe') - | ('extern' 'unsafe'?) ; -destructor_body: - block ; - -/////////////////////////////////////////////////////// -struct_declaration: - attributes? struct_modifiers? 'partial'? 'struct' identifier generic_parameter_list? - 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: - constant_declaration - | field_declaration - | method_declaration - | property_declaration - | event_declaration - | indexer_declaration - | operator_declaration - | constructor_declaration - | static_constructor_declaration - | type_declaration ; - -/////////////////////////////////////////////////////// -interface_declaration: - attributes? interface_modifiers? 'partial'? 'interface' identifier variant_generic_parameter_list? - interface_base? type_parameter_constraints_clauses? interface_body ';'? - -> ^(INTERFACE_DECL attributes? interface_modifiers? 'partial'? identifier variant_generic_parameter_list? - interface_base? type_parameter_constraints_clauses? interface_body) ; -interface_modifiers: - interface_modifier+ ; -interface_modifier: - 'new' | 'public' | 'protected' | 'internal' | 'private' | 'unsafe' ; -interface_base: - ':' interface_type_list ; -interface_body: - '{' interface_member_declarations? '}' ; -interface_member_declarations: - interface_member_declaration+ ; -interface_member_declaration: - interface_property_declaration - | interface_method_declaration - | interface_event_declaration - | interface_indexer_declaration ; -interface_method_declaration: - attributes? 'new'? method_modifiers? return_type identifier generic_parameter_list? - '('! formal_parameter_list? ')'! type_parameter_constraints_clauses? ';' ; -interface_property_declaration: - attributes? 'new'? type identifier '{' interface_accessor_declarations '}' ; -interface_accessor_declarations: - interface_get_accessor_declaration interface_set_accessor_declaration? - | interface_set_accessor_declaration interface_get_accessor_declaration? ; -interface_get_accessor_declaration: - attributes? 'get' ';' ; // no body / modifiers -interface_set_accessor_declaration: - attributes? 'set' ';' ; // no body / modifiers -interface_event_declaration: - attributes? 'new'? 'event' type identifier ';' ; -interface_indexer_declaration: - attributes? 'new'? type 'this' '[' formal_parameter_list ']' '{' interface_accessor_declarations '}' ; - -/////////////////////////////////////////////////////// - -enum_declaration: - attributes? enum_modifiers? 'enum' identifier enum_base? enum_body ';'? -> - ^(ENUM_DECL attributes? enum_modifiers? 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' ; -/////////////////////////////////////////////////////// - -statement: - labeled_statement - | declaration_statement - | embedded_statement ; -embedded_statement: - block - | empty_statement - | expression_statement - | selection_statement - | iteration_statement - | jump_statement - | try_statement - | checked_statement - | unchecked_statement - | lock_statement - | using_statement - | yield_statement - | unsafe_statement - | fixed_statement; -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 - | expression; -unsafe_statement: - 'unsafe' block; -block: - ';' -> - | '{' statement_list? '}' -> ^(BLOCK statement_list?); -statement_list: - statement+ ; -empty_statement: - ';' ; -labeled_statement: - identifier ':' statement ; -declaration_statement: - (local_variable_declaration - | local_constant_declaration) ';' ; -local_variable_declaration: - local_variable_type local_variable_declarators -> - ^(LOCAL_VAR local_variable_type local_variable_declarators); -local_variable_type: - type - | 'var' - | 'dynamic'; -local_variable_declarators: - local_variable_declarator (',' local_variable_declarator)* ; -local_variable_declarator: - identifier ('=' local_variable_initializer)? - -> ^(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 ';' ; -statement_expression: - invocation_expression - | object_creation_expression - | assignment - | unary_expression; -selection_statement: - if_statement - | switch_statement ; -if_statement: - 'if' '(' boolean_expression ')' embedded_statement else_statement? - -> ^(IF '(' boolean_expression ')' embedded_statement else_statement?) ; -else_statement: - 'else' embedded_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 - | 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 ';') - | ('goto' 'case' constant_expression ';') - | ('goto' 'default' ';') ; -return_statement: - 'return' expression? ';' ; -throw_statement: - 'throw' expression? ';' ; -try_statement: - ('try' block catch_clauses finally_clause?) - | ('try' block finally_clause); -catch_clauses: - (specific_catch_clauses general_catch_clause?) - | (specific_catch_clauses? general_catch_clause) ; -specific_catch_clauses: - specific_catch_clause+ ; -specific_catch_clause: - 'catch' '(' class_type identifier? ')' block ; -general_catch_clause: - 'catch' 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 - | expression ; -yield_statement: - ('yield' 'return' expression ';') - | ('yield' 'break' ';') ; - -identifier: - IDENTIFIER -> ^(ID IDENTIFIER) - | also_keyword -> ^(ID also_keyword); - -literal: - Real_literal - | NUMBER - | Hex_number - | Character_literal - | STRINGLITERAL - | Verbatim_string_literal - | TRUE - | FALSE - | NULL - ; - -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' | 'event' | 'method' | 'param' | 'property' | 'type' - | 'yield' | 'from' | 'into' | 'join' | 'on' | 'where' | 'orderby' | 'group' | 'by' | 'ascending' - | 'descending' | 'equals' | 'select' | 'pragma' | 'let' | 'remove' | 'set' | 'var' | '__arglist' | 'dynamic'; -/////////////////////////////////////////////////////// -// Lexar Section -/////////////////////////////////////////////////////// - -TRUE : 'true'; -FALSE: 'false' ; -NULL : 'null' ; -DOT : '.' ; -PTR : '->' ; -MINUS : '-' ; -GT : '>' ; -USING : 'using'; -ENUM : 'enum'; -GET : 'get'; -SET : 'set'; -IF: 'if'; -ELSE: 'else'; -ELIF: 'elif'; -ENDIF: 'endif'; -DEFINE: 'define'; -UNDEF: 'undef'; -SEMI: ';'; -RPAREN: ')'; - -WS: - (' ' | '\r' | '\t' | '\n' ) - { Skip(); } ; -fragment -TS: - (' ' | '\t' ) - { Skip(); } ; -DOC_LINE_COMMENT - : ('///' ~('\n'|'\r')* ('\r' | '\n')+) - { Skip(); } ; -LINE_COMMENT - : ('//' ~('\n'|'\r')* ('\r' | '\n')+) - { Skip(); } ; -COMMENT: - '/*' - (options {greedy=false;} : . )* - '*/' - { Skip(); } ; -STRINGLITERAL - : - '"' (EscapeSequence | ~('"' | '\\'))* '"' ; -Verbatim_string_literal: - '@' '"' Verbatim_string_literal_character* '"' ; -fragment -Verbatim_string_literal_character: - '"' '"' | ~('"') ; -NUMBER: - Decimal_digits INTEGER_TYPE_SUFFIX? ; -// For the rare case where 0.ToString() etc is used. -GooBall -@after -{ - CommonToken int_literal = new CommonToken(NUMBER, $dil.text); - CommonToken dot = new CommonToken(DOT, "."); - CommonToken iden = new CommonToken(IDENTIFIER, $s.text); - - Emit(int_literal); - Emit(dot); - Emit(iden); - Console.Error.WriteLine("\tFound GooBall {0}", $text); -} - : - dil = Decimal_integer_literal d = '.' s=GooBallIdentifier - ; - -fragment GooBallIdentifier - : IdentifierStart IdentifierPart* ; - -//--------------------------------------------------------- -Real_literal: - Decimal_digits '.' Decimal_digits Exponent_part? Real_type_suffix? - | '.' Decimal_digits Exponent_part? Real_type_suffix? - | Decimal_digits Exponent_part Real_type_suffix? - | Decimal_digits Real_type_suffix ; -Character_literal: - '\'' - ( EscapeSequence - // upto 3 multi byte unicode chars - | ~( '\\' | '\'' | '\r' | '\n' ) - | ~( '\\' | '\'' | '\r' | '\n' ) ~( '\\' | '\'' | '\r' | '\n' ) - | ~( '\\' | '\'' | '\r' | '\n' ) ~( '\\' | '\'' | '\r' | '\n' ) ~( '\\' | '\'' | '\r' | '\n' ) - ) - '\'' ; -IDENTIFIER: - IdentifierStart IdentifierPart* ; -Pragma: - // ignore everything after the pragma since the escape's in strings etc. are different - '#' TS* ('pragma' | 'region' | 'endregion' | 'line' | 'warning' | 'error') ~('\n'|'\r')* ('\r' | '\n')+ - { Skip(); } ; -PREPROCESSOR_DIRECTIVE: - | PP_CONDITIONAL; -fragment -PP_CONDITIONAL: - (IF_TOKEN - | DEFINE_TOKEN - | ELSE_TOKEN - | ENDIF_TOKEN - | UNDEF_TOKEN) TS* (LINE_COMMENT? | ('\r' | '\n')+) ; -fragment -IF_TOKEN - @init { bool process = true; }: - ('#' TS* 'if' TS+ ppe = PP_EXPRESSION) -{ - // if our parent is processing check this if - Debug.Assert(Processing.Count > 0, "Stack underflow preprocessing. IF_TOKEN"); - if (Processing.Count > 0 && Processing.Peek()) - Processing.Push(Returns.Pop()); - else - Processing.Push(false); -} ; -fragment -DEFINE_TOKEN: - '#' TS* 'define' TS+ define = IDENTIFIER - { - MacroDefines.Add($define.Text, ""); - } ; -fragment -UNDEF_TOKEN: - '#' TS* 'undef' TS+ define = IDENTIFIER - { - if (MacroDefines.ContainsKey($define.Text)) - MacroDefines.Remove($define.Text); - } ; -fragment -ELSE_TOKEN: - ( '#' TS* e = 'else' - | '#' TS* 'elif' TS+ PP_EXPRESSION) - { - // We are in an elif - if ($e == null) - { - Debug.Assert(Processing.Count > 0, "Stack underflow preprocessing. ELIF_TOKEN"); - if (Processing.Count > 0 && Processing.Peek() == false) - { - Processing.Pop(); - // if our parent was processing, do else logic - Debug.Assert(Processing.Count > 0, "Stack underflow preprocessing. ELIF_TOKEN2"); - if (Processing.Count > 0 && Processing.Peek()) - Processing.Push(Returns.Pop()); - else - Processing.Push(false); - } - else - { - Processing.Pop(); - Processing.Push(false); - } - } - else - { - // we are in a else - if (Processing.Count > 0) - { - bool bDoElse = !Processing.Pop(); - - // if our parent was processing - Debug.Assert(Processing.Count > 0, "Stack underflow preprocessing, ELSE_TOKEN"); - if (Processing.Count > 0 && Processing.Peek()) - Processing.Push(bDoElse); - else - Processing.Push(false); - } - } - Skip(); - } ; -fragment -ENDIF_TOKEN: - '#' TS* 'endif' - { - if (Processing.Count > 0) - Processing.Pop(); - Skip(); - } ; - - - - -fragment -PP_EXPRESSION: - PP_OR_EXPRESSION; -fragment -PP_OR_EXPRESSION: - PP_AND_EXPRESSION TS* ('||' TS* PP_AND_EXPRESSION TS* )* ; -fragment -PP_AND_EXPRESSION: - PP_EQUALITY_EXPRESSION TS* ('&&' TS* PP_EQUALITY_EXPRESSION TS* )* ; -fragment -PP_EQUALITY_EXPRESSION: - PP_UNARY_EXPRESSION TS* (('=='| ne = '!=') TS* PP_UNARY_EXPRESSION - { - bool rt1 = Returns.Pop(), rt2 = Returns.Pop(); - Returns.Push(rt1 == rt2 == ($ne == null)); - } - TS* )* - ; -fragment -PP_UNARY_EXPRESSION: - pe = PP_PRIMARY_EXPRESSION - | '!' TS* ue = PP_UNARY_EXPRESSION { Returns.Push(!Returns.Pop()); } - ; -fragment -PP_PRIMARY_EXPRESSION: - IDENTIFIER - { - Returns.Push(MacroDefines.ContainsKey($IDENTIFIER.Text)); - } - | '(' PP_EXPRESSION ')' - ; - - - -fragment -IdentifierStart - : '@' | '_' | 'A'..'Z' | 'a'..'z' ; -fragment -IdentifierPart -: 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' ; -fragment -EscapeSequence - : '\\' ( - 'b' - | 't' - | 'n' - | 'f' - | 'r' - | 'v' - | 'a' - | '\"' - | '\'' - | '\\' - | ('0'..'3') ('0'..'7') ('0'..'7') - | ('0'..'7') ('0'..'7') - | ('0'..'7') - | 'x' HEX_DIGIT - | 'x' HEX_DIGIT HEX_DIGIT - | 'x' HEX_DIGIT HEX_DIGIT HEX_DIGIT - | 'x' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT - | 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT - | 'U' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT - ) ; -fragment -Decimal_integer_literal: - Decimal_digits INTEGER_TYPE_SUFFIX? ; -//-------------------------------------------------------- -Hex_number: - '0'('x'|'X') HEX_DIGITS INTEGER_TYPE_SUFFIX? ; -fragment -Decimal_digits: - DECIMAL_DIGIT+ ; -fragment -DECIMAL_DIGIT: - '0'..'9' ; -fragment -INTEGER_TYPE_SUFFIX: - 'U' | 'u' | 'L' | 'l' | 'UL' | 'Ul' | 'uL' | 'ul' | 'LU' | 'Lu' | 'lU' | 'lu' ; -fragment HEX_DIGITS: - HEX_DIGIT+ ; -fragment HEX_DIGIT: - '0'..'9'|'A'..'F'|'a'..'f' ; -fragment -Exponent_part: - ('e'|'E') Sign? Decimal_digits; -fragment -Sign: - '+'|'-' ; -fragment -Real_type_suffix: - 'F' | 'f' | 'D' | 'd' | 'M' | 'm' ; - -// Testing rules - so you can just use one file with a list of items -assignment_list: - (assignment ';')+ ; -field_declarations: - field_declaration+ ; -property_declaration_list: - property_declaration+ ; -member_access_list: - member_access+ ; -constant_declarations: - constant_declaration+; -literals: - literal+ ; -delegate_declaration_list: - delegate_declaration+ ; -local_variable_declaration_list: - (local_variable_declaration ';')+ ; -local_variable_initializer_list: - (local_variable_initializer ';')+ ; -expression_list_test: - (expression ';')+ ; -unary_expression_list: - (unary_expression ';')+ ; -invocation_expression_list: - (invocation_expression ';')+ ; -primary_expression_list: - (primary_expression ';')+ ; -static_constructor_modifiers_list: - (static_constructor_modifiers ';')+ ; \ No newline at end of file +grammar cs; + +options { + memoize=true; + language=CSharp2; + output=AST; +} + + +tokens { + BLOCK; +} + +@namespace { RusticiSoftware.Translator.CSharp } + +@lexer::header +{ + using System.Collections.Generic; + using Debug = System.Diagnostics.Debug; +} + +@lexer::members { + // Preprocessor Data Structures - see lexer section below and PreProcessor.cs + protected Dictionary MacroDefines = new Dictionary(); + protected Stack Processing = new Stack(); + + // Uggh, lexer rules don't return values, so use a stack to return values. + protected Stack Returns = new Stack(); +} + +@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] '}' -> ^(BLOCK namespace_body) ; +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 '<') => 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 '<') => identifier generic_argument_list + | identifier ; + +qid: // qualified_identifier v2 + qid_start qid_part* + ; +qid_start: + predefined_type + | (identifier '<') => 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 unary_operator_declarator + | overloadable_binary_operator 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 '<') => 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 | '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' | 'set' | 'var' | '__arglist' | 'dynamic'; + +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' | 'event' | 'method' | 'param' | 'property' | 'type' + | 'yield' | 'from' | 'into' | 'join' | 'on' | 'where' | 'orderby' | 'group' | 'by' | 'ascending' | 'descending' + | 'equals' | 'select' | 'pragma' | 'let' | 'remove' | 'set' | 'var' | '__arglist' | 'dynamic'; + +literal: + Real_literal + | NUMBER + | Hex_number + | Character_literal + | STRINGLITERAL + | Verbatim_string_literal + | TRUE + | FALSE + | NULL + ; + +/////////////////////////////////////////////////////// + +TRUE : 'true'; +FALSE: 'false' ; +NULL : 'null' ; +DOT : '.' ; +PTR : '->' ; +MINUS : '-' ; +GT : '>' ; +USING : 'using'; +ENUM : 'enum'; +IF: 'if'; +ELIF: 'elif'; +ENDIF: 'endif'; +DEFINE: 'define'; +UNDEF: 'undef'; +SEMI: ';'; +RPAREN: ')'; + +WS: + (' ' | '\r' | '\t' | '\n' ) + { Skip(); } ; +fragment +TS: + (' ' | '\t' ) + { Skip(); } ; +DOC_LINE_COMMENT + : ('///' ~('\n'|'\r')* ('\r' | '\n')+) + { Skip(); } ; +LINE_COMMENT + : ('//' ~('\n'|'\r')* ('\r' | '\n')+) + { Skip(); } ; +COMMENT: + '/*' + (options {greedy=false;} : . )* + '*/' + { Skip(); } ; +STRINGLITERAL + : + '"' (EscapeSequence | ~('"' | '\\'))* '"' ; +Verbatim_string_literal: + '@' '"' Verbatim_string_literal_character* '"' ; +fragment +Verbatim_string_literal_character: + '"' '"' | ~('"') ; +NUMBER: + Decimal_digits INTEGER_TYPE_SUFFIX? ; +// For the rare case where 0.ToString() etc is used. +GooBall +@after +{ + CommonToken int_literal = new CommonToken(NUMBER, $dil.text); + CommonToken dot = new CommonToken(DOT, "."); + CommonToken iden = new CommonToken(IDENTIFIER, $s.text); + + Emit(int_literal); + Emit(dot); + Emit(iden); + Console.Error.WriteLine("\tFound GooBall {0}", $text); +} + : + dil = Decimal_integer_literal d = '.' s=GooBallIdentifier + ; + +fragment GooBallIdentifier + : IdentifierStart IdentifierPart* ; + +//--------------------------------------------------------- +Real_literal: + Decimal_digits '.' Decimal_digits Exponent_part? Real_type_suffix? + | '.' Decimal_digits Exponent_part? Real_type_suffix? + | Decimal_digits Exponent_part Real_type_suffix? + | Decimal_digits Real_type_suffix ; +Character_literal: + '\'' + ( EscapeSequence + // upto 3 multi byte unicode chars + | ~( '\\' | '\'' | '\r' | '\n' ) + | ~( '\\' | '\'' | '\r' | '\n' ) ~( '\\' | '\'' | '\r' | '\n' ) + | ~( '\\' | '\'' | '\r' | '\n' ) ~( '\\' | '\'' | '\r' | '\n' ) ~( '\\' | '\'' | '\r' | '\n' ) + ) + '\'' ; +IDENTIFIER: + IdentifierStart IdentifierPart* ; +Pragma: + // ignore everything after the pragma since the escape's in strings etc. are different + '#' ('pragma' | 'region' | 'endregion' | 'line' | 'warning' | 'error') ~('\n'|'\r')* ('\r' | '\n')+ + { Skip(); } ; +PREPROCESSOR_DIRECTIVE: + | PP_CONDITIONAL; +fragment +PP_CONDITIONAL: + (IF_TOKEN + | DEFINE_TOKEN + | ELSE_TOKEN + | ENDIF_TOKEN + | UNDEF_TOKEN) TS* (LINE_COMMENT? | ('\r' | '\n')+) ; +fragment +IF_TOKEN + @init { bool process = true; }: + ('#' TS* 'if' TS+ ppe = PP_EXPRESSION) +{ + // if our parent is processing check this if + Debug.Assert(Processing.Count > 0, "Stack underflow preprocessing. IF_TOKEN"); + if (Processing.Count > 0 && Processing.Peek()) + Processing.Push(Returns.Pop()); + else + Processing.Push(false); +} ; +fragment +DEFINE_TOKEN: + '#' TS* 'define' TS+ define = IDENTIFIER + { + MacroDefines.Add($define.Text, ""); + } ; +fragment +UNDEF_TOKEN: + '#' TS* 'undef' TS+ define = IDENTIFIER + { + if (MacroDefines.ContainsKey($define.Text)) + MacroDefines.Remove($define.Text); + } ; +fragment +ELSE_TOKEN: + ( '#' TS* e = 'else' + | '#' TS* 'elif' TS+ PP_EXPRESSION) + { + // We are in an elif + if ($e == null) + { + Debug.Assert(Processing.Count > 0, "Stack underflow preprocessing. ELIF_TOKEN"); + if (Processing.Count > 0 && Processing.Peek() == false) + { + Processing.Pop(); + // if our parent was processing, do else logic + Debug.Assert(Processing.Count > 0, "Stack underflow preprocessing. ELIF_TOKEN2"); + if (Processing.Count > 0 && Processing.Peek()) + Processing.Push(Returns.Pop()); + else + Processing.Push(false); + } + else + { + Processing.Pop(); + Processing.Push(false); + } + } + else + { + // we are in a else + if (Processing.Count > 0) + { + bool bDoElse = !Processing.Pop(); + + // if our parent was processing + Debug.Assert(Processing.Count > 0, "Stack underflow preprocessing, ELSE_TOKEN"); + if (Processing.Count > 0 && Processing.Peek()) + Processing.Push(bDoElse); + else + Processing.Push(false); + } + } + Skip(); + } ; +fragment +ENDIF_TOKEN: + '#' 'endif' + { + if (Processing.Count > 0) + Processing.Pop(); + Skip(); + } ; + + + + +fragment +PP_EXPRESSION: + PP_OR_EXPRESSION; +fragment +PP_OR_EXPRESSION: + PP_AND_EXPRESSION TS* ('||' TS* PP_AND_EXPRESSION TS* )* ; +fragment +PP_AND_EXPRESSION: + PP_EQUALITY_EXPRESSION TS* ('&&' TS* PP_EQUALITY_EXPRESSION TS* )* ; +fragment +PP_EQUALITY_EXPRESSION: + PP_UNARY_EXPRESSION TS* (('=='| ne = '!=') TS* PP_UNARY_EXPRESSION + { + bool rt1 = Returns.Pop(), rt2 = Returns.Pop(); + Returns.Push(rt1 == rt2 == ($ne == null)); + } + TS* )* + ; +fragment +PP_UNARY_EXPRESSION: + pe = PP_PRIMARY_EXPRESSION + | '!' TS* ue = PP_UNARY_EXPRESSION { Returns.Push(!Returns.Pop()); } + ; +fragment +PP_PRIMARY_EXPRESSION: + IDENTIFIER + { + Returns.Push(MacroDefines.ContainsKey($IDENTIFIER.Text)); + } + | '(' PP_EXPRESSION ')' + ; + + + +fragment +IdentifierStart + : '@' | '_' | 'A'..'Z' | 'a'..'z' ; +fragment +IdentifierPart +: 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' ; +fragment +EscapeSequence + : '\\' ( + 'b' + | 't' + | 'n' + | 'f' + | 'r' + | 'v' + | 'a' + | '\"' + | '\'' + | '\\' + | ('0'..'3') ('0'..'7') ('0'..'7') + | ('0'..'7') ('0'..'7') + | ('0'..'7') + | 'x' HEX_DIGIT + | 'x' HEX_DIGIT HEX_DIGIT + | 'x' HEX_DIGIT HEX_DIGIT HEX_DIGIT + | 'x' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT + | 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT + | 'U' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT + ) ; +fragment +Decimal_integer_literal: + Decimal_digits INTEGER_TYPE_SUFFIX? ; +//-------------------------------------------------------- +Hex_number: + '0'('x'|'X') HEX_DIGITS INTEGER_TYPE_SUFFIX? ; +fragment +Decimal_digits: + DECIMAL_DIGIT+ ; +fragment +DECIMAL_DIGIT: + '0'..'9' ; +fragment +INTEGER_TYPE_SUFFIX: + 'U' | 'u' | 'L' | 'l' | 'UL' | 'Ul' | 'uL' | 'ul' | 'LU' | 'Lu' | 'lU' | 'lu' ; +fragment HEX_DIGITS: + HEX_DIGIT+ ; +fragment HEX_DIGIT: + '0'..'9'|'A'..'F'|'a'..'f' ; +fragment +Exponent_part: + ('e'|'E') Sign? Decimal_digits; +fragment +Sign: + '+'|'-' ; +fragment +Real_type_suffix: + 'F' | 'f' | 'D' | 'd' | 'M' | 'm' ; + +// Testing rules - so you can just use one file with a list of items +assignment_list: + (assignment ';')+ ; +field_declarations: + (attributes? modifiers? type field_declaration)+ ; +property_declaration_list: + (attributes? modifiers? type property_declaration)+ ; +constant_declarations: + constant_declaration+; +literals: + literal+ ; +delegate_declaration_list: + (attributes? modifiers? delegate_declaration)+ ; +local_variable_declaration_list: + (local_variable_declaration ';')+ ; +local_variable_initializer_list: + (local_variable_initializer ';')+ ; +expression_list_test: + (expression ';')+ ; +unary_expression_list: + (unary_expression ';')+ ; +invocation_expression_list: + (invocation_expression ';')+ ; +primary_expression_list: + (primary_expression ';')+ ; +non_assignment_expression_list: + (non_assignment_expression ';')+ ; +method_declarations: + (modifiers? ('void' | type) method_declaration)+ ; \ No newline at end of file diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/minDriver.cs b/CSharpTranslator/antlr3/src/cs2j/CSharp/minDriver.cs index 0705499..18191c8 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/minDriver.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/minDriver.cs @@ -51,8 +51,7 @@ namespace RusticiSoftware.Translator.CSharp TemplateExtracter templateWalker = new TemplateExtracter(nodes); - templateWalker.DebugLevel = 10; - templateWalker.compilation_unit(); + templateWalker.compilation_unit(new CS2JSettings()); } } diff --git a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj index 90cc844..034f5c7 100644 --- a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj +++ b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj @@ -39,7 +39,7 @@ DEBUG;TRACE prompt 4 - -genTemplate /Users/keving/gitrepos/cs2j/CSharpTranslator/antlr3/src/cs2j/bin/Debug/Antlr3-2.Runtime.DotNet20.dll Antlr.Runtime.CommonTokenStream + -dumpcsharp /Users/keving/gitrepos/cs2j/CSharpTranslator/antlr3/cs2jTest/TestDLLs/Various.cs pdbonly @@ -54,7 +54,6 @@ - @@ -66,6 +65,7 @@ +