protected static Token UnexpectedTokenType(Token t) { throw new SyntaxErrorException(t, "unexpected symbol near '{0}'", t.Text) { IsPrematureStreamTermination = (t.Type == TokenType.Eof) }; }
public static double ParseNumber(Token T) { var txt = T.Text; double res; if (!double.TryParse(txt, NumberStyles.Float, CultureInfo.InvariantCulture, out res)) throw new SyntaxErrorException(T, "malformed number near '{0}'", txt); return res; }
public static double ParseHexInteger(Token T) { var txt = T.Text; if ((txt.Length < 2) || (txt[0] != '0' && (char.ToUpper(txt[1]) != 'X'))) throw new InternalErrorException("hex numbers must start with '0x' near '{0}'.", txt); ulong res; if (!ulong.TryParse(txt.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out res)) throw new SyntaxErrorException(T, "malformed number near '{0}'", txt); return res; }
public Token PeekNext() { int snapshot = m_Cursor; Token current = m_Current; int line = m_Line; int col = m_Col; Next(); Token t = Current; m_Cursor = snapshot; m_Current = current; m_Line = line; m_Col = col; return t; }
protected static Token CheckMatch(ScriptLoadingContext lcontext, Token originalToken, TokenType expectedTokenType, string expectedTokenText) { Token t = lcontext.Lexer.Current; if (t.Type != expectedTokenType) { throw new SyntaxErrorException(lcontext.Lexer.Current, "'{0}' expected (to close '{1}' at line {2}) near '{3}'", expectedTokenText, originalToken.Text, originalToken.FromLine, t.Text) { IsPrematureStreamTermination = (t.Type == TokenType.Eof) }; } lcontext.Lexer.Next(); return t; }
public Token PeekNext() { var snapshot = m_Cursor; var current = m_Current; var line = m_Line; var col = m_Col; Next(); var t = Current; m_Cursor = snapshot; m_Current = current; m_Line = line; m_Col = col; return t; }
public static double ParseHexFloat(Token T) { var s = T.Text; try { if ((s.Length < 2) || (s[0] != '0' && (char.ToUpper(s[1]) != 'X'))) throw new InternalErrorException("hex float must start with '0x' near '{0}'", s); s = s.Substring(2); var value = 0.0; int dummy, exp = 0; s = ReadHexProgressive(s, ref value, out dummy); if (s.Length > 0 && s[0] == '.') { s = s.Substring(1); s = ReadHexProgressive(s, ref value, out exp); } exp *= -4; if (s.Length > 0 && char.ToUpper(s[0]) == 'P') { if (s.Length == 1) throw new SyntaxErrorException(T, "invalid hex float format near '{0}'", s); s = s.Substring(s[1] == '+' ? 2 : 1); var exp1 = int.Parse(s, CultureInfo.InvariantCulture); exp += exp1; } var result = value*Math.Pow(2, exp); return result; } catch (FormatException) { throw new SyntaxErrorException(T, "malformed number near '{0}'", s); } }
public static string UnescapeLuaString(Token token, string str) { if (!str.Contains('\\')) return str; var sb = new StringBuilder(); var escape = false; var hex = false; var unicode_state = 0; var hexprefix = ""; var val = ""; var zmode = false; foreach (var c in str) { redo: if (escape) { if (val.Length == 0 && !hex && unicode_state == 0) { if (c == 'a') { sb.Append('\a'); escape = false; zmode = false; } else if (c == '\r') { } // this makes \\r\n -> \\n else if (c == '\n') { sb.Append('\n'); escape = false; } else if (c == 'b') { sb.Append('\b'); escape = false; } else if (c == 'f') { sb.Append('\f'); escape = false; } else if (c == 'n') { sb.Append('\n'); escape = false; } else if (c == 'r') { sb.Append('\r'); escape = false; } else if (c == 't') { sb.Append('\t'); escape = false; } else if (c == 'v') { sb.Append('\v'); escape = false; } else if (c == '\\') { sb.Append('\\'); escape = false; zmode = false; } else if (c == '"') { sb.Append('\"'); escape = false; zmode = false; } else if (c == '\'') { sb.Append('\''); escape = false; zmode = false; } else if (c == '[') { sb.Append('['); escape = false; zmode = false; } else if (c == ']') { sb.Append(']'); escape = false; zmode = false; } else if (c == 'x') { hex = true; } else if (c == 'u') { unicode_state = 1; } else if (c == 'z') { zmode = true; escape = false; } else if (char.IsDigit(c)) { val = val + c; } else throw new SyntaxErrorException(token, "invalid escape sequence near '\\{0}'", c); } else { if (unicode_state == 1) { if (c != '{') throw new SyntaxErrorException(token, "'{' expected near '\\u'"); unicode_state = 2; } else if (unicode_state == 2) { if (c == '}') { var i = int.Parse(val, NumberStyles.HexNumber, CultureInfo.InvariantCulture); sb.Append(ConvertUtf32ToChar(i)); unicode_state = 0; val = string.Empty; escape = false; } else if (val.Length >= 8) { throw new SyntaxErrorException(token, "'}' missing, or unicode code point too large after '\\u'"); } else { val += c; } } else if (hex) { if (CharIsHexDigit(c)) { val += c; if (val.Length == 2) { var i = int.Parse(val, NumberStyles.HexNumber, CultureInfo.InvariantCulture); sb.Append(ConvertUtf32ToChar(i)); zmode = false; escape = false; } } else { throw new SyntaxErrorException(token, "hexadecimal digit expected near '\\{0}{1}{2}'", hexprefix, val, c); } } else if (val.Length > 0) { if (char.IsDigit(c)) { val = val + c; } if (val.Length == 3 || !char.IsDigit(c)) { var i = int.Parse(val, CultureInfo.InvariantCulture); if (i > 255) throw new SyntaxErrorException(token, "decimal escape too large near '\\{0}'", val); sb.Append(ConvertUtf32ToChar(i)); zmode = false; escape = false; if (!char.IsDigit(c)) goto redo; } } } } else { if (c == '\\') { escape = true; hex = false; val = ""; } else { if (!zmode || !char.IsWhiteSpace(c)) { sb.Append(c); zmode = false; } } } } if (escape && !hex && val.Length > 0) { var i = int.Parse(val, CultureInfo.InvariantCulture); sb.Append(ConvertUtf32ToChar(i)); escape = false; } if (escape) { throw new SyntaxErrorException(token, "unfinished string near '\"{0}\"'", sb.ToString()); } return sb.ToString(); }
private Token CreateToken(TokenType tokenType, int fromLine, int fromCol, string text = null) { Token t = new Token(tokenType, m_SourceId, fromLine, fromCol, m_Line, m_Col, m_PrevLineTo, m_PrevColTo) { Text = text }; m_PrevLineTo = m_Line; m_PrevColTo = m_Col; return t; }
public void Next() { m_Current = FetchNewToken(); }
internal Debugging.SourceRef GetSourceRefUpTo(Token to, bool isStepStop = true) { return new Debugging.SourceRef(this.SourceId, this.FromCol, to.PrevCol, this.FromLine, to.PrevLine, isStepStop); }
internal SyntaxErrorException(Token t, string message) : base(message) { Token = t; }
internal SyntaxErrorException(Token t, string format, params object[] args) : base(format, args) { Token = t; }
internal SourceRef GetSourceRefUpTo(Token to, bool isStepStop = true) { return new SourceRef(SourceId, FromCol, to.PrevCol, FromLine, to.PrevLine, isStepStop); }