Exemple #1
0
 /// <summary>
 /// Find the next token
 /// </summary>
 /// <returns>The type of the next token</returns>
 /// <remarks>Sets tokenSpelling to be the characters in the token</remarks>
 private TokenType ScanToken()
 {
     TokenSpelling.Clear();
     if (char.IsLetter(Reader.Current))
     {
         // Identifiers and Keywords
         TakeIt();
         while (char.IsLetter(Reader.Current))
         {
             TakeIt();
         }
         if (TokenTypes.IsKeyword(TokenSpelling))
         {
             return(TokenTypes.GetTokenForKeyword(TokenSpelling));
         }
         else
         {
             return(TokenType.Identifier);
         }
     }
     else if (char.IsDigit(Reader.Current))
     {
         // Integer Literals
         TakeIt();
         while (char.IsDigit(Reader.Current))
         {
             TakeIt();
         }
         return(TokenType.IntLiteral);
     }
     else if (Reader.Current == ':')
     {
         // Handle ":" and ":="
         TakeIt();
         if (Reader.Current == '=')
         {
             TakeIt();
             return(TokenType.Becomes);
         }
         return(TokenType.Colon);
     }
     else if (Reader.Current == ';')
     {
         // ";"
         TakeIt();
         return(TokenType.Semicolon);
     }
     else if (Reader.Current == '(')
     {
         // "("
         TakeIt();
         return(TokenType.LeftBracket);
     }
     else if (Reader.Current == ')')
     {
         // ")"
         TakeIt();
         return(TokenType.RightBracket);
     }
     else if (Reader.Current == '~')
     {
         // "Is"
         TakeIt();
         return(TokenType.Is);
     }
     else if (Reader.Current == '\'')
     {
         // Charcter Literal
         TakeIt();
         TakeIt();
         if (Reader.Current == '\'')
         {
             TakeIt();
             return(TokenType.CharLiteral);
         }
         return(TokenType.Error);
     }
     else if (IsOperator(Reader.Current))
     {
         // Operators
         TakeIt();
         return(TokenType.Operator);
     }
     else if (Reader.Current == default(char))
     {
         // Read the end of the file
         TakeIt();
         return(TokenType.EndOfText);
     }
     else
     {
         // Encountered a character we weren't expecting
         TakeIt();
         return(TokenType.Error);
     }
 }
 /// <summary>
 /// Find the next token
 /// </summary>
 /// <returns>The type of the next token</returns>
 /// <remarks>Sets tokenSpelling to be the characters in the token</remarks>
 private TokenType ScanToken()
 {
     TokenSpelling.Clear();
     if (char.IsLetter(Reader.Current))
     {
         // Reading an identifier
         TakeIt(); // takes the charcter
         while (isLowerLetterDigit(Reader.Current))
         {
             TakeIt();                                             //keep taking it as long as its letter or a digit
         }
         if (TokenTypes.IsKeyword(TokenSpelling))                  // if it is a keyword...
         {
             return(TokenTypes.GetTokenForKeyword(TokenSpelling)); // return word
         }
         else if (TokenSpelling.ToString().Any(char.IsUpper))
         {
             return(TokenType.Error);
         }
         else
         {
             return(TokenType.Identifier); // if not, retun idenitfer
         }
     }
     else if (Reader.Current == '_')
     {
         TakeIt(); // take the _
         while (isLowerLetter(Reader.Current))
         {         // if the next bit is a leter keep taking it
             TakeIt();
         }
         // once it finds a non letter item...
         return(TokenType.Identifier);
     }
     else if (Reader.Current == '’')
     {
         TakeIt();
         // do while is lower letter or a figit or a punctuation mark or white space
         while (isLowerLetterDigit(Reader.Current) || IsPunctuation(Reader.Current) || IsWhiteSpace(Reader.Current)) // == graphic
         {
             TakeIt();
         }
         if (Reader.Current == '’')         //and ends with ' it is a character literal
         {
             return(TokenType.CharLiteral); //it is a character literal
         }
         else
         {
             return(TokenType.Error);
         }
     }
     else if (char.IsDigit(Reader.Current))
     {
         // Reading an integer
         TakeIt();
         while (char.IsDigit(Reader.Current))
         {
             TakeIt();
         }
         return(TokenType.IntLiteral);
     }
     else if (IsOperator(Reader.Current))
     {
         // Read an operator
         TakeIt();
         // if the next this is an = sign then take it to before delaring it as an oeprator
         if (Reader.Current == '=')
         {
             TakeIt();
         }
         return(TokenType.Operator);
     }
     else if (Reader.Current == ':')
     {
         // Read an :
         // Is it a : or a :=
         TakeIt();
         if (Reader.Current == '=')
         {
             TakeIt();
             return(TokenType.Becomes);
         }
         else
         {
             return(TokenType.Colon);
         }
     }
     else if (Reader.Current == ';')
     {
         // Read a ;
         TakeIt();
         return(TokenType.Semicolon);
     }
     else if (Reader.Current == '~')
     {
         // Read a ~
         TakeIt();
         return(TokenType.Is);
     }
     else if (Reader.Current == '(')
     {
         // Read a (
         TakeIt();
         return(TokenType.LeftBracket);
     }
     else if (Reader.Current == ')')
     {
         // Read a )
         TakeIt();
         return(TokenType.RightBracket);
     }
     else if (Reader.Current == '\'')
     {
         // Read a '
         TakeIt();
         // Take whatever the character is
         TakeIt();
         // Try getting the closing '
         if (Reader.Current == '\'')
         {
             TakeIt();
             return(TokenType.CharLiteral);
         }
         else
         {
             // Could do some better error handling here but we weren't asked to
             return(TokenType.Error);
         }
     }
     else if (Reader.Current == default(char))
     {
         // Read the end of the file
         TakeIt();
         return(TokenType.EndOfText);
     }
     else
     {
         // Encountered a character we weren't expecting
         TakeIt();
         return(TokenType.Error);
     }
 }
Exemple #3
0
 /// <summary>
 /// Find the next token
 /// </summary>
 /// <returns>The type of the next token</returns>
 /// <remarks>Sets tokenSpelling to be the characters in the token</remarks>
 private TokenType ScanToken()
 {
     TokenSpelling.Clear();
     if (char.IsLetter(Reader.Current))
     {
         // Reading an identifier
         TakeIt();
         while (char.IsLetterOrDigit(Reader.Current))
         {
             TakeIt();
         }
         if (TokenTypes.IsKeyword(TokenSpelling))
         {
             return(TokenTypes.GetTokenForKeyword(TokenSpelling));
         }
         else
         {
             return(TokenType.Identifier);
         }
     }
     else if (char.IsDigit(Reader.Current))
     {
         // Reading an integer
         TakeIt();
         while (char.IsDigit(Reader.Current))
         {
             TakeIt();
         }
         return(TokenType.IntLiteral);
     }
     else if (IsOperator(Reader.Current))
     {
         // Read an operator
         TakeIt();
         return(TokenType.Operator);
     }
     else if (Reader.Current == ':')
     {
         // Read an :
         // Is it a : or a :=
         TakeIt();
         if (Reader.Current == '=')
         {
             TakeIt();
             return(TokenType.Becomes);
         }
         else
         {
             return(TokenType.Colon);
         }
     }
     else if (Reader.Current == ';')
     {
         // Read a ;
         TakeIt();
         return(TokenType.Semicolon);
     }
     else if (Reader.Current == '~')
     {
         // Read a ~
         TakeIt();
         return(TokenType.Is);
     }
     else if (Reader.Current == '(')
     {
         // Read a (
         TakeIt();
         return(TokenType.LeftBracket);
     }
     else if (Reader.Current == ')')
     {
         // Read a )
         TakeIt();
         return(TokenType.RightBracket);
     }
     else if (Reader.Current == '\'')
     {
         // Read a '
         TakeIt();
         // Take whatever the character is
         TakeIt();
         // Try getting the closing '
         if (Reader.Current == '\'')
         {
             TakeIt();
             return(TokenType.CharLiteral);
         }
         else
         {
             // Could do some better error handling here but we weren't asked to
             return(TokenType.Error);
         }
     }
     else if (Reader.Current == default(char))
     {
         // Read the end of the file
         TakeIt();
         return(TokenType.EndOfText);
     }
     else
     {
         // Encountered a character we weren't expecting
         TakeIt();
         return(TokenType.Error);
     }
 }