Example #1
0
        public virtual CompiledTemplate LookupTemplate(TemplateName name)
        {
            if (!alreadyLoaded)
                Load();

            CompiledTemplate code;
            if (!templates.TryGetValue(name, out code))
            {
                code = LookupImportedTemplate(name);
            }

            return code;
        }
Example #2
0
        public CompiledTemplate RawGetTemplate(TemplateName name)
        {
            CompiledTemplate template;
            if (!templates.TryGetValue(name, out template))
                return null;

            return template;
        }
Example #3
0
        /** The primary means of getting an instance of a template from this
         *  group. Must be absolute name like /a/b
         */
        public virtual Template GetInstanceOf(TemplateName name)
        {
            if (!name.IsRooted)
                name = TemplateName.Combine(TemplateName.Root, name);

            //Console.WriteLine("GetInstanceOf(" + name + ")");
            CompiledTemplate c = LookupTemplate(name);
            if (c != null)
            {
                Template instanceST = CreateStringTemplate();
                instanceST.groupThatCreatedThisInstance = this;
                instanceST.code = c;
                return instanceST;
            }
            return null;
        }
Example #4
0
        // TODO: make this happen in background then flip ptr to new list of templates/dictionaries?
        public virtual void LoadGroupFile(TemplateName prefix, string fileName)
        {
            string absoluteFileName = Path.Combine(fullyQualifiedRootDirName, fileName);
            //Console.WriteLine("load group file " + absoluteFileName);
            GroupParser parser = null;
            try
            {
                ANTLRFileStream fs = new ANTLRFileStream(absoluteFileName, encoding);
                GroupLexer lexer = new GroupLexer(fs);
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                parser = new GroupParser(tokens);
                parser.group(this, prefix);
            }
            #if false
            catch (RecognitionException e)
            {
                if (e.Token.Type == TemplateLexer.EOF_TYPE)
                    ErrorManager.SyntaxError(ErrorType.SyntaxError, e, "premature EOF", absoluteFileName);
                else
                    ErrorManager.SyntaxError(ErrorType.SyntaxError, e, absoluteFileName);
            }
            #endif
            catch (Exception e)
            {
                if (ErrorManager.IsCriticalException(e))
                    throw;

                ErrorManager.IOError(null, ErrorType.CantLoadGroupFile, e, absoluteFileName);
            }
        }
        protected void _Load(TemplateName prefix)
        {
            if (prefix == null)
                throw new ArgumentNullException("prefix");
            if (!prefix.IsRooted)
                throw new ArgumentException();

            string relativePrefix = prefix.FullName.Substring(1);

            string dir = Path.Combine(fullyQualifiedRootDirName, relativePrefix);
            //Console.WriteLine("load dir '" + prefix + "' under " + fullyQualifiedRootDirName);

            foreach (var d in Directory.GetDirectories(dir))
            {
                _Load(TemplateName.Combine(prefix, Path.GetFileName(d)));
            }

            foreach (var f in Directory.GetFiles(dir))
            {
                if (Path.GetExtension(f).Equals(".st", System.StringComparison.OrdinalIgnoreCase))
                    LoadTemplateFile(prefix, Path.GetFileName(f));
                else if (Path.GetExtension(f).Equals(".stg", System.StringComparison.OrdinalIgnoreCase))
                    LoadGroupFile(TemplateName.Combine(prefix, Path.GetFileNameWithoutExtension(f)), Path.Combine(relativePrefix, Path.GetFileName(f)));
            }
        }
Example #6
0
 public virtual Template GetEmbeddedInstanceOf(Template enclosingInstance, int ip, TemplateName name)
 {
     Template st = GetInstanceOf(name);
     if (st == null)
     {
         ErrorManager.RuntimeError(enclosingInstance, ip, ErrorType.NoSuchTemplate, name.Name);
         return Template.Blank;
     }
     st.enclosingInstance = enclosingInstance;
     return st;
 }
Example #7
0
 // for testing
 public CompiledTemplate DefineTemplate(TemplateName name, string template)
 {
     return DefineTemplate(TemplateName.Root, new CommonToken(GroupParser.ID, name.Name), FormalArgument.Unknown, template);
 }
Example #8
0
 protected void RawDefineTemplate(TemplateName name, CompiledTemplate code, IToken definingToken)
 {
     CompiledTemplate prev;
     if (templates.TryGetValue(name, out prev))
     {
         if (!prev.isRegion)
         {
             ErrorManager.CompileTimeError(ErrorType.TemplateRedefinition, definingToken);
             return;
         }
         if (prev.isRegion && prev.regionDefType == Template.RegionType.Embedded)
         {
             ErrorManager.CompileTimeError(ErrorType.EmbeddedRegionRedefinition, definingToken, GetUnmangledTemplateName(name));
             return;
         }
         else if (prev.isRegion && prev.regionDefType == Template.RegionType.Explicit)
         {
             ErrorManager.CompileTimeError(ErrorType.RegionRedefinition, definingToken, GetUnmangledTemplateName(name));
             return;
         }
     }
     templates[name] = code;
 }
Example #9
0
 /// <summary>
 /// Return "t.foo" from "region__t__foo"
 /// </summary>
 public static string GetUnmangledTemplateName(TemplateName mangledName)
 {
     string name = mangledName.Name;
     string t = name.Substring("region__".Length, name.LastIndexOf("__") - "region__".Length);
     string r = name.Substring(name.LastIndexOf("__") + 2);
     return t + '.' + r;
 }
Example #10
0
 public CompiledTemplate DefineRegion(TemplateName prefix,
                                      TemplateName enclosingTemplateName,
                                      IToken regionToken,
                                      string template)
 {
     string name = regionToken.Text;
     CompiledTemplate code = Compile(prefix, enclosingTemplateName, template);
     code.Name = TemplateName.Combine(prefix, GetMangledRegionName(enclosingTemplateName, name));
     code.isRegion = true;
     code.regionDefType = Template.RegionType.Explicit;
     RawDefineTemplate(code.Name, code, regionToken);
     return code;
 }
Example #11
0
 /** The "foo" of t() ::= "&lt;@foo()&gt;" is mangled to "region#t#foo" */
 public static TemplateName GetMangledRegionName(TemplateName enclosingTemplateName,
                                           string name)
 {
     return new TemplateName("region__" + enclosingTemplateName + "__" + name);
 }
Example #12
0
        public static TemplateName Combine(TemplateName left, TemplateName right)
        {
            if (left == null)
                throw new ArgumentNullException("left");
            if (right == null)
                throw new ArgumentNullException("right");

            if (left == Root)
                return right.IsRooted ? right : new TemplateName(TemplateDirectorySeparator + right.FullName);

            if (right.IsRooted)
                return new TemplateName(left.FullName + right.FullName);
            else
                return new TemplateName(left.FullName + TemplateDirectorySeparator + right.FullName);
        }
Example #13
0
 public static TemplateName Combine(TemplateName left, string right)
 {
     return Combine(left, new TemplateName(right));
 }
Example #14
0
 protected internal CompiledTemplate Compile(TemplateName prefix, TemplateName enclosingTemplateName, string template)
 {
     TemplateCompiler c = new TemplateCompiler(prefix, enclosingTemplateName);
     CompiledTemplate code = c.Compile(template);
     code.nativeGroup = this;
     code.template = template;
     return code;
 }
Example #15
0
        // can't trap recog errors here; don't know where in file template is defined
        public virtual CompiledTemplate DefineTemplate(TemplateName prefix, IToken nameToken, IDictionary<string, FormalArgument> args, string template)
        {
            if (nameToken == null)
                throw new ArgumentNullException("nameToken");

            TemplateName name = new TemplateName(nameToken.Text);
            CompiledTemplate code = Compile(prefix, name, template);
            code.Name = name;
            code.formalArguments = args;
            RawDefineTemplate(TemplateName.Combine(prefix, name), code, nameToken);
            if (args != null)
            { // compile any default args
                foreach (string a in args.Keys)
                {
                    FormalArgument fa = args[a];
                    if (fa.defaultValueToken != null)
                    {
                        TemplateCompiler c2 = new TemplateCompiler(prefix, name);
                        string defArgTemplate = Misc.Strip(fa.defaultValueToken.Text, 1);
                        fa.compiledDefaultValue = c2.Compile(defArgTemplate);
                        fa.compiledDefaultValue.Name = new TemplateName(fa.name + "-default-value");
                    }
                }
            }

            // define any anonymous subtemplates
            DefineImplicitlyDefinedTemplates(code);

            return code;
        }
Example #16
0
        protected internal CompiledTemplate LookupImportedTemplate(TemplateName name)
        {
            //Console.WriteLine("look for " + name + " in " + imports);

            if (imports == null)
                return null;

            foreach (var g in imports)
            {
                CompiledTemplate code = g.LookupTemplate(name);
                if (code != null)
                    return code;
            }

            return null;
        }
Example #17
0
 /** Make name and alias for target.  Replace any previous def of name */
 public CompiledTemplate DefineTemplateAlias(IToken aliasToken, IToken targetToken)
 {
     TemplateName alias = new TemplateName(aliasToken.Text);
     TemplateName target = new TemplateName(targetToken.Text);
     CompiledTemplate targetCode;
     if (!templates.TryGetValue(target, out targetCode))
     {
         NoViableAltException e = null;
         ErrorManager.SyntaxError(ErrorType.AliasTargetUndefined, e, alias.Name, target.Name);
         return null;
     }
     templates[alias] = targetCode;
     return targetCode;
 }
Example #18
0
        public virtual CompiledTemplate DefineTemplate(TemplateName name,
                                         List<string> args,
                                         string template)
        {
            IDictionary<string, FormalArgument> margs = null;
            if (args != null)
                margs = args.ToDictionary(arg => arg, arg => new FormalArgument(arg));

            return DefineTemplate(TemplateName.Root, name, margs, template);
        }
Example #19
0
        public CompiledTemplate LoadTemplateFile(TemplateName prefix, string fileName)
        {
            if (prefix == null)
                throw new ArgumentNullException("prefix");
            if (fileName == null)
                throw new ArgumentNullException("fileName");
            if (!prefix.IsRooted)
                throw new ArgumentException("Expected the prefix to be a rooted name.", "prefix");

            TemplateName templateName = TemplateName.Combine(prefix, new TemplateName(Path.GetFileNameWithoutExtension(fileName)));

            // load from disk
            string absoluteFileName = Path.Combine(Path.Combine(fullyQualifiedRootDirName, prefix.FullName.Substring(1)), fileName);

            //Console.WriteLine("load " + absoluteFileName);
            if (!File.Exists(absoluteFileName))
            {
                // TODO: add tolerance check here
                return null;
            }
            try
            {
                if (ErrorManager.CompatibilityMode)
                {
                    string template = File.ReadAllText(absoluteFileName);
                    template = template.Trim();
                    DefineTemplate(prefix, new CommonToken(GroupParser.ID, templateName.Name), null, template);
                }
                else
                {
                    ANTLRFileStream fs = new ANTLRFileStream(absoluteFileName, encoding);
                    GroupLexer lexer = new GroupLexer(fs);
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    GroupParser parser = new GroupParser(tokens);
                    parser._group = this;
                    parser.templateDef(prefix);
                }

                CompiledTemplate code;
                if (!templates.TryGetValue(templateName, out code))
                    return null;

                return code;
            }
            catch (Exception e)
            {
                if (ErrorManager.IsCriticalException(e))
                    throw;

                ErrorManager.IOError(null, ErrorType.CantLoadTemplateFile, e, absoluteFileName);
                Console.Error.WriteLine(e.StackTrace);
            }
            return null;
        }