Beispiel #1
0
        /// <summary>
        /// Check if text has symbol for current action.
        /// </summary>
        /// <param name="text">Text to check.</param>
        /// <param name="index">Index in text.</param>
        /// <param name="action">Action to check.</param>
        /// <param name="length">Length of symbol is set here.</param>
        /// <returns></returns>
        internal static bool IsSymbol(string text, int index, ParserActions action, ref int length)
        {
            // Get symbol list for action.
            SymbolList list = null;

            switch (action)
            {
            case ParserActions.Escape: list = EscapeSymbol; break;

            case ParserActions.VariableStart: list = VariableStartSymbol; break;

            case ParserActions.VariableEnd: list = VariableEndSymbol; break;

            case ParserActions.VariableDelimiter: list = VariableDelimiterSymbol; break;

            case ParserActions.ArgumentStart: list = ArgumentStartSymbol; break;

            case ParserActions.ArgumentEnd: list = ArgumentEndSymbol; break;

            case ParserActions.ArgumentDelimiter: list = ArgumentDelimiterSymbol; break;

            case ParserActions.LiteralStart: list = LiteralStartSymbol; break;

            case ParserActions.LiteralEnd: list = LiteralEndSymbol; break;

            case ParserActions.BlockIgnore: list = IgnoreBlockSymbol; break;

            case ParserActions.AlwaysIgnore: list = IgnoreAlwaysSymbol; break;

            default:
                throw new InvalidOperationException();
            }

            return(list.Test(text, index, ref length));
        }
Beispiel #2
0
        /// <summary>
        /// Perform static initialization.
        /// </summary>
        static Parser()
        {
            EscapeSymbol            = new SymbolList("\\");
            VariableStartSymbol     = new SymbolList("<");
            VariableEndSymbol       = new SymbolList(">");
            VariableDelimiterSymbol = new SymbolList(".");
            ArgumentStartSymbol     = new SymbolList("(");
            ArgumentEndSymbol       = new SymbolList(")");
            ArgumentDelimiterSymbol = new SymbolList(",");
            LiteralStartSymbol      = new SymbolList("\"");
            LiteralEndSymbol        = new SymbolList("\"");
            IgnoreBlockSymbol       = new SymbolList(" ", "\t");
            IgnoreAlwaysSymbol      = new SymbolList();

            // Register if function.
            Functions["if"] = q =>
            {
                if (q.Length <= 1)
                {
                    return(null);
                }

                if (q.Length == 2)
                {
                    Value v  = new Value(q[0]);
                    bool  qr = false;
                    if (v.TryToBoolean(out qr))
                    {
                        return(qr ? q[1] : "");
                    }
                    return(null);
                }

                if (q.Length == 3)
                {
                    Value v  = new Value(q[0]);
                    bool  qr = false;
                    if (v.TryToBoolean(out qr))
                    {
                        return(qr ? q[1] : q[2]);
                    }
                    return(null);
                }

                return(null);
            };

            // Register randomizer function.
            Functions["rand"] = q =>
            {
                if (q.Length == 0)
                {
                    return(new Value(Randomizer.NextDouble()).ToString());
                }
                if (q.Length == 1)
                {
                    Value  v = new Value(q[0]);
                    double qv;
                    if (v.TryToDouble(out qv))
                    {
                        return(Randomizer.Roll(qv) ? "1" : "0");
                    }
                    return(null);
                }
                else if (q.Length == 2)
                {
                    Value v1 = new Value(q[0]);
                    Value v2 = new Value(q[1]);

                    double qv1, qv2;
                    if (!v1.TryToDouble(out qv1) || !v2.TryToDouble(out qv2))
                    {
                        return(null);
                    }

                    if (qv2 < qv1)
                    {
                        return(v1.ToString());
                    }

                    return(new Value(Randomizer.NextDouble() * (qv2 - qv1) + qv1).ToString());
                }

                return(null);
            };
            Functions["rnd"]    = Functions["rand"];
            Functions["random"] = Functions["rand"];
        }