Пример #1
0
        public List <LexerInfo> GetLexerInfo(string line)
        {
            Dictionary <AutomateData, List <int> > acceptAutomates = InitAcceptAutomates();
            var value     = "";
            var index     = 0;
            var lexerInfo = new List <LexerInfo>();

            foreach (var ch in line)
            {
                if (ch == ' ')
                {
                    foreach (var automate in acceptAutomates)
                    {
                        if (automate.Value.Count == 0)
                        {
                            continue;
                        }
                        if (!automate.Key.FinishStates.Contains(automate.Value.Last()))
                        {
                            continue;
                        }
                        var isReserve = _controller.ReserveWords.Contains(value.ToLower());
                        lexerInfo.Add(new LexerInfo(value, TypeLexem.GetToken(automate.Key, automate.Value.Last()), isReserve));
                    }
                    value           = "";
                    acceptAutomates = InitAcceptAutomates();
                }
                else if (CheckOperation(ch) && index + 1 < line.Length && !char.IsNumber(line[index + 1]))
                {
                    lexerInfo.Add(new LexerInfo(value, TypeLexem.OPERATION, false));
                    value = "";
                }
                else if (_controller.SplitSymbols.Contains(ch))
                {
                    foreach (var automate in acceptAutomates)
                    {
                        if (automate.Value.Count == 0)
                        {
                            continue;
                        }
                        if (!automate.Key.FinishStates.Contains(automate.Value.Last()))
                        {
                            continue;
                        }
                        var isReserve = _controller.ReserveWords.Contains(value.ToLower());
                        lexerInfo.Add(new LexerInfo(value, TypeLexem.GetToken(automate.Key, automate.Value.Last()), isReserve));
                    }
                    lexerInfo.Add(new LexerInfo(ch.ToString(), TypeLexem.DELIMITER, false));
                    value           = "";
                    acceptAutomates = InitAcceptAutomates();
                }
                else
                {
                    acceptAutomates = CheckLexem(ch, acceptAutomates, value);
                    if (acceptAutomates.Count == 0)
                    {
                        throw new Exception(line[index] + " <--------- FAIL!!!");
                    }
                    value += ch;
                }

                index++;
            }
            foreach (var automate in acceptAutomates)
            {
                if (automate.Value.Count == 0)
                {
                    continue;
                }
                if (!automate.Key.FinishStates.Contains(automate.Value.Last()))
                {
                    continue;
                }
                var isReserve = _controller.ReserveWords.Contains(value.ToLower());
                lexerInfo.Add(new LexerInfo(value, TypeLexem.GetToken(automate.Key, automate.Value.Last()), isReserve));
            }

            return(lexerInfo);
        }
Пример #2
0
        public List <LexerInfo> GetLexerInfo(string line, int numberString)
        {
            Dictionary <AutomateData, List <int> > acceptAutomates = InitAcceptAutomates();
            var value     = "";
            var index     = 0;
            var isString  = false;
            var lexerInfo = new List <LexerInfo>();

            for (index = 0; index < line.Length; index++)
            {
                var ch = line[index];
                if (!isString)
                {
                    if (ch == ' ' || ch == '\t')
                    {
                        foreach (var automate in acceptAutomates)
                        {
                            if (automate.Value.Count == 0)
                            {
                                continue;
                            }
                            if (!automate.Key.FinishStates.Contains(automate.Value.Last()))
                            {
                                continue;
                            }
                            var isReserve = _controller.ReserveWords.Contains(value.ToLower());
                            lexerInfo.Add(new LexerInfo(value, TypeLexem.GetToken(automate.Key, automate.Value.Last()), isReserve, numberString, index));
                        }
                        value           = "";
                        acceptAutomates = InitAcceptAutomates();
                    }
                    else if (_controller.MathWords.Contains(ch))
                    {
                        lexerInfo.Add(new LexerInfo(ch.ToString(), TypeLexem.MATH, false, numberString, index));
                        value           = "";
                        acceptAutomates = InitAcceptAutomates();
                    }
                    else if (value + ch == "true" || value + ch == "false")
                    {
                        lexerInfo.Add(new LexerInfo(value, TypeLexem.BOOLEAN, true, numberString, index));
                        value           = "";
                        acceptAutomates = InitAcceptAutomates();
                        continue;
                    }
                    else if (index < line.Length - 1 && CheckComparison(ch, line[index + 1], ref index, ref value))
                    {
                        lexerInfo.Add(new LexerInfo(value, TypeLexem.COMPARISON, false, numberString, index));
                        value           = "";
                        acceptAutomates = InitAcceptAutomates();
                        continue;
                    }
                    else if (_controller.SplitSymbols.Contains(ch))
                    {
                        foreach (var automate in acceptAutomates)
                        {
                            if (automate.Value.Count == 0)
                            {
                                continue;
                            }
                            if (!automate.Key.FinishStates.Contains(automate.Value.Last()))
                            {
                                continue;
                            }
                            var isReserve = _controller.ReserveWords.Contains(value.ToLower());
                            lexerInfo.Add(new LexerInfo(value, TypeLexem.GetToken(automate.Key, automate.Value.Last()), isReserve, numberString, index));
                        }
                        lexerInfo.Add(new LexerInfo(ch.ToString(), TypeLexem.DELIMITER, false, numberString, index));
                        value           = "";
                        acceptAutomates = InitAcceptAutomates();
                        if (lexerInfo.Count > 1 && lexerInfo[lexerInfo.Count - 2].Type == TypeLexem.TEXT)
                        {
                            continue;
                        }
                    }
                    else if (CheckOperation(ch))
                    {
                        lexerInfo.Add(new LexerInfo(ch.ToString(), TypeLexem.OPERATION, false, numberString, index));
                        value = "";
                    }
                    else
                    {
                        acceptAutomates = CheckLexem(ch, acceptAutomates, value);
                        if (acceptAutomates.Count == 0)
                        {
                            throw new Exception($"({numberString}, {index}): " + line[index] + " <--------- FAIL!!!");
                        }
                        value += ch;
                    }
                }
                else
                {
                    // обработка скобок (текста)
                    value += ch;
                    if (index + 1 < line.Length && line[index + 1] == '"')
                    {
                        isString = false;
                        lexerInfo.Add(new LexerInfo(value, TypeLexem.TEXT, false, numberString, index));
                        value           = "";
                        acceptAutomates = InitAcceptAutomates();
                    }
                }

                if (ch == '"')
                {
                    isString = true;
                }

                //index++;
            }
            foreach (var automate in acceptAutomates)
            {
                if (automate.Value.Count == 0)
                {
                    continue;
                }
                if (!automate.Key.FinishStates.Contains(automate.Value.Last()))
                {
                    continue;
                }
                var isReserve = _controller.ReserveWords.Contains(value.ToLower());
                lexerInfo.Add(new LexerInfo(value, TypeLexem.GetToken(automate.Key, automate.Value.Last()), isReserve, numberString, index));
            }

            return(lexerInfo);
        }