Exemplo n.º 1
0
        /// <summary>Skip lex and get value to _lex</summary>
        /// <param name="ch">Current character.</param>
        public static void NextLex(char ch)
        {
            //ch = Text.Ch().
            if (ch == Text.ChSPACE || ch == Text.ChTAB || ch == Text.ChEOL)
            {
                Text.NextCh();
                while (Text.Ch() == Text.ChSPACE || Text.Ch() == Text.ChTAB || Text.Ch() == Text.ChEOL)
                {
                    Text.NextCh();
                }
                ch = Text.Ch();
            }

            if (Char.IsLetter(ch) || ch == '\u005f' || ch == '\u0024')
            {
                ScanIdent(ch);
            }
            else if (ch == '/')
            {
                Text.NextCh();
                ch = Text.Ch();
                if (ch == '*' || ch == '/')
                {
                    Comment();
                    NextLex(Text.Ch());
                }
                else if (ch == '=')
                {
                    _lex = Lex.ASSDIV;                     // /=.
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.DIV;                     // /.
                }
            }
            else if (ch == '(')
            {
                _lex = Lex.LPAR;
                Text.NextCh();
            }
            else if (ch == ')')
            {
                _lex = Lex.RPAR;
                Text.NextCh();
            }
            else if (ch == '{')
            {
                _lex = Lex.LBRACE;
                Text.NextCh();
            }
            else if (ch == '}')
            {
                _lex = Lex.RBRACE;
                Text.NextCh();
            }
            else if (ch == '[')
            {
                _lex = Lex.LSQUAREPAR;
                Text.NextCh();
            }
            else if (ch == ']')
            {
                _lex = Lex.RSQUAREPAR;
                Text.NextCh();
            }
            else if (ch == '.')
            {
                _lex = NumberAnalysis.FractionalPart(true);
            }
            else if (ch == ',')
            {
                _lex = Lex.COMMA;
                Text.NextCh();
            }
            else if (ch == ';')
            {
                _lex = Lex.SEMI;
                Text.NextCh();
            }
            else if (ch == '=')
            {
                Text.NextCh();
                if (Text.Ch() == '=')
                {
                    _lex = Lex.EQ;
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.ASS;
                }
            }
            else if (ch == '>')
            {
                Text.NextCh();
                if (Text.Ch() == '=')
                {
                    _lex = Lex.GE;
                    Text.NextCh();
                }
                else if (Text.Ch() == '>')
                {
                    Text.NextCh();
                    if (Text.Ch() == '=')
                    {
                        _lex = Lex.ASSRSHIFT;                         // >>=
                        Text.NextCh();
                    }
                    else if (Text.Ch() == '>')
                    {
                        Text.NextCh();
                        if (Text.Ch() == '=')
                        {
                            _lex = Lex.ASSZEROSHIFT;                             // >>>= .
                            Text.NextCh();
                        }
                        else
                        {
                            _lex = Lex.ZEROSHIFT;                             // >>>.
                        }
                    }
                    else
                    {
                        _lex = Lex.RSHIFT;                         // >>.
                    }
                }
                else
                {
                    _lex = Lex.GT;                     // >=.
                }
            }
            else if (ch == '<')
            {
                Text.NextCh();
                if (Text.Ch() == '=')
                {
                    _lex = Lex.LE;
                    Text.NextCh();
                }
                else if (Text.Ch() == '<')
                {
                    Text.NextCh();
                    if (Text.Ch() == '=')
                    {
                        _lex = Lex.ASSLSHIFT;                         // <<= .
                        Text.NextCh();
                    }
                    else
                    {
                        _lex = Lex.LSHIFT;                         // <<.
                    }
                }
                else
                {
                    _lex = Lex.LT;                     // <=.
                }
            }
            else if (ch == '!')
            {
                Text.NextCh();
                if (Text.Ch() == '=')
                {
                    _lex = Lex.NE;
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.INVERSION;
                }
            }
            else if (ch == '~')
            {
                _lex = Lex.BITADD;                 //Bit addition.
                Text.NextCh();
            }
            else if (ch == '?')
            {
                _lex = Lex.TERNARYIF;                 //Ternary operator.
                Text.NextCh();
            }
            else if (ch == ':')
            {
                _lex = Lex.TERNARYELSE;                 //Ternary operator.
                Text.NextCh();
            }
            else if (ch == '&')
            {
                Text.NextCh();
                if (Text.Ch() == '&')
                {
                    _lex = Lex.AND;                     // && .
                    Text.NextCh();
                }
                else if (Text.Ch() == '=')
                {
                    _lex = Lex.ASSBITAND;                     // &= .
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.BITAND;                     // &.
                }
            }
            else if (ch == '|')
            {
                Text.NextCh();
                if (Text.Ch() == '|')
                {
                    _lex = Lex.OR;
                    Text.NextCh();
                }
                else if (Text.Ch() == '=')
                {
                    _lex = Lex.ASSBITOR;
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.BITOR;
                }
            }
            else if (ch == '+')
            {
                Text.NextCh();
                if (Text.Ch() == '+')
                {
                    _lex = Lex.INC;                     // ++.
                    Text.NextCh();
                }
                else if (Text.Ch() == '=')
                {
                    _lex = Lex.ASSADD;
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.PLUS;                     // +.
                }
            }
            else if (ch == '-')
            {
                Text.NextCh();
                if (Text.Ch() == '-')
                {
                    _lex = Lex.DEC;                     // --.
                    Text.NextCh();
                }
                else if (Text.Ch() == '=')
                {
                    _lex = Lex.ASSMINUS;                     // =-.
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.MINUS;                     // -.
                }
            }
            else if (ch == '*')
            {
                Text.NextCh();
                if (Text.Ch() == '=')
                {
                    _lex = Lex.ASSMULT;                     // *= .
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.MULT;                     //*.
                }
            }
            else if (ch == '^')
            {
                Text.NextCh();
                if (Text.Ch() == '=')
                {
                    _lex = Lex.ASSXOR;
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.XOR;
                }
            }
            else if (ch == '%')
            {
                Text.NextCh();
                if (Text.Ch() == '=')
                {
                    _lex = Lex.ASSMOD;
                    Text.NextCh();
                }
                else
                {
                    _lex = Lex.MOD;
                }
            }
            else if (Char.IsDigit(ch))
            {
                ScanNumber();
            }
            else if (ch == (char)39)
            {
                ScanChar();
            }
            else if (ch == '"')
            {
                ScanString();
            }
            else if (ch == Text.ChEOT)
            {
                _lex = Lex.EOT;
            }
            else if (ch == '\\')
            {
                NextLex(UnicodeTranslate());
            }
            else
            {
                Error.LexError("Недопустимый символ");
            }
        }
Exemplo n.º 2
0
        /// <summary>Number literal scan.</summary>
        public static void ScanNumber()
        {
            //Text.Ch() is first digit.

            string octal = "01234567";

            if (Text.Ch() == '0')
            {
                Text.NextCh();
                if (Text.Ch() == 'x' || Text.Ch() == 'X')
                {
                    _lex = NumberAnalysis.HexadecimalScan();
                }
                else if (octal.Contains(Text.Ch()))
                {
                    _lex = NumberAnalysis.OctalNumber();
                }
                else if (Text.Ch() == 'L' || Text.Ch() == 'l')
                {
                    Text.NextCh();
                    _lex = Lex.LONGLITERAL;
                }
                else if (Text.Ch() == 'F' || Text.Ch() == 'f')
                {
                    Text.NextCh();
                    _lex = Lex.FLOATLITERAL;
                }
                else if (Text.Ch() == 'd' || Text.Ch() == 'D')
                {
                    Text.NextCh();
                    _lex = Lex.DOUBLELITERAL;
                }
                else if (Char.IsDigit(Text.Ch()))
                {
                    _lex = Lex.INTLITERAL;
                }
                else if (Text.Ch() == '.')
                {
                    Text.NextCh();
                    _lex = NumberAnalysis.FractionalPart(false);
                }
                else if (Text.Ch() == 'e' || Text.Ch() == 'E')
                {
                    _lex = NumberAnalysis.ExponentScan();
                }
                else if (Text.Ch() == '\\')
                {
                    char check = UnicodeTranslate();
                    if (check == 'x' || check == 'X')
                    {
                        _lex = NumberAnalysis.HexadecimalScan();
                    }
                    else if (octal.Contains(check))
                    {
                        Text.NextCh();
                        _lex = NumberAnalysis.OctalNumber();
                    }
                    else if (check == 'L' || check == 'l')
                    {
                        Text.NextCh();
                        _lex = Lex.LONGLITERAL;
                    }
                    else if (check == 'F' || check == 'f')
                    {
                        Text.NextCh();
                        _lex = Lex.FLOATLITERAL;
                    }
                    else if (check == 'd' || check == 'D')
                    {
                        Text.NextCh();
                        _lex = Lex.DOUBLELITERAL;
                    }
                    else if (Char.IsDigit(check))
                    {
                        Count++;
                        NextLex(check);
                        _lex = Lex.INTLITERAL;
                    }
                    else if (check == '.')
                    {
                        Text.NextCh();
                        _lex = NumberAnalysis.FractionalPart(false);
                    }
                    else if (check == 'e' || check == 'E')
                    {
                        _lex = NumberAnalysis.ExponentScan();
                    }
                    else
                    {
                        Count++;
                        NextLex(check);
                        _lex = Lex.INTLITERAL;
                    }
                }
                else
                {
                    _lex = Lex.INTLITERAL;
                }
            }
            //If number in decimal number system.
            else
            {
                _lex = NumberAnalysis.Number();
            }
        }