Esempio n. 1
0
        /** Load template stream into this group. unqualifiedFileName is "a.st".
         *  The prefix is path from group root to unqualifiedFileName like /subdir
         *  if file is in /subdir/a.st
         */
        public virtual CompiledTemplate LoadTemplateFile(string prefix, string unqualifiedFileName, ICharStream templateStream)
        {
            GroupLexer lexer = new GroupLexer(templateStream);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            GroupParser parser = new GroupParser(tokens);
            parser.Group = this;
            lexer.group = this;
            try
            {
                parser.templateDef(prefix);
            }
            catch (RecognitionException re)
            {
                ErrorManager.GroupSyntaxError(ErrorType.SYNTAX_ERROR, unqualifiedFileName, re, re.Message);
            }

            string templateName = Path.GetFileNameWithoutExtension(unqualifiedFileName);
            if (!string.IsNullOrEmpty(prefix))
                templateName = prefix + templateName;

            CompiledTemplate impl = RawGetTemplate(templateName);
            impl.Prefix = prefix;
            return impl;
        }
Esempio n. 2
0
        public override void Load()
        {
            if (alreadyLoaded)
                return;

            alreadyLoaded = true;
            GroupParser parser = null;
            try
            {
                ANTLRStringStream fs = new ANTLRStringStream(text);
                fs.name = sourceName;
                GroupLexer lexer = new GroupLexer(fs);
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                parser = new GroupParser(tokens);
                // no prefix since this group file is the entire group, nothing lives
                // beneath it.
                parser.group(this, "/");
            }
            catch (Exception e)
            {
                ErrorManager.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, FileName);
            }
        }
Esempio n. 3
0
        /** Load a group file with full path fileName; it's relative to root by prefix. */
        public virtual void LoadGroupFile(string prefix, string fileName)
        {
            if (Verbose)
            {
                Console.Out.WriteLine("{0}.LoadGroupFile(prefix={1}, fileName={2})",
                    GetType().FullName, prefix, fileName);
            }

            GroupParser parser = null;
            try
            {
                Uri f = new Uri(fileName);
                ANTLRReaderStream fs = new ANTLRReaderStream(new System.IO.StreamReader(f.LocalPath, Encoding));

                var timer = System.Diagnostics.Stopwatch.StartNew();

                string cachePath = Path.Combine(Path.GetTempPath(), "ST4TemplateCache");
                if (EnableCache && TryLoadGroupFromCache(cachePath, prefix, fileName))
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("Successfully loaded the group from cache {0} in {1}ms.", Name, timer.ElapsedMilliseconds));
                }
                else
                {
                    GroupLexer lexer = new GroupLexer(fs);
                    fs.name = fileName;
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    parser = new GroupParser(tokens);
                    parser.group(this, prefix);

                    System.Diagnostics.Debug.WriteLine(string.Format("Successfully loaded the group {0} in {1}ms.", Name, timer.ElapsedMilliseconds));

                    if (EnableCache)
                        CacheCompiledGroup(cachePath, prefix, fileName, File.GetLastWriteTimeUtc(f.LocalPath));
                }

            }
            catch (Exception e)
            {
                e.PreserveStackTrace();
                if (e.IsCritical())
                    throw;

                ErrorManager.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
            }
        }