Beispiel #1
0
        private void SkipNewline()
        {
            char c = Code.GetChar(Index++);

            if (CharUtils.IsNewLine(c))
            {
                if (c == '\r' && Code.GetChar(Index) == '\n')
                {
                    ++Index;
                }
                ++CurrentLine;
                CurrentLineStart = Index;
            }
        }
Beispiel #2
0
        private Token NextStringLiteral()
        {
            bool err   = true;
            int  start = Index++;
            var  str   = GetStringBuilder();

            while (!IsEof())
            {
                char c = Code.GetChar(Index);
                if (c == '"')
                {
                    c = Code.GetChar(++Index);
                    if (c == '"')
                    {
                        ++Index;
                        str.Append(c);
                    }
                    else
                    {
                        err = false;
                        break;
                    }
                }
                else if (CharUtils.IsNewLine(c))
                {
                    break;
                }
                else
                {
                    ++Index;
                    str.Append(c);
                }
            }

            if (err)
            {
                throw VBSyntaxError(VBSyntaxErrorCode.UnterminatedStringConstant);
            }

            return(new StringLiteralToken
            {
                Start = start,
                End = Index - 1,
                LineNumber = CurrentLine,
                LineStart = CurrentLineStart,
                Value = str.ToString(),
            });
        }
Beispiel #3
0
        private Token NextLineTermination()
        {
            int  start   = Index;
            int  line    = CurrentLine;
            bool isColon = false;

            while (!IsEof())
            {
                char c = Code.GetChar(Index);
                if (CharUtils.IsLineTerminator(c))
                {
                    if (c == '\r' && Code.GetChar(Index + 1) == '\n')
                    {
                        ++Index;
                    }

                    ++Index;
                    isColon |= (c == ':');

                    if (c != ':')
                    {
                        ++CurrentLine;
                        CurrentLineStart = Index;
                    }
                }
                else
                {
                    break;
                }

                SkipWhitespaces();
            }

            Token token = isColon && line == CurrentLine
                ? new ColonLineTerminationToken()
                : new LineTerminationToken();

            token.Start      = start;
            token.End        = Index;
            token.LineNumber = CurrentLine - 1;
            token.LineStart  = CurrentLineStart;

            return(token);
        }
Beispiel #4
0
        private string GetIdentifierName()
        {
            int start = Index;

            while (!IsEof())
            {
                char c = Code.GetChar(Index);
                if (CharUtils.IsIdentifier(c))
                {
                    ++Index;
                }
                else
                {
                    break;
                }
            }

            return(Code.Slice(start, Index));
        }
Beispiel #5
0
        private LiteralToken NextHexIntLiteral()
        {
            int start = Index;

            Index += 2;

            string str = GetHexStr();
            char   c   = Code.GetChar(Index);

            if (CharUtils.IsIdentifierStart(c))
            {
                throw VBSyntaxError(VBSyntaxErrorCode.ExpectedEndOfStatement);
            }

            var result = ParseInteger(str, 16);

            result.Start = start;

            return(result);
        }
Beispiel #6
0
        public void SkipWhitespaces()
        {
            void SkipWSOnly()
            {
                char c = Code.GetChar(Index);

                while (CharUtils.IsWhiteSpace(c))
                {
                    c = Code.GetChar(++Index);
                }
            }

            while (!IsEof())
            {
                SkipWSOnly();
                char c = Code.GetChar(Index);
                if (c == '_')
                {
                    ++Index;
                    SkipWSOnly();
                    c = Code.GetChar(Index);
                    if (CharUtils.IsNewLine(c))
                    {
                        SkipNewline();
                    }
                    else
                    {
                        throw VBSyntaxError(VBSyntaxErrorCode.InvalidCharacter);
                    }
                }
                else
                {
                    break;
                }
            }
        }
Beispiel #7
0
        private Token NextNumericLiteral()
        {
            int  start = Index;
            char c     = Code.GetChar(Index);
            char next  = Code.GetChar(Index + 1);

            string?       dec  = null;
            StringBuilder?fstr = null;

            if (c != '.')
            {
                if (c == '&')
                {
                    if (CharUtils.Equals(next, 'h'))
                    {
                        return(NextHexIntLiteral());
                    }
                    else if (CharUtils.IsOctDigit(next))
                    {
                        return(NextOctIntLiteral());
                    }
                    else
                    {
                        throw VBSyntaxError(VBSyntaxErrorCode.SyntaxError);
                    }
                }
                else
                {
                    dec = GetDecStr();

                    if (CharUtils.IsIdentifierStart(Code.GetChar(Index)))
                    {
                        throw VBSyntaxError(VBSyntaxErrorCode.ExpectedEndOfStatement);
                    }
                }
            }

            c = Code.GetChar(Index);
            if (c == '.')
            {
                ++Index;
                fstr ??= GetStringBuilder();
                fstr.Append('.').Append(GetDecStr());
                c = Code.GetChar(Index);
            }

            if (CharUtils.Equals(c, 'e'))
            {
                fstr ??= GetStringBuilder();
                fstr.Append('e');

                c = Code.GetChar(++Index);
                if (c == '+' || c == '-')
                {
                    ++Index;
                    fstr.Append(c);
                }

                c = Code.GetChar(Index);
                if (CharUtils.IsDecDigit(c))
                {
                    fstr.Append(GetDecStr());
                }
                else
                {
                    throw VBSyntaxError(VBSyntaxErrorCode.InvalidNumber);
                }
            }

            c = Code.GetChar(Index);
            if (CharUtils.IsIdentifierStart(c))
            {
                throw VBSyntaxError(VBSyntaxErrorCode.ExpectedEndOfStatement);
            }

            if (fstr != null && dec != null)
            {
                fstr.Insert(0, dec);
            }

            if (fstr != null)
            {
                return(new FloatLiteralToken
                {
                    Start = start,
                    End = Index,
                    LineNumber = CurrentLine,
                    LineStart = CurrentLineStart,
                    Value = ParseDouble(fstr.ToString()),
                });
            }

            var result = ParseInteger(dec !, 10);

            result.Start = start;

            return(result);
        }
Beispiel #8
0
        public Token NextToken()
        {
            SkipWhitespaces();

            if (IsEof())
            {
                return(new EofToken
                {
                    Start = Index,
                    End = Index,
                    LineNumber = CurrentLine,
                    LineStart = CurrentLineStart,
                });
            }

            char c    = Code.GetChar(Index);
            char next = Code.GetChar(Index + 1);

            if (CharUtils.IsLineTerminator(c))
            {
                return(NextLineTermination());
            }

            var comment = NextComment();

            if (comment != null)
            {
                return(comment);
            }

            if (CharUtils.IsIdentifierStart(c))
            {
                return(NextIdentifier());
            }

            if (c == '"')
            {
                return(NextStringLiteral());
            }

            if (c == '.')
            {
                if (CharUtils.IsDecDigit(next))
                {
                    return(NextNumericLiteral());
                }
                return(NextPunctuation());
            }

            if (CharUtils.IsDecDigit(c))
            {
                return(NextNumericLiteral());
            }

            if (c == '&')
            {
                if (CharUtils.Equals(next, 'h') || CharUtils.IsDecDigit(next))
                {
                    return(NextNumericLiteral());
                }
                return(NextPunctuation());
            }

            if (c == '#')
            {
                return(NextDateLiteral());
            }

            if (c == '[')
            {
                return(NextExtendedIdentifier());
            }

            return(NextPunctuation());
        }