Beispiel #1
0
 public void addAttributes(CodeTypeDeclaration newClass, XSharpParser.AttributesContext attributes)
 {
     if (attributes != null)
     {
         newClass.CustomAttributes = GenerateAttributes(attributes);
     }
 }
Beispiel #2
0
        public CodeAttributeDeclarationCollection GenerateAttributes(XSharpParser.AttributesContext context)
        {
            /*
             *  Parser definition for attributes
             *  attributes          : ( AttrBlk+=attributeBlock )+
             *                      ;
             *  attributeBlock      : LBRKT Target=attributeTarget? Attributes+=attribute (COMMA Attributes+=attribute)* RBRKT
             *                      ;
             *  attributeTarget     : Id=identifier COLON
             | Kw=keyword COLON
             |                      ;
             |  attribute           : Name=name (LPAREN (Params+=attributeParam (COMMA Params+=attributeParam)* )? RPAREN )?
             |                      ;
             |  attributeParam      : Name=identifierName Op=assignoperator Expr=expression
             | Expr=expression
             | attributes      = CodeAttributeDeclarationCollection
             | attributeBlock  = list of attributes: CodeAttributeDeclaration
             | attributeTarget = not used at this level.
             | attribute       = CodeAttributeDeclaration
             | arg for attrib = CodeAttributeArgument
             | list of args   = CodeAttributeArgumentCollection
             */
            var result = new CodeAttributeDeclarationCollection();

            foreach (var blk in context._AttrBlk)
            {
                foreach (var attr in blk._Attributes)
                {
                    var name     = attr.Name.GetText();
                    var codeattr = new CodeAttributeDeclaration(name);
                    foreach (XSharpParser.AttributeParamContext par in attr._Params)
                    {
                        var arg = new CodeAttributeArgument();
                        if (par is XSharpParser.PropertyAttributeParamContext)
                        {
                            var papc = par as XSharpParser.PropertyAttributeParamContext;
                            if (papc.Name != null)
                            {
                                arg.Name = papc.Name.GetText();
                            }
                            arg.Value = BuildExpression(papc.Expr, false);
                        }
                        else if (par is XSharpParser.ExprAttributeParamContext)
                        {
                            var eapc = par as XSharpParser.ExprAttributeParamContext;
                            arg.Value = BuildExpression(eapc.Expr, false);
                        }
                        codeattr.Arguments.Add(arg);
                    }
                    result.Add(codeattr);
                }
            }
            return(result);
        }