//проверка типа лексемы private void CheckLexType(Lex lex, LexTypes type) { if (lex.Type != type) { throw new Exception(SyntaxisErrorMessage(lex, type)); } }
public static string LexTypesToString(LexTypes lexTypes) { if (lexTypes < (LexTypes)256 && lexTypes > 0) { return(string.Format("{0}", (char)lexTypes)); } return(lexTypes.ToString()); }
// Получем следующую лексему. private void GetToken() { tokType = LexTypes.NONE; token = ""; // Конец выражения. Опускаем пробел if (expIdx == exp.Length) { return; } while (expIdx < exp.Length && char.IsWhiteSpace(exp[expIdx])) { ++expIdx; } // Хвостовой пробел завершает выражение. if (expIdx == exp.Length) { return; } if (IsDelim(exp[expIdx])) { token += exp[expIdx]; expIdx++; tokType = LexTypes.DELIMITER; } else if (char.IsDigit(exp[expIdx])) { // Это число? while (!IsDelim(exp[expIdx])) { token += exp[expIdx]; expIdx++; if (expIdx >= exp.Length) { break; } } tokType = LexTypes.NUMBER; } }
public void Match(LexTypes type) { if (TokenType != type) { var expectedName = Enum.GetName(typeof(LexTypes), type); if (string.IsNullOrEmpty(expectedName)) { expectedName = string.Format("{0}", ((char)type)); } var foundName = Enum.GetName(typeof(LexTypes), TokenType); if (string.IsNullOrEmpty(foundName)) { foundName = string.Format("{0}", (char)TokenType); } throw new ScriptException(string.Format("Unexpected token type. Expected {0}, found {1}. Line: {2}, Col: {3}", expectedName, foundName, LineNumber, ColumnNumber)); } GetNextToken(); }
public void Match(LexTypes type) { if (TokenType != type) { String expectedName = Enum.GetName(typeof(LexTypes), type); if (String.IsNullOrEmpty(expectedName)) { expectedName = String.Format("{0}", ((char)type)); } String foundName = Enum.GetName(typeof(LexTypes), TokenType); if (String.IsNullOrEmpty(foundName)) { foundName = String.Format("{0}", ((char)TokenType)); } throw new ScriptException(String.Format("Unexpected token type. Expected {0}, found {1}", expectedName, foundName)); } GetNextToken(); }
//построение сообщения о ошибке private string SyntaxisErrorMessage(Lex lex, LexTypes expect) { return($"Syntaxis error in line {lex.LineNo}.\n" + "Expected " + expect.ToString() + ", but get " + lex.Type.ToString() + "."); }