public void ForCoverage()
        {
            var statement = new MacroStatement();
            statement.Block.Statements.Add(new MacroStatement());

            CodeBuilderHelper.CreateCallableFromMacroBody(new BooCodeBuilder(new TypeSystemServices()), statement);
        }
Exemplo n.º 2
0
        protected override Statement ExpandImpl(MacroStatement macro){
            var result = new Block();
            foreach (Statement st in macro.Body.Statements){
                var decl = st as DeclarationStatement;
                var refer = st as ExpressionStatement;
                if(null==decl){
                    var ex = refer.Expression;
                    if (ex is MethodInvocationExpression){
                        decl =
                            new DeclarationStatement(
                                new Declaration(((MethodInvocationExpression) refer.Expression).Target.ToCodeString(),
                                                null), null);
                    }
                    if(ex is BinaryExpression){
                        var b = ex as BinaryExpression;
                        decl = new DeclarationStatement(
                            new Declaration(b.Left.ToCodeString(),null),b.Right
                            );
                    }
                }

                var bin = new BinaryExpression(BinaryOperatorType.Assign,
                                               new TryCastExpression(new ReferenceExpression(decl.Declaration.Name),
                                                                     decl.Declaration.Type),
                                               decl.Initializer);
                var def = new MacroStatement("definebrailproperty");
                def.Arguments.Add(bin);
                result.Add(def);
            }
            return result;
        }
Exemplo n.º 3
0
 public override Statement Expand(MacroStatement macro)
 {
     var instance = macro.Arguments[0] as ReferenceExpression;
     var transformer = new WithExpander(instance);
     transformer.Visit(macro.Body);
     return macro.Body;
 }
Exemplo n.º 4
0
 protected override Statement ExpandImpl(MacroStatement macro){
     if (macro.Arguments.Count == 0){
         throw new Exception("output must be called with arguemnts");
     }
     UnescapeInitialAndClosingDoubleQuotes(macro);
     return PrintMacroModule.expandPrintMacro(macro, output, output);
 }
Exemplo n.º 5
0
		public override Statement Expand(MacroStatement macro)
		{
			if (macro.Arguments.Count == 0)
				throw new RailsException("output must be called with arguemnts");
			UnescapeInitialAndClosingDoubleQuotes(macro);
			return Expand(macro, output, output);
		}
Exemplo n.º 6
0
        /// <summary>
        /// Perform the actual expansion of the macro
        /// </summary>
        /// <param name="macro">The macro.</param>
        /// <returns></returns>
        protected override Statement DoExpand(MacroStatement macro)
        {
            List<string> columns = new List<string>();

            if(macro.Block.HasStatements)
            {
                Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "GroupBy cannot contain statements"));
                return null;
            }
            foreach (Expression argument in macro.Arguments)
            {
                ReferenceExpression expr = argument as ReferenceExpression;
                if(expr==null)
                {
                    Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "GroupBy arguments must be refernce expressions. Example: groupBy name, surname"));
                    return null;
                }
                columns.Add(expr.Name);
            }

            Method method = CreateGetColumnsToGroupByMethod(macro, columns);

            ParentMethods.Add(method);

            return null;
        }
Exemplo n.º 7
0
        void ExpandIncludeMacro(MacroStatement macro)
        {
            if (macro.Arguments.Count != 1)
                throw new ScriptParsingException("include requires a single literal string argument ('filename').");

            var include = macro.Arguments[0] as StringLiteralExpression;
            if (include == null)
                throw new ScriptParsingException("include argument should be a literal string ('filename').");

            var fullPath = Path.Combine(
                Path.GetDirectoryName(macro.LexicalInfo.FullPath),
                include.Value
                );

            var compiled = compiler.CompileInclude(fullPath).CompileUnit.Modules[0];

            var module = macro.GetAncestor<Module>();
            foreach (var import in compiled.Imports) {
                module.Imports.Add(import);
            }

            var type = macro.GetAncestor<TypeDefinition>();
            foreach (var member in compiled.Members) {
                type.Members.Add(member);
            }

            var parent = (Block) macro.ParentNode;
            var currentPosition = parent.Statements.IndexOf(macro);

            RemoveCurrentNode();
            foreach (var global in compiled.Globals.Statements.Reverse()) {
                parent.Insert(currentPosition, global);
            }
        }
Exemplo n.º 8
0
        public override void OnMacroStatement(MacroStatement node)
        {
            base.OnMacroStatement(node);

            if (node.Name == "include")
                ExpandIncludeMacro(node);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Perform the actual expansion of the macro
        /// </summary>
        /// <param name="macro">The macro.</param>
        /// <returns></returns>
        protected override Statement DoExpand(MacroStatement macro)
        {
            if (macro.Arguments.Count != 0)
            {
                Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo,"No arguments allowed for action statement"));
                return null;
            }

            Method mergeRowsMethod = new Method("MergeRows");
            mergeRowsMethod.Modifiers = TypeMemberModifiers.Override;
            mergeRowsMethod.Parameters.Add(new ParameterDeclaration("left", new SimpleTypeReference(typeof(Row).FullName)));
            mergeRowsMethod.Parameters.Add(new ParameterDeclaration("right", new SimpleTypeReference(typeof(Row).FullName)));
            CodeBuilder.DeclareLocal(mergeRowsMethod, "row", TypeSystemServices.Map(typeof(Row)));
            mergeRowsMethod.Body.Add(
                new BinaryExpression(BinaryOperatorType.Assign,
                                     new ReferenceExpression("row"),
                                     new MethodInvocationExpression(
                                         AstUtil.CreateReferenceExpression(typeof(Row).FullName))
                    )
                );
            mergeRowsMethod.Body.Add(macro.Body);
            mergeRowsMethod.Body.Add(new ReturnStatement(new ReferenceExpression("row")));

            ParentMethods.Add(mergeRowsMethod);
            return null;
        }
Exemplo n.º 10
0
        protected override Statement ExpandImpl(MacroStatement macro){
            if (macro.Arguments.Count == 0){
                Context.Errors.Add(new CompilerError(macro.LexicalInfo,
                                                     "sub macro requires at least one reference or string attribute for subview name"));
            }

            var call = new MethodInvocationExpression(AstUtil.CreateReferenceExpression("OutputSubView"));
            int i = 0;
            foreach (Expression argument in macro.Arguments){
                i++;
                Expression exp = argument;
                if (i == 1){
//action and contrller parameters
                    if (argument is ReferenceExpression && !(argument is MemberReferenceExpression)){
                        if (argument.ToCodeString().StartsWith("@") || argument.ToCodeString().Contains(".")){
                            exp = AstUtil.CreateReferenceExpression(argument.ToCodeString().Substring(1));
                        }
                        else{
                            exp = new StringLiteralExpression(argument.ToCodeString());
                        }
                    }
                }
                call.Arguments.Add(exp);
            }
            return new ExpressionStatement(call);
        }
Exemplo n.º 11
0
 public override void OnMacroStatement(MacroStatement node)
 {
     using (enter()){
         defaultProcess(node);
         base.OnMacroStatement(node);    
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// Expands the specified macro, validate that the parent is a valid parent, 
        /// leave the actual processing to a base class
        /// </summary>
        /// <param name="macro">The macro.</param>
        /// <returns></returns>
        public override Statement Expand(MacroStatement macro)
        {
            if (ValidateParent(macro, out parent) == false)
                return null;

            return DoExpand(macro);
        }
Exemplo n.º 13
0
            protected override Statement ExpandImpl(MacroStatement macro){
                string tagname = macro.Arguments[0].ToCodeString();

                
                bool useend = macro.Arguments.Contains(x=>x.ToCodeString()=="___end");
                bool usestart = macro.Arguments.Contains(x => x.ToCodeString() == "___start");
                bool wastagmodifiers = useend||usestart;
        
                if(!wastagmodifiers){
                    useend = true;
                    usestart = true;
                }

                IEnumerable<Expression> argsource = macro.Arguments.Skip(1).Where(x=>!(x.ToCodeString().StartsWith("___")));
                StatementCollection statements = macro.Body.Statements;
                
                
                Statement result = tryResolveTemplate(macro, tagname, argsource);
                if (null == result){

                    result = expandBmlElement(tagname, argsource, statements, usestart,useend);
                }
                return result;
            
            }
Exemplo n.º 14
0
        /// <summary>
        /// Expands the specified macro.
        /// </summary>
        /// <param name="macro">The macro.</param>
        /// <returns></returns>
        public override Statement Expand(MacroStatement macro)
        {
            Property property = new Property(propertyName);
            property.LexicalInfo = macro.LexicalInfo;
            property.Getter = new Method();
            if(macro.Arguments.Count==1)
            {
                property.Getter.Body.Add(
                    new ReturnStatement(macro.Arguments[0])
                    );
            }
            else if(
                macro.Arguments.Count == 0 &&
                macro.Body != null &&
				macro.Body.IsEmpty == false)//use the macro block
            {
				property.Getter.Body = macro.Body;
            }
            else
            {
                CompilerErrorFactory.CustomError(macro.LexicalInfo,
                                                 macro.Name + " must have a single expression argument or a block");
                return null;
            }

            ClassDefinition clazz = (ClassDefinition) macro.GetAncestor(NodeType.ClassDefinition);
            clazz.Members.Add(property);

            return null;
        }
Exemplo n.º 15
0
 protected static void PromoteExtensions(MacroStatement macro, MacroStatement parent)
 {
     ApplyExtensions(macro, delegate(Expression extension)
                 {
                     RegisterExtension(parent, extension);
                 });
 }
Exemplo n.º 16
0
 private static void UnescapeInitialAndClosingDoubleQuotes(MacroStatement macro){
     var value = macro.Arguments[0] as StringLiteralExpression;
     if (value == null){
         return;
     }
     value.Value = BrailPreProcessor.UnescapeInitialAndClosingDoubleQuotes(value.Value);
 }
Exemplo n.º 17
0
		public void MacroCompilerIsTakenFromTheEnvironment()
		{
			var compiler = new Mock<MacroCompiler>(MockBehavior.Strict);

			ActiveEnvironment.With(CompilerContextEnvironmentWith(compiler), ()=>
			{
				var module = CreateModule();
				var macroApplication = new MacroStatement(new LexicalInfo("file.boo", 1, 1), "foo");
				module.Globals.Add(macroApplication);

				var macroDefinition = CreateClassOn(module, "FooMacro");

				compiler.Setup(o => o.AlreadyCompiled(macroDefinition)).Returns(false);
				compiler.Setup(o => o.Compile(macroDefinition)).Returns((Type)null);

				var expander = My<MacroExpander>.Instance;
				Assert.IsFalse(expander.ExpandAll());

				var errors = CompilerErrors();
				Assert.AreEqual(1, errors.Count);
				Assert.AreEqual(CompilerErrorFactory.AstMacroMustBeExternal(macroApplication, (IType) macroDefinition.Entity).ToString(), errors[0].ToString());
			});

			compiler.VerifyAll();
		}
Exemplo n.º 18
0
 protected override IEnumerable<Node> ExpandGeneratorImpl(MacroStatement macro){
     var method = new Method("_key"){
                                        ReturnType = new SimpleTypeReference("string"),
                                        Modifiers = TypeMemberModifiers.Public | TypeMemberModifiers.Override,
                                        Body = macro.extractMethodBody()
                                    };
     yield return method;
 }
Exemplo n.º 19
0
 protected override IEnumerable<Node> ExpandGeneratorImpl(MacroStatement macro){
     string typename = macro.Arguments[0].ToCodeString() + ", " + macro.Arguments[1].ToCodeString();
     //for normalizing nested classes parsed as add operators
     typename = typename.Replace(" ", "");
     var type = Type.GetType(typename);
     var generator = (IAstIncludeSource)type.GetConstructor(Type.EmptyTypes).Invoke(null);
     return generator.Substitute(macro);
 }
Exemplo n.º 20
0
 public override void OnMacroStatement(MacroStatement node)
 {
     if (node.Name == "BODY")
     {
         node.ReplaceBy(this.block);
     }
     else{
         base.OnMacroStatement(node);
     }
 }
Exemplo n.º 21
0
 protected override Statement ExpandImpl(MacroStatement macro)
 {
     var result = new MacroStatement("output");
     foreach (var argument in macro.Arguments){
         result.Arguments.Add(argument);
         
     }
     result.Arguments.Add(new StringLiteralExpression(Environment.NewLine));
     return result;
 }
Exemplo n.º 22
0
 protected override Statement ExpandImpl(MacroStatement macro){
     var call = new MethodInvocationExpression(
         new ReferenceExpression("__Export")
         );
     call.Arguments.Add(new StringLiteralExpression(macro.Arguments[0].ToCodeString()));
     if(macro.Arguments.Count==2){
         call.Arguments.Add(macro.Arguments[1]);
     }
     return new ExpressionStatement(call);
 }
Exemplo n.º 23
0
 protected override Statement ExpandImpl(MacroStatement macro){
     if (macro.Body != null && !macro.Body.IsEmpty){
         appendCell(macro,
                    new CellDefinition{Value = macro.Body, Attributes = macro.Arguments});
     }
     else{
         appendCell(macro,
                    new CellDefinition{Value = macro.Arguments[0], Attributes = macro.Arguments.Skip(1)});
     }
     return null;
 }
Exemplo n.º 24
0
        protected static void RegisterExtension(MacroStatement macro, Expression extension)
        {
            var extenstions = macro[ExtensionsKey] as List<Expression>;
            if (extenstions == null)
            {
                extenstions = new List<Expression>();
                macro[ExtensionsKey] = extenstions;
            }

            extenstions.Add(extension);
        }
Exemplo n.º 25
0
        /// <summary>
        /// Perform the actual expansion of the macro
        /// </summary>
        /// <param name="macro">The macro.</param>
        /// <returns></returns>
        protected override Statement DoExpand(MacroStatement macro)
        {
            Method rowProcessed = new Method("OnFinishedProcessing");
            rowProcessed.Modifiers = TypeMemberModifiers.Override;
            rowProcessed.Parameters.Add(new ParameterDeclaration("op", CodeBuilder.CreateTypeReference(typeof(IOperation))));

            rowProcessed.Body = macro.Body;

            ParentMethods.Add(rowProcessed);

            return null;
        }
Exemplo n.º 26
0
        /// <summary>
        /// Perform the actual expansion of the macro
        /// </summary>
        /// <param name="macro">The macro.</param>
        /// <returns></returns>
        protected override Statement DoExpand(MacroStatement macro)
        {
            Method accumulate = new Method("FinishAggregation");
            accumulate.Modifiers = TypeMemberModifiers.Override;
            accumulate.Parameters.Add(new ParameterDeclaration("aggregate", CodeBuilder.CreateTypeReference(typeof(Row))));

            accumulate.Body = macro.Body;

            ParentMethods.Add(accumulate);

            return null;
        }
Exemplo n.º 27
0
        protected static bool ProcessStatements(MacroStatement macro, Predicate<Statement> action)
        {
            if (macro.Block.HasStatements)
            {
                foreach (Statement statement in macro.Block.Statements)
                {
                    if (!action(statement)) return false;
                }
            }

            return true;
        }
Exemplo n.º 28
0
        protected static bool ProcessStatements(MacroStatement macro, Predicate<Statement> action)
        {
            if (macro.Body.IsEmpty == false)
            {
                foreach (var statement in macro.Body.Statements)
                {
                    if (action(statement) == false)
                        return false;
                }
            }

            return true;
        }
Exemplo n.º 29
0
        protected static bool ApplyExtensions(MacroStatement macro,
		                                      Action<Expression> onExtension)
        {
            var extenstions = macro[ExtensionsKey] as List<Expression>;
            if (extenstions != null && extenstions.Count > 0)
            {
                foreach(var extenstion in extenstions)
                {
                    onExtension(extenstion);
                }
            }
            return true;
        }
Exemplo n.º 30
0
 private Statement tryResolveTemplate(MacroStatement macro, string tagname, IEnumerable<Expression> argsource){
     MacroStatement bml = macro.findMacroContainer("bml");
     IDictionary<string, MacroStatement> templates = null;
     if (bml.ContainsAnnotation("templates")){
         templates = bml.get<IDictionary<string, MacroStatement>>("templates");
     }
     if (templates != null && templates.ContainsKey(tagname)){
         MacroStatement targetmacro = templates[tagname].CloneNode();
         bindParameters(macro,targetmacro, argsource);
         return targetmacro.Body;
     }
     return null;
 }
Exemplo n.º 31
0
 B.MacroStatement CreateMacro(INode node, string name, Statement embedded, params Expression[] arguments)
 {
     B.MacroStatement macro = new B.MacroStatement(GetLexicalInfo(node));
     macro.Name = name;
     ConvertExpressions(arguments, macro.Arguments);
     if (embedded is BlockStatement)
     {
         macro.Body = ConvertBlock((BlockStatement)embedded);
     }
     else
     {
         macro.Body = new B.Block();
         macro.Body.Add((B.Statement)embedded.AcceptVisitor(this, null));
     }
     return(macro);
 }
Exemplo n.º 32
0
        public MacroStatement GetParentMacroByName(string name)
        {
            MacroStatement parent = GetAncestor <MacroStatement>();

            while (null != parent)
            {
                if (parent.Name == name)
                {
                    return(parent);
                }
                else if (parent.Name == "macro")                 //macro macro
                {
                    if (name == (parent.Arguments[0] as ReferenceExpression).Name)
                    {
                        return(parent);
                    }
                }
                parent = parent.GetAncestor <MacroStatement>();
            }
            return(null);
        }
Exemplo n.º 33
0
 public abstract IEnumerable <Ast.Node> ExpandGenerator(Ast.MacroStatement macro);
Exemplo n.º 34
0
 public abstract Ast.Statement Expand(Ast.MacroStatement macro);