Exemplo n.º 1
0
        public Template Parse(string toParse)
        {
            StringBuilder builder = new StringBuilder(toParse);

            CompositeMacro macro = new CompositeMacro();
            MacroTable     table = new MacroTable();

            while (builder.Length > 0)
            {
                IParser parser = GetParser(builder);

                if (parser != null)
                {
                    macro.Add(parser.Parse(ref builder));
                }
                else
                {
                    Tuple <Tuple <string, int>, string> macroToAdd = GetMacroDefinition(ref builder);

                    table.AddMacros(macroToAdd.Item1.Item1, macroToAdd.Item1.Item2, macroToAdd.Item2);
                }
            }

            return(new Template(macro, table));
        }
Exemplo n.º 2
0
        public string Execute(Context c, MacroTable table)
        {
            string toReturn = text.Replace(@"\.", ".");

            toReturn = toReturn.Replace(@"\#", "#");
            toReturn = toReturn.Replace(@"\$", "$");
            toReturn = toReturn.Replace("\\\"", "\"");
            return(toReturn);
        }
Exemplo n.º 3
0
        public string Execute(Context c, MacroTable table)
        {
            StringBuilder toReturn = new StringBuilder();

            foreach (IMacroElement el in children)
            {
                toReturn.Append(el.Execute(c, table));
            }

            return(toReturn.ToString());
        }
Exemplo n.º 4
0
        public string Execute(Context c = null, MacroTable table = null)
        {
            if (c != null)
            {
                context = c;
            }

            if (table != null)
            {
                macroTable = table;
            }

            return(aggregate.Execute(context, macroTable));
        }
Exemplo n.º 5
0
        public string Execute(Context c, MacroTable table)
        {
            ParseManager manager = new ParseManager();

            string toReturn = "";

            if (conditionSignatures[conditionSignatures.Count - 1].Item1 == "else")
            {
                bool executed = false;

                for (int i = 0; i < conditionSignatures.Count - 1; ++i)
                {
                    string toCheck = conditionSignatures[i].Item2;

                    if (LogicManager.IsTrue(toCheck, c))
                    {
                        toReturn = manager.Parse(conditionBodies[i]).Execute(c);
                        executed = true;
                        break;
                    }
                }

                if (!executed)
                {
                    toReturn = manager.Parse(conditionBodies[conditionBodies.Count - 1]).Execute(c);
                }
            }
            else
            {
                for (int i = 0; i < conditionSignatures.Count; ++i)
                {
                    string toCheck = manager.Parse(conditionSignatures[i].Item2).Execute(c);

                    if (LogicManager.IsTrue(toCheck, c))
                    {
                        toReturn = manager.Parse(conditionBodies[i]).Execute(c, table);
                        break;
                    }
                }
            }

            return(toReturn);
        }
Exemplo n.º 6
0
        public string Execute(Context c, MacroTable table)
        {
            KeyValuePair <Tuple <string, int>, string> data = table.GetMacros(name, variables.Count());

            if (data.Key.Item2 != -1)
            {
                List <string> parameters = data.Key.Item1.Split(' ').ToList();
                parameters.RemoveAll(x => x == "");
                parameters = parameters.Skip(1).ToList();

                for (int i = 0; i < parameters.Count; ++i)
                {
                    parameters[i] = parameters[i].Substring(1);
                }

                Context newContext = (Context)c.Clone();

                for (int i = 0; i < parameters.Count; ++i)
                {
                    if (variables[i].Length > 1 && variables[i][0] == '$')
                    {
                        string toPass = c.GetValue(variables[i].Substring(1)).ToString();
                        newContext.SetValue(parameters[i], toPass);
                    }
                    else
                    {
                        newContext.SetValue(parameters[i], variables[i]);
                    }
                }

                ParseManager manager = new ParseManager();

                Template innerMacros = manager.Parse(data.Value);

                string toReturn = innerMacros.Execute(newContext, table);

                return(toReturn);
            }
            else
            {
                throw new ArgumentException(String.Format("Macro table does not contain definition for method {0} with {1} parameters", name, variables.Count()));
            }
        }
Exemplo n.º 7
0
        public string Execute(Context c, MacroTable table)
        {
            object parameter = c.GetValue(name);

            if (parameter != null)
            {
                if (properties != null)
                {
                    for (int i = 0; i < properties.Count; ++i)
                    {
                        parameter = GetPropertyData(parameter, properties[i]);
                    }
                }

                return(parameter.ToString());
            }
            else
            {
                throw new ArgumentException("Variable macro: context does not contain proper variable");
            }
        }
Exemplo n.º 8
0
        public string Execute(Context c, MacroTable table)
        {
            object containerData = c.GetValue(containerName.Substring(1, containerName.Length - 1));

            if (containerData != null)
            {
                IEnumerable <object> objects = containerData as IEnumerable <object>;

                if (objects != null)
                {
                    ParseManager manager = new ParseManager();

                    Template t = manager.Parse(loopBody);

                    string toReturn = "";

                    foreach (object obj in objects)
                    {
                        Context toPass = (Context)c.Clone();

                        toPass.SetValue(itemName.Substring(1, itemName.Length - 1), obj);

                        toReturn += t.Execute(toPass, table);
                    }

                    return(toReturn);
                }
                else
                {
                    throw new ArgumentException("Wrong data type for container");
                }
            }
            else
            {
                throw new ArgumentException("Container not found");
            }
        }
Exemplo n.º 9
0
 public Template(IMacroElement el = null, MacroTable table = null, Context c = null)
 {
     aggregate  = el;
     macroTable = table;
     context    = c;
 }