Пример #1
0
        public Expression Parse(CsExpression pStatement, FactoryExpressionCreator pCreator)
        {
            //expression "." identifier (type-argument-list?)
            CsPrimaryExpressionMemberAccess ex = (CsPrimaryExpressionMemberAccess)pStatement;
            string name;
            bool   renamed = Helpers.GetRealName(ex, ex.identifier.identifier, out name);

            if (renamed)
            {
                return(new Expression(name, pStatement.entity_typeref));
            }

            CsEntityProperty p          = ex.entity as CsEntityProperty;
            bool             isInternal = false;

            if (p != null && p.decl != null)
            {
                TheClass theClass = TheClassFactory.Get(p, pCreator);
                TheClass parent   = theClass;

                //Am I extending a standard flash class? Do not rename then...
                bool isStandardGetSet = false;
                while (parent.Base != null)
                {
                    isStandardGetSet |= parent.FullName.StartsWith("flash.");
                    parent            = parent.Base;
                }

                if (!isStandardGetSet)
                {
                    TheProperty theProperty = theClass.GetProperty((CsProperty)p.decl);
                    if (theProperty != null)
                    {
                        if (ex.parent is CsAssignmentExpression)
                        {
                            //setter
                            isInternal = true;
                            name       = "set_" + name + "({0})";
                        }
                        else
                        {
                            //getter, rename
                            name = "get_" + name + "()";
                        }
                    }
                }
            }
            else if (ex.ec == expression_classification.ec_event_access)                //remove eventhandler name
            {
                name = string.Empty;
            }

            return(new Expression(
                       pCreator.Parse(ex.expression).Value + "." + name,
                       pStatement.entity_typeref,
                       isInternal
                       ));
        }
Пример #2
0
        internal static Expression getIndexerExpression(CsExpression pStatement, IEnumerable <CsExpression> pList,
                                                        Expression pIndexer, bool pForce, bool pGetSetter, FactoryExpressionCreator pCreator)
        {
            TheClass k = TheClassFactory.Get(pStatement, pCreator);

            List <string>          indexes = new List <string>();
            List <CsEntityTypeRef> param   = new List <CsEntityTypeRef>();

            foreach (CsExpression ex in pList)
            {
                Expression te = pCreator.Parse(ex);
                indexes.Add(te.Value);
                param.Add(te.Type);
            }

            string     exp        = pIndexer == null ? "super" : pIndexer.Value;
            bool       isInternal = false;
            TheIndexer i          = null;

            if (k != null)
            {
                i = k.GetIndexer(pStatement);
            }

            //TODO: Check array access...
            if (i == null || pIndexer == null || (pIndexer.Type != null && pIndexer.Type.type == cs_entity_type.et_array))
            {
                //Array access or unknown accessor
                exp += "[" + indexes[0] + "]";
            }
            else
            {
                //TheIndexers i = k.GetIndexerBySignature(param);
                isInternal = true;

                CsAssignmentExpression parent = pStatement.parent as CsAssignmentExpression;

                if (!pForce)
                {
                    pGetSetter = parent != null && parent.lhs.Equals(pStatement);
                }

                if (pGetSetter)
                {
                    exp += "." + i.Setter.Name + "(" + string.Join(", ", indexes.ToArray()) + ", {0})";
                }
                else
                {
                    exp += "." + i.Getter.Name + "(" + string.Join(", ", indexes.ToArray()) + ")";
                }
            }

            return(new Expression(exp, pStatement.entity_typeref, isInternal));
        }
Пример #3
0
        public Expression Parse(CsExpression pStatement, FactoryExpressionCreator pCreator)
        {
            CsInvocationExpression ex = (CsInvocationExpression)pStatement;

            List <string> indexes = new List <string>();

            if (ex.argument_list != null)
            {
                foreach (CsArgument argument in ex.argument_list.list)
                {
                    indexes.Add(pCreator.Parse(argument.expression).Value);
                }
            }

            TheClass  c = TheClassFactory.Get(pStatement, pCreator);
            TheMethod m;

            CsEntityMethod   method         = ex.entity as CsEntityMethod;
            CsEntityDelegate entityDelegate = ex.entity as CsEntityDelegate;

            string name = pCreator.Parse(ex.expression).Value;

            //call es de tipo super. Necesito saber cuál es la clase heredada)
            if (name.EndsWith("super.", StringComparison.Ordinal))
            {
                c    = c.Base;
                m    = c.GetMethod(method, pCreator);
                name = name + m.Name;
            }
            else if (method != null)
            {
                //si es una expresión de tipo xx.yy.method(), tengo que revisar la expresión
                //porque la invocación se da como expresión completa...
                c = TheClassFactory.Get(method.parent, pCreator);
                m = c.GetMethod(method, pCreator);

                if (m.IsExtensionMethod)
                {
                    int fnIndex = name.IndexOf(m.Name);
                    if (fnIndex > 0)
                    {
                        fnIndex--;
                    }

                    indexes.Insert(0, name.Substring(0, fnIndex));

                    if (Helpers.HasAttribute(((CsEntityClass)method.parent).attributes, "As3ExtensionAttribute"))
                    {
                        name = m.MyClass.FullName;
                        name = name.Substring(0, name.LastIndexOf('.'));
                        name = name + "." + m.Name;
                        ImportStatementList.AddImport(name);
                        name = m.Name;
                    }
                    else
                    {
                        name = m.FullName;
                    }
                }
                else
                {
                    name = name.Replace(m.Name, m.Name);
                }
            }
            else if (entityDelegate != null)
            {
                //es un evento?
                if (ex.expression.ec == expression_classification.ec_event_access)
                {
                    TheEvent theEvent = c.GetEvent(name);
                    name = theEvent.IsFlashEvent ?
                           "dispatchEvent" :
                           string.Format(@"if (_e{0}) _e{0}.fire", name);
                }
            }

            //patch
            if (name.Contains("{0}"))
            {
                string p = indexes[0];
                indexes.RemoveAt(0);
                name = string.Format(name, p, string.Join(", ", indexes.ToArray()));
            }
            else
            {
                name = name + "(" + string.Join(", ", indexes.ToArray()) + ")";
            }

            return(new Expression(
                       name,
                       ex.entity_typeref
                       ));
        }
Пример #4
0
        public static void Parse(CsClass pCsClass, CodeBuilder pBuilder, FactoryExpressionCreator pCreator)
        {
            ExtensionName = null;

            StringBuilder sb             = new StringBuilder();
            CodeBuilder   privateClasses = new CodeBuilder();

            TheClass myClass = TheClassFactory.Get(pCsClass, pCreator);

            IsMainClass = Helpers.HasAttribute(pCsClass.attributes, "JsMainClassAttribute");
            bool isResource = Helpers.HasAttribute(pCsClass.attributes, "JsEmbedAttribute");

            IsExtension = Helpers.HasAttribute(pCsClass.attributes, "JsExtensionAttribute");

            if (IsMainClass)
            {
                JsNamespaceParser.MainClassName = myClass.FullName;
                AttributeItem vals = Helpers.GetAttributeValue(pCsClass.attributes, "JsMainClassAttribute", pCreator)[0];
                sb.AppendFormat(@"[SWF(width=""{0}"", height=""{1}"", frameRate=""{2}"", backgroundColor=""{3}"")]",
                                vals.Parameters[0],
                                vals.Parameters[1],
                                vals.Parameters[2],
                                vals.Parameters[3]
                                );
                sb.AppendLine();
                sb.Append("\t");
            }

            if (isResource)
            {
                AttributeItem vals = Helpers.GetAttributeValue(pCsClass.attributes, "JsEmbedAttribute", pCreator)[0];

                string path = vals.Parameters[0] as String;
                if (!string.IsNullOrEmpty(path))
                {
                    path = Path.Combine(Project.Root, path);
                    string ex = Path.GetExtension(path).Substring(1);
                    string mimeType;

                    if (vals.NamedArguments.ContainsKey("mimeType"))
                    {
                        mimeType = vals.NamedArguments["mimeType"].Value;
                    }
                    else
                    {
                        switch (ex)
                        {
                        case @"gif":
                        case @"png":
                        case @"jpg":
                        case @"jpeg":
                        case @"svg":
                            mimeType = "image/" + ex;
                            break;

                        case @"mp3":
                            mimeType = @"audio/mpeg";
                            break;

                        case @"swf":
                            mimeType = @"application/x-shockwave-flash";
                            break;

                        case @"ttf":
                        case @"otf":
                            mimeType = @"application/x-font";
                            break;

                        default:
                            mimeType = @"application/octet-stream";
                            break;
                        }
                    }

                    StringBuilder addParams = new StringBuilder();
                    foreach (var item in vals.NamedArguments.Where(pItem => !pItem.Key.Equals("mimeType")))
                    {
                        addParams.AppendFormat(@", {0}=""{1}""", item.Key, item.Value.Value);
                    }

                    sb.AppendFormat(@"[Embed(source=""{0}"", mimeType=""{1}""{2})]",
                                    path.Replace("\\", "\\\\"),
                                    mimeType,
                                    addParams
                                    );

                    sb.AppendLine();
                    sb.Append("\t");
                }
            }

            if (!IsExtension)
            {
                sb.AppendFormat("{1}class {0}",
                                myClass.Name,
                                JsHelpers.ConvertModifiers(myClass.Modifiers, _notValidClassMod));

                if (myClass.Extends.Count != 0)
                {
                    sb.AppendFormat(" extends {0}", JsHelpers.Convert(myClass.Extends[0]));
                }

                if (myClass.Implements.Count != 0)
                {
                    sb.Append(" implements ");
                    foreach (string s in myClass.Implements)
                    {
                        sb.Append(JsHelpers.Convert(s));
                        sb.Append(", ");
                    }

                    sb.Remove(sb.Length - 2, 2);
                }

                sb.Append(" {");
                sb.AppendLine();

                pBuilder.Append(sb.ToString());
                pBuilder.AppendLineAndIndent();
            }

            if (IsMainClass)
            {
                ImportStatementList.List.Add("flash.events.Event");
                pBuilder.AppendFormat(
                    @"public function {0}() {{
			if (stage) $ctor();
			else addEventListener(Event.ADDED_TO_STAGE, __loaded);
		}}

		private function __loaded(e:Event = null):void {{
			removeEventListener(Event.ADDED_TO_STAGE, __loaded);
			$ctor();
		}}
",
                    myClass.Name);
                pBuilder.AppendLine();
            }

            if (pCsClass.member_declarations != null)
            {
                if (!myClass.IsPrivate)
                {
                    ImportStatementList.List.Add(myClass.NameSpace + ".*");
                }

                foreach (CsNode memberDeclaration in pCsClass.member_declarations)
                {
                    if (memberDeclaration is CsConstructor)
                    {
                        MethodParser.Parse(myClass.GetConstructor((CsConstructor)memberDeclaration), pBuilder, pCreator);
                    }
                    else if (memberDeclaration is CsMethod)
                    {
                        MethodParser.Parse(myClass.GetMethod((CsMethod)memberDeclaration), pBuilder, pCreator);
                        if (IsExtension && string.IsNullOrEmpty(ExtensionName))
                        {
                            ExtensionName = ((CsMethod)memberDeclaration).identifier.identifier;
                        }
                    }
                    else if (memberDeclaration is CsIndexer)
                    {
                        IndexerParser.Parse(myClass.GetIndexer((CsIndexer)memberDeclaration), pBuilder, pCreator);
                    }
                    else if (memberDeclaration is CsVariableDeclaration)
                    {
                        VariableParser.Parse(myClass.GetVariable((CsVariableDeclaration)memberDeclaration), pBuilder);
                    }
                    else if (memberDeclaration is CsConstantDeclaration)
                    {
                        ConstantParser.Parse(myClass.GetConstant((CsConstantDeclaration)memberDeclaration), pBuilder);
                    }
                    else if (memberDeclaration is CsDelegate)
                    {
                        DelegateParser.Parse(myClass.GetDelegate((CsDelegate)memberDeclaration), pBuilder);
                    }
                    else if (memberDeclaration is CsEvent)
                    {
                        EventParser.Parse(myClass.GetEvent(((CsEvent)memberDeclaration).declarators.First.Value.identifier.identifier), pBuilder);
                    }
                    else if (memberDeclaration is CsProperty)
                    {
                        PropertyParser.Parse(myClass.GetProperty((CsProperty)memberDeclaration), pBuilder, pCreator);
                    }
                    else if (memberDeclaration is CsClass)
                    {
                        Parse((CsClass)memberDeclaration, privateClasses, pCreator);
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
            }

            string imports = getImports();

            pBuilder.Replace(IMPORT_MARKER, imports);

            pBuilder.AppendLineAndUnindent("}");

            if (IsExtension)
            {
                return;
            }

            if (!myClass.IsPrivate)
            {
                pBuilder.AppendLineAndUnindent("}");
            }

            if (privateClasses.Length == 0)
            {
                return;
            }

            pBuilder.AppendLine();
            pBuilder.Append(JsNamespaceParser.Using);
            pBuilder.AppendLine(imports);
            pBuilder.Append(privateClasses);
        }
Пример #5
0
        public static void Parse(CsInterface pCsInterface, CodeBuilder pBuilder, FactoryExpressionCreator pCreator)
        {
            StringBuilder sb      = new StringBuilder();
            TheClass      myClass = TheClassFactory.Get(pCsInterface, pCreator);

            sb.AppendFormat("{1}interface {0}",
                            myClass.Name,
                            As3Helpers.ConvertModifiers(myClass.Modifiers, _notValidClassMod));

            if (myClass.Implements.Count != 0)
            {
                sb.Append(" extends ");
                foreach (string s in myClass.Implements)
                {
                    sb.Append(As3Helpers.Convert(s));
                    sb.Append(", ");
                }

                sb.Remove(sb.Length - 2, 2);
            }

            sb.Append(" {");
            sb.AppendLine();

            pBuilder.Append(sb.ToString());
            pBuilder.AppendLineAndIndent();

            if (pCsInterface.member_declarations != null)
            {
                foreach (CsNode memberDeclaration in pCsInterface.member_declarations)
                {
                    //if (memberDeclaration is CsConstructor) {
                    //    MethodParser.Parse(myClass.GetConstructor((CsConstructor)memberDeclaration), pBuilder);
                    //} else
                    if (memberDeclaration is CsMethod)
                    {
                        MethodParser.Parse(myClass.GetMethod((CsMethod)memberDeclaration), pBuilder, pCreator);
                    }
                    else if (memberDeclaration is CsIndexer)
                    {
                        IndexerParser.Parse(myClass.GetIndexer((CsIndexer)memberDeclaration), pBuilder, pCreator);
                    }
                    else if (memberDeclaration is CsVariableDeclaration)
                    {
                        VariableParser.Parse(myClass.GetVariable((CsVariableDeclaration)memberDeclaration), pBuilder);
                    }
                    else
                    //if (memberDeclaration is CsConstantDeclaration) {
                    //    ConstantParser.Parse(myClass.GetConstant((CsConstantDeclaration)memberDeclaration), pBuilder);
                    //} else
                    //if (memberDeclaration is CsDelegate) {
                    //    DelegateParser.Parse(myClass.GetDelegate((CsDelegate)memberDeclaration), pBuilder);
                    //} else
                    //if (memberDeclaration is CsEvent) {
                    //    EventParser.Parse(myClass.GetEvent(((CsEvent)memberDeclaration).declarators.First.Value.identifier.identifier),
                    //                      pBuilder);
                    //} else
                    if (memberDeclaration is CsProperty)
                    {
                        PropertyParser.Parse(myClass.GetProperty((CsProperty)memberDeclaration), pBuilder, pCreator);
                        //} else if (memberDeclaration is CsInterface) {
                        //    Parse((CsInterface)memberDeclaration, privateClasses);
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
            }

            pBuilder.AppendLineAndUnindent("}");
            pBuilder.AppendLineAndUnindent("}");
            pBuilder.AppendLine();
            string imports = ClassParser.getImports();

            pBuilder.Replace(ClassParser.IMPORT_MARKER, imports);
        }
Пример #6
0
        private static void parseForeachStatement(CsStatement pStatement, CodeBuilder pSb, FactoryExpressionCreator pCreator)
        {
            CsForeachStatement fes = (CsForeachStatement)pStatement;

            Expression ex   = pCreator.Parse(fes.expression);
            string     type = As3Helpers.Convert(Helpers.GetType(fes.type));

            pSb.AppendLine();

            TheClass theClass = TheClassFactory.Get(ex.Type, pCreator);

            if (ex.Type.type == cs_entity_type.et_array || ex.IsAs3Generic ||
                (theClass != null && @"System.Array".Equals(theClass.FullName)))                     //foreach
            {
                pSb.AppendFormat("for each(var {0}:{1} in {2}){{",
                                 fes.identifier.identifier,
                                 type,
                                 ex.Value);
                pSb.AppendLine();
            }
            else if (ex.Type.type == cs_entity_type.et_object ||
                     ex.Type.type == cs_entity_type.et_generic_param ||
                     (theClass != null && @"flash.utils.Dictionary".Equals(theClass.FullName)))
            {
                pSb.AppendFormat("for (var {0}:{1} in {2}){{",
                                 fes.identifier.identifier,
                                 type,
                                 ex.Value);

                pSb.AppendLine();
                if (ex.Type.type == cs_entity_type.et_object)
                {
                    pSb.AppendFormat("	if (!{1}.hasOwnProperty({0})) continue;",
                                     fes.identifier.identifier,
                                     ex.Value
                                     );
                }

                pSb.AppendLine();
            }
            else
            {
                _enumCount++;
                //TheClass theClass = TheClassFactory.Get(fes.expression.entity_typeref);

                string enumName = String.Format(@"__ie{0}", _enumCount);
                pSb.AppendFormat("var {0}:IEnumerator = {1}.GetEnumerator();", enumName, ex.Value);
                pSb.AppendLine();
                pSb.AppendFormat("while ({0}.MoveNext()){{", enumName);
                pSb.AppendLine();

                pSb.AppendFormat("\tvar {1}:{2} = {0}.get_Current() as {2};",
                                 enumName,
                                 fes.identifier.identifier,
                                 type
                                 );

                pSb.AppendLine();
            }

            ParseNode(fes.statement, pSb, pCreator);
            pSb.AppendLine("}");
            pSb.AppendLine();
        }
Пример #7
0
        public Expression Parse(CsExpression pStatement, FactoryExpressionCreator pCreator)
        {
            CsAssignmentExpression ex = (CsAssignmentExpression)pStatement;

            Expression left  = pCreator.Parse(ex.lhs);
            Expression right = pCreator.Parse(ex.rhs);

            if (((ex.lhs is CsElementAccess) || (ex.lhs is CsPrimaryExpressionMemberAccess)) && left.InternalType)
            {
                switch (ex.oper)
                {
                case CsTokenType.tkASSIGN:
                    return(new Expression(string.Format(left.Value, right.Value), pStatement.entity_typeref));

                case CsTokenType.tkPLUS_EQ:
                case CsTokenType.tkMINUS_EQ:
                case CsTokenType.tkDIV_EQ:
                case CsTokenType.tkMUL_EQ:
                    string getter = ElementAccessHelper.parseElementAccess(ex.lhs, true, false, pCreator).Value;
                    return(new Expression(string.Format(left.Value, getter + As3Helpers.ConvertTokens(Helpers.GetTokenType(convertToken(ex.oper))) + right.Value), pStatement.entity_typeref));
                }
            }

            if ((ex.lhs is CsSimpleName) && left.InternalType)
            {
                switch (ex.oper)
                {
                case CsTokenType.tkASSIGN:
                    return(new Expression(string.Format(left.Value, right.Value), pStatement.entity_typeref));

                case CsTokenType.tkPLUS_EQ:
                case CsTokenType.tkMINUS_EQ:
                case CsTokenType.tkDIV_EQ:
                case CsTokenType.tkMUL_EQ:
                    string getter = SimpleNameHelper.ParseSimpleName(ex.lhs, true, false).Value;
                    return(new Expression(string.Format(left.Value, getter + As3Helpers.ConvertTokens(Helpers.GetTokenType(convertToken(ex.oper))) + right.Value), pStatement.entity_typeref));
                }
            }

            if (ex.lhs is CsPrimaryExpressionMemberAccess)
            {
                if (ex.lhs.ec == expression_classification.ec_event_access)
                {
                    CsEntityEvent ev = (CsEntityEvent)ex.lhs.entity;

                    TheClass theClass = TheClassFactory.Get(ev.parent, pCreator);
                    string   eventName;
                    //flash event on flash.xxxx
                    if (theClass == null)
                    {
                        eventName = Helpers.GetEventFromAttr(ev.attributes);

                        return(new Expression(
                                   left.Value +
                                   (ex.oper == CsTokenType.tkPLUS_EQ ?
                                    ("addEventListener(" + eventName + ", " + right.Value + ", false, 0, true)") :
                                    ("removeEventListener(" + eventName + ", " + right.Value + ")"))
                                   , pStatement.entity_typeref
                                   ));
                    }

                    TheEvent theEvent = theClass.GetEvent(ev.name);

                    if (theEvent != null && string.IsNullOrEmpty(theEvent.EventName))                      //custom event on the same class
                    {
                        return(new Expression(
                                   //event name == left => left IS the event name. Do not add twice
                                   left.Value + (ev.name.Equals(left.Value, StringComparison.Ordinal) ? string.Empty : ev.name) +
                                   (ex.oper == CsTokenType.tkPLUS_EQ ? ".add" : ".remove") +
                                   "(" + right.Value + ")"
                                   , pStatement.entity_typeref
                                   ));
                    }

                    eventName = theEvent == null?Helpers.GetEventFromAttr(ev.attributes) : theEvent.EventName;

                    //flash event on the same class.
                    return(new Expression(
                               //event name == left => left IS the event name. Do not add twice
                               (ev.name.Equals(left.Value, StringComparison.Ordinal) ? string.Empty : left.Value) +
                               (ex.oper == CsTokenType.tkPLUS_EQ
                                                        ? ("addEventListener(" + eventName + ", " + right.Value + ", false, 0, true)")
                                                        : ("removeEventListener(" + eventName + ", " + right.Value + ")"))
                               , pStatement.entity_typeref));
                }
                //else {
                //    left = FactoryExpressionCreator.Parse(ex.lhs);
                //    CsPrimaryExpressionMemberAccess primaryExpressionMemberAccess = (CsPrimaryExpressionMemberAccess)ex.lhs;
                //    TheClass theClass = TheClassFactory.Get((CsEntity)primaryExpressionMemberAccess.entity);
                //    //TheProperty theProp = theClass.GetProperty();
                //}
            }



            return(new Expression(string.Format("{0} {2} {1}", left.Value, right.Value, As3Helpers.ConvertTokens(Helpers.GetTokenType(ex.oper))), pStatement.entity_typeref));
        }