Esempio n. 1
0
        private static object CompileStream(int iCfg, ISrgsParser srgsParser, string srgsPath, string filename, Stream stream, bool fOutputCfg, StringBuilder innerCode, object cfgResources, out CultureInfo culture, string[] referencedAssemblies, string keyFile)
        {
            Backend       backend       = new Backend();
            CustomGrammar customGrammar = new CustomGrammar();
            SrgsElementCompilerFactory srgsElementCompilerFactory = (SrgsElementCompilerFactory)(srgsParser.ElementFactory = new SrgsElementCompilerFactory(backend, customGrammar));

            srgsParser.Parse();
            backend.Optimize();
            culture = ((backend.LangId == 21514) ? new CultureInfo("es-us") : new CultureInfo(backend.LangId));
            if (customGrammar._codebehind.Count > 0 && !string.IsNullOrEmpty(srgsPath))
            {
                for (int i = 0; i < customGrammar._codebehind.Count; i++)
                {
                    if (!File.Exists(customGrammar._codebehind[i]))
                    {
                        customGrammar._codebehind[i] = srgsPath + "\\" + customGrammar._codebehind[i];
                    }
                }
            }
            if (referencedAssemblies != null)
            {
                foreach (string item in referencedAssemblies)
                {
                    customGrammar._assemblyReferences.Add(item);
                }
            }
            customGrammar._keyFile = keyFile;
            backend.ScriptRefs     = customGrammar._scriptRefs;
            if (!fOutputCfg)
            {
                CustomGrammar.CfgResource cfgResource = new CustomGrammar.CfgResource();
                cfgResource.data = BuildCfg(backend).ToArray();
                cfgResource.name = iCfg.ToString(CultureInfo.InvariantCulture) + ".CFG";
                ((List <CustomGrammar.CfgResource>)cfgResources).Add(cfgResource);
                innerCode.Append(customGrammar.CreateAssembly(iCfg, filename, culture));
                return(customGrammar);
            }
            if (customGrammar._scriptRefs.Count > 0 && !customGrammar.HasScript)
            {
                XmlParser.ThrowSrgsException(SRID.NoScriptsForRules);
            }
            CreateAssembly(backend, customGrammar);
            if (!string.IsNullOrEmpty(filename))
            {
                stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
            }
            try
            {
                using (StreamMarshaler streamBuffer = new StreamMarshaler(stream))
                {
                    backend.Commit(streamBuffer);
                    return(customGrammar);
                }
            }
            finally
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    stream.Close();
                }
            }
        }
Esempio n. 2
0
        private static object CompileStream(int iCfg, ISrgsParser srgsParser, string srgsPath, string filename, Stream stream, bool fOutputCfg, StringBuilder innerCode, object cfgResources, out CultureInfo culture, string[] referencedAssemblies, string keyFile)
        {
            Backend       backend = new();
            CustomGrammar cg      = new();
            SrgsElementCompilerFactory elementFactory = new(backend, cg);

            srgsParser.ElementFactory = elementFactory;
            srgsParser.Parse();

            // Optimize in-memory graph representation of the grammar.
            backend.Optimize();
            culture = backend.LangId == 0x540A ? new CultureInfo("es-us") : new CultureInfo(backend.LangId);

            // A grammar may contains references to other files in codebehind.
            // Set the current directory to the location where is the grammar
            if (cg._codebehind.Count > 0 && !string.IsNullOrEmpty(srgsPath))
            {
                for (int i = 0; i < cg._codebehind.Count; i++)
                {
                    if (!File.Exists(cg._codebehind[i]))
                    {
                        cg._codebehind[i] = srgsPath + "\\" + cg._codebehind[i];
                    }
                }
            }

            // Add the referenced assemblies
            if (referencedAssemblies != null)
            {
                foreach (string assembly in referencedAssemblies)
                {
                    cg._assemblyReferences.Add(assembly);
                }
            }

            // Assign the key file
            cg._keyFile = keyFile;

            // Assign the Scripts to the backend
            backend.ScriptRefs = cg._scriptRefs;

            // If the target is a dll, then create first the CFG and stuff it as an embedded resource
            if (!fOutputCfg)
            {
                throw new PlatformNotSupportedException();
            }
            else
            {
                //if semantic processing for a rule is defined, a script needs to be defined
                if (cg._scriptRefs.Count > 0 && !cg.HasScript)
                {
                    XmlParser.ThrowSrgsException(SRID.NoScriptsForRules);
                }

                // Creates a CFG with IL embedded
                CreateAssembly(backend, cg);

                // Save binary grammar to dest
                if (!string.IsNullOrEmpty(filename))
                {
                    // Create a stream if a filename was given
                    stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
                }
                try
                {
                    using (StreamMarshaler streamHelper = new(stream))
                    {
                        backend.Commit(streamHelper);
                    }
                }
                finally
                {
                    if (!string.IsNullOrEmpty(filename))
                    {
                        stream.Close();
                    }
                }
            }
            return(cg);
        }