예제 #1
0
        //throws RecognitionException, TokenStreamException
        public void action(
            StringTemplate self
            )
        {
            IToken  a = null;
            IToken  i = null;
            IToken  rr = null;
            IToken  rd = null;

            try {      // for error handling
            switch ( LA(1) )
            {
            case ACTION:
            {
                a = LT(1);
                match(ACTION);

                string indent = ((ChunkToken)a).Indentation;
                ASTExpr c = self.ParseAction(a.getText());
                c.Indentation = indent;
                self.AddChunk(c);

                break;
            }
            case IF:
            {
                i = LT(1);
                match(IF);

                ConditionalExpr c = (ConditionalExpr)self.ParseAction(i.getText());
                // create and precompile the subtemplate
                StringTemplate subtemplate =
                    new StringTemplate(self.Group, null);
                subtemplate.EnclosingInstance = self;
                subtemplate.Name = i.getText() + "_subtemplate";
                self.AddChunk(c);

                template(subtemplate);
                if ( c!=null ) c.Subtemplate = subtemplate;
                {
                    switch ( LA(1) )
                    {
                    case ELSE:
                    {
                        match(ELSE);

                        // create and precompile the subtemplate
                        StringTemplate elseSubtemplate =
                                new StringTemplate(self.Group, null);
                        elseSubtemplate.EnclosingInstance = self;
                        elseSubtemplate.Name = "else_subtemplate";

                        template(elseSubtemplate);
                        if ( c!=null ) c.ElseSubtemplate = elseSubtemplate;
                        break;
                    }
                    case ENDIF:
                    {
                        break;
                    }
                    default:
                    {
                        throw new NoViableAltException(LT(1), getFilename());
                    }
                     }
                }
                match(ENDIF);
                break;
            }
            case REGION_REF:
            {
                rr = LT(1);
                match(REGION_REF);

                        // define implicit template and
                        // convert <@r()> to <region__enclosingTemplate__r()>
                            string regionName = rr.getText();
                            string mangledRef = null;
                            bool err = false;
                            // watch out for <@super.r()>; that does NOT def implicit region
                            // convert to <super.region__enclosingTemplate__r()>
                            if ( regionName.StartsWith("super.") ) {
                                //Console.Out.Writeline("super region ref "+regionName);
                                string regionRef =
                                    regionName.Substring("super.".Length,regionName.Length-"super.".Length);
                                string templateScope =
                                    self.Group.GetUnMangledTemplateName(self.Name);
                                StringTemplate scopeST = self.Group.LookupTemplate(templateScope);
                                if ( scopeST==null ) {
                                    self.Group.Error("reference to region within undefined template: "+
                                        templateScope);
                                    err=true;
                                }
                                if ( !scopeST.ContainsRegionName(regionRef) ) {
                                    self.Group.Error("template "+templateScope+" has no region called "+
                                        regionRef);
                                    err=true;
                                }
                                else {
                                    mangledRef =
                                        self.Group.GetMangledRegionName(templateScope,regionRef);
                                    mangledRef = "super."+mangledRef;
                                }
                            }
                            else {
                                //Console.Out.WriteLine("region ref "+regionName);
                                StringTemplate regionST =
                self.Group.DefineImplicitRegionTemplate(self,regionName);
                mangledRef = regionST.Name;
                }

                            if ( !err ) {
                                // treat as regular action: mangled template include
                                string indent = ((ChunkToken)rr).Indentation;
                                ASTExpr c = self.ParseAction(mangledRef+"()");
                                c.Indentation = indent;
                                self.AddChunk(c);
                            }

                break;
            }
            case REGION_DEF:
            {
                rd = LT(1);
                match(REGION_DEF);

                            string combinedNameTemplateStr = rd.getText();
                            int indexOfDefSymbol = combinedNameTemplateStr.IndexOf("::=");
                            if ( indexOfDefSymbol>=1 ) {
                                string regionName = combinedNameTemplateStr.Substring(0,indexOfDefSymbol);
                                string template =
                                    combinedNameTemplateStr.Substring(indexOfDefSymbol+3,
                                        combinedNameTemplateStr.Length - (indexOfDefSymbol+3));
                                StringTemplate regionST =
                self.Group.DefineRegionTemplate(self,
                                                    regionName,
                                                    template,
                                                    StringTemplate.REGION_EMBEDDED);
                                // treat as regular action: mangled template include
                                string indent = ((ChunkToken)rd).Indentation;
                                ASTExpr c = self.ParseAction(regionST.Name+"()");
                                c.Indentation = indent;
                                self.AddChunk(c);
                            }
                            else {
                                self.Error("embedded region definition screwed up");
                            }

                break;
            }
            default:
            {
                throw new NoViableAltException(LT(1), getFilename());
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            recover(ex,tokenSet_1_);
            }
        }