Exemple #1
0
        internal static void CompileStream(XmlReader[] xmlReaders, string filename, Stream stream, bool fOutputCfg, Uri originalUri, string[] referencedAssemblies, string keyFile)
        {
            int num = xmlReaders.Length;
            List <CustomGrammar.CfgResource> cfgResources = new List <CustomGrammar.CfgResource>();
            CustomGrammar customGrammar = new CustomGrammar();

            for (int i = 0; i < num; i++)
            {
                string srgsPath = null;
                Uri    uri      = originalUri;
                if (uri == null && xmlReaders[i].BaseURI != null && xmlReaders[i].BaseURI.Length > 0)
                {
                    uri = new Uri(xmlReaders[i].BaseURI);
                }
                if (uri != null && (!uri.IsAbsoluteUri || uri.IsFile))
                {
                    srgsPath = Path.GetDirectoryName(uri.IsAbsoluteUri ? uri.AbsolutePath : uri.OriginalString);
                }
                StringBuilder stringBuilder = new StringBuilder();
                ISrgsParser   srgsParser    = new XmlParser(xmlReaders[i], uri);
                CultureInfo   culture;
                object        obj = CompileStream(i + 1, srgsParser, srgsPath, filename, stream, fOutputCfg, stringBuilder, cfgResources, out culture, referencedAssemblies, keyFile);
                if (!fOutputCfg)
                {
                    customGrammar.Combine((CustomGrammar)obj, stringBuilder.ToString());
                }
            }
            if (!fOutputCfg)
            {
                customGrammar.CreateAssembly(filename, cfgResources);
            }
        }
Exemple #2
0
 private static void CreateAssembly(Backend backend, CustomGrammar cg)
 {
     if (cg.HasScript)
     {
         byte[] il;
         byte[] pdb;
         cg.CreateAssembly(out il, out pdb);
         backend.IL  = il;
         backend.PDB = pdb;
     }
 }
 internal void Combine(CustomGrammar cg, string innerCode)
 {
     if (_rules.Count == 0)
     {
         _language = cg._language;
     }
     else if (_language != cg._language)
     {
         XmlParser.ThrowSrgsException(SRID.IncompatibleLanguageProperties);
     }
     if (_namespace == null)
     {
         _namespace = cg._namespace;
     }
     else if (_namespace != cg._namespace)
     {
         XmlParser.ThrowSrgsException(SRID.IncompatibleNamespaceProperties);
     }
     _fDebugScript |= cg._fDebugScript;
     foreach (string item in cg._codebehind)
     {
         if (!_codebehind.Contains(item))
         {
             _codebehind.Add(item);
         }
     }
     foreach (string assemblyReference in cg._assemblyReferences)
     {
         if (!_assemblyReferences.Contains(assemblyReference))
         {
             _assemblyReferences.Add(assemblyReference);
         }
     }
     foreach (string importNamespace in cg._importNamespaces)
     {
         if (!_importNamespaces.Contains(importNamespace))
         {
             _importNamespaces.Add(importNamespace);
         }
     }
     _keyFile = cg._keyFile;
     _types.AddRange(cg._types);
     foreach (Rule rule in cg._rules)
     {
         if (_types.Contains(rule.Name))
         {
             XmlParser.ThrowSrgsException(SRID.RuleDefinedMultipleTimes2, rule.Name);
         }
     }
     _script.Append(innerCode);
 }
Exemple #4
0
        internal static void CompileStream(SrgsDocument srgsGrammar, string filename, Stream stream, bool fOutputCfg, string[] referencedAssemblies, string keyFile)
        {
            ISrgsParser srgsParser = new SrgsDocumentParser(srgsGrammar.Grammar);
            List <CustomGrammar.CfgResource> cfgResources = new List <CustomGrammar.CfgResource>();
            StringBuilder stringBuilder = new StringBuilder();

            srgsGrammar.Grammar.Validate();
            CultureInfo culture;
            object      obj = CompileStream(1, srgsParser, null, filename, stream, fOutputCfg, stringBuilder, cfgResources, out culture, referencedAssemblies, keyFile);

            if (!fOutputCfg)
            {
                CustomGrammar customGrammar = new CustomGrammar();
                customGrammar.Combine((CustomGrammar)obj, stringBuilder.ToString());
                customGrammar.CreateAssembly(filename, cfgResources);
            }
        }
 internal GrammarElement(Backend backend, CustomGrammar cg)
     : base(null)
 {
     _cg      = cg;
     _backend = backend;
 }
Exemple #6
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();
                }
            }
        }
 internal SrgsElementCompilerFactory(Backend backend, CustomGrammar cg)
 {
     _backend = backend;
     _cg      = cg;
     _grammar = new GrammarElement(backend, cg);
 }