Inheritance: ITokenSource
Beispiel #1
0
        /** Compile full template with respect to a list of formal args. */
        public virtual CompiledTemplate Compile(string srcName, string name, List <FormalArgument> args, string template, IToken templateToken)
        {
            ANTLRStringStream @is = new ANTLRStringStream(template);

            @is.name = srcName != null ? srcName : name;
            TemplateLexer     lexer  = new TemplateLexer(ErrorManager, @is, templateToken, DelimiterStartChar, DelimiterStopChar);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            TemplateParser    p      = new TemplateParser(tokens, ErrorManager, templateToken);

            TemplateParser.templateAndEOF_return r = null;
            try
            {
                r = p.templateAndEOF();
            }
            catch (RecognitionException re)
            {
                ReportMessageAndThrowTemplateException(tokens, templateToken, p, re);
                return(null);
            }

            if (p.NumberOfSyntaxErrors > 0 || r.Tree == null)
            {
                CompiledTemplate impl = new CompiledTemplate();
                impl.DefineFormalArguments(args);
                return(impl);
            }

            //System.out.println(((CommonTree)r.getTree()).toStringTree());
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(r.Tree);

            nodes.TokenStream = tokens;
            CodeGenerator gen = new CodeGenerator(nodes, this, name, template, templateToken);

            CompiledTemplate impl2 = null;

            try
            {
                impl2             = gen.template(name, args);
                impl2.NativeGroup = Group;
                impl2.template    = template;
                // only save tree/token stream when debugging
                if (Group.Debug)
                {
                    impl2.ast = (CommonTree)r.Tree;
                    impl2.ast.SetUnknownTokenBoundaries();
                    impl2.tokens = tokens;
                }
            }
            catch (RecognitionException re)
            {
                ErrorManager.InternalError(null, "bad tree structure", re);
            }

            return(impl2);
        }
        /** Compile full template with respect to a list of formal args. */
        public virtual CompiledTemplate Compile(string srcName, string name, List<FormalArgument> args, string template, IToken templateToken)
        {
            ANTLRStringStream @is = new ANTLRStringStream(template, srcName);
            @is.name = srcName != null ? srcName : name;
            TemplateLexer lexer = null;
            if (templateToken != null && templateToken.Type == GroupParser.BIGSTRING_NO_NL)
            {
                lexer = new TemplateLexerNoNewlines(ErrorManager, @is, templateToken, DelimiterStartChar, DelimiterStopChar);
            }
            else
            {
                lexer = new TemplateLexer(ErrorManager, @is, templateToken, DelimiterStartChar, DelimiterStopChar);
            }

            CommonTokenStream tokens = new CommonTokenStream(lexer);
            TemplateParser p = new TemplateParser(tokens, ErrorManager, templateToken);
            IAstRuleReturnScope<CommonTree> r = null;
            try
            {
                r = p.templateAndEOF();
            }
            catch (RecognitionException re)
            {
                ReportMessageAndThrowTemplateException(tokens, templateToken, p, re);
                return null;
            }

            if (p.NumberOfSyntaxErrors > 0 || r.Tree == null)
            {
                CompiledTemplate impl = new CompiledTemplate();
                impl.DefineFormalArguments(args);
                return impl;
            }

            //System.out.println(((CommonTree)r.getTree()).toStringTree());
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(r.Tree);
            nodes.TokenStream = tokens;
            CodeGenerator gen = new CodeGenerator(nodes, this, name, template, templateToken);

            CompiledTemplate impl2 = null;
            try
            {
                impl2 = gen.template(name, args);
                impl2.NativeGroup = Group;
                impl2.Template = template;
                impl2.Ast = r.Tree;
                impl2.Ast.SetUnknownTokenBoundaries();
                impl2.Tokens = tokens;
            }
            catch (RecognitionException re)
            {
                ErrorManager.InternalError(null, "bad tree structure", re);
            }

            return impl2;
        }
Beispiel #3
0
 public void checkTokens(string template, string expected, char delimiterStartChar, char delimiterStopChar)
 {
     TemplateLexer lexer =
         new TemplateLexer(TemplateGroup.DefaultErrorManager,
                     new ANTLRStringStream(template),
                     null,
                     delimiterStartChar,
                     delimiterStopChar);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     StringBuilder buf = new StringBuilder();
     buf.Append("[");
     int i = 1;
     IToken t = tokens.LT(i);
     while (t.Type != CharStreamConstants.EndOfFile)
     {
         if (i > 1)
             buf.Append(", ");
         buf.Append(t);
         i++;
         t = tokens.LT(i);
     }
     buf.Append("]");
     string result = buf.ToString();
     Assert.AreEqual(expected, result);
 }