Beispiel #1
0
        public string ReadNumber(out Region reg)
        {
            reg = StartRegion();
            if ((CurChar == '+' || CurChar == '-') && Peek(1) == '.' && !IsDigit(Peek(2)))
            {
                Throw($"Expected number, got {new string(new char[] { CurChar, Peek(1), Peek(2) }).Inspect()}...");
            }
            else if ((CurChar == '+' || CurChar == '-') && !IsDigit(Peek(1)))
            {
                Throw($"Expected number, got {new string(new char[] { CurChar, Peek(1) }).Inspect()}...");
            }
            else if (CurChar == '.' && !IsDigit(Peek(1)))
            {
                Throw($"Expected number, got {new string(new char[] { CurChar, Peek(1) }).Inspect()}...");
            }
            else if (!IsDigit(CurChar))
            {
                Throw($"Expected number, got {CurChar.Inspect()}");
            }
            if (CurChar == '0' && Peek(1) == 'x')
            {
                Move(2);
                while (!EOF && IsHexDigit(CurChar))
                {
                    Move();
                }
                reg.End();
                return(reg.ToString());
            }
            Move();
            if (CurChar == '.')
            {
                Move();
            }
            while (!EOF && IsDigit(CurChar))
            {
                Move();
            }

            if (CurChar == '.')
            {
                Move();
                while (!EOF && IsDigit(CurChar))
                {
                    Move();
                }
            }

            reg.End();

            return(reg.ToString());
        }
Beispiel #2
0
 public string ReadIdentifier(out Region reg)
 {
     reg = StartRegion();
     if (CurChar != '_' && !(CurChar >= 'a' && CurChar <= 'z') && !(CurChar >= 'A' && CurChar <= 'Z'))
     {
         Throw($"Expected identifier start, got {CurChar.Inspect()}");
     }
     Move();
     while (!EOF && (CurChar == '_' || (CurChar >= 'a' && CurChar <= 'z') || (CurChar >= 'A' && CurChar <= 'Z') || (CurChar >= '0' && CurChar <= '9')))
     {
         Move();
     }
     reg.End();
     return(reg.ToString());
 }
Beispiel #3
0
        public string ReadPunctuation(out Region reg)
        {
            if (!IsPunctuation(CurChar))
            {
                Throw($"Expected punctuation, got {CurChar.Inspect()}");
            }

            var c = CurChar;

            reg = StartRegion();
            var p  = Peek(1);
            var p2 = Peek(2);

            reg.End();
            Move();

            // complex punctuation
            if (c == '=' && p == '=')
            {
                reg.End(); Move(); return("==");
            }
            if (c == '<' && p == '=')
            {
                reg.End(); Move(); return("<=");
            }
            if (c == '>' && p == '=')
            {
                reg.End(); Move(); return(">=");
            }
            if (c == '.' && p == '.' && p2 == '.')
            {
                reg.End(); Move(); Move(); return("...");
            }
            if (c == '.' && p == '.')
            {
                reg.End(); Move(); return("..");
            }
            if (c == '~' && p == '=')
            {
                reg.End(); Move(); return("~=");
            }
            if (c == ':' && p == ':')
            {
                reg.End(); Move(); return("::");
            }

            return(c.ToString());
        }
Beispiel #4
0
        public string ReadNumber(out Region reg)
        {
            //INT : Digit+
            //HEX : '0' [xX] HexDigit+
            //FLOAT : Digit+ '.' Digit* ExponentPart?
            //		| '.' Digit+ ExponentPart?
            //		| Digit+ ExponentPart
            //HEX_FLOAT : '0' [xX] HexDigit+ '.' HexDigit* HexExponentPart?
            //			| '0' [xX] '.' HexDigit+ HexExponentPart?
            //			| '0' [xX] HexDigit+ HexExponentPart
            //
            // ExponentPart : [eE] [+-]? Digit+
            // HexExponentPart : [pP] [+-]? Digit+

            #region 校验
            if ((CurChar == '+' || CurChar == '-') && Peek(1) == '.' && !IsDigit(Peek(2)))
            {
                Throw($"Expected number, got {new string(new char[] { CurChar, Peek(1), Peek(2) }).Inspect()}...");
            }
            else if ((CurChar == '+' || CurChar == '-') && !IsDigit(Peek(1)))
            {
                Throw($"Expected number, got {new string(new char[] { CurChar, Peek(1) }).Inspect()}...");
            }
            else if (CurChar == '.' && !IsDigit(Peek(1)))
            {
                Throw($"Expected number, got {new string(new char[] { CurChar, Peek(1) }).Inspect()}...");
            }
            else if (!IsDigit(CurChar))
            {
                Throw($"Expected number, got {CurChar.Inspect()}");
            }
            #endregion

            reg = StartRegion();
            if (CurChar == '0' && Peek(1) == 'x')
            {
                Move(2);
                while (!EOF && IsHexDigit(CurChar))
                {
                    Move();
                }

                if (CurChar == '.')
                {
                    Move();
                    while (!EOF && IsHexDigit(CurChar))
                    {
                        Move();
                    }
                }

                if (CurChar == 'p' || CurChar == 'P')
                {
                    Move();
                    if (!TryReadDigits())
                    {
                        Throw($"Expected number with HexExponentPart, got {new string(new char[] { CurChar, Peek(1) }).Inspect()}...");
                    }
                }

                reg.End();
                return(reg.ToString());
            }

            Move();
            if (CurChar == '.')
            {
                Move();
            }

            while (!EOF && IsDigit(CurChar))
            {
                Move();
            }

            if (CurChar == '.')
            {
                Move();
                while (!EOF && IsDigit(CurChar))
                {
                    Move();
                }
            }

            if (CurChar == 'e' || CurChar == 'E')
            {
                Move();
                if (!TryReadDigits())
                {
                    Throw($"Expected number with ExponentPart, got {new string(new char[] { CurChar, Peek(1) }).Inspect()}...");
                }
            }

            reg.End();

            return(reg.ToString());
        }
Beispiel #5
0
 public override string ToString()
 {
     return(CurChar.ToString());
 }
Beispiel #6
0
        static void Main(string[] args)
        {
            string     StringPlane = "что !,:? ты сейчас \"сделал\"?";
            MachStates State       = MachStates.ScanNormal;//init to 1
            char       CurChar;
            string     CurWord = "";

            List <string> finalList = new List <string>()
            {
                ""
            };                                                 //создаем пустой лист

            //заготовка под ввод с консоли

            /*
             * Console.ReadLine()...
             * if(!Plane.length)
             *  >gg, wp, close*/
            Console.WriteLine(StringPlane);
            Console.WriteLine("\n");

            for (int Inx = 0; Inx < StringPlane.Length; Inx++)
            {
                CurChar = StringPlane[Inx];

                /*обработка состояния*/
                switch (State)
                {
                case MachStates.ScanNormal:
#if (DEB)
                    Console.WriteLine("Entered Scan Normal!");
#endif
                    if (CurChar == '\"')
                    {
                        if (CurWord != "")
                        {
                            finalList.Add(CurWord);
                        }
                        CurWord = "\"";
                        State   = MachStates.ScanQuoted;
                    }
                    //то есть Текущий символ не был найден в списке restricted
                    else if (ScanRestrictedChars(CurChar, restricted_chars) != 0)
                    {
                        if (CurWord != "")
                        {
                            finalList.Add(CurWord);
                            CurWord = "";
                        }
                        State = MachStates.ScanPunctuation;
                    }
                    else
                    {
                        CurWord += CurChar;
                    }
                    break;

                case MachStates.ScanQuoted:
#if (DEB)
                    Console.WriteLine("Entered Scan Quoted!");
#endif
                    CurWord += CurChar;
                    if (CurChar == '\"')
                    {
                        finalList.Add(CurWord);
                        CurWord = " ";
                        State   = MachStates.ScanNormal;
                    }
                    break;

                case MachStates.ScanPunctuation:
#if (DEB)
                    Console.WriteLine("Entered Scan Punc!");
#endif
                    if (CurChar == '"')
                    {
                        CurWord = "\"";
                        State   = MachStates.ScanQuoted;
                    }
                    else if (ScanRestrictedChars(CurChar, restricted_chars) == 0)
                    {
                        CurWord = CurChar.ToString();
                        State   = MachStates.ScanNormal;
                    }

                    break;
                }

                /*if(State == MachStates.ScanQuoted) { Console.WriteLine("ERROR:по достижению строки текущее состояние - ScanQuoted"); }
                 * if (CurWord != " ")
                 *  finalList.Add(CurWord);*/
            }
            finalList.ForEach(delegate(String name)
            {
                Console.Write(name);
            }
                              );
            Console.ReadLine();
        }