/// <summary> /// Perform the actual expansion of the macro /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> protected override Statement DoExpand(MacroStatement macro) { Block body = (Block)macro.GetAncestor(NodeType.Block); MacroStatement macroParent = (MacroStatement)macro.GetAncestor(NodeType.MacroStatement); if (macro.Body.Statements.Count < 1 && parent.Name == "join") { Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Join " + name + " section must contain at least a single expression statement")); return(null); } if (macro.Arguments.Count == 0 && parent.Name != "join") { Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Join " + name + " section must contain at least one key column")); return(null); } if (macro.Arguments.Count > 0) { ArrayLiteralExpression ale = new ArrayLiteralExpression(macro.LexicalInfo); ale.Type = new ArrayTypeReference(CodeBuilder.CreateTypeReference(typeof(string))); foreach (Expression argument in macro.Arguments) { ReferenceExpression expr = argument as ReferenceExpression; if (expr == null) { Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Join " + name + " section arguments must be reference expressions. Example: " + name + " name, surname")); return(null); } ale.Items.Add(new StringLiteralExpression(expr.Name)); } var keyExpr = new BinaryExpression(BinaryOperatorType.Assign, new ReferenceExpression(name + "KeyColumns"), ale); macroParent.Arguments.Add(keyExpr); } foreach (Statement statement in macro.Body.Statements) { ExpressionStatement exprStmt = statement as ExpressionStatement; if (exprStmt == null) { Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Join " + name + " section can only contain expressions")); return(null); } Expression expr = exprStmt.Expression; parent.Arguments.Add(new MethodInvocationExpression(new ReferenceExpression(name), expr)); } return(null); }
/// <summary> /// Expands the specified macro. /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> public override Statement Expand(MacroStatement macro) { var 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); } var clazz = (ClassDefinition)macro.GetAncestor(NodeType.ClassDefinition); clazz.Members.Add(property); return(null); }
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); } }
private void ExpandImport(Import import) { ImportCollection imports = _node.GetAncestor <Module>().Imports; if (imports.Contains(import.Matches)) { return; } imports.Add(import); BindImport(import); }
/// <summary> /// Gets the name of the anonymous class. /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> private string GetAnonymousClassName(MacroStatement macro) { if (macro.GetAncestor(NodeType.MacroStatement) == null) { Errors.Add( CompilerErrorFactory.CustomError(macro.LexicalInfo, GetType().Name + " must have a single parameter, the name of the " + GetType().Name)); return(null); } isAnonymous = true; string name = typeof(T).Name.Replace("Abstract", "").Replace("Operation", ""); if (macro["anonymous_name_index"] == null) { macro["anonymous_name_index"] = Context.GetUniqueName(); } return("Anonymous_" + name + "_" + macro["anonymous_name_index"]); }
/// <summary> /// Expands the specified macro /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> public override Statement Expand(MacroStatement macro) { classDefinition = CreateClassDefinition(macro); CreateMethodFromMacroBlock(macro, classDefinition); Module ancestor = (Module)macro.GetAncestor(NodeType.Module); ancestor.Members.Add(classDefinition); AddMemberMethods(macro); if (isAnonymous == false) { return(null); } ReferenceExpression typeName = AstUtil.CreateReferenceExpression(GetClassName(macro)); MethodInvocationExpression createInstance = new MethodInvocationExpression(typeName); return(new ExpressionStatement(createInstance)); }
/// <summary> /// Perform the actual expansion of the macro /// </summary> /// <param name="macro">The macro.</param> /// <returns></returns> protected override Statement DoExpand(MacroStatement macro) { Block body = (Block)macro.GetAncestor(NodeType.Block); if (macro.Body.Statements.Count < 1) { Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Join" + name + " section must contain at least a single expression statement")); return(null); } foreach (Statement statement in macro.Body.Statements) { ExpressionStatement exprStmt = statement as ExpressionStatement; if (exprStmt == null) { Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "Join" + name + " section can only contain expressions")); return(null); } Expression expr = exprStmt.Expression; MethodInvocationExpression expression = new MethodInvocationExpression(new ReferenceExpression(name), expr); body.Add(new ExpressionStatement(expression)); } return(null); }
public override Statement Expand(MacroStatement macro) { componentContextName = ComponentNaming.GetComponentContextName(macro); componentFactoryName = ComponentNaming.GetComponentFactoryName(macro); componentVariableName = ComponentNaming.GetComponentNameFor(macro); if (macro.Arguments.Count == 0) { throw new MonoRailException("Component must be called with a name"); } var block = new Block(); var method = (Method)macro.GetAncestor(NodeType.Method); var componentName = new StringLiteralExpression(macro.Arguments[0].ToString()); var dictionary = CreateParametersDictionary(macro); var macroBody = CodeBuilderHelper.CreateCallableFromMacroBody(CodeBuilder, macro); var initContext = new MethodInvocationExpression { Target = AstUtil.CreateReferenceExpression("Castle.MonoRail.Views.Brail.BrailViewComponentContext") }; initContext.Arguments.Extend( new[] { new SelfLiteralExpression(), macroBody, componentName, AstUtil.CreateReferenceExpression("OutputStream"), dictionary }); // compilerContext = BrailViewComponentContext(macroBodyClosure, "componentName", OutputStream, dictionary) block.Add(new BinaryExpression(BinaryOperatorType.Assign, new ReferenceExpression(componentContextName), initContext)); // AddViewComponentProperties( compilerContext.ComponentParams ) var addProperties = new MethodInvocationExpression(AstUtil.CreateReferenceExpression("AddViewComponentProperties")); addProperties.Arguments.Add(AstUtil.CreateReferenceExpression(componentContextName + ".ComponentParameters")); block.Add(addProperties); var viewComponentFactoryLocal = CodeBuilder.DeclareLocal(method, componentFactoryName, TypeSystemServices.Map( typeof(IViewComponentFactory))); // viewComponentFactory = context.GetService(IViewComponentFactory) var callService = new MethodInvocationExpression( AstUtil.CreateReferenceExpression("context.GetService")); callService.Arguments.Add(CodeBuilder.CreateTypeofExpression(typeof(IViewComponentFactory))); block.Add(new BinaryExpression(BinaryOperatorType.Assign, CodeBuilder.CreateLocalReference(componentFactoryName, viewComponentFactoryLocal), callService)); // component = viewComponentFactory.Create( componentName) var createComponent = new MethodInvocationExpression( new MemberReferenceExpression(CodeBuilder.CreateLocalReference(componentFactoryName, viewComponentFactoryLocal), "Create")); createComponent.Arguments.Add(componentName); block.Add(new BinaryExpression(BinaryOperatorType.Assign, new ReferenceExpression(componentVariableName), createComponent)); AddSections(block, macro); // component.Init(context, componentContext) var initComponent = new MethodInvocationExpression( AstUtil.CreateReferenceExpression(componentVariableName + ".Init")); initComponent.Arguments.Extend( new Expression[] { new ReferenceExpression("context"), new ReferenceExpression(componentContextName) }); block.Add(initComponent); // component.Render() block.Add(new MethodInvocationExpression( AstUtil.CreateReferenceExpression(componentVariableName + ".Render"))); // if component.ViewToRender is not null: // OutputSubView("/"+component.ViewToRender, context.CompnentParameters) var renderView = new Block(); var outputSubView = new MethodInvocationExpression( AstUtil.CreateReferenceExpression("OutputSubView")); outputSubView.Arguments.Add(new BinaryExpression(BinaryOperatorType.Addition, new StringLiteralExpression("/"), AstUtil.CreateReferenceExpression(componentContextName + ".ViewToRender"))); outputSubView.Arguments.Add(AstUtil.CreateReferenceExpression(componentContextName + ".ComponentParameters")); renderView.Add(outputSubView); block.Add(new IfStatement(AstUtil.CreateReferenceExpression(componentContextName + ".ViewToRender"), renderView, new Block())); // RemoveViewComponentProperties( compilerContext.ComponentParams ) var removeProperties = new MethodInvocationExpression(AstUtil.CreateReferenceExpression("RemoveViewComponentProperties")); removeProperties.Arguments.Add(AstUtil.CreateReferenceExpression(componentContextName + ".ComponentParameters")); block.Add(removeProperties); return(block); }