예제 #1
0
        internal override ExpressionType EmitCs(EmitCodeContext codecontext)
        {
            codecontext.WriteLine("public partial class " + ClassName + " : ExtractionObjectBase");
            codecontext.WriteLine("{");
            EmitCsInnerClass(codecontext.NewScope());
            codecontext.WriteLine("}");

            return(ExpressionType.VoidType);
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="codecontext"></param>
        /// <returns></returns>
        internal override ExpressionType EmitCs(EmitCodeContext codecontext)
        {
            codecontext.WriteLine("{");
            codecontext.Level++;
            // push scope
            bool contextchanged = false;

            foreach (var x in DataContexts)
            {
                if (!contextchanged)
                {
                    contextchanged = true;
                    codecontext.Write(scopeLocalVarName + ".Push(" + scopeLocalVarName + ".context", codecontext.Level);
                }
                //.OpenContextDynamic(MethodName, new object[] { arg1, arg2, ... })
                codecontext.Write(".OpenContextDynamic(\"" + x.MethodName + "\", new object[] {");
                bool firstArg = true;
                foreach (var a in x.CallArguments)
                {
                    if (!firstArg)
                    {
                        codecontext.Write(", ");
                    }
                    else
                    {
                        firstArg = false;
                    }

                    a.EmitCs(codecontext);
                }
                codecontext.Write("})");
            }
            if (contextchanged)
            {
                codecontext.Write(", null);" + codecontext.Output.NewLine);
            }

            // emit statements
            foreach (var s in Statements)
            {
                s.EmitCs(codecontext);
                codecontext.Write(codecontext.Output.NewLine);
            }

            // pop scope
            if (contextchanged)
            {
                codecontext.WriteLine(scopeLocalVarName + ".Pop();");
            }

            codecontext.Level--;
            codecontext.WriteLine("}");

            return(ExpressionType.VoidType);
        }
예제 #3
0
        internal override ExpressionType EmitCs(EmitCodeContext codecontext)
        {
            if (Body != null)
            {
                codecontext.WriteLine("// " + DeclMethodName);
                codecontext.WriteLine("private static void " + GeneratedMethodName + "(DataContext _datacontext, LocalVariables _parameters)");
                codecontext.WriteLine("{");

                EmitCodeContext bodycontext = codecontext.NewScope();

                // declare parameters as local variables
                foreach (var x in MethodArguments)
                {
                    // declare variable from _parameters argument
                    // checks for declaration duplicity
                    bodycontext.DeclareLocalVar(
                        x.VariableType,
                        x.VariableName,
                        new CustomExpression(x.Position, x.VariableType, "(" + x.VariableType.CsArgumentTypeName + ")_parameters[\"" + x.VariableName + "\"]")
                        );
                }

                // declare special scope variable
                bodycontext.WriteLine("ScopesStack " + scopeLocalVarName + " = new ScopesStack(_datacontext, null);");

                // emit method body
                Body.EmitCs(bodycontext);

                //
                codecontext.WriteLine("}" + codecontext.Output.NewLine);

                return(ExpressionType.VoidType);
            }
            else if (BodyCSharp != null)
            {
                codecontext.WriteLine("// " + DeclMethodName);
                codecontext.WriteLine("private static " + ReturnType.CsArgumentTypeName + " " + GeneratedMethodName + "(" + CsArgList + ")");
                codecontext.WriteLine("{");
                string[] lines = BodyCSharp.Split(new string[] { codecontext.Output.NewLine }, StringSplitOptions.None);
                foreach (var line in lines)
                {
                    codecontext.WriteLine(line);
                }
                codecontext.WriteLine("}");

                return(ReturnType);
            }


            return(ExpressionType.VoidType);
        }
예제 #4
0
        internal override ExpressionType EmitCs(EmitCodeContext codecontext)
        {
            codecontext.WriteLine("#region Public classes declaration");
            foreach (var c in Classes)
            {
                c.Value.EmitCs(codecontext);
            }
            codecontext.WriteLine("#endregion" + codecontext.Output.NewLine);

            codecontext.WriteLine("#region Private extraction methods");
            foreach (var m in Methods)
            {
                m.EmitCs(codecontext);
            }
            codecontext.WriteLine("#endregion" + codecontext.Output.NewLine);

            return(ExpressionType.VoidType);
        }
예제 #5
0
        /// <summary>
        /// Emit the C# source code.
        /// </summary>
        /// <param name="output">Output stream.</param>
        /// <param name="level">The level of code indent, default 0.</param>
        /// <param name="declaredVariables">List of variables declared in the current context and their type.</param>
        internal override ExpressionType EmitCs(EmitCodeContext codecontext)
        {
            codecontext.WriteLine("/*");
            codecontext.WriteLine(" * Generated LinqToWeb context");
            codecontext.WriteLine(" * " + DateTime.Now.ToString());
            codecontext.WriteLine(" */");
            codecontext.WriteLine();
            codecontext.WriteLine("using System;");
            codecontext.WriteLine("using System.Collections.Generic;");
            codecontext.WriteLine("using System.Text;");
            codecontext.WriteLine("using System.Diagnostics;");
            codecontext.WriteLine("using linqtoweb.Core.datacontext;");
            codecontext.WriteLine("using linqtoweb.Core.extraction;");
            codecontext.WriteLine("using linqtoweb.Core.methods;");
            codecontext.WriteLine("using linqtoweb.Core.storage;");
            codecontext.WriteLine();
            codecontext.WriteLine("namespace " + codecontext.NamespaceName);
            codecontext.WriteLine("{");

            EmitCodeContext indentc = codecontext.NewScope();

            indentc.WriteLine("public partial class " + codecontext.ContextName + " : ExtractionContext");
            indentc.WriteLine("{");

            Declarations.EmitCs(indentc.NewScope());

            // emit initialize, emit vars (use arguments from main methods), constructors
            EmitCs_Init(indentc.NewScope());

            indentc.WriteLine("}");

            codecontext.WriteLine("}");


            return(ExpressionType.VoidType);
        }
예제 #6
0
        private void EmitCsInnerClass(EmitCodeContext codecontext)
        {
            //
            List <string> propsInit = new List <string>();

            // properties
            codecontext.WriteLine("#region Properties");

            foreach (var x in ClassProperties)
            {
                if (x.VariableName.StartsWith("_"))
                {
                    throw new GeneratorException(Position, "Class property cannot start with _. It is reserved system name.");
                }

                // comment
                codecontext.WriteLine("// " + x.ToString());

                if (x.VariableType.IsExtractionObject)
                {
                    codecontext.WriteLine("public readonly " + x.VariableType.CsPropertyTypeName + " " + x.VariableName + ";");
                    propsInit.Add(x.VariableName + " = " + x.VariableType.CsPropertyInitValue + ";");
                }
                else
                {
                    string privatePropName = "_" + x.VariableName;

                    // extracting on request (public property)
                    // public property
                    string format = "public " + x.VariableType.CsPropertyTypeName + " " + x.VariableName +
                                    "{get{while(" + privatePropName + "==" + x.VariableType.CsPropertyDefaultValue + "){if (!DoNextAction<object>(null))throw new NotExtractedDataException(\"" + x.VariableName + " cannot reach any data.\");} return " + privatePropName + ";}set{" + privatePropName + "=value;}}";

                    codecontext.WriteLine(format);

                    // private property value
                    string decl = "private " + x.VariableType.CsPropertyTypeName + " " + privatePropName;
                    codecontext.WriteLine(decl + " = " + x.VariableType.CsPropertyDefaultValue + ";");
                }

                codecontext.WriteLine("");
            }

            codecontext.WriteLine("#endregion");

            // ctor
            codecontext.WriteLine("#region Constructors");

            // no-param ctor
            codecontext.WriteLine("public " + ClassName + "():this(null){}");
            // ctor with parent given
            codecontext.WriteLine("public " + ClassName + "(ExtractionObjectBase parent):base(parent)");
            codecontext.WriteLine("{");
            codecontext.Level++;
            foreach (string strline in propsInit)
            {
                codecontext.WriteLine(strline);
            }
            codecontext.Level--;
            codecontext.WriteLine("}");
            //
            codecontext.WriteLine("#endregion");
        }
예제 #7
0
        internal void EmitCs_Init(EmitCodeContext codecontext)
        {
            Dictionary <string, ExpressionType> contextVars = new Dictionary <string, ExpressionType>();

            List <MethodDecl> mainMethods = new List <MethodDecl>();

            // collect global vars, extraction arguments
            foreach (var m in codecontext.Declarations.Methods)
            {
                if (m.IsMainMethod)
                {
                    mainMethods.Add(m);

                    foreach (var arg in m.MethodArguments)
                    {
                        ExpressionType vartype;
                        if (contextVars.TryGetValue(arg.VariableName, out vartype))
                        {
                            if (vartype != arg.VariableType)
                            {
                                throw new GeneratorException(Position, "Two context variables with different type defined.");
                            }
                        }
                        else
                        {
                            contextVars[arg.VariableName] = arg.VariableType;
                        }
                    }
                }
            }

            // emit properties
            codecontext.WriteLine("#region Public extracted data");
            foreach (var v in contextVars)
            {
                codecontext.WriteLine("// " + v.Value.ToString() + " " + v.Key);

                if (v.Value.IsExtractionObject)
                {
                    // emit prop
                    codecontext.WriteLine("public readonly " + v.Value.CsPropertyTypeName + " " + v.Key + " = " + v.Value.CsPropertyRootInitValue + ";");
                }

                /*else
                 * {
                 *  // emit context parameter
                 *  codecontext.WriteLine("private readonly " + v.Value.CsArgumentTypeName + " " + v.Key + ";");
                 * }*/
            }
            codecontext.WriteLine("#endregion" + codecontext.Output.NewLine);

            //
            // context arguments, initialization
            //
            string ctorArgs = null;
            string argsPass = null;

            foreach (var x in contextVars)
            {
                if (!x.Value.IsExtractionObject)
                {
                    // argument
                    if (ctorArgs != null)
                    {
                        ctorArgs += ", ";
                    }
                    if (argsPass != null)
                    {
                        argsPass += ", ";
                    }

                    ctorArgs = ctorArgs + x.Value.CsArgumentTypeName + " " + x.Key;
                    argsPass = argsPass + x.Key;
                }
            }

            // emit initializing actions
            codecontext.WriteLine("#region Context construct");
            codecontext.WriteLine("private void InitActionsToDo(" + ctorArgs + ")");
            codecontext.WriteLine("{"); codecontext.Level++;

            foreach (var m in mainMethods)
            {
                codecontext.WriteLine("ActionItem.AddAction(" + m.GeneratedMethodName + ", InitialDataContext, new LocalVariables(){");
                codecontext.Level++;

                bool bfirstarg = true;
                foreach (var arg in m.MethodArguments)
                {
                    if (bfirstarg)
                    {
                        bfirstarg = false;
                    }
                    else
                    {
                        codecontext.Write("," + codecontext.Output.NewLine);
                    }
                    codecontext.Write("{\"" + arg.VariableName + "\", " + arg.VariableName + "}", codecontext.Level);
                }

                codecontext.Write("});" + codecontext.Output.NewLine);
                codecontext.Level--;
            }

            codecontext.Level--;
            codecontext.WriteLine("}");

            // emit ctors
            codecontext.WriteLine("#region Constructors");
            codecontext.WriteLine("public " + codecontext.ContextName + "(" + ctorArgs + "):base(){InitActionsToDo(" + argsPass + ");}");
            codecontext.WriteLine("public " + codecontext.ContextName + "(" + ((ctorArgs == null) ? "" : (ctorArgs + ", ")) + "StorageBase cache):base(cache){InitActionsToDo(" + argsPass + ");}");
            codecontext.WriteLine("#endregion");

            codecontext.WriteLine("#endregion" + codecontext.Output.NewLine);
        }
예제 #8
0
        internal override ExpressionType EmitCs(EmitCodeContext codecontext)
        {
            if (Body != null)
            {
                MethodCall foreachMethod = ForeachExpression as MethodCall;
                if (foreachMethod == null)
                {
                    throw new GeneratorException(Position, "argument of foreach must be a method call");
                }

                /*  // emit this
                 *  foreach (var x in ForeachMethods.regexp(l.context, @"Porno\s+(?<Title>\w+)"))
                 *  {
                 *      l.Push(null, x);    // or   l.Push(x, null);
                 *
                 *      {Body}
                 *
                 *      l.Pop();
                 *  }
                 */

                string foreachVarName = "__fe" + Position.StartLine + "_" + Position.StartColumn;

                // write foreach header
                codecontext.Write("foreach(var " + foreachVarName + " in ForeachMethods." + foreachMethod.MethodName + "(" + scopeLocalVarName + ".context", codecontext.Level);
                // method arguments
                List <ExpressionType> methodargs = new List <ExpressionType>();
                foreach (var arg in foreachMethod.CallArguments)
                {
                    codecontext.Write(", ");
                    methodargs.Add(arg.EmitCs(codecontext));
                }
                codecontext.Write("))" + codecontext.Output.NewLine);

                // check signature
                Type vartype = CheckMethodSignature(foreachMethod.MethodName, methodargs.ToArray());

                // foreach block
                codecontext.WriteLine("{");

                codecontext.Level++;

                string DataContextVar = (vartype == typeof(linqtoweb.Core.datacontext.DataContext)) ? foreachVarName : "null";
                string VariablesVar   = (vartype == typeof(linqtoweb.Core.extraction.LocalVariables)) ? foreachVarName : "null";

                codecontext.WriteLine(scopeLocalVarName + ".Push(" + DataContextVar + "," + VariablesVar + ");");

                // Body
                Body.EmitCs(codecontext);

                //
                codecontext.WriteLine(scopeLocalVarName + ".Pop();");


                //
                codecontext.Level--;
                codecontext.WriteLine("}");
            }

            return(ExpressionType.VoidType);
        }