コード例 #1
0
ファイル: JassParser.cs プロジェクト: yungshing/War3Net
        public static FileSyntax ParseFile(string filePath)
        {
            var tokenizer = new JassTokenizer(File.ReadAllText(filePath));
            var parser    = new JassParser(tokenizer);

            return(parser.Parse());
        }
コード例 #2
0
        public static FileSyntax ParseString(string text)
        {
            var tokenizer = new JassTokenizer(text);
            var parser    = new JassParser(tokenizer);

            return(parser.Parse());
        }
コード例 #3
0
ファイル: JassParser.cs プロジェクト: TriggerEdge/War3Net
 public JassParser(JassTokenizer tokenizer /*, CancellationToken cancellationToken = default*/)
 {
     _tokenizer = tokenizer;
 }
コード例 #4
0
        public static bool CompileCSharpFromJass(
            string filePath,
            string assemblyName,
            string namespaceName,
            string className,
            MetadataReference[] metadataReferences,
            UsingDirectiveSyntax[] usingDirectives,
            out MetadataReference outReference,
            out UsingDirectiveSyntax outDirective,
            out EmitResult emitResult,
            OutputKind outputKind,
            bool applyNativeMemberAttributes = false,
            string outputSource   = null,
            string outputEmit     = null,
            string outputLuaTypes = null)
        {
            var directiveName = $"{namespaceName}.{className}";

            outDirective = SyntaxFactory.UsingDirective(SyntaxFactory.Token(SyntaxKind.StaticKeyword), null, SyntaxFactory.ParseName(directiveName));

            if (outputLuaTypes != null)
            {
                // These two are incompatible, because enums can't freely inherit from other types (example: igamestate and fgamestate enums inherit from gamestate).
                TranspileToEnumHandler.Reset();
            }

            var tokenizer  = new JassTokenizer(File.ReadAllText(filePath));
            var parser     = new JassParser(tokenizer);
            var fileSyntax = parser.Parse();

            if (outputLuaTypes != null)
            {
                // Output lua source code for JASS type definitions.
                TranspileTypesToLua(fileSyntax, $"{namespaceName}{className}", outputLuaTypes);
            }

            var compilationUnit = Transpile(
                fileSyntax,
                namespaceName,
                className,
                applyNativeMemberAttributes,
                usingDirectives).NormalizeWhitespace();

            if (outputSource != null)
            {
                new FileInfo(outputSource).Directory.Create();

                // Output C# source code.
#pragma warning disable CA2000 // Dispose objects before losing scope
                CompilationHelper.SerializeTo(compilationUnit, File.OpenWrite(outputSource), false);
#pragma warning restore CA2000 // Dispose objects before losing scope
            }

            var compilation = CompilationHelper.PrepareCompilation(
                compilationUnit,
                outputKind,
                assemblyName ?? directiveName,
                metadataReferences);

            if (outputEmit is null)
            {
                var peStream = new MemoryStream();
                emitResult = compilation.Emit(peStream, options: new EmitOptions(metadataOnly: true)); // TODO: set metadataOnly to applyNativeMemberAttributes?
                peStream.Seek(0, SeekOrigin.Begin);

                if (emitResult.Success)
                {
                    outReference = MetadataReference.CreateFromStream(peStream);
                    return(true);
                }

                peStream.Dispose();
            }
            else
            {
                new FileInfo(outputEmit).Directory.Create();

                // Output .dll file.
                emitResult = compilation.Emit(outputEmit);

                if (emitResult.Success)
                {
                    outReference = MetadataReference.CreateFromFile(outputEmit);
                    return(true);
                }
            }

            outReference = null;
            return(false);
        }