示例#1
0
 private static bool lexLineComment(LexBuffer source)
 {
     if (!source.tryRead("//"))
     {
         return(false);
     }
     source.readTillLineEnd();
     return(true);
 }
示例#2
0
 private static bool lexBlockComment(LexBuffer source)
 {
     if (!source.tryRead("/*"))
     {
         return(false);
     }
     do
     {
         source.readTillOneOf(new string[] { "/*", "*/" });
         lexBlockComment(source);
     } while (!source.tryRead("*/"));
     return(true);
 }
示例#3
0
        /// <summary>
        /// Extracts all tokens from the provided buffer and returns a list of tokens.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public List <Token> lexAll(LexBuffer source)
        {
            List <Token> tokens = new List <Token>();

            Token?next;

            while ((next = lexOne(source)).HasValue)
            {
                tokens.Add(next.Value);
            }

            return(tokens);
        }
示例#4
0
        private static string lexName(LexBuffer source)
        {
            char first = source.nextChar();

            if (!isLetter(first) && first != '_')
            {
                return(null);
            }

            return(source.readTill(c =>
                                   !isLetter(c) &&
                                   !isDigit(c) &&
                                   c != '_'));
        }
示例#5
0
        /// <summary>
        /// Removes the next token in the buffer and returns it. If the end of the source is reached, returns null
        /// </summary>
        /// <param name="source">soure to extract a token from</param>
        /// <returns>the next token or null if end of source</returns>
        public Token?lexOne(LexBuffer source)
        {
            while (!source.atEnd())
            {
                source.consumeWhitespace();

                if (source.atEnd())
                {
                    return(null);
                }

                CodeLocation startLocation = source.GetLocation();

                if (lexLineComment(source) || lexBlockComment(source))
                {
                    continue;
                }

                string stringLiteral = lexStringLiteral(source);
                if (stringLiteral != null)
                {
                    return(new Token(TokenType.STRING, stringLiteral, startLocation, source.GetLocation()));
                }

                string name = lexName(source);
                if (name != null)
                {
                    return(new Token(TokenType.NAME, name, startLocation, source.GetLocation()));
                }

                string number = lexNumber(source);
                if (number != null)
                {
                    return(new Token(TokenType.NUMBER, number, startLocation, source.GetLocation()));
                }

                return(new Token(source.readChar(), startLocation, source.GetLocation()));
            }
            return(null);
        }
示例#6
0
        private static string lexStringLiteral(LexBuffer source)
        {
            if (!source.tryRead("\""))
            {
                return(null);
            }
            string stringContents = "";

            while (true)
            {
                stringContents += source.readTillOneOf(new string[] { "\\", "\"" });
                if (source.tryRead("\""))
                {
                    break;
                }
                else
                {
                    source.consume("\\");
                    // TODO: handle other backslash character combos (like \n)
                    stringContents += source.readChar();
                }
            }
            return(stringContents);
        }