public void MatchNewlineR()
 {
     var test = '\r';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Whitespace, character.CharKind);
 }
 public void MatchPlus()
 {
     var test = '+';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Plus, character.CharKind);
 }
 public void MatchLeftParen()
 {
     var test = '(';
     var character = new Character(test);
     Assert.AreEqual(CharKind.LeftParen, character.CharKind);
 }
 public void MatchLetter()
 {
     var test = 'g';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Letter, character.CharKind);
 }
 public void MatchEqual()
 {
     var test = '=';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Equal, character.CharKind);
 }
 public void MatchHyphen()
 {
     var test = '-';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Hyphen, character.CharKind);
 }
 public void MatchDigit()
 {
     var test = '2';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Digit, character.CharKind);
 }
 public void MatchComma()
 {
     var test = ',';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Comma, character.CharKind);
 }
 public void IdentifyUnknown()
 {
     var test = '>';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Unknown, character.CharKind);
 }
示例#10
0
 public void MatchSemicolon()
 {
     var test = ';';
     var character = new Character(test);
     Assert.AreEqual(CharKind.Semicolon, character.CharKind);
 }
示例#11
0
        //public Token PeekNextToken()
        //{
        //    Token currentToken;
        //    if (this.sourceProgram.IsAtEnd())
        //    {
        //        currentToken = TokenFactory.CreateEndOfFileToken();
        //    }
        //    else
        //    {
        //        currentToken = GetNextToken();
        //        if (currentToken == null)
        //        {
        //            currentToken = this.Scan();
        //        }
        //    }
        //    return currentToken();
        //}
        /// <summary>
        /// Contains the logic needed to detect and return the next token in the source program.
        /// TODO: Refactor into helper methods and recursive functions. Use Character directly and not 'char'
        /// </summary>
        /// <returns>The next token in the source pogram.</returns>
        private Token GetNextToken()
        {
            // start with the next available character in the source program
            char nextChar = this.sourceProgram.ReadCurrentCharacter();
            var character = new Character(nextChar);

            // store internal buffer to make sharing any lexical errors easier.
            Buffer nextBuffer = new Buffer();
            nextBuffer.Add(nextChar);

            // create placeholder for token to be returned
            Token nextToken = null;

            // this could use some cleanup, but essentially we just perform the already discussed algorithm based on
            // the kind of character we encounter as we scan the file. Each case handled a different kind.
            switch (character.CharKind)
            {
                case CharKind.Letter:
                    this.buffer.Add(character.Value);
                    char nextUp = this.sourceProgram.Inspect();
                    var nextUpCharacter = new Character(nextUp);
                    while (CharKind.Letter.Equals(nextUpCharacter.CharKind) || CharKind.Digit.Equals(nextUpCharacter.CharKind))
                    {
                        this.buffer.Add(nextUpCharacter.Value);
                        this.sourceProgram.Advance();

                        nextUp = this.sourceProgram.Inspect();
                        nextUpCharacter = new Character(nextUp);
                    }
                    nextToken = CheckReserved(this.buffer.Flush());
                    break;
                case CharKind.Digit:
                    this.buffer.Add(character.Value);
                    char nextUpD = this.sourceProgram.Inspect();
                    var nextUpCharacterD = new Character(nextUpD);
                    while (CharKind.Digit.Equals(nextUpCharacterD.CharKind))
                    {
                        this.buffer.Add(nextUpCharacterD.Value);
                        this.sourceProgram.Advance();

                        nextUpD = this.sourceProgram.Inspect();
                        nextUpCharacterD = new Character(nextUpD);
                    }
                    nextToken = TokenFactory.CreateIntLiteralToken(this.buffer.Flush());
                    break;
                case CharKind.Colon:
                    char nextUpE = this.sourceProgram.Inspect();
                    var nextUpCharacterE = new Character(nextUpE);
                    if (nextUpCharacterE.CharKind.Equals(CharKind.Equal))
                    {
                        this.sourceProgram.Advance();
                        nextToken = TokenFactory.CreateAssignToken();
                    }
                    else
                    {
                        nextBuffer.Add(nextUpCharacterE.Value);
                        nextToken = TokenFactory.CreateLexicalError(nextBuffer.Flush());
                    }
                    break;
                case CharKind.Hyphen:
                    char nextUpH = this.sourceProgram.Inspect();
                    var nextUpCharacterH = new Character(nextUpH);
                    if (nextUpCharacterH.CharKind.Equals(CharKind.Hyphen))
                    {
                        // comment - so advance until end of line
                        char inCommentChar = this.sourceProgram.ReadCurrentCharacter();
                        while (!inCommentChar.Equals('\n'))
                        {
                            inCommentChar = this.sourceProgram.ReadCurrentCharacter();
                        }
                    }
                    else
                    {
                        nextToken = TokenFactory.CreateMinusToken();
                    }
                    break;
                case CharKind.Whitespace:
                    break;
                case CharKind.LeftParen:
                    nextToken = TokenFactory.CreateLeftParenToken();
                    break;
                case CharKind.RightParen:
                    nextToken = TokenFactory.CreateRightParenToken();
                    break;
                case CharKind.Semicolon:
                    nextToken = TokenFactory.CreateSemiColonToken();
                    break;
                case CharKind.Comma:
                    nextToken = TokenFactory.CreateCommaToken();
                    break;
                case CharKind.Plus:
                    nextToken = TokenFactory.CreatePlusToken();
                    break;
                default:
                    nextToken = TokenFactory.CreateLexicalError(nextBuffer.Flush());
                    break;
            }

            return nextToken;
        }