Exemplo n.º 1
0
        private void SkipBlock(ParsingScript script)
        {
            int blockStart = script.Pointer;
            int startCount = 0;
            int endCount   = 0;

            while (startCount == 0 || startCount > endCount)
            {
                if (!script.StillValid())
                {
                    throw new ArgumentException("Couldn't skip block [" +
                                                script.Substr(blockStart,Constants.MAX_CHARS_TO_SHOW) + "]");
                }
                char currentChar = script.CurrentAndForward();
                switch (currentChar)
                {
                case Constants.START_GROUP: startCount++; break;

                case Constants.END_GROUP: endCount++; break;
                }
            }

            if (startCount != endCount)
            {
                throw new ArgumentException("Mismatched parentheses");
            }
        }
Exemplo n.º 2
0
        private void SkipBlock(ParsingScript script)
        {
            int  blockStart = script.Pointer;
            int  startCount = 0;
            int  endCount   = 0;
            bool inQuotes   = false;
            bool inQuotes1  = false;
            bool inQuotes2  = false;
            char previous   = Constants.EMPTY;
            char prevprev   = Constants.EMPTY;

            while (startCount == 0 || startCount > endCount)
            {
                if (!script.StillValid())
                {
                    throw new ArgumentException("Couldn't skip block [" +
                                                script.Substr(blockStart, Constants.MAX_CHARS_TO_SHOW) + "]");
                }
                char currentChar = script.CurrentAndForward();
                switch (currentChar)
                {
                case Constants.QUOTE1:
                    if (!inQuotes2 && (previous != '\\' || prevprev == '\\'))
                    {
                        inQuotes = inQuotes1 = !inQuotes1;
                    }
                    break;

                case Constants.QUOTE:
                    if (!inQuotes1 && (previous != '\\' || prevprev == '\\'))
                    {
                        inQuotes = inQuotes2 = !inQuotes2;
                    }
                    break;

                case Constants.START_GROUP:
                    if (!inQuotes)
                    {
                        startCount++;
                    }
                    break;

                case Constants.END_GROUP:
                    if (!inQuotes)
                    {
                        endCount++;
                    }
                    break;
                }
                prevprev = previous;
                previous = currentChar;
            }

            if (startCount != endCount)
            {
                throw new ArgumentException("Mismatched parentheses");
            }
        }
Exemplo n.º 3
0
        public static string ExtractNextToken(ParsingScript script, char[] to, ref bool inQuotes,
                                              ref int arrayIndexDepth, ref int negated, out char ch, out string action, bool throwExc = true)
        {
            StringBuilder item = new StringBuilder();

            ch     = Constants.EMPTY;
            action = null;
            do
            {
                string negateSymbol = Utils.IsNotSign(script.Rest);
                if (negateSymbol != null && !inQuotes)
                {
                    negated++;
                    script.Forward(negateSymbol.Length);
                    continue;
                }

                ch = script.CurrentAndForward();
                CheckQuotesIndices(script, ch, ref inQuotes, ref arrayIndexDepth);

                bool keepCollecting = inQuotes || arrayIndexDepth > 0 ||
                                      StillCollecting(item.ToString(), to, script, ref action);
                if (keepCollecting)
                {
                    // The char still belongs to the previous operand.
                    item.Append(ch);

                    bool goForMore = script.StillValid() &&
                                     (inQuotes || arrayIndexDepth > 0 || !to.Contains(script.Current));
                    if (goForMore)
                    {
                        continue;
                    }
                }

                if (SkipOrAppendIfNecessary(item, ch, to))
                {
                    continue;
                }
                break;
            }while (true);

            string result = item.ToString();

            result = result.Replace("\\\\", "\\");
            result = result.Replace("\\\"", "\"");
            result = result.Replace("\\'", "'");

            if (throwExc && string.IsNullOrWhiteSpace(result) && action != "++" && action != "--" &&
                Utils.IsAction(script.Prev) && Utils.IsAction(script.PrevPrev))
            {
                Utils.ThrowErrorMsg("Can't process token [" + script.PrevPrev + script.Prev + script.Current +
                                    "].", script, script.Current.ToString());
            }

            return(result);
        }
Exemplo n.º 4
0
        private static List <Variable> Split(ParsingScript script, char[] to)
        {
            List <Variable> listToMerge = new List <Variable>(16);

            if (!script.StillValid() || to.Contains(script.Current))
            {
                listToMerge.Add(Variable.EmptyInstance);
                script.Forward();
                return(listToMerge);
            }

            StringBuilder item            = new StringBuilder();
            int           arrayIndexDepth = 0;
            bool          inQuotes        = false;
            int           negated         = 0;

            string rest = script.Rest;

            //if (rest == "b[a[0]];") {
            //  int stop = 1;
            //}

            do // Main processing cycle of the first part.
            {
                string negateSymbol = Utils.IsNotSign(script.Rest);
                if (negateSymbol != null && !inQuotes)
                {
                    negated++;
                    script.Forward(negateSymbol.Length);
                    continue;
                }

                char ch = script.CurrentAndForward();
                CheckQuotesIndices(script, ch, ref inQuotes, ref arrayIndexDepth);
                string action = null;

                bool keepCollecting = inQuotes || arrayIndexDepth > 0 ||
                                      StillCollecting(item.ToString(), to, script, ref action);
                if (keepCollecting)
                {
                    // The char still belongs to the previous operand.
                    item.Append(ch);

                    bool goForMore = script.StillValid() &&
                                     (inQuotes || arrayIndexDepth > 0 || !to.Contains(script.Current));
                    if (goForMore)
                    {
                        continue;
                    }
                }

                if (SkipOrAppendIfNecessary(item, ch, to))
                {
                    continue;
                }

                string token = item.ToString();

                bool ternary = UpdateIfTernary(script, token, ch, ref listToMerge);
                if (ternary)
                {
                    return(listToMerge);
                }

                CheckConsistency(token, listToMerge, script);

                script.MoveForwardIf(Constants.SPACE);

                if (action != null && action.Length > 1)
                {
                    script.Forward(action.Length - 1);
                }

                // We are done getting the next token. The getValue() call below may
                // recursively call loadAndCalculate(). This will happen if extracted
                // item is a function or if the next item is starting with a START_ARG '('.
                ParserFunction func    = new ParserFunction(script, token, ch, ref action);
                Variable       current = func.GetValue(script);
                current.ParsingToken = token;

                if (negated > 0 && current.Type == Variable.VarType.NUMBER)
                {
                    // If there has been a NOT sign, this is a boolean.
                    // Use XOR (true if exactly one of the arguments is true).
                    bool neg = !((negated % 2 == 0) ^ Convert.ToBoolean(current.Value));
                    current = new Variable(Convert.ToDouble(neg));
                    negated = 0;
                }

                if (action == null)
                {
                    action = UpdateAction(script, to);
                }
                else
                {
                    script.MoveForwardIf(action[0]);
                }

                char next = script.TryCurrent(); // we've already moved forward
                bool done = listToMerge.Count == 0 &&
                            (next == Constants.END_STATEMENT ||
                             (action == Constants.NULL_ACTION && current.Type != Variable.VarType.NUMBER) ||
                             current.IsReturn);
                if (done)
                {
                    if (action != null && action != Constants.END_ARG_STR)
                    {
                        throw new ArgumentException("Action [" +
                                                    action + "] without an argument.");
                    }
                    // If there is no numerical result, we are not in a math expression.
                    listToMerge.Add(current);
                    return(listToMerge);
                }

                Variable cell = current.Clone();
                cell.Action = action;

                bool addIt = UpdateIfBool(script, ref cell, ref listToMerge);
                if (addIt)
                {
                    listToMerge.Add(cell);
                }
                item.Clear();
            } while (script.StillValid() &&
                     (inQuotes || arrayIndexDepth > 0 || !to.Contains(script.Current)));

            // This happens when called recursively inside of the math expression:
            script.MoveForwardIf(Constants.END_ARG);

            return(listToMerge);
        }