Пример #1
0
 public static ExprEntry Create(ExprLogic parent, string keyword = "")
 {
     return(new ExprEntry()
     {
         Parent = parent, Keyword = keyword, EntryLogic = parent.ChildEntryLogic, Operator = Operator.Nop, Value = ""
     });
 }
Пример #2
0
        static (bool, int, ExprLogic) ReadKeword(int pos, string source, ExprLogic currectGrp)
        {
            var x  = source[pos];
            var sb = new StringBuilder();

            if (!IsKeywordFirstSymbol(x))
            {
                return(false, pos, currectGrp);
            }

            while (char.IsLetterOrDigit(x))
            {
                sb.Append(x);

                pos++;

                if (pos < source.Length)
                {
                    x = source[pos];
                }
                else
                {
                    break;
                }
            }

            currectGrp.ExprEntries.Add(ExprEntry.Create(currectGrp, sb.ToString()));

            currectGrp.ChildEntryLogic = Logic.None;

            return(true, pos, currectGrp);
        }
Пример #3
0
        public static ExprLogic Parse(string exprText)
        {
            var exprSchema = DeclareSchema();
            var refPointer = exprSchema[0];

            var resultLogic  = ExprLogic.CreateGroup(null);
            var currentGroup = resultLogic;

            var i           = 0;
            var exprTextLen = exprText.Length;

            while (i < exprTextLen)
            {
                var(pos, exprGrp, schemaNode) = HandleNode(refPointer, currentGroup, i, exprText);

                refPointer   = schemaNode;
                currentGroup = exprGrp;
                i            = pos;
            }

            if (currentGroup.Parent != null)
            {
                throw new KeywordParserException("Open and close brackets mismatch!");
            }

            return(resultLogic);
        }
Пример #4
0
        static (bool, int, ExprLogic) ReadLogic(Dictionary <Logic, List <string> > logicDict, bool entry,
                                                int pos, string source, ExprLogic currectGrp)
        {
            var res      = false;
            var startPos = pos;

            foreach (var key in logicDict.Keys)
            {
                var logop  = "";
                var logics = logicDict[key];

                foreach (var lop in logics)
                {
                    if (pos + lop.Length > source.Length)
                    {
                        continue;
                    }
                    if (pos != source.IndexOf(lop, pos, lop.Length))
                    {
                        continue;
                    }

                    logop = lop;
                    break;
                }

                if (string.IsNullOrEmpty(logop))
                {
                    continue;
                }

                if (entry)
                {
                    currectGrp.ChildEntryLogic = key;
                }
                else
                {
                    currectGrp.ActiveEntry.JoinLogic = key;
                }

                pos += logop.Length;
                res  = true;
                break;
            }

            if (res)
            {
                //if (startPos > 0 && !char.IsWhiteSpace(source[startPos-1]))
                //    throw new KeywordParserException($"The space must precede the logical operator. Expected space in the the position {startPos + 1}");

                if (pos < source.Length && IsKeywordFirstSymbol(source[pos]))
                {
                    throw new KeywordParserException($"A logical operator must be followed by a space. Expected space in the the position {pos + 1}");
                }
            }

            return(res, pos, currectGrp);
        }
Пример #5
0
 public static ExprLogic CreateGroup(ExprLogic parent)
 {
     return(new ExprLogic()
     {
         Parent = parent,
         EntryLogic = parent != null ? parent.ChildEntryLogic : Logic.None,
         ExprEntries = new List <ExprEntryBase>()
     });
 }
Пример #6
0
        static (bool, int, ExprLogic) PassWhitespace(int pos, string source, ExprLogic currectGrp)
        {
            var res = false;

            while (pos < source.Length && char.IsWhiteSpace(source[pos]))
            {
                pos++; res = true;
            }

            return(res, pos, currectGrp);
        }
Пример #7
0
        static (int, ExprLogic, GraphNode) HandleNode(GraphNode graphNode, ExprLogic currentGroup, int pos, string source)
        {
            foreach (GraphNodeItem nodeItem in graphNode)
            {
                var(res, newPos, exprGrp) = nodeItem.Handler(pos, source, currentGroup);

                if (!res)
                {
                    continue;
                }

                return(newPos, exprGrp, nodeItem.Next);
            }

            throw new KeywordParserException($"Unexpected character '{source[pos]}' in the position {pos + 1}.");
        }
Пример #8
0
        static (bool, int, ExprLogic) CloseGroup(int pos, string source, ExprLogic currectGrp)
        {
            // close group
            var res = false;

            if (source[pos] == CloseGrp)
            {
                if (currectGrp.Parent == null)
                {
                    throw new KeywordParserException($"Unexpected character '{CloseGrp}' in the position {pos}.");
                }

                pos++; res = true;
            }

            return(res, pos, currectGrp.Parent);
        }
Пример #9
0
        static (bool, int, ExprLogic) OpenGroup(int pos, string source, ExprLogic currectGrp)
        {
            // open group
            var       res      = false;
            ExprLogic newGroup = null;

            if (source[pos] == OpenGrp)
            {
                pos++;  res = true;
            }

            if (res)
            {
                newGroup = currectGrp.AddGroup(ExprLogic.CreateGroup(currectGrp));

                currectGrp.ChildEntryLogic = Logic.None;
            }

            return(res, pos, res ? newGroup : currectGrp);
        }
Пример #10
0
        static (bool, int, ExprLogic) ReadOperator(int pos, string source, ExprLogic currectGrp)
        {
            var res = false;

            foreach (var key in GraphMeta.Operators.Keys)
            {
                var oper = GraphMeta.Operators[key];
                if (pos + oper.Length > source.Length)
                {
                    continue;
                }
                if (pos != source.IndexOf(oper, pos, oper.Length))
                {
                    continue;
                }

                currectGrp.ActiveExpr.Operator = key;
                pos += oper.Length;
                res  = true;
                break;
            }

            return(res, pos, currectGrp);
        }
Пример #11
0
 public ExprLogic AddGroup(ExprLogic group)
 {
     return((ExprLogic)AddEntry(group));
 }
Пример #12
0
        static (bool, int, ExprLogic) ReadValue(int pos, string source, ExprLogic currectGrp)
        {
            var x  = source[pos];
            var sb = new StringBuilder();

            if (char.IsWhiteSpace(x))
            {
                return(false, pos, currectGrp);
            }

            bool handleEscaped(char ch)
            {
                if (pos < source.Length - 1 && source[pos] == ch && source[pos + 1] == ch)
                {
                    sb.Append(ch);
                    pos++;
                    return(false);
                }

                return(true);
            }

            var inQuote = (x == Quote);

            if (inQuote)
            {
                pos++;
            }

            while (pos < source.Length)
            {
                x = source[pos];

                if (!inQuote)
                {
                    if (x == Quote)
                    {
                        if (handleEscaped(x))
                        {
                            throw new KeywordParserException($"Unescaped quote symbol {Quote} found in the position {pos + 1}!");
                        }
                    }
                    else if (IsStopSymbol(x))
                    {
                        break;
                    }
                    else
                    {
                        if (!char.IsWhiteSpace(x))
                        {
                            sb.Append(x);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    if (x == Quote)
                    {
                        if (handleEscaped(x))
                        {
                            inQuote = false; pos++; break;
                        }
                    }
                    else if (IsStopSymbol(x))
                    {
                        if (handleEscaped(x))
                        {
                            throw new KeywordParserException($"Unescaped symbol {Quote} found in the position {pos + 1}!");
                        }
                    }
                    else
                    {
                        sb.Append(x);
                    }
                }

                pos++;
            }

            if (inQuote)
            {
                throw new KeywordParserException($"Unexpected end of quoted string found!");
            }

            currectGrp.ActiveExpr.Value = sb.ToString();

            return(true, pos, currectGrp);
        }
Пример #13
0
 static (bool, int, ExprLogic) ReadEntryLogic(int pos, string source, ExprLogic currectGrp)
 {
     return(ReadLogic(GraphMeta.Unaries, true, pos, source, currectGrp));
 }
Пример #14
0
 static (bool, int, ExprLogic) ReadJoinLogic(int pos, string source, ExprLogic currectGrp)
 {
     return(ReadLogic(GraphMeta.Logics, false, pos, source, currectGrp));
 }