Ejemplo n.º 1
0
		public void Parse(CsNamespace pNameSpace, IEnumerable<CsUsingDirective> pUsing, string pOutputFolder) {
			if (pNameSpace == null) {
				return;
			}

			_outputFolder = pOutputFolder;
			string name = getNamespace(pNameSpace);
			string packDir = pOutputFolder + name.Replace('.', '\\');
			Directory.CreateDirectory(packDir);
			CodeBuilder builder = new CodeBuilder();

			foreach (CsNode cn in pNameSpace.member_declarations) {
				builder.Append("package ");

				builder.Append(name);
				builder.AppendLineAndIndent(" {");

				StringBuilder usings = new StringBuilder();

				parseUsing(pUsing, usings);
				parseUsing(pNameSpace.using_directives, usings);
				Using = usings.ToString();

				builder.Append(usings);

				builder.AppendLine(ClassParser.IMPORT_MARKER);
				ImportStatementList.Init();

				CsClass csClass = cn as CsClass;

				if (csClass != null) {
					ClassParser.Parse(csClass, builder, _creator);
					if (ClassParser.IsExtension) {
						File.WriteAllText(packDir + "\\" + ClassParser.ExtensionName + ".as", builder.ToString());

					} else {
						File.WriteAllText(packDir + "\\" + csClass.identifier.identifier + ".as", builder.ToString());
					}

					builder.Length = 0;
					continue;
				}

				CsInterface csInterface = cn as CsInterface;
				if (csInterface != null) {
					InterfaceParser.Parse(csInterface, builder, _creator);
					File.WriteAllText(packDir + "\\" + csInterface.identifier.identifier + ".as", builder.ToString());
					builder.Length = 0;
					continue;
				}

				if (csClass == null) {
					throw new Exception("Unknown type");
				}
			}
		}
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
		}
Ejemplo n.º 5
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);
		}
Ejemplo n.º 6
0
        public void Parse(CsNamespace pNameSpace, IEnumerable <CsUsingDirective> pUsing, string pOutputFolder)
        {
            if (pNameSpace == null)
            {
                return;
            }

            _outputFolder = pOutputFolder;
            string name    = getNamespace(pNameSpace);
            string packDir = pOutputFolder + name.Replace('.', '\\');

            Directory.CreateDirectory(packDir);
            CodeBuilder builder = new CodeBuilder();

            foreach (CsNode cn in pNameSpace.member_declarations)
            {
                builder.Append("package ");

                builder.Append(name);
                builder.AppendLineAndIndent(" {");

                StringBuilder usings = new StringBuilder();

                parseUsing(pUsing, usings);
                parseUsing(pNameSpace.using_directives, usings);
                Using = usings.ToString();

                builder.Append(usings);

                builder.AppendLine(ClassParser.IMPORT_MARKER);
                ImportStatementList.Init();

                CsClass csClass = cn as CsClass;

                if (csClass != null)
                {
                    ClassParser.Parse(csClass, builder, _creator);
                    if (ClassParser.IsExtension)
                    {
                        File.WriteAllText(packDir + "\\" + ClassParser.ExtensionName + ".as", builder.ToString());
                    }
                    else
                    {
                        File.WriteAllText(packDir + "\\" + csClass.identifier.identifier + ".as", builder.ToString());
                    }

                    builder.Length = 0;
                    continue;
                }

                CsInterface csInterface = cn as CsInterface;
                if (csInterface != null)
                {
                    InterfaceParser.Parse(csInterface, builder, _creator);
                    File.WriteAllText(packDir + "\\" + csInterface.identifier.identifier + ".as", builder.ToString());
                    builder.Length = 0;
                    continue;
                }

                if (csClass == null)
                {
                    throw new Exception("Unknown type");
                }
            }
        }