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

📄 vbnet.atg

📁 c#源代码
💻 ATG
📖 第 1 页 / 共 5 页
字号:
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using ICSharpCode.SharpRefactory.Parser.AST.VB;
using ICSharpCode.SharpRefactory.Parser.VB;

COMPILER VBNET

private string assemblyName = null;
public CompilationUnit compilationUnit;
private ArrayList importedNamespaces = null;
private Stack withStatements;
private bool isLabel = false;
private LabelStatement labelStatement = null;

public string ContainingAssembly
{
	set { assemblyName = value; }
}

Token t
{
	get {
		return lexer.Token;
	}
}
Token la
{
	get {
		return lexer.LookAhead;
	}
}

void updateLabelStatement(Statement stmt)
{
	if(isLabel) {
		labelStatement.EmbeddedStatement = stmt;
		isLabel = false;
	} else {
		compilationUnit.AddChild(stmt);
	}
}

/* Return the n-th token after the current lookahead token */
void StartPeek()
{
	lexer.StartPeek();
}

Token Peek()
{
	return lexer.Peek();
}

Token Peek (int n)
{
	lexer.StartPeek();
	Token x = la;
	while (n > 0) {
		x = lexer.Peek();
		n--;
	}
	return x;
}

public void Error(string s)
{
	if (errDist >= minErrDist) {
		errors.Error(la.line, la.col, s);
	}
	errDist = 0;
}

public Expression ParseExpression(Lexer lexer)
{
	this.errors = lexer.Errors;
	this.lexer = lexer;
	errors.SynErr = new ErrorCodeProc(SynErr);
	lexer.NextToken();
	Expression expr;
	Expr(out expr);
	return expr;
}

bool IsEndStmtAhead()
{
	int peek = Peek(1).kind;
	return la.kind == Tokens.End && (peek == Tokens.EOL || peek == Tokens.Colon);
}

bool IsNotClosingParenthesis() {
	return la.kind != Tokens.CloseParenthesis;
}

/*
	True, if ident is followed by "="
*/
bool IdentAndAsgn () {
	if(la.kind == Tokens.Identifier) {
		if(Peek(1).kind == Tokens.Assign) return true;
		if(Peek(1).kind == Tokens.Colon && Peek(2).kind == Tokens.Assign) return true;
	}
	return false;
}

/*
	True, if ident is followed by "=" or by ":" and "="
*/
bool IsNamedAssign() {
//	if(Peek(1).kind == Tokens.Assign) return true; // removed: not in the lang spec
	if(Peek(1).kind == Tokens.Colon && Peek(2).kind == Tokens.Assign) return true;
	return false;
}

bool IsObjectCreation() {
	return la.kind == Tokens.As && Peek(1).kind == Tokens.New;
}

/*
	True, if "<" is followed by the ident "assembly" or "module"
*/
bool IsGlobalAttrTarget () {
	Token pt = Peek(1);
	return la.kind == Tokens.LessThan && ( pt.val.ToLower(System.Globalization.CultureInfo.InvariantCulture) == "assembly" || pt.val.ToLower(System.Globalization.CultureInfo.InvariantCulture) == "module");
}

/*
	True if the next token is a "(" and is followed by "," or ")"
*/
bool IsRank()
{
	int peek = Peek(1).kind;
	return la.kind == Tokens.OpenParenthesis
						&& (peek == Tokens.Comma || peek == Tokens.CloseParenthesis);
}

bool IsDims()
{
	int peek = Peek(1).kind;
	int peek_n = Peek(2).kind;
	return la.kind == Tokens.OpenParenthesis
						&& (peek == Tokens.LiteralInteger && peek_n == Tokens.CloseParenthesis);
}

bool IsSize()
{
	return la.kind == Tokens.OpenParenthesis;
}

/*
	True, if the comma is not a trailing one,
	like the last one in: a, b, c,
*/
bool NotFinalComma() {
	int peek = Peek(1).kind;
	return la.kind == Tokens.Comma &&
		   peek != Tokens.CloseCurlyBrace && peek != Tokens.CloseSquareBracket;
}

/*
	True, if the next token is "Else" and this one
	if followed by "If"
*/
bool IsElseIf()
{
	int peek = Peek(1).kind;
	return la.kind == Tokens.Else && peek == Tokens.If;
}

/*
	True if the next token is goto and this one is
	followed by minus ("-") (this is allowd in in
	error clauses)
*/
bool IsNegativeLabelName()
{
	int peek = Peek(1).kind;
	return la.kind == Tokens.GoTo && peek == Tokens.Minus;
}

/*
	True if the next statement is a "Resume next" statement
*/
bool IsResumeNext()
{
	int peek = Peek(1).kind;
	return la.kind == Tokens.Resume && peek == Tokens.Next;
}

/*
	True, if ident/literal integer is followed by ":"
*/
bool IsLabel()
{
	return (la.kind == Tokens.Identifier || la.kind == Tokens.LiteralInteger)
			&& Peek(1).kind == Tokens.Colon;
}

bool IsNotStatementSeparator()
{
	return la.kind == Tokens.Colon && Peek(1).kind == Tokens.EOL;
}

bool IsAssignment ()
{
	return IdentAndAsgn();
}

bool IsMustOverride(Modifiers m)
{
	return m.Contains(Modifier.MustOverride);
}

/*
	True, if lookahead is a local attribute target specifier,
	i.e. one of "event", "return", "field", "method",
	"module", "param", "property", or "type"
*/
bool IsLocalAttrTarget() {
	// TODO
	return false;
}

TOKENS
	/*----- terminal classes -----*/
	/* EOF is 0 */
	EOL
	ident
	LiteralString
	LiteralCharacter
	LiteralInteger
	LiteralDouble
	LiteralSingle
	LiteralDecimal
	LiteralDate
	
	/*----- special character -----*/
	"."
	"="
	","
	":"
	"+"
	"-"
	"*"
	"/"
	"\\"
	"&"
	"^"
	
	"{"
	"}"
	
	"["
	"]"
	
	"("
	")"
	
	">"
	"<"
	
	"<>"
	">="
	"<="
	
	"<<"
	">>"
	
	"+="
	"^="
	"-="
	"*="
	"/="
	"\\="
	"<<="
	">>="
	"&="
	
	/*----- VB.NET keywords -----*/
	"AddHandler"
	"AddressOf"
	"Alias"
	"And"
	"AndAlso"
	"Ansi"
	"As"
	"Assembly"
	"Auto"
	"Binary"
	"Boolean"
	"ByRef"
	"Byte"
	"ByVal"
	"Call"
	"Case"
	"Catch"
	"CBool"
	"CByte"
	"CChar"
	"CDate"
	"CDbl"
	"CDec"
	"Char"
	"CInt"
	"Class"
	"CLng"
	"CObj"
	"Compare"
	"Const"
	"CShort"
	"CSng"
	"CStr"
	"CType"
	"Date"
	"Decimal"
	"Declare"
	"Default"
	"Delegate"
	"Dim"
	"DirectCast"
	"Do"
	"Double"
	"Each"
	"Else"
	"ElseIf"
	"End"
	"EndIf"
	"Enum"
	"Erase"
	"Error"
	"Event"
	"Exit"
	"Explicit"
	"False"
	"Finally"
	"For"
	"Friend"
	"Function"
	"Get"
	"GetType"
	"GoSub"
	"GoTo"
	"Handles"
	"If"
	"Implements"
	"Imports"
	"In"
	"Inherits"
	"Integer"
	"Interface"
	"Is"
	"Let"
	"Lib"
	"Like"
	"Long"
	"Loop"
	"Me"
	"Mod"
	"Module"
	"MustInherit"
	"MustOverride"
	"MyBase"
	"MyClass"
	"Namespace"
	"New"
	"Next"
	"Not"
	"Nothing"
	"NotInheritable"
	"NotOverridable"
	"Object"
	"Off"
	"On"
	"Option"
	"Optional"
	"Or"
	"OrElse"
	"Overloads"
	"Overridable"
	"Override"
	"Overrides"
	"ParamArray"
	"Preserve"
	"Private"
	"Property"
	"Protected"
	"Public"
	"RaiseEvent"
	"ReadOnly"
	"ReDim"
	"RemoveHandler"
	"Resume"
	"Return"
	"Select"
	"Set"
	"Shadows"
	"Shared"
	"Short"
	"Single"
	"Static"
	"Step"
	"Stop"
	"Strict"
	"String"
	"Structure"
	"Sub"
	"SyncLock"
	"Text"
	"Then"
	"Throw"
	"To"
	"True"
	"Try"
	"TypeOf"
	"Unicode"
	"Until"
	"Variant"
	"Wend"
	"When"
	"While"
	"With"
	"WithEvents"
	"WriteOnly"
	"Xor"
	
PRODUCTIONS

VBNET
	(.
		compilationUnit = new CompilationUnit();
		withStatements = new Stack();
	.) =
	{ EOL }
	{ OptionStmt }
	{ ImportsStmt}
	{ IF (IsGlobalAttrTarget()) GlobalAttributeSection }
	{ NamespaceMemberDecl }
	EOF
	.

OptionStmt (. INode node = null; bool val = true; .) =
	"Option" (. Point startPos = t.Location; .)
	(
		"Explicit" [ OptionValue<ref val> ]
		(. node = new OptionExplicitDeclaration(val); .)
		|
		"Strict" [ OptionValue<ref val> ]
		(. node = new OptionStrictDeclaration(val); .)
		|
		"Compare" ( "Binary" (. node = new OptionCompareDeclaration(CompareType.Binary); .)
				  | "Text" (. node = new OptionCompareDeclaration(CompareType.Text); .)
				  )
	)
	EndOfStmt
	(.
		node.StartLocation = startPos;
		node.EndLocation   = t.Location;
		compilationUnit.AddChild(node);
	.)
	.

OptionValue<ref bool val> =
	(
		"On" (. val = true; .)
	|
		"Off" (. val = true; .)
	)
	.

EndOfStmt =
	(
		EOL
	|
		":"
	)
	.

ImportsStmt
	(.
		ArrayList importClauses = new ArrayList();
		importedNamespaces = new ArrayList();
		object importClause;
	.) =
	"Imports"
	(.
		Point startPos = t.Location;
		ImportsStatement importsStatement = new ImportsStatement(null);
	.)
	ImportClause<out importClause> (. importClauses.Add(importClause); .)
	{
		"," ImportClause<out importClause> (. importClauses.Add(importClause); .)
	}
	EndOfStmt
	(.
		importsStatement.ImportClauses = importClauses;
		importsStatement.StartLocation = startPos;
		importsStatement.EndLocation   = t.Location;
		compilationUnit.AddChild(importsStatement);
	.)
	.

⌨️ 快捷键说明

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