📄 syntaxanalyser.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
namespace Lane.Compiler.WordAnalyser.Core
{
public class SyntaxAnalyser
{
Token[] tokens;
Token currentToken;
int nextIndex;
bool insideFunction;
List<SyntaxError> errors = new List<SyntaxError>();
List<Symbol> globalSymbols = new List<Symbol>();
List<Symbol> localSymbols = new List<Symbol>();
List<Lane.Compiler.WordAnalyser.ExpressionCalculation.Expression> express = new List<Lane.Compiler.WordAnalyser.ExpressionCalculation.Expression>();
public List<Lane.Compiler.WordAnalyser.ExpressionCalculation.Expression> Expressions
{
get { return express; }
}
public List<SyntaxError> Errors
{
get { return errors; }
}
public bool Analysis(Token[] tokens)
{
if (tokens == null || tokens.Length == 0)
return false;
this.tokens = tokens;
nextIndex = 0;
insideFunction = false;
currentToken = null;
errors.Clear();
globalSymbols.Clear();
localSymbols.Clear();
express.Clear();
Loop();
return errors.Count == 0;
}
private void Loop()
{
while (nextIndex < tokens.Length)
{
Begin();
if (errors.Count > 0)
break;
}
}
private void Begin()
{
Token t = NextToken();
switch (t.Group)
{
case GroupNames.Type:
State_Definition();
break;
case GroupNames.ID:
State_Expression();
break;
default:
AddError("Type descriptor is expected.");
break;
}
}
private void State_Expression()
{
if (!insideFunction)
{
AddError("Type descriptor is expected.");
return;
}
Token t = currentToken;
string type = GetSymbolType(t.Value);
if (type == null)
{
AddError(string.Format("{0} is not defined.", t.Value));
}
t = NextToken();
if (t.Value == "(")
{
t = NextToken();
if (t.Value != ")")
{
AddError("')' is missing.");
}
t = NextToken();
}
else if (t.Value == "=")
{
Token eqToken = t;
t = NextToken();
List<Token> exp = new List<Token>();
while (nextIndex < tokens.Length)
{
if (t.Value == ";")
break;
if (t.Group == GroupNames.ID || t.Group == GroupNames.Number || t.Group == GroupNames.Operator)
{
exp.Add(t);
}
else
{
AddError("ID, operator or number is expected in expression.");
break;
}
t = NextToken();
}
if (exp.Count == 0)
{
AddError("Value Expression is expected.");
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append(exp[0].Value);
for (int i = 1; i < exp.Count; i++)
{
sb.Append(" ");
sb.Append(exp[i].Value);
}
bool ok;
ExpressionCalculation.Expression e = null;
try
{
e = new Lane.Compiler.WordAnalyser.ExpressionCalculation.Expression(sb.ToString());
ok = e.CanCalculate();
}
catch
{
ok = false;
}
if (ok == false)
{
AddError("Expression Calculation ERROR.");
}
else
{
e.Token = eqToken;
express.Add(e);
}
}
}
else
{
AddError("Expression error.");
}
if (t.Value != ";")
AddError("';' is missing.");
}
private string GetSymbolType(string name)
{
List<Symbol>[] lists = new List<Symbol>[] { localSymbols, globalSymbols };
foreach (List<Symbol> list in lists)
{
foreach (Symbol s in list)
{
if (s.Name == name)
{
return s.Type;
}
}
}
return null;
}
private void State_Definition()
{
Token t = null;
t = currentToken;
string type = t.Value;
t = NextToken();
if (t.Group != GroupNames.ID)
{
AddError("ID symbol is expected.");
return;
}
string id = t.Value;
// Predict
t = NextToken();
if (t.Group == GroupNames.Operator)
{
//nextIndex++;
// Function Defined
if (t.Value == "(")
{
globalSymbols.Add(new Symbol(id, "Function"));
State_Definition_Function();
}
else if (t.Value == ";")
{
if (insideFunction)
{
localSymbols.Add(new Symbol(id, type));
}
else
{
globalSymbols.Add(new Symbol(id, type));
}
}
else
{
AddError("Definition is incompleted. May be ';' is missing.");
}
}
else
{
AddError("Definition is incompleted. May be ';' is missing.");
}
}
private void State_Definition_Function()
{
if (insideFunction)
{
AddError("Function define inside function is not support.");
return;
}
Token t = NextToken();
if (t.Value != ")")
{
AddError("')' is missing in function definition.");
return;
}
t = NextToken();
if (t.Value != "{")
{
AddError("'{' is missing in function definition.");
return;
}
insideFunction = true;
while (nextIndex < tokens.Length)
{
if (tokens[nextIndex].Value == "}")
{
NextToken();
break;
}
Begin();
if (errors.Count > 0)
break;
}
insideFunction = false;
}
private Token NextToken()
{
return currentToken = tokens[nextIndex++];
}
private void AddError(string message)
{
SyntaxError se = new SyntaxError(currentToken, message);
errors.Add(se);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -