Пример #1
0
 public ParseException(InputLocation location, string message, Exception inner)
     : this(FormatMessage(location, message), inner)
 {
 }
Пример #2
0
        public Token?GetNextToken()
        {
            if (Input.Peek() == -1)
            {
                return(null);
            }

            char ch = '\0';

            for (; ; index++, lastChar = ch)
            {
                if (Input.Peek() == -1)
                {
                    break;
                }

                ch = (char)Input.Read();
                column++;
                if (ch == '\n')
                {
                    line++;
                    column = 0;
                }
                bool startString = false;

                if (currentTokenType == TokenType.None)
                {
                    currentTokenType = GetNewTokenType(ch);
                    if (currentTokenType == TokenType.Unknown)
                    {
                        throw new TokenizationException(

                                  CurrentLocation,
                                  string.Format("Unknown character '{0}'", ch.ToString()));
                    }
                    newTokenChars.Clear();
                    tokenLocation = new InputLocation(line, column, index, Filename);
                    if (currentTokenType == TokenType.String)
                    {
                        startString = true;
                    }
                }


                if (IsValidChar(currentTokenType, ch))
                {
                    newTokenChars.Add(ch);
                    if (!startString && IsForceEndChar(currentTokenType, ch))
                    {
                        var value = new string(newTokenChars.ToArray());
                        var token = new Token(currentTokenType, value, tokenLocation);
                        currentTokenType = TokenType.None;
                        if (!ShouldIgnoreToken(token))
                        {
                            index++;
                            lastChar = ch;
                            return(token);
                        }
                    }
                    else
                    {
                    }
                }
                else if (IsEndingChar(currentTokenType, lastChar))
                {
                    var value = new string(newTokenChars.ToArray());
                    var token = new Token(currentTokenType, value, tokenLocation);

                    currentTokenType = GetNewTokenType(ch);
                    if (currentTokenType == TokenType.Unknown)
                    {
                        throw new TokenizationException(
                                  CurrentLocation,
                                  string.Format("Unknown character '{0}'", ch.ToString()));
                    }
                    newTokenChars.Clear();
                    newTokenChars.Add(ch);
                    tokenLocation = new InputLocation(line, column, index, Filename);
                    if (!ShouldIgnoreToken(token))
                    {
                        index++;
                        lastChar = ch;
                        return(token);
                    }
                }
                else
                {
                    throw new TokenizationException(
                              CurrentLocation,
                              string.Format("Invalid character '{0}' at index {1}", ch.ToString(), index));
                }
            }

            if (currentTokenType != TokenType.None)
            {
                if (!IsEndingChar(currentTokenType, ch))
                {
                    throw new TokenizationException(
                              CurrentLocation,
                              "Bad ending char");
                }

                var value = new string(newTokenChars.ToArray());
                var token = new Token(currentTokenType, value, tokenLocation);
                if (!ShouldIgnoreToken(token))
                {
                    return(token);
                }
            }

            return(null);
        }
Пример #3
0
 static string FormatMessage(InputLocation location, string message)
 {
     return(string.Format("at {0}:{1},{2}: {3}", location.Filename, location.Line, location.Column, message));
 }
Пример #4
0
 public TokenizationException(InputLocation location, string message)
     : this(FormatMessage(location, message))
 {
 }
Пример #5
0
 public ConversionException(InputLocation location, string message)
     : this(FormatMessage(location, message))
 {
 }