Lexer.GetNextToken C# (CSharp) Méthode

GetNextToken() private méthode

private GetNextToken ( ) : Token,
Résultat Token,
    Token GetNextToken()
    {
	String value;
	
	    switch (file[index]){
	    case '\n':
	    case '\r':
	    case '\t':
	    case ' ':
		index++;
		return null;
	    case ';':
		index++;
		while (file[index] != '\n'){
		    index++;
		}
		return null;
		
	    case '0':
	    case '1':
	    case '2':
	    case '3':
	    case '4':
	    case '5':
	    case '6':
	    case '7':
	    case '8':
	    case '9':
		value = Convert.ToString(file[index]);
		index++;
		while (Char.IsDigit(file[index])){
		    value = value + file[index];
		    index++;
		}
		return new NumberToken(value);
	    case '<':
		index++;
		if (file[index] == '='){
		    index++;
		    return new Token(TokenType.LE);
		}
		else
		return new Token(TokenType.LT);
	    case '>':
		index++;
		if (file[index] == '='){
		    index++;
		    return new Token(TokenType.GE);
		}
		else
		    return new Token(TokenType.GT);
	    case '=':
		index++;
		return new Token(TokenType.EQ);
	    case '+':
		index++;
		if (Char.IsDigit(file[index])){
		    value = Convert.ToString('+');
		    while(Char.IsDigit(file[index])){
			value = value + file[index];
			index++;
		    }
		    return new NumberToken(value);
		}
		else    
		    return new Token(TokenType.PLUS);

       	    case '-':
		index++;
		if (Char.IsDigit(file[index])){
		    value = Convert.ToString('-');
		    while(Char.IsDigit(file[index])){
			value = value + file[index];
			index++;
		    }
		    return new NumberToken(value);
		}
		else    
		    return new Token(TokenType.MINUS);

	    case '*':
		index++;
		return new Token(TokenType.MUL);
	    case '/':
		index++;
		return new Token(TokenType.DIVIDE);
	    case '(':
		index++;
		return new Token(TokenType.LPAREN);
	    case ')':
		index++;
		return new Token(TokenType.RPAREN); 
	    case '\'':
		index++;
		value = null;
		if (Char.IsLetterOrDigit(file[index])){
		    value = file[index].ToString();
		    index++;
		    while(Char.IsLetterOrDigit(file[index])){
			value = value + file[index];
			index++;
		    }
		    if (value.CompareTo("NIL") == 0)
			return new Token(TokenType.NIL);
		    if (value.ToUpper().CompareTo(value.ToLower()) == 0)
			return new NumberToken(value);
		}
		else if(file[index] == '('){
		    value = "(";
		    index++;
		    int count = 1;
		    while(file[index] != ')' || --count > 0){
			if(file[index] == '(')
			    count++;
			value = value + file[index];
			index++;
		    }
		    value = value + ")";
		    index++;
		}
		return new StringToken(value);
		
	    default:
		if(Char.IsLetter(file[index])){
		       value = Convert.ToString(file[index]);
		       index++;		       
		       while(Char.IsLetterOrDigit(file[index])){
			   value = value + file[index];
			   index++;
		       }
		       if(value.CompareTo("cdr") == 0)
			   return new Token(TokenType.CDR);
		       if(value.CompareTo("car") == 0)
			   return new Token(TokenType.CAR);
		       if (value.CompareTo("cons") == 0)
			   return new Token(TokenType.CONS);
		       if (value.CompareTo("subst") == 0)
			   return new Token(TokenType.SUBST);
		       if (value.CompareTo("defun") == 0)
			   return new Token(TokenType.DEFUN);
		       if (value.CompareTo("setq") == 0)
			   return new Token(TokenType.SETQ);
		       if (value.CompareTo("null") == 0)
			   return new Token(TokenType.NULL);
		       if (value.CompareTo("if") == 0)
			   return new Token(TokenType.IF);
		       if (value.CompareTo("atom") == 0)
			   return new Token(TokenType.ATOM);
		       if (value.CompareTo("do") == 0)
			   return new Token(TokenType.DO);
		       //else
			   return new AlphaToken(value);			   
			   
		}
		else{
		    Console.WriteLine("Error in Input");
		    index++;
		    success = false;
		    return null;
		   }
		
	    }
    }
}

Usage Example

Exemple #1
0
        public void FloatLiteralMustFinishWithF()
        {
            var input = new InputString("1.5F 23.7F 5.0 5.0.0 \0");
            var lexer = new Lexer(input);

            var expectedTypes   = new TokenType[] { TokenType.LITERAL_FLOAT, TokenType.LITERAL_FLOAT };
            var expectedLexemas = new string[] { "1.5F", "23.7F" };

            var current = lexer.GetNextToken();
            var i       = 0;

            while (current.type != TokenType.EOF && i < expectedTypes.Length - 1)
            {
                Assert.Equal(expectedTypes[i], current.type);
                Assert.Equal(expectedLexemas[i++], current.lexema);
                current = lexer.GetNextToken();
            }

            // Exception ex = Assert.Throws<FloatLiteralException>(() => lexer.GetNextToken());

            // Assert.Equal("Float Literal must finish with F.", ex.Message);

            // ex = Assert.Throws<FloatLiteralException>(() => lexer.GetNextToken());

            // Assert.Equal("Float Literal must finish with F.", ex.Message);
        }
All Usage Examples Of Lexer::GetNextToken