Esempio n. 1
0
		public static int ParseNumber(Token token, string value, int radix)
		{
			value = value.ToLowerInvariant();
			int output = 0;
			for (int i = 0; i < value.Length; ++i)
			{
				int digitValue = NUMBER_DIGITS.IndexOf(value[i]);
				if (digitValue == -1)
				{
					throw new ParserException(token, "Invalid base " + radix + " constant.");
				}
				output = output * radix + digitValue;
			}
			return output;
		}
Esempio n. 2
0
		public static double ParseDouble(Token token)
		{
			string[] parts = token.Value.ToLowerInvariant().Split('e');
			if (parts.Length > 2) throw new ParserException(token, "Invalid float constant.");
			string value = parts[0];
			string exponent = parts.Length == 1 ? "1" : parts[1];

			double rootValue;
			if (!double.TryParse(value, out rootValue)) throw new ParserException(token, "Invalid float constant.");
			int exponentValue;
			if (!int.TryParse(exponent, out exponentValue)) throw new ParserException(token, "Invalid float constant.");

			if (rootValue == 0 && exponentValue == 0) return 0.0;

			return System.Math.Pow(rootValue, exponentValue);
		}
Esempio n. 3
0
		public static bool IsIdentifier(Token token)
		{
			string value = token.Value;
			for (int i = value.Length - 1; i >= 0; --i)
			{
				char c = value[i];
				if (i == 0)
				{
					if (c >= '0' && c <= '9') return false;
				}
				if (!IDENTIFIER_CHARS.Contains(c))
				{
					return false;
				}
			}
			return true;
		}
Esempio n. 4
0
		private static string GetTokenInfo(Token token)
		{
			if (token == null) return "[No token info] ";
			return token.File + ", Line " + (token.Line + 1) + ", Col " + (token.Col + 1) + " ";
		}
Esempio n. 5
0
		public ParserException(Token token, string message) : base(GetTokenInfo(token) + message)
		{

		}
Esempio n. 6
0
		public static string RemoveEscapeSequences(Token token, string value)
		{
			StringBuilder sb = new StringBuilder();
			char c;
			for (int i = 0; i < value.Length; ++i)
			{
				c = value[i];
				if (c == '\\')
				{
					if (i + 1 < value.Length)
					{
						switch (value[i + 1])
						{
							case 'n': c = '\n'; break;
							case 'r': c = '\r'; break;
							case 't': c = '\t'; break;
							case '\\': c = '\\'; break;
							case '0': c = '\0'; break;
							case '"': c = '"'; break;
							case '\'': c = '\''; break;
							default: throw new ParserException(token, "Invalid escape sequence in string.");
						}
						++i;
					}
					else
					{
						throw new ParserException(token, "Backslash at the end.");
					}
				}

				sb.Append(c);
			}
			return sb.ToString();
		}