/// <summary> /// Get the identifier type of token /// </summary> private Token GetIdentifierToken() { Token token = new Token(); do { token.Value += Char.ToUpper(GetSourceChar()); SourceIndex++; } while (Char.IsLetterOrDigit(GetSourceChar()) || GetSourceChar().Equals('_')); token.Type = TokenType.Identifier; return token; }
/// <summary> /// Gets the number type of token /// </summary> private Token GetNumberToken() { Token token = new Token(); do { token.Value += GetSourceChar(); SourceIndex++; } while (Char.IsDigit(GetSourceChar())); token.Type = TokenType.Number; return token; }
/// <summary> /// Reads the next token from the code /// </summary> private Token ReadNextToken() { //Skip redundant data SkipWhitespace(); Token nextToken = null; if (GetSourceChar().Equals('\n')) { CurrentLine++; nextToken = new Token(); nextToken.Type = TokenType.EOL; nextToken.Line = CurrentLine - 1; SourceIndex++; return nextToken; } else if (Char.IsLetter(GetSourceChar())) { nextToken = GetIdentifierToken(); } else if (Char.IsDigit(GetSourceChar())) { nextToken = GetNumberToken(); } else if (GetSourceChar().Equals('\"')) { nextToken = GetStringToken(); } else if ("(){}.,;*&[]<>=!+-/?".Contains(GetSourceChar())) { nextToken = GetSymbolToken(); } else { Console.WriteLine("Error - undefined token"); } nextToken.Line = CurrentLine; return nextToken; }
/// <summary> /// Gets the symbol type of token /// </summary> private Token GetSymbolToken() { Token token = new Token(); token.Value += GetSourceChar(); //In case of <=, >=, == or != we have two characters as token if ((GetSourceChar().Equals('<') || GetSourceChar().Equals('>') || GetSourceChar().Equals('!') || GetSourceChar().Equals('=')) && GetNextSourceChar().Equals('=')) { SourceIndex++; token.Value += GetSourceChar(); } SourceIndex++; token.Type = TokenType.Symbol; return token; }
/// <summary> /// Gets the string type of token /// </summary> private Token GetStringToken() { //Skip the first quote mark SourceIndex++; Token token = new Token(); do { //Can't EOL in middle of string if (GetSourceChar().Equals('\n')) Console.WriteLine("Error - expected ending quotation mark"); token.Value += GetSourceChar(); SourceIndex++; } //Scan until end quote mark while (!GetSourceChar().Equals('\"')); SourceIndex++; token.Type = TokenType.String; return token; }
/// <summary> /// Parses all the tokens /// </summary> public void ParseProgram() { //Last Token must be EOL so add one in case Token eol = new Token(); eol.Line = Tokens[Tokens.Count - 1].Line + 1; eol.Type = TokenType.EOL; eol.Value = "\n"; Tokens.Add(eol); //Start at the first Token TokenIndex = 0; for (int line = 0; line < LineCount && GetCurrentToken() != null; line++) { Node currentNode = new Node(); Node currentNodeMarker; if (!GetCurrentToken().Type.Equals(TokenType.EOL)) { currentNodeMarker = null; if (line == LineCount - 1) { currentNode = ParseStatement(currentNode, ref currentNodeMarker); RootNode.InsertFinal(currentNode); if (currentNodeMarker != null) RootNode.CurrentNode = currentNodeMarker; } else { RootNode.Insert("STATEMENTS", ParseStatement(currentNode, ref currentNodeMarker)); if (currentNodeMarker != null) RootNode.CurrentNode = currentNodeMarker; } } TokenIndex++; } RootNode.NullifyEmptyChildren(); }