📄 token.cs
字号:
{
tempOperator += '='; // adjust the current token
charIndex++; // cause the '=' to be skipped on the next iteration
}
}
}
catch (Exception err)
{
IsError = true;
ErrorMsg = "Error while determining if the next character is <, location = 2, Error message = " + err.Message;
return false;
}
break;
}
// create the operator
tokenItems.Add(new TokenItem(tempOperator, TokenType.Token_Operator, InOperandFunction));
// we added an operator, so our next parse state should be operand
NextParseState = ParseState.Parse_State_Operand;
}
else if (Support.DataTypeCheck.ContainsOperator(CurrentToken, out tempOperand, out tempOperator) == true)
{
// our current token contains an operand and a operator
// make sure our operand is not a reserved word
if (Support.DataTypeCheck.IsReservedWord(tempOperand) == true)
{
IsError = true;
ErrorMsg = "The operand \"" + tempOperand + "\" is a reserved word.";
return false;
}
// validate and add the new operand
if (Support.DataTypeCheck.IsInteger(tempOperand) == true)
tokenItems.Add(new TokenItem(tempOperand, TokenType.Token_Operand, TokenDataType.Token_DataType_Int, InOperandFunction));
else if (Support.DataTypeCheck.IsDouble(tempOperand) == true)
tokenItems.Add(new TokenItem(tempOperand, TokenType.Token_Operand, TokenDataType.Token_DataType_Double, InOperandFunction));
else if (Support.DataTypeCheck.IsDate(tempOperand) == true)
tokenItems.Add(new TokenItem(tempOperand, TokenType.Token_Operand, TokenDataType.Token_DataType_Date, InOperandFunction));
else if (Support.DataTypeCheck.IsBoolean(tempOperand) == true)
tokenItems.Add(new TokenItem(tempOperand, TokenType.Token_Operand, TokenDataType.Token_DataType_Boolean, InOperandFunction));
else if (Support.DataTypeCheck.IsNULL(tempOperand) == true)
tokenItems.Add(new TokenItem(tempOperand, TokenType.Token_Operand, TokenDataType.Token_DataType_NULL, InOperandFunction));
else
{
// add the token as a variable
tokenItems.Add(new TokenItem(tempOperand, TokenType.Token_Operand, TokenDataType.Token_DataType_Variable, InOperandFunction));
variables.Add(tempOperand);
}
// add the operator, but check the next character first
// the problem is, the next character in the token
// may also make an operator. for example, if the
// current token is < we need to see if the next token is =
// clean the token before we create the operator
tempOperator = tempOperator.Trim();
switch (tempOperator.Trim().ToLower())
{
case "<":
try
{
// see if the next character is = or >
if (charIndex < ruleSyntax.Length - 1)
{
char nextChar = ruleSyntax[charIndex];//charIndex has already been incremented
if (nextChar == '=')
{
tempOperator += '='; // adjust the current token
charIndex++; // cause the '=' to be skipped on the next iteration
}
else if (nextChar == '>')
{
tempOperator += '>'; // adjust the current token
charIndex++; // cause the '>' to be skipped on the next iteration
}
}
}
catch (Exception err)
{
IsError = true;
ErrorMsg = "Error while determining if the next character is <, location = 3, Error message = " + err.Message;
return false;
}
break;
case ">":
try
{
// see if the next character is =
if (charIndex < ruleSyntax.Length - 1)
{
char nextChar = ruleSyntax[charIndex];//charIndex has already been incremented
if (nextChar == '=')
{
tempOperator += '='; // adjust the current token
charIndex++; // cause the '=' to be skipped on the next iteration
}
}
}
catch (Exception err)
{
IsError = true;
ErrorMsg = "Error while determining if the next character is >, location = 4, Error message = " + err.Message;
return false;
}
break;
}
// create the operator
tokenItems.Add(new TokenItem(tempOperator, TokenType.Token_Operator, InOperandFunction));
// we added an operand and a operator...our next parse state should be operand
NextParseState = ParseState.Parse_State_Operand;
//}
}
else if (WaitForCreation == false)
{
// make sure our operand is not a reserved word
if (Support.DataTypeCheck.IsReservedWord(CurrentToken) == true)
{
IsError = true;
ErrorMsg = "The operand \"" + CurrentToken + "\" is a reserved word.";
return false;
}
// create a new operand
if (Support.DataTypeCheck.IsInteger(CurrentToken) == true)
tokenItems.Add(new TokenItem(CurrentToken, TokenType.Token_Operand, TokenDataType.Token_DataType_Int, InOperandFunction));
else if (Support.DataTypeCheck.IsDouble(CurrentToken) == true)
tokenItems.Add(new TokenItem(CurrentToken, TokenType.Token_Operand, TokenDataType.Token_DataType_Double, InOperandFunction));
else if (Support.DataTypeCheck.IsDate(CurrentToken) == true)
tokenItems.Add(new TokenItem(CurrentToken, TokenType.Token_Operand, TokenDataType.Token_DataType_Date, InOperandFunction));
else if (Support.DataTypeCheck.IsBoolean(CurrentToken) == true)
tokenItems.Add(new TokenItem(CurrentToken, TokenType.Token_Operand, TokenDataType.Token_DataType_Boolean, InOperandFunction));
else if (Support.DataTypeCheck.IsNULL(CurrentToken) == true)
tokenItems.Add(new TokenItem(CurrentToken, TokenType.Token_Operand, TokenDataType.Token_DataType_NULL, InOperandFunction));
else if (Support.DataTypeCheck.IsText(CurrentToken) == true)
tokenItems.Add(new TokenItem(CurrentToken, TokenType.Token_Operand, TokenDataType.Token_DataType_String, InOperandFunction));
else
{
// add the token as a variable
tokenItems.Add(new TokenItem(CurrentToken, TokenType.Token_Operand, TokenDataType.Token_DataType_Variable, InOperandFunction));
variables.Add(CurrentToken);
}
// we added an operand, our next parse state should be operator
NextParseState = ParseState.Parse_State_Operator;
}
else
{
return false;
}
return true;
}
/// <summary>
/// Check if we found the assignment token
/// </summary>
/// <returns></returns>
private bool FoundAssignment()
{
try
{
// see if the next character is =
if (charIndex < ruleSyntax.Length - 1)
{
char nextChar = ruleSyntax[charIndex];//charIndex has already been incremented
return (nextChar == '=');
}
else
return false;
}
catch (Exception err)
{
return false;
}
}
/// <summary>
/// This is called by the constructor.
/// </summary>
private void GetTokens_Old_1()
{
// local variables
ParseState parseState = ParseState.Parse_State_Operand; // start be searching for an operand
ParseState nextParseState = ParseState.Parse_State_Comment; // the next parse state after we have processed a comment
ParseState opFuncParseState = ParseState.Parse_State_Operand; // The parse state within the operand function
ParseState tempParseState = ParseState.Parse_State_Operand; // temporary variable to hold a parse state
string currentToken = "";
int parenthesisMatch = 0; // the number of open and close parenthesis should match
int squareMatch = 0; // the number of open and close square parenthesis should match
int tildaMatch = 0; // the open and close tildas should match
int quoteMatch = 0; // the open and close quotes should match
bool isError = false;
bool tokenCreated = false;
int operandFunctionDepth = 0; // operand function can contain operand function...This is the depth of the operand functions
// make sure we have some syntax to parse
if (String.IsNullOrEmpty(ruleSyntax) == true) return;
// create a stop watch to time the parse
System.Diagnostics.Stopwatch parseTime = System.Diagnostics.Stopwatch.StartNew();
// start at the first character
charIndex = 0;
do
{
// double check the exit condition, just in case
if (charIndex >= ruleSyntax.Length) break;
// the character to be processed
char c = ruleSyntax[charIndex];
// Increment the counter for the next character
charIndex++;
System.Diagnostics.Debug.WriteLine("c = " + c.ToString() + "\tcurrentToken = " + currentToken + "\tparse state = " + parseState.ToString() + "\tOp Parse State = " + opFuncParseState.ToString());
#region New Line and Tab check
// check for new line and tab
if ((c == '\n') || (c == '\t'))
{
// new lines and tabs are always ignored.
c = ' '; // force the new line or tab to be a space and continue processing.
}
#endregion
#region Comment Checking
// check if we are in a comment
if (parseState == ParseState.Parse_State_Comment)
{
if (c == '~')
{
// we found the end of our comment, continue with what we were looking for
parseState = nextParseState;
tildaMatch--;
}
// continue with the next item in the loop
continue;
}
else
{
if (c == '~')
{
// we found the start of a comment
tildaMatch++;
nextParseState = parseState; // save the parse state
parseState = ParseState.Parse_State_Comment;
continue; // continue with the next item in the loop
}
}
#endregion
// determine out current parse state
switch (parseState)
{
case ParseState.Parse_State_Operand:
#region Parse_State_Operand
if (c == '"')
{
#region Quote Handling
try
{
// we have a quote in an operand...This is probably the start of a string operand
quoteMatch++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -