Esempio n. 1
0
        public static void Parse(TheVariable pVariable, CodeBuilder pBuilder)
        {
            foreach (Variable declarator in pVariable.Variables)
            {
                StringBuilder sb = new StringBuilder();

                sb.AppendFormat("{0}var {1}:{2}",
                                As3Helpers.ConvertModifiers(pVariable.Modifiers, _notValidVariableMod),
                                declarator.Name,
                                As3Helpers.Convert(declarator.ReturnType)
                                );

                if (declarator.Initializer == null)
                {
                    sb.Append(";");
                }
                else
                {
                    sb.AppendFormat(" = {0};", declarator.Initializer.Value);
                }

                pBuilder.Append(sb.ToString());
                pBuilder.AppendLine();
            }
        }
Esempio n. 2
0
        public static void Parse(TheConstructor pConstructor, CodeBuilder pBuilder, FactoryExpressionCreator pCreator)
        {
            if (pConstructor.IsStaticConstructor)
            {
                pBuilder.Append("{");
            }
            else
            {
                pBuilder.AppendFormat("{4}{0}function {1}({2}){3} {{",
                                      ClassParser.IsMainClass ? "private " : As3Helpers.ConvertModifiers(pConstructor.Modifiers, _notValidConstructorMod),
                                      ClassParser.IsMainClass ? @"$ctor" : pConstructor.Name,
                                      As3Helpers.GetParameters(pConstructor.Arguments),
                                      ClassParser.IsMainClass ? ":void" : string.Empty,                          // pConstructor.MyClass.Name,
                                      pConstructor.OverridesBaseConstructor ? "override " : string.Empty
                                      );
            }

            pBuilder.AppendLine();

            if (pConstructor.HasBaseCall)
            {
                pBuilder.AppendFormat("\tsuper({0});", As3Helpers.GetCallingArguments(pConstructor.BaseArguments));
                pBuilder.AppendLine();
            }

            BlockParser.Parse(pConstructor.CodeBlock, pBuilder, pCreator);

            pBuilder.AppendLine("}");
            pBuilder.AppendLine();
        }
Esempio n. 3
0
        public static void Parse(TheIndexer pGetIndexer, CodeBuilder pBuilder, FactoryExpressionCreator pCreator)
        {
            bool isInterface = pGetIndexer.MyClass.IsInterface;

            if (pGetIndexer.Getter != null)
            {
                pBuilder.AppendFormat("{0}function {1}({2}):{3}{4}",
                                      As3Helpers.ConvertModifiers(pGetIndexer.Getter.Modifiers, _notValidMod),
                                      pGetIndexer.Getter.Name,
                                      As3Helpers.GetParameters(pGetIndexer.Getter.Arguments),
                                      As3Helpers.Convert(pGetIndexer.ReturnType),
                                      isInterface ? ";":" {"
                                      );
                pBuilder.AppendLine();

                if (!isInterface)
                {
                    BlockParser.Parse(pGetIndexer.Getter.CodeBlock, pBuilder, pCreator);
                    pBuilder.AppendLine();
                    pBuilder.AppendLine("}");
                    pBuilder.AppendLine();
                }
            }

            if (pGetIndexer.Setter == null)
            {
                return;
            }

            pBuilder.AppendFormat(
                "{0}function {1}({2}):void{3}",
                As3Helpers.ConvertModifiers(pGetIndexer.Setter.Modifiers, _notValidMod),
                pGetIndexer.Setter.Name,
                As3Helpers.GetParameters(pGetIndexer.Setter.Arguments),
                isInterface ? ";" : " {"
                );

            pBuilder.AppendLine();
            if (isInterface)
            {
                return;
            }
            //BlockParser.InsideSetter = true;
            BlockParser.Parse(pGetIndexer.Setter.CodeBlock, pBuilder, pCreator);
            //BlockParser.InsideSetter = false;
            pBuilder.AppendLine();
            pBuilder.AppendLine("}");
            pBuilder.AppendLine();
        }
Esempio n. 4
0
        public static void Parse(TheConstant pConstant, CodeBuilder pBuilder)
        {
            string modifiers = As3Helpers.ConvertModifiers(pConstant.Modifiers);

            foreach (Constant declarator in pConstant.Constants)
            {
                StringBuilder sb = new StringBuilder();

                sb.AppendFormat(@"{0}const {1}:{2} = {3};",
                                modifiers,
                                declarator.Name,
                                As3Helpers.Convert(declarator.ReturnType),
                                declarator.Initializer.Value
                                );

                pBuilder.Append(sb.ToString());
                pBuilder.AppendLine();
            }
        }
Esempio n. 5
0
        public static void Parse(TheEvent pEvent, CodeBuilder pBuilder)
        {
            if (pEvent.IsFlashEvent)
            {
                return;
            }

            ImportStatementList.AddImport(@"System.EventHandler");

            bool isStatic = pEvent.Modifiers.Contains("static");

            pBuilder.AppendFormat(@"private {1} var _e{0}:EventHandler;
		{2}{1} function get {0}():EventHandler {{
			if (_e{0} == null) _e{0} = new EventHandler();
			return _e{0};
		}}"        ,
                                  pEvent.Name,
                                  isStatic ? "static" : string.Empty,
                                  As3Helpers.ConvertModifiers(pEvent.Modifiers, _notValidClassMod)
                                  //,isStatic ? pEvent.MyClass.Name : "this"
                                  );

            pBuilder.AppendLine();
        }
Esempio n. 6
0
        public static void Parse(TheMethod pMethod, CodeBuilder pBuilder, FactoryExpressionCreator pCreator)
        {
            if (pMethod == null)
            {
                return;
            }
            bool isInterface = pMethod.MyClass.IsInterface;

            Dictionary <string, string> nonValidMethod = new Dictionary <string, string>(_notValidMethodMod);

            if (ClassParser.IsExtension)
            {
                nonValidMethod.Add("static", string.Empty);
            }

            pBuilder.AppendFormat("{0}function {1}({2}):{3}{4}",
                                  As3Helpers.ConvertModifiers(pMethod.Modifiers, nonValidMethod),
                                  pMethod.Name,
                                  As3Helpers.GetParameters(pMethod.Arguments),
                                  As3Helpers.Convert(pMethod.ReturnType),
                                  isInterface ? ";":" {"
                                  );

            pBuilder.AppendLine();

            if (isInterface)
            {
                return;
            }

            pBuilder.AppendLine();
            BlockParser.Parse(pMethod.CodeBlock, pBuilder, pCreator);
            pBuilder.AppendLine();
            pBuilder.AppendLine("}");
            pBuilder.AppendLine();
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public static void Parse(TheProperty pProperty, CodeBuilder pBuilder, FactoryExpressionCreator pCreator)
        {
            if (pProperty == null)
            {
                return;
            }
            bool   isInterface = pProperty.MyClass.IsInterface;
            string type        = As3Helpers.Convert(pProperty.ReturnType);

            if (pProperty.IsEmpty && !isInterface)
            {
                pBuilder.AppendFormat("private var _{0}:{1};", pProperty.Name, type);
                pBuilder.AppendLine();
            }

            TheClass parent           = pProperty.MyClass;
            bool     isStandardGetSet = false;

            while (parent.Base != null)
            {
                isStandardGetSet |= parent.FullName.StartsWith("flash.");
                parent            = parent.Base;
            }

            if (pProperty.Getter != null)              //Getter
            {
                if (isStandardGetSet)                  //base is flash, use standard setter/getter
                {
                    pBuilder.AppendFormat("{0}function get {1}():{2}{3}",
                                          As3Helpers.ConvertModifiers(pProperty.Getter.Modifiers, _notValidPropertyMod),
                                          pProperty.Name,
                                          type,
                                          isInterface ? ";" : " {"
                                          );
                }
                else
                {
                    bool isEnum = false;

                    foreach (string s in pProperty.MyClass.Implements)
                    {
                        if (!s.StartsWith("IEnumerator", StringComparison.Ordinal))
                        {
                            continue;
                        }

                        isEnum = true;
                        break;
                    }

                    isEnum &= pProperty.Getter.Name.Equals("get_Current");

                    //bool isEnum =
                    //    pProperty.Getter.Name.Equals("get_Current") &&
                    //    pProperty.MyClass.Implements.Contains()

                    pBuilder.AppendFormat("{0}function {1}():{2}{3}",
                                          As3Helpers.ConvertModifiers(pProperty.Getter.Modifiers, _notValidPropertyMod),
                                          pProperty.Getter.Name,
                                          isEnum ? "*" : type,
                                          isInterface ? ";" : " {"
                                          );
                }

                pBuilder.AppendLine();

                if (!isInterface)
                {
                    if (pProperty.IsEmpty)
                    {
                        pBuilder.Indent();
                        pBuilder.AppendFormat("return _{0};", pProperty.Name);
                        pBuilder.Unindent();
                    }
                    else
                    {
                        BlockParser.Parse(pProperty.Getter.CodeBlock, pBuilder, pCreator);
                    }

                    pBuilder.AppendLine();
                    pBuilder.AppendLine("}");
                    pBuilder.AppendLine();
                }
            }

            if (pProperty.Setter == null)
            {
                return;
            }

            if (isStandardGetSet)              //Setter
//base is flash, use standard setter/getter
            {
                pBuilder.AppendFormat("{0}function set {1}(value:{2}):void{3}",
                                      As3Helpers.ConvertModifiers(pProperty.Setter.Modifiers, _notValidPropertyMod),
                                      pProperty.Name,
                                      type,
                                      isInterface ? ";" : " {"
                                      );
            }
            else
            {
                pBuilder.AppendFormat("{0}function {1}(value:{2}):{2}{3}",
                                      As3Helpers.ConvertModifiers(pProperty.Setter.Modifiers, _notValidPropertyMod),
                                      pProperty.Setter.Name,
                                      type,
                                      isInterface ? ";" : " {"
                                      );
            }

            pBuilder.AppendLine();

            if (!isInterface)
            {
                if (pProperty.IsEmpty)
                {
                    pBuilder.Indent();
                    pBuilder.AppendFormat("_{0} = value;", pProperty.Name);
                    pBuilder.AppendLine();
                    pBuilder.Append("return value;");
                    pBuilder.Unindent();
                }
                else
                {
                    BlockParser.InsideSetter = !isStandardGetSet;
                    BlockParser.Parse(pProperty.Setter.CodeBlock, pBuilder, pCreator);
                    BlockParser.InsideSetter = false;
                    if (!isStandardGetSet)
                    {
                        pBuilder.AppendLine("	return value;");
                    }
                }

                pBuilder.AppendLine();
                pBuilder.AppendLine("}");
            }

            pBuilder.AppendLine();
        }
Esempio n. 9
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, "As3MainClassAttribute");
            bool isResource = Helpers.HasAttribute(pCsClass.attributes, "As3EmbedAttribute");

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

            if (IsMainClass)
            {
                As3NamespaceParser.MainClassName = myClass.FullName;
                AttributeItem vals = Helpers.GetAttributeValue(pCsClass.attributes, "As3MainClassAttribute", 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, "As3EmbedAttribute", 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,
                                As3Helpers.ConvertModifiers(myClass.Modifiers, _notValidClassMod));

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

                if (myClass.Implements.Count != 0)
                {
                    sb.Append(" implements ");
                    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 (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(As3NamespaceParser.Using);
            pBuilder.AppendLine(imports);
            pBuilder.Append(privateClasses);
        }