📄 parser.cs
字号:
#line 1 "VBNET.ATG"
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using ICSharpCode.CsVbRefactory.Parser.AST;
using ICSharpCode.CsVbRefactory.Parser.VB;
using System;
using System.Reflection;
namespace ICSharpCode.CsVbRefactory.Parser.VB {
public class Parser : IDisposable
{
const int maxT = 188;
const bool T = true;
const bool x = false;
const int minErrDist = 2;
const string errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
int errDist = minErrDist;
Errors errors;
Lexer lexer;
public Errors Errors {
get {
return errors;
}
}
#line 10 "VBNET.ATG"
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.Statement = 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() == "assembly" || pt.val.ToLower() == "module");
}
/*
True if the next token is a "(" and is followed by "," or ")"
*/
bool IsDims()
{
int peek = Peek(1).kind;
return la.kind == Tokens.OpenParenthesis
&& (peek == Tokens.Comma || peek == 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;
}
/*
*/
void SynErr(int n)
{
if (errDist >= minErrDist) {
errors.SynErr(lexer.LookAhead.line, lexer.LookAhead.col, n);
}
errDist = 0;
}
public void SemErr(string msg)
{
if (errDist >= minErrDist) {
errors.Error(lexer.Token.line, lexer.Token.col, msg);
}
errDist = 0;
}
void Expect(int n)
{
if (lexer.LookAhead.kind == n) {
lexer.NextToken();
} else {
SynErr(n);
}
}
bool StartOf(int s)
{
return set[s, lexer.LookAhead.kind];
}
void ExpectWeak(int n, int follow)
{
if (lexer.LookAhead.kind == n) {
lexer.NextToken();
} else {
SynErr(n);
while (!StartOf(follow)) {
lexer.NextToken();
}
}
}
bool WeakSeparator(int n, int syFol, int repFol)
{
bool[] s = new bool[maxT + 1];
if (lexer.LookAhead.kind == n) {
lexer.NextToken();
return true;
} else if (StartOf(repFol)) {
return false;
} else {
for (int i = 0; i <= maxT; i++) {
s[i] = set[syFol, i] || set[repFol, i] || set[0, i];
}
SynErr(n);
while (!s[lexer.LookAhead.kind]) {
lexer.NextToken();
}
return StartOf(syFol);
}
}
void VBNET() {
#line 423 "VBNET.ATG"
compilationUnit = new CompilationUnit();
withStatements = new Stack();
while (la.kind == 1) {
lexer.NextToken();
}
while (la.kind == 137) {
OptionStmt();
}
while (la.kind == 109) {
ImportsStmt();
}
while (
#line 429 "VBNET.ATG"
IsGlobalAttrTarget()) {
GlobalAttributeSection();
}
while (StartOf(1)) {
NamespaceMemberDecl();
}
Expect(0);
}
void OptionStmt() {
#line 434 "VBNET.ATG"
INode node = null; bool val = true;
Expect(137);
#line 435 "VBNET.ATG"
Point startPos = t.Location;
if (la.kind == 96) {
lexer.NextToken();
if (la.kind == 135 || la.kind == 136) {
OptionValue(
#line 437 "VBNET.ATG"
ref val);
}
#line 438 "VBNET.ATG"
node = new OptionExplicitDeclaration(val);
} else if (la.kind == 166) {
lexer.NextToken();
if (la.kind == 135 || la.kind == 136) {
OptionValue(
#line 440 "VBNET.ATG"
ref val);
}
#line 441 "VBNET.ATG"
node = new OptionStrictDeclaration(val);
} else if (la.kind == 71) {
lexer.NextToken();
if (la.kind == 52) {
lexer.NextToken();
#line 443 "VBNET.ATG"
node = new OptionCompareDeclaration(CompareType.Binary);
} else if (la.kind == 171) {
lexer.NextToken();
#line 444 "VBNET.ATG"
node = new OptionCompareDeclaration(CompareType.Text);
} else SynErr(189);
} else SynErr(190);
EndOfStmt();
#line 449 "VBNET.ATG"
node.StartLocation = startPos;
node.EndLocation = t.Location;
compilationUnit.AddChild(node);
}
void ImportsStmt() {
#line 473 "VBNET.ATG"
ArrayList importClauses = new ArrayList();
importedNamespaces = new ArrayList();
object importClause;
Expect(109);
#line 479 "VBNET.ATG"
Point startPos = t.Location;
ImportsStatement importsStatement = new ImportsStatement(null);
ImportClause(
#line 482 "VBNET.ATG"
out importClause);
#line 482 "VBNET.ATG"
importClauses.Add(importClause);
while (la.kind == 12) {
lexer.NextToken();
ImportClause(
#line 484 "VBNET.ATG"
out importClause);
#line 484 "VBNET.ATG"
importClauses.Add(importClause);
}
EndOfStmt();
#line 488 "VBNET.ATG"
importsStatement.ImportClauses = importClauses;
importsStatement.StartLocation = startPos;
importsStatement.EndLocation = t.Location;
compilationUnit.AddChild(importsStatement);
}
void GlobalAttributeSection() {
#line 1795 "VBNET.ATG"
Point startPos = t.Location;
Expect(28);
if (la.kind == 50) {
lexer.NextToken();
} else if (la.kind == 122) {
lexer.NextToken();
} else SynErr(191);
#line 1798 "VBNET.ATG"
string attributeTarget = t.val.ToLower();
ArrayList attributes = new ArrayList();
ICSharpCode.SharpRefactory.Parser.AST.VB.Attribute attribute;
Expect(13);
Attribute(
#line 1802 "VBNET.ATG"
out attribute);
#line 1802 "VBNET.ATG"
attributes.Add(attribute);
while (
#line 1803 "VBNET.ATG"
NotFinalComma()) {
Expect(12);
Attribute(
#line 1803 "VBNET.ATG"
out attribute);
#line 1803 "VBNET.ATG"
attributes.Add(attribute);
}
if (la.kind == 12) {
lexer.NextToken();
}
Expect(27);
EndOfStmt();
#line 1808 "VBNET.ATG"
AttributeSection section = new AttributeSection(attributeTarget, attributes);
section.StartLocation = startPos;
section.EndLocation = t.EndLocation;
compilationUnit.AddChild(section);
}
void NamespaceMemberDecl() {
#line 518 "VBNET.ATG"
Modifiers m = new Modifiers(this);
AttributeSection section;
ArrayList attributes = new ArrayList();
string qualident;
if (la.kind == 127) {
lexer.NextToken();
#line 525 "VBNET.ATG"
Point startPos = t.Location;
Qualident(
#line 527 "VBNET.ATG"
out qualident);
#line 529 "VBNET.ATG"
INode node = new NamespaceDeclaration(qualident);
node.StartLocation = startPos;
compilationUnit.AddChild(node);
compilationUnit.BlockStart(node);
Expect(1);
NamespaceBody();
#line 537 "VBNET.ATG"
node.EndLocation = t.Location;
compilationUnit.BlockEnd();
} else if (StartOf(2)) {
while (la.kind == 28) {
AttributeSection(
#line 541 "VBNET.ATG"
out section);
#line 541 "VBNET.ATG"
attributes.Add(section);
}
while (StartOf(3)) {
TypeModifier(
#line 542 "VBNET.ATG"
m);
}
NonModuleDeclaration(
#line 542 "VBNET.ATG"
m, attributes);
} else SynErr(192);
}
void OptionValue(
#line 455 "VBNET.ATG"
ref bool val) {
if (la.kind == 136) {
lexer.NextToken();
#line 457 "VBNET.ATG"
val = true;
} else if (la.kind == 135) {
lexer.NextToken();
#line 459 "VBNET.ATG"
val = true;
} else SynErr(193);
}
void EndOfStmt() {
if (la.kind == 1) {
lexer.NextToken();
} else if (la.kind == 13) {
lexer.NextToken();
} else SynErr(194);
}
void ImportClause(
#line 495 "VBNET.ATG"
out object importClause) {
#line 497 "VBNET.ATG"
string qualident = null;
string aliasident = null;
importClause = null;
if (
#line 501 "VBNET.ATG"
IsAssignment()) {
Identifier();
#line 501 "VBNET.ATG"
aliasident = t.val;
Expect(11);
}
Qualident(
#line 502 "VBNET.ATG"
out qualident);
#line 504 "VBNET.ATG"
if (qualident != null && qualident.Length > 0) {
if (aliasident != null) {
importClause = new ImportsAliasDeclaration(aliasident, qualident);
} else {
importedNamespaces.Add(qualident);
importClause = new ImportsDeclaration(qualident);
}
}
}
void Identifier() {
if (la.kind == 2) {
lexer.NextToken();
} else if (la.kind == 171) {
lexer.NextToken();
} else if (la.kind == 52) {
lexer.NextToken();
} else if (la.kind == 71) {
lexer.NextToken();
} else SynErr(195);
}
void Qualident(
#line 2506 "VBNET.ATG"
out string qualident) {
#line 2507 "VBNET.ATG"
string name = String.Empty;
Identifier();
#line 2508 "VBNET.ATG"
StringBuilder qualidentBuilder = new StringBuilder(t.val);
while (la.kind == 10) {
lexer.NextToken();
IdentifierOrKeyword(
#line 2510 "VBNET.ATG"
out name);
#line 2510 "VBNET.ATG"
qualidentBuilder.Append('.');
qualidentBuilder.Append(name);
}
#line 2514 "VBNET.ATG"
qualident = qualidentBuilder.ToString();
}
void NamespaceBody() {
while (StartOf(1)) {
NamespaceMemberDecl();
}
Expect(89);
Expect(127);
Expect(1);
}
void AttributeSection(
#line 1866 "VBNET.ATG"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -