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

📄 cs.atg

📁 c#源代码
💻 ATG
📖 第 1 页 / 共 5 页
字号:
/* True, if lookahead is a local attribute target specifier, *
 * i.e. one of "event", "return", "field", "method",         *
 *             "module", "param", "property", or "type"      */
bool IsLocalAttrTarget () {
	int cur = la.kind;
	string val = la.val;

	return (cur == Tokens.Event || cur == Tokens.Return ||
	        (cur == Tokens.Identifier &&
	         (val == "field" || val == "method"   || val == "module" ||
	          val == "param" || val == "property" || val == "type"))) &&
	       Peek(1).kind == Tokens.Colon;
}


/*------------------------------------------------------------------------*
 *----- LEXER TOKEN LIST  ------------------------------------------------*
 *------------------------------------------------------------------------*/
TOKENS
	/*----- terminal classes -----*/
	/* EOF is 0 */
	ident
	literal
	
	/*----- special character -----*/
	"=" /* 3 */
	
	"+"
	"-"
	"*"
	"/"
	"%"
	
	":"
	";"
	"?"
	","
	"."
	
	"{"
	"}"
	
	"["
	"]"
	
	"("
	")"
	
	">"
	"<"
	
	"!"
	"&&"
	"||"
	
	"~"
	"&"
	"|"
	"^"

	/*----- special character sequences -----*/
	"++" /* 29 */
	"--"
	"=="
	"!="
	">="
	"<="
	
	"<<"
	">>"
	
	"+="
	"-="
	"*="
	"/="
	"%="
	"&="
	"|="
	"^="
	"<<="
	">>="
	
	"->"
	
	/*----- C# keywords -----*/
	"abstract" /* 48 */
	"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"

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

PRODUCTIONS

/*--- compilation unit: */
CS
(. compilationUnit = new CompilationUnit(); .)
=
	{ UsingDirective }
	{ IF (IsGlobalAttrTarget()) GlobalAttributeSection }
	{ NamespaceMemberDecl }
	EOF
.

UsingDirective
(.
	usingNamespaces = new ArrayList();
	string qualident = null, aliasident = null;
.)
=
	"using"                     (. Point startPos = t.Location;
	                               INode node     = null; 
	                            .)
	[ IF (IsAssignment()) ident (. aliasident = t.val; .) "="  ]  /*--- using alias directive */
	Qualident<out qualident>    (. if (qualident != null && qualident.Length > 0) {
	                                 if (aliasident != null) {
	                                   node = new UsingAliasDeclaration(aliasident, qualident);
	                                 } else {
	                                     usingNamespaces.Add(qualident);
	                                     node = new UsingDeclaration(qualident);
	                                 }
	                               }
	                            .)
	";"                         (. node.StartLocation = startPos;
	                               node.EndLocation   = t.EndLocation;
	                               compilationUnit.AddChild(node);
	                            .)
.

GlobalAttributeSection
=
	
	"[" (. Point startPos = t.Location; .) ident                   (. if (t.val != "assembly") Error("global attribute target specifier (\"assembly\") expected");
	                               string attributeTarget = t.val;
	                               ArrayList attributes = new ArrayList();
	                               ICSharpCode.SharpRefactory.Parser.AST.Attribute 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 ICSharpCode.SharpRefactory.Parser.AST.Attribute attribute>
(. string qualident; .)
=
	Qualident<out qualident>           (. ArrayList positional = new ArrayList();
	                                      ArrayList named      = new ArrayList();
	                                      string name = qualident;
	                                   .)
	[ AttributeArguments<ref positional, ref named> ] (. attribute  = new ICSharpCode.SharpRefactory.Parser.AST.Attribute(name, positional, named);.)
.

AttributeArguments<ref ArrayList positional, ref ArrayList named>
(.
	bool nameFound = false;
	string name = "";
	Expression expr;
.)
=
	"("
	[
		[
			IF (IsAssignment())          (. nameFound = true; .)
			ident                        (. name = t.val; .)
			"="
		] Expr<out expr>                 (. if(name == "") positional.Add(expr);
		                                    else { named.Add(new NamedArgument(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(name == "") positional.Add(expr);
				                              else { named.Add(new NamedArgument(name, expr)); name = ""; }
				                           .)
		}
	]
	")"
.

AttributeSection<out AttributeSection section>
(.
	string attributeTarget = "";
	ArrayList attributes = new ArrayList();
	ICSharpCode.SharpRefactory.Parser.AST.Attribute attribute;
	
.)
=
	"[" (. Point 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;
	ArrayList attributes = new ArrayList();
	Modifiers m = new Modifiers(this);
	string qualident;
.)
= /*--- namespace declaration: */
	"namespace"                  (. Point 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<Modifiers m, ArrayList attributes>
(.
	TypeReference type;
	StringCollection names;
	ArrayList p; string name;
.)
= /*--- class declaration: */      (. m.Check(Modifier.Classes); .)
	"class"                        (. TypeDeclaration newType = new TypeDeclaration();
	                                  compilationUnit.AddChild(newType);
	                                  compilationUnit.BlockStart(newType);
	                                  
	                                  newType.Type = Types.Class;
	                                  newType.Modifier = m.Modifier;
	                                  newType.Attributes = attributes;
	                                .)
	ident                           (. newType.Name = t.val; .)
	[ ClassBase<out names>          (. newType.BaseTypes = names; .) ] (. newType.StartLocation = t.EndLocation; .)
	ClassBody
	[ ";" ]                         (. newType.EndLocation = t.Location; 
	                                   compilationUnit.BlockEnd();
	                                .)
	| /*--- struct declaration: */  (. m.Check(Modifier.StructsInterfacesEnumsDelegates); .)
	( "struct"                      (. TypeDeclaration newType = new TypeDeclaration();
	                                   compilationUnit.AddChild(newType);
	                                   compilationUnit.BlockStart(newType);
	                                   newType.Type = Types.Struct; 
	                                   newType.Modifier = m.Modifier;
	                                   newType.Attributes = attributes;

⌨️ 快捷键说明

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