Example #1
0
        /*
         * Expect functions return Lexemes of the tokens they expect.
         * If the input contains any unexpected characters,
         * an exception is thrown.
         *
         * Expected regular definition: comment
         */
        // comment := "/*" commentend
        // commentend := ([^/][^*] | comment)* "*/"
        public Lexeme ExpectBlockComment()
        {
            InputBuffer.Position pos = input.GetPosition();
            if (!ExpectToken("/*")) // block comment start
            {
                throw new LexerException("blockcomment expected", pos);
            }
            string s = "";
            int    nestedComments = 0; // counter for level of comment nesting

            if (input.HasCharacter())
            {
                do
                {
                    if (input.HasNextCharacter() && input.PeekCharacter() == '/' && input.PeekNext() == '*')
                    {
                        // nested comment start found
                        // read /*
                        s += input.PeekCharacter();
                        if (input.Next())
                        {
                            s += input.PeekCharacter();
                        }
                        ++nestedComments;
                        continue;
                    }
                    if (input.HasNextCharacter() && input.PeekCharacter() == '*' && input.PeekNext() == '/')
                    {
                        // comment end found
                        if (nestedComments <= 0)
                        {
                            // the end is final comment end
                            // skip */
                            input.Next();
                            input.Next();
                            return(new Lexeme(TokenType.BLOCKCOMMENT, pos, s));
                        }
                        // the end is nested
                        // read */
                        s += input.PeekCharacter();
                        if (input.Next())
                        {
                            s += input.PeekCharacter();
                        }
                        --nestedComments;
                        continue;
                    }
                    s += input.PeekCharacter();
                } while (input.Next());
            }
            throw new LexerException(string.Format("unexpected end of blockcomment starting at {0}", pos.ToString()), input.GetPosition());
        }
Example #2
0
        /*
         * Expect functions return Lexemes of the tokens they expect.
         * If the input contains any unexpected characters,
         * an exception is thrown.
         *
         * Expected regex: "(\\[^\n]|[^"\n])*"
         */
        public Lexeme ExpectString()
        {
            InputBuffer.Position pos = input.GetPosition();
            if (!ExpectToken("\"")) // Expect string starting quote
            {
                throw new LexerException("string expected", pos);
            }
            string s = "";

            if (input.HasCharacter())
            {
                do
                {
                    if (input.PeekCharacter() == '\n') // unexpected newline in middle of string
                    {
                        break;
                    }
                    if (input.PeekCharacter() == '\\') // escaped character
                    {
                        // skip escape character
                        if (!input.Next())
                        {
                            break; // end of input after escape character
                        }
                        // handle escaped character
                        switch (input.PeekCharacter())
                        {
                        case 'n': s += '\n'; break;

                        case 't': s += '\t'; break;

                        case '"': s += '"'; break;

                        default:
                            throw new LexerException(
                                      string.Format("unrecognized escape character {0}", input.PeekCharacter()),
                                      input.GetPosition()
                                      );
                        }
                        continue;
                    }
                    if (input.PeekCharacter() == '"') // string end quote
                    {
                        input.Next();
                        return(new Lexeme(TokenType.STRING, pos, s));
                    }
                    s += input.PeekCharacter();
                } while (input.Next());
            }
            throw new LexerException(string.Format("unexpected end of string starting at {0}", pos.ToString()), input.GetPosition());
        }