From d68a8dd4a05ee48693b126463480141bec19011f Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Fri, 16 Jul 2010 12:15:00 -0500 Subject: [PATCH] add minimal, but WORKING, tree crawler :) --- .../antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs | 54 +++++ .../antlr3/src/cs2j/CSharp/csCrawl.g | 192 ++++++++++++++++++ .../antlr3/src/cs2j/CSharp/minDriver.cs | 12 +- CSharpTranslator/antlr3/src/cs2j/README.txt | 3 + CSharpTranslator/antlr3/src/cs2j/cs2j.csproj | 5 +- 5 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 CSharpTranslator/antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs create mode 100644 CSharpTranslator/antlr3/src/cs2j/CSharp/csCrawl.g diff --git a/CSharpTranslator/antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs b/CSharpTranslator/antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs new file mode 100644 index 0000000..80bbeb1 --- /dev/null +++ b/CSharpTranslator/antlr3/src/cs2j/AntlrUtils/AntlrUtils.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Antlr.Runtime.Tree; + +namespace RusticiSoftware.Translator.AntlrUtils +{ + public class AntlrUtils + { + /// DumpNodes + /// The CommonTreeNodeStream has a tree in "flat form". The UP and DOWN tokens represent the branches of the + /// tree. Dump these out in tree form to the console. + /// + public static void DumpNodes(CommonTreeNodeStream nodes) + { + Console.ForegroundColor = ConsoleColor.Magenta; + Console.WriteLine("Nodes"); + int spaces = 0; + string str_spaces = " "; + object o_prev = string.Empty; + //for (int n = 0; n < nodes.Count; ++n) + object o = nodes.NextElement(); + while (!nodes.IsEndOfFile(o)) + { + //object o = nodes.Get(n); + //object o = nodes[n]; + + if (o.ToString() == "DOWN") + { + spaces += 2; + if (o_prev.ToString() != "UP" && o_prev.ToString() != "DOWN") + Console.Write("\r\n{0} {1}", str_spaces.Substring(0, spaces), o_prev); + } + else if (o.ToString() == "UP") + { + spaces -= 2; + if (o_prev.ToString() != "UP" && o_prev.ToString() != "DOWN") + Console.Write(" {0}\r\n{1}", o_prev, str_spaces.Substring(0, spaces)); + } + else if (o_prev.ToString() != "UP" && o_prev.ToString() != "DOWN") + Console.Write(" {0}", o_prev.ToString()); + + o_prev = o; + o = nodes.NextElement(); + } + if (o_prev.ToString() != "UP" && o_prev.ToString() != "DOWN") + Console.WriteLine(" {0}", o_prev.ToString()); + Console.ResetColor(); + } + + + } +} diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/csCrawl.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/csCrawl.g new file mode 100644 index 0000000..d109429 --- /dev/null +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/csCrawl.g @@ -0,0 +1,192 @@ +// csCrawl.g +// +// CSharp AST Crawler +// +// Kevin Glynn +// kevin.glynn@twigletsoftware.com +// June 2010 + +tree grammar csCrawl; + +options { + tokenVocab=cs; + ASTLabelType=CommonTree; + language=CSharp2; + backtrack=true; +} + +@namespace { RusticiSoftware.Translator.CSharp } + +@header +{ + using System.Text; +} + +/******************************************************************************************** + Parser section +*********************************************************************************************/ + +/////////////////////////////////////////////////////// +compilation_unit2: + { Console.Out.WriteLine("Debug: start"); } using_directives2 + ; + +using_directives2: + ^(USING_DIRECTIVE 'using' { Console.Out.WriteLine("Debug: using"); } namespace_name2 ';') + ; + +namespace_name2: + ^(NAMESPACE_OR_TYPE_NAME namespace_component2) + ; + +namespace_component2: + ^(NSTN identifier2) + ; + +identifier2: + ^(ID id=IDENTIFIER { Console.Out.WriteLine("Identifier: " + id.Text);}) + ; + +compilation_unit: +// extern_alias_directives? + using_directives? +// global_attributes? + namespace_body? // specific namespace or in the global namespace + ; +namespace_declaration: + ^(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_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: + ^(QID_PART access_operator identifier generic_argument_list?) ; + + +// B.2.1 Basic Concepts +namespace_name + : namespace_or_type_name ; +type_name: + ^(TYPE_NAME namespace_or_type_name) ; +namespace_or_type_name: + ^(NAMESPACE_OR_TYPE_NAME + ^(NSTN identifier generic_argument_list?) + ) ; + // ^(NSTN '::' identifier generic_argument_list?)? + // ^(NSTN identifier generic_argument_list?)* + // ) ; +// | qualified_alias_member (the :: part) + //; + +type: + ^(TYPE type_name? predefined_type? rank_specifiers '*'*) + | ^(TYPE type_name '*'+) + | ^(TYPE type_name '?') + | ^(TYPE type_name) + | ^(TYPE predefined_type '*'+) + | ^(TYPE predefined_type '?') + | ^(TYPE predefined_type) + | ^(TYPE 'void' '*'+) ; + +rank_specifiers: + rank_specifier+ ; +rank_specifier: + '[' dim_separators? ']' ; +dim_separators: + ','+ ; +type_list: + type (',' type)* ; + +type_arguments: + type_argument+ + ; +type_argument: + type ; +type_variable_name: + identifier ; + +generic_argument_list: + '<' type_arguments '>' ; + +access_operator: + '.' | '->' ; + +predefined_type: + ^(PREDEFINED_TYPE 'bool' ) + | ^(PREDEFINED_TYPE 'byte' ) + | ^(PREDEFINED_TYPE 'char' ) + | ^(PREDEFINED_TYPE 'decimal') + | ^(PREDEFINED_TYPE 'double' ) + | ^(PREDEFINED_TYPE 'float' ) + | ^(PREDEFINED_TYPE 'int' ) + | ^(PREDEFINED_TYPE 'long' ) + | ^(PREDEFINED_TYPE 'object' ) + | ^(PREDEFINED_TYPE 'sbyte' ) + | ^(PREDEFINED_TYPE 'short' ) + | ^(PREDEFINED_TYPE 'string' ) + | ^(PREDEFINED_TYPE 'uint' ) + | ^(PREDEFINED_TYPE 'ulong' ) + | ^(PREDEFINED_TYPE 'ushort' ); + +identifier: + ^(ID id=IDENTIFIER { Console.Out.WriteLine("Identifier: " + id.Text);}) + | ^(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'; + \ 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 83069c9..f85468a 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/minDriver.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/minDriver.cs @@ -38,8 +38,18 @@ namespace RusticiSoftware.Translator.CSharp csParser.compilation_unit_return parser_rt; parser_rt = p.compilation_unit(); + ITree parse_tree = (ITree)parser_rt.Tree; + Console.Out.WriteLine(parse_tree.ToStringTree()); + + CommonTreeNodeStream display_nodes = new CommonTreeNodeStream(parse_tree); + AntlrUtils.AntlrUtils.DumpNodes(display_nodes); + + BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(parse_tree); + + + csCrawl walker = new csCrawl(nodes); + walker.compilation_unit2(); - Console.Out.WriteLine(((ITree)parser_rt.Tree).ToStringTree()); } } else diff --git a/CSharpTranslator/antlr3/src/cs2j/README.txt b/CSharpTranslator/antlr3/src/cs2j/README.txt index c3ab13c..13969cf 100644 --- a/CSharpTranslator/antlr3/src/cs2j/README.txt +++ b/CSharpTranslator/antlr3/src/cs2j/README.txt @@ -13,3 +13,6 @@ cd "C:\Documents and Settings\developer\My Documents\gitrepos\cs2j\CSharpTransla src\cs2j\bin\Debug\cs2j.exe -netdir "c:\Documents and Settings\s\developer\My Documents\gitrepos\cs2j\CS2JLibrary\NetTranslations" -appdir "c:\Documents and Settings\developer\My Documents\Visual Studio 2005\Projects\ScormEngineNetTrunk\src\app\ScormEngine.Core" "c:\Documents and Settings\developer\My Documents\Visual Studio 2005\Projects\ScormEngineNetTrunk\src\app\ScormEngine.Core\Logic" +-- arguments to VS Debug + +-dumpcsharp -dumpxml -xmldir c:\xmls -netdir "C:\Documents and Settings\developer\My Documents\gitrepos\cs2j\CS2JLibrary\NetTranslations" "C:\Documents and Settings\developer\My Documents\Visual Studio 2005\Projects\ScormEngineNetTrunk\src\app\ScormEngine.Core\Util\Encryption\DataProtection.cs" diff --git a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj index 5161c35..3cd6af7 100644 --- a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj +++ b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj @@ -54,18 +54,21 @@ + + - + +