⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cs.atg

📁 根据cs源码解析为codedom
💻 ATG
📖 第 1 页 / 共 5 页
字号:
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Ast;
using ASTAttribute = ICSharpCode.NRefactory.Ast.Attribute;
using Types = ICSharpCode.NRefactory.Ast.ClassType;

COMPILER CS    /* AW 2002-12-30 renamed from CompilationUnit to CS  */


/*------------------------------------------------------------------------*
 *----- LEXER TOKEN LIST  ------------------------------------------------*
 *------------------------------------------------------------------------*/

/* START AUTOGENERATED TOKENS SECTION */
TOKENS
	/* ----- terminal classes ----- */
	/* EOF is 0 */
	ident
	Literal

	/* ----- special character ----- */
	"="
	"+"
	"-"
	"*"
	"/"
	"%"
	":"
	"::"
	";"
	"?"
	"??"
	","
	"."
	"{"
	"}"
	"["
	"]"
	"("
	")"
	">"
	"<"
	"!"
	"&&"
	"||"
	"~"
	"&"
	"|"
	"^"
	"++"
	"--"
	"=="
	"!="
	">="
	"<="
	"<<"
	"+="
	"-="
	"*="
	"/="
	"%="
	"&="
	"|="
	"^="
	"<<="
	"->"

	/* ----- keywords ----- */
	"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"
	"while"
/* END AUTOGENERATED TOKENS SECTION */

/*------------------------------------------------------------------------*
 *----- PARSER -----------------------------------------------------------*
 *------------------------------------------------------------------------*/

PRODUCTIONS

/*--- compilation unit: */
CS
(. lexer.NextToken(); /* get the first token */
   compilationUnit = new CompilationUnit(); .)
=
	{ UsingDirective }
	{ IF (IsGlobalAttrTarget()) GlobalAttributeSection }
	{ NamespaceMemberDecl }
	EOF
.

UsingDirective
(.
	string qualident = null; TypeReference aliasedType = null;
.)
=
	"using"                     (. Location startPos = t.Location; .)
	Qualident<out qualident>
	[ "=" NonArrayType<out aliasedType> ]
	";"                         (. 
	                               if (qualident != null && qualident.Length > 0) {
	                                 INode node;
	                                 if (aliasedType != null) {
	                                     node = new UsingDeclaration(qualident, aliasedType);
	                                 } else {
	                                     node = new UsingDeclaration(qualident);
	                                 }
	                                 node.StartLocation = startPos;
	                                 node.EndLocation   = t.EndLocation;
	                                 compilationUnit.AddChild(node);
	                               }
	                            .)
.

GlobalAttributeSection
=
	"[" (. Location startPos = t.Location; .) ident
	                            (. if (t.val != "assembly") Error("global attribute target specifier (\"assembly\") expected");
	                               string attributeTarget = t.val;
	                               List<ASTAttribute> attributes = new List<ASTAttribute>();
	                               ASTAttribute attribute;
	                            .)
	":" Attribute<out attribute> (. attributes.Add(attribute); .)
	{ IF (NotFinalComma()) "," Attribute<out attribute> (. attributes.Add(attribute); .)}
	[ "," ]
	"]"                         (. AttributeSection section = new AttributeSection(attributeTarget, attributes);
	                               section.StartLocation = startPos;
	                               section.EndLocation = t.EndLocation;
	                               compilationUnit.AddChild(section);
	                            .)
.

Attribute<out ASTAttribute attribute>
(. string qualident;
   string alias = null;
.)
=
	[ IF (la.kind == Tokens.Identifier && Peek(1).kind == Tokens.DoubleColon)
		ident (. alias = t.val; .)
		"::"
	]
	Qualident<out qualident> 
	(. List<Expression> positional = new List<Expression>();
	   List<NamedArgumentExpression> named = new List<NamedArgumentExpression>();
	   string name = (alias != null && alias != "global") ? alias + "." + qualident : qualident;
	.)
	[ AttributeArguments<positional, named> ] (. attribute  = new ASTAttribute(name, positional, named);.)
.

AttributeArguments<List<Expression> positional, List<NamedArgumentExpression> named>
(.
	bool nameFound = false;
	string name = "";
	Expression expr;
.)
=
	"("
	[
		[
			IF (IsAssignment())          (. nameFound = true; .)
			ident                        (. name = t.val; .)
			"="
		] Expr<out expr>                 (. if (expr != null) {if(name == "") positional.Add(expr);
		                                    else { named.Add(new NamedArgumentExpression(name, expr)); name = ""; }
		                                    }
		                                 .)
		
		{
			","
				(
					IF (IsAssignment())    (. nameFound = true; .)
					ident                      (. name = t.val; .)
					"="
					| /*Empty*/                (. if (nameFound) Error("no positional argument after named argument"); .)
				) Expr<out expr>           (. if (expr != null) { if(name == "") positional.Add(expr);
				                              else { named.Add(new NamedArgumentExpression(name, expr)); name = ""; }
				                              }
				                           .)
		}
	]
	")"
.

AttributeSection<out AttributeSection section>
(.
	string attributeTarget = "";
	List<ASTAttribute> attributes = new List<ASTAttribute>();
	ASTAttribute attribute;
	
.)
=
	"[" (. Location startPos = t.Location; .) /*--- attribute target specifier: */
	[ IF (IsLocalAttrTarget())
		( "event"                       (. attributeTarget = "event";.)
		| "return"                      (. attributeTarget = "return";.)
		| ident                         (. if (t.val != "field"    || t.val != "method" ||
		                                      t.val != "module"   || t.val != "param"  ||
		                                      t.val != "property" || t.val != "type")
		                                    Error("attribute target specifier (event, return, field," +
		                                          "method, module, param, property, or type) expected");
		                                   attributeTarget = t.val;
		                                .)
		) ":" 
	]
	/*--- attribute list: */
	Attribute<out attribute>            (. attributes.Add(attribute); .)
	{ IF (NotFinalComma()) "," Attribute<out attribute> (. attributes.Add(attribute); .)}
	[ "," ]
	"]"                         (. section = new AttributeSection(attributeTarget, attributes);
	                               section.StartLocation = startPos;
	                               section.EndLocation = t.EndLocation;
	                            .)
.

NamespaceMemberDecl
(.
	AttributeSection section;
	List<AttributeSection> attributes = new List<AttributeSection>();
	ModifierList m = new ModifierList();
	string qualident;
.)
= /*--- namespace declaration: */
	"namespace"                  (. Location startPos = t.Location; .)
	Qualident<out qualident>     (. INode node =  new NamespaceDeclaration(qualident);
	                                node.StartLocation = startPos;
	                                compilationUnit.AddChild(node);
	                                compilationUnit.BlockStart(node);
	                              .)
	"{"
	{ UsingDirective }
	{ NamespaceMemberDecl }
	"}"
	[ ";" ]                       (. node.EndLocation   = t.EndLocation;
	                                 compilationUnit.BlockEnd();
	                              .)
	/*--- type declaration: */
	| { AttributeSection<out section> (. attributes.Add(section); .) }
	  { TypeModifier<m> }
	  TypeDecl<m, attributes>
.

TypeDecl<ModifierList m, List<AttributeSection> attributes>
(.
	TypeReference type;
	List<TypeReference> names;
	List<ParameterDeclarationExpression> p = new List<ParameterDeclarationExpression>();
	string name;
	List<TemplateDefinition> templates;
.)
= /*--- class declaration: */      (. m.Check(Modifiers.Classes); .)
	"class"                        (. TypeDeclaration newType = new TypeDeclaration(m.Modifier, attributes);
	                                  templates = newType.Templates;
	                                  compilationUnit.AddChild(newType);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -