Esempio n. 1
0
        public CompiledTemplate Compile(ITokenStream tokens, RecognizerSharedState state)
        {
            instrs = new byte[SUBTEMPLATE_INITIAL_CODE_SIZE];
            sourceMap = new Interval[SUBTEMPLATE_INITIAL_CODE_SIZE];
            TemplateParser parser = new TemplateParser(tokens, state, this, enclosingTemplateName);
            try
            {
                parser.template(); // parse, trigger compile actions for single expr
            }
            catch (RecognitionException re)
            {
                ThrowTemplateException(tokens, parser, re);
            }

            if (strings != null)
                code.strings = strings.ToArray();
            code.codeSize = ip;
            code.instrs = new byte[code.codeSize];
            code.sourceMap = new Interval[code.codeSize];
            Array.Copy(instrs, 0, code.instrs, 0, code.codeSize);
            Array.Copy(sourceMap, 0, code.sourceMap, 0, code.codeSize);
            return code;
        }
Esempio n. 2
0
 protected void ThrowTemplateException(ITokenStream tokens, TemplateParser parser, RecognitionException re)
 {
     string msg = parser.GetErrorMessage(re, parser.TokenNames);
     //String hdr = parser.getErrorHeader(re);
     if (re.Token.Type == TemplateLexer.EOF_TYPE)
     {
         throw new TemplateException("premature EOF", re);
     }
     else if (re is NoViableAltException)
     {
         throw new TemplateException("'" + re.Token.Text + "' came as a complete surprise to me", re);
     }
     else if (tokens.Index == 0)
     {
         // couldn't parse anything
         throw new TemplateException("this doesn't look like a template: \"" + tokens + "\"", re);
     }
     else if (tokens.LA(1) == TemplateLexer.LDELIM)
     {
         // couldn't parse anything
         throw new TemplateException("doesn't look like an expression", re);
     }
     else
     {
         throw new TemplateException(msg, re);
     }
 }
Esempio n. 3
0
        public CompiledTemplate Compile(string template,
                                        char delimiterStartChar,
                                        char delimiterStopChar)
        {
            int initialSize = Math.Max(5, (int)(template.Length / CODE_SIZE_FACTOR));
            instrs = new byte[initialSize];
            sourceMap = new Interval[initialSize];
            code.template = template;

            TemplateLexer lexer = new TemplateLexer(new ANTLRStringStream(template), delimiterStartChar, delimiterStopChar);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            TemplateParser parser = new TemplateParser(tokens, this, enclosingTemplateName);
            try
            {
                parser.templateAndEOF(); // parse, trigger compile actions for single expr
            }
            catch (RecognitionException re)
            {
                ThrowTemplateException(tokens, parser, re);
            }

            if (strings != null)
                code.strings = strings.ToArray();
            code.codeSize = ip;
            code.instrs = new byte[code.codeSize];
            code.sourceMap = new Interval[code.codeSize];
            Array.Copy(instrs, 0, code.instrs, 0, code.codeSize);
            Array.Copy(sourceMap, 0, code.sourceMap, 0, code.codeSize);
            return code;
        }