private static bool IsToken(Token token, string value, TokenType type) { if (token == null) return false; if (token.TokenType != type) return false; return token.Value.Equals(value); }
public void PushTwoToken2() { Lexer lexer = new Lexer(string.Empty); Token token = new Token() { TokenType = TokenType.Name, Value = "name" }; Token token2 = new Token() { TokenType = TokenType.Name, Value = "name2" }; lexer.PushToken(token); lexer.PushToken(token2); Assert.AreEqual(token2, lexer.NextToken()); Assert.AreEqual(token, lexer.NextToken()); }
private static bool IsName(Token token, string value) { return IsToken(token, value, TokenType.Name); }
private Token NextReal(string integerPart) { string real = integerPart + "."; char ch; try { ch = this.NextChar(); while (char.IsDigit(ch)) { real += ch; ch = this.NextChar(); } this.PushChar(ch); } catch (EndOfInputException) { } Token token = new Token(); token.Value = real; token.TokenType = TokenType.Real; return token; }
private Token NextString() { StringBuilder sb = new StringBuilder(); char ch; char lastChar = (char)0; ch = this.NextChar(); while (ch != StringChar || lastChar == '\\') { if (lastChar == '\\') { switch (ch) { case 't': sb.Length--; sb.Append('\t'); break; case 'a': sb.Length--; sb.Append('\a'); break; case 'b': sb.Length--; sb.Append('\b'); break; case 'e': sb.Length--; sb.Append((char)27); break; case 'f': sb.Length--; sb.Append('\f'); break; case 'n': sb.Length--; sb.Append('\n'); break; case 'r': sb.Length--; sb.Append('\r'); break; case 'v': sb.Length--; sb.Append('\v'); break; case '\\': break; default: sb.Length--; sb.Append(ch); break; } lastChar = (char)0; } else { sb.Append(ch); lastChar = ch; } ch = this.NextChar(); } Token token = new Token(); token.Value = sb.ToString(); token.TokenType = TokenType.String; return token; }
private Token NextName(char ch) { string name = ch.ToString(); try { ch = this.NextChar(); while (char.IsLetterOrDigit(ch) || ch == '_') { name += ch; ch = this.NextChar(); } this.PushChar(ch); } catch (EndOfInputException) { } Token token = new Token(); token.Value = name; token.TokenType = TokenType.Name; if (name == "true" || name == "false") token.TokenType = TokenType.Boolean; if (name == "null" || name == "undefined") token.TokenType = TokenType.Object; return token; }
private Token NextQuotedString() { StringBuilder sb = new StringBuilder(); char ch; char lastChar = (char)0; ch = this.NextChar(); while (ch != QuotedStringChar) { sb.Append(ch); lastChar = ch; ch = this.NextChar(); } Token token = new Token(); token.Value = sb.ToString(); token.TokenType = TokenType.String; return token; }
public void PushToken(Token token) { if (this.tokens == null) this.tokens = new Stack<Token>(); this.tokens.Push(token); }
private Token NextInteger(char ch) { string integer = ch.ToString(); try { ch = this.NextChar(); while (char.IsDigit(ch)) { integer += ch; ch = this.NextChar(); } if (ch == '.') { return this.NextReal(integer); } this.PushChar(ch); } catch (EndOfInputException) { } Token token = new Token(); token.Value = integer; token.TokenType = TokenType.Integer; return token; }
public void PushToken() { Lexer lexer = new Lexer(""); Token token = new Token() { TokenType = TokenType.Name, Value = "name" }; lexer.PushToken(token); Assert.AreEqual(token, lexer.NextToken()); }
public UnexpectedTokenException(Token token) : base(string.Format(CultureInfo.CurrentCulture, "Unexpected '{0}'", token.Value)) { }
private Token NextReal(string integerPart) { string real = integerPart + "."; char? nch; nch = this.NextChar(); while (nch.HasValue && char.IsDigit(nch.Value)) { real += nch; nch = this.NextChar(); } if (nch.HasValue) this.PushChar(nch.Value); Token token; if (real.EndsWith(".")) { this.PushChar('.'); token = new Token(); token.Value = integerPart; token.TokenType = TokenType.Integer; return token; } token = new Token(); token.Value = real; token.TokenType = TokenType.Real; return token; }
private Token NextName(char? nch) { string name = nch.ToString(); nch = this.NextChar(); while (nch.HasValue && (char.IsLetterOrDigit(nch.Value) || nch.Value == '_')) { name += nch.Value; nch = this.NextChar(); } if (nch.HasValue) this.PushChar(nch.Value); Token token = new Token(); token.Value = name; token.TokenType = TokenType.Name; return token; }
private Token NextInteger(char? nch) { string integer = nch.ToString(); nch = this.NextChar(); while (nch.HasValue && char.IsDigit(nch.Value)) { integer += nch; nch = this.NextChar(); } if (nch == '.') { return this.NextReal(integer); } if (nch.HasValue) this.PushChar(nch.Value); Token token = new Token(); token.Value = integer; token.TokenType = TokenType.Integer; return token; }