private void CommonStartup() { keywords = new HashSet <string> { "if", "while" }; escapeLookup = new Dictionary <char, char> { { 'n', '\n' }, { 'r', '\r' }, { 't', '\t' }, { '\\', '\\' }, { '"', '"' } }; lineNumber = currentLineNumber = 1; positionNumber = currentLexedPosition = 0; // cause we gonna call NextChar one time first. isEOF = false; currentValue = new StringBuilder(string.Empty); StateFunction = RootState; hasDecimalPlace = false; // we begin by reading in the first character NextChar(); }
private bool StringState(char c, out LexedToken token) { token = null; if (c == '\\') { // we gonna get the escaped character from the next state StateFunction = EscapeStringState; // consume this slash return(true); } else if (c == '"') { // return the built string token = new LexedToken(LexedTokenType.String, currentValue.ToString()); // consume this closing quotes return(true); } // build up the string currentValue.Append(c); return(true); }
private bool EscapeStringState(char c, out LexedToken token) { token = null; if (escapeLookup.ContainsKey(c)) { // we support this escape character currentValue.Append(escapeLookup[c]); // set the state back to string builder StateFunction = StringState; // consume this character return(true); } else { // return an invalid token and consume this MarkPosition(); token = new LexedToken(LexedTokenType.Invalid, "\\" + c.ToString()); return(true); } }
/// <summary> /// The initial state /// </summary> private bool RootState(char c, out LexedToken token) { token = null; if (char.IsWhiteSpace(c)) // skip whitespaces { return(true); } if (char.IsLetter(c) || c == '_') { MarkPosition(); // we bumped into a letter currentValue.Append(c); // so we change to an appropriate state StateFunction = LetterState; // consume this character return(true); } else if (char.IsDigit(c)) { MarkPosition(); // we got a first digit, let's not consume it first and let the // state function do that StateFunction = NumberState; return(false); } else if (c == '"') { MarkPosition(); // the start of a string StateFunction = StringState; // consume this character return(true); } else if (c == '+') { token = SingularToken(LexedTokenType.OperatorPlus, c); return(true); } else if (c == '-') { token = SingularToken(LexedTokenType.OperatorMinus, c); return(true); } else if (c == '*') { token = SingularToken(LexedTokenType.OperatorMultiply, c); return(true); } else if (c == '/') { token = SingularToken(LexedTokenType.OperatorDivide, c); return(true); } else if (c == '(') { token = SingularToken(LexedTokenType.OpenParen, c); return(true); } else if (c == ')') { token = SingularToken(LexedTokenType.CloseParen, c); return(true); } else if (c == '{') { token = SingularToken(LexedTokenType.OpenBraces, c); return(true); } else if (c == '}') { token = SingularToken(LexedTokenType.CloseBraces, c); return(true); } else if (c == ';') { token = SingularToken(LexedTokenType.Semicolon, c); return(true); } else if (c == '=') { MarkPosition(); // TODO: Transition to a state to get the == currentValue.Append(c); token = new LexedToken(LexedTokenType.OperatorEqual, currentValue.ToString()); return(true); } MarkPosition(); currentValue.Append(c); token = new LexedToken(LexedTokenType.Invalid, currentValue.ToString()); return(true); }
private void ClearState() { currentValue.Clear(); StateFunction = RootState; hasDecimalPlace = false; }