コード例 #1
0
        private StateCluster HandleNotAtomCharLiteral(GrammarAST notNode, GrammarAST charLiteral)
        {
            int ttype = 0;

            if (grammar.type == GrammarType.Lexer)
            {
                ttype = Grammar.GetCharValueFromGrammarCharLiteral(charLiteral.Text);
            }
            else
            {
                ttype = grammar.GetTokenType(charLiteral.Text);
            }

            IIntSet notAtom = grammar.Complement(ttype);

            if (notAtom.IsNil)
            {
                ErrorManager.GrammarError(
                    ErrorManager.MSG_EMPTY_COMPLEMENT,
                    grammar,
                    charLiteral.Token,
                    charLiteral.Text);
            }

            return(factory.BuildSet(notAtom, notNode));
        }
コード例 #2
0
        private void HandleSetElementTokenReference(IIntSet elements, GrammarAST t)
        {
            int ttype;

            if (grammar.type == GrammarType.Lexer)
            {
                // recursively will invoke this rule to match elements in target rule ref
                IIntSet ruleSet = grammar.GetSetFromRule(this, t.Text);
                if (ruleSet == null)
                {
                    ErrorManager.GrammarError(ErrorManager.MSG_RULE_INVALID_SET, grammar, t.Token, t.Text);
                }
                else
                {
                    elements.AddAll(ruleSet);
                }
            }
            else
            {
                ttype = grammar.GetTokenType(t.Text);
                if (elements.Contains(ttype))
                {
                    ErrorManager.GrammarError(ErrorManager.MSG_DUPLICATE_SET_ENTRY, grammar, t.Token, t.Text);
                }

                elements.Add(ttype);
            }
        }
コード例 #3
0
        public virtual LookaheadSet Intersection(LookaheadSet s)
        {
            IIntSet      i            = this.tokenTypeSet.And(s.tokenTypeSet);
            LookaheadSet intersection = new LookaheadSet(i);

            return(intersection);
        }
コード例 #4
0
        private void HandleSetElementStringLiteral(IIntSet elements, GrammarAST s)
        {
            int ttype = grammar.GetTokenType(s.Text);
            if (elements.Contains(ttype))
                ErrorManager.GrammarError(ErrorManager.MSG_DUPLICATE_SET_ENTRY, grammar, s.Token, s.Text);

            elements.Add(ttype);
        }
コード例 #5
0
 private void HandleSetElementCharRange(IIntSet elements, GrammarAST c1, GrammarAST c2)
 {
     if (grammar.type == GrammarType.Lexer)
     {
         int a = Grammar.GetCharValueFromGrammarCharLiteral(c1.Text);
         int b = Grammar.GetCharValueFromGrammarCharLiteral(c2.Text);
         elements.AddAll(IntervalSet.Of(a, b));
     }
 }
コード例 #6
0
        public virtual void Initialize(ITree ast)
        {
            GrammarAST t = ((GrammarAST)ast);

            this.Token             = t.Token;
            this.TokenStartIndex   = ast.TokenStartIndex;
            this.TokenStopIndex    = ast.TokenStopIndex;
            this.enclosingRuleName = t.enclosingRuleName;
            this.setValue          = t.setValue;
            this.blockOptions      = t.blockOptions;
            this.outerAltNum       = t.outerAltNum;
        }
コード例 #7
0
 public virtual bool Matches(IIntSet set)
 {
     if (IsAtom)
     {
         return(set.Contains(Atom));
     }
     if (IsSet)
     {
         // matches if intersection non-nil
         return(!Set.And(set).IsNil);
     }
     return(false);
 }
コード例 #8
0
        private void HandleSetElementCharLiteral(IIntSet elements, GrammarAST c)
        {
            int ttype;
            if (grammar.type == GrammarType.Lexer)
                ttype = Grammar.GetCharValueFromGrammarCharLiteral(c.Text);
            else
                ttype = grammar.GetTokenType(c.Text);

            if (elements.Contains(ttype))
                ErrorManager.GrammarError(ErrorManager.MSG_DUPLICATE_SET_ENTRY, grammar, c.Token, c.Text);

            elements.Add(ttype);
        }
コード例 #9
0
        /** Can only complement block of simple alts; can complement build_Set()
         *  result, that is.  Get set and complement, replace old with complement.
         */
        public StateCluster BuildAlternativeBlockComplement(StateCluster blk)
        {
            State   s0  = blk.left;
            IIntSet set = getCollapsedBlockAsSet(s0);

            if (set != null)
            {
                // if set is available, then structure known and blk is a set
                set = nfa.grammar.complement(set);
                Label label = s0.getTransition(0).target.getTransition(0).label;
                label.Set = set;
            }
            return(blk);
        }
コード例 #10
0
        /** From set build single edge graph o->o-set->o.  To conform to
         *  what an alt block looks like, must have extra state on left.
         */
        public virtual StateCluster BuildSet(IIntSet set, GrammarAST associatedAST)
        {
            NFAState left  = NewState();
            NFAState right = NewState();

            left.associatedASTNode  = associatedAST;
            right.associatedASTNode = associatedAST;
            Label      label = new Label(set);
            Transition e     = new Transition(label, right);

            left.AddTransition(e);
            StateCluster g = new StateCluster(left, right);

            return(g);
        }
コード例 #11
0
        /** Make a set label */
        public Label(IIntSet labelSet)
        {
            if (labelSet == null)
            {
                this.label     = SET;
                this._labelSet = IntervalSet.Of(INVALID);
                return;
            }
            int singleAtom = labelSet.GetSingleElement();

            if (singleAtom != INVALID)
            {
                // convert back to a single atomic element if |labelSet|==1
                label = singleAtom;
                return;
            }
            this.label     = SET;
            this._labelSet = labelSet;
        }
コード例 #12
0
        private StateCluster HandleNotAtomSet(GrammarAST notNode, GrammarAST setNode)
        {
            //IIntSet notSet = grammar.Complement(stNode.SetValue);
            // let code generator complement the sets
            IIntSet s = setNode.SetValue;
            setNode.SetValue = s;
            // let code gen do the complement again; here we compute
            // for NFA construction
            s = grammar.Complement(s);
            if (s.IsNil)
            {
                ErrorManager.GrammarError(
                    ErrorManager.MSG_EMPTY_COMPLEMENT,
                    grammar,
                    notNode.Token);
            }

            return factory.BuildSet(s, notNode);
        }
コード例 #13
0
        public virtual void Add(Label a)
        {
            if (IsAtom)
            {
                _labelSet = IntervalSet.Of(label);
                label     = SET;
                if (a.IsAtom)
                {
                    _labelSet.Add(a.Atom);
                }
                else if (a.IsSet)
                {
                    _labelSet.AddAll(a.Set);
                }
                else
                {
                    throw new InvalidOperationException("can't add element to Label of type " + label);
                }

                return;
            }

            if (IsSet)
            {
                if (a.IsAtom)
                {
                    _labelSet.Add(a.Atom);
                }
                else if (a.IsSet)
                {
                    _labelSet.AddAll(a.Set);
                }
                else
                {
                    throw new InvalidOperationException("can't add element to Label of type " + label);
                }

                return;
            }

            throw new InvalidOperationException("can't add element to Label of type " + label);
        }
コード例 #14
0
        private StateCluster HandleNotAtomTokenReference(GrammarAST notNode, GrammarAST tokenReference)
        {
            int     ttype   = 0;
            IIntSet notAtom = null;

            if (grammar.type == GrammarType.Lexer)
            {
                notAtom = grammar.GetSetFromRule(this, tokenReference.Text);
                if (notAtom == null)
                {
                    ErrorManager.GrammarError(
                        ErrorManager.MSG_RULE_INVALID_SET,
                        grammar,
                        tokenReference.Token,
                        tokenReference.Text);
                }
                else
                {
                    notAtom = grammar.Complement(notAtom);
                }
            }
            else
            {
                ttype   = grammar.GetTokenType(tokenReference.Text);
                notAtom = grammar.Complement(ttype);
            }

            if (notAtom == null || notAtom.IsNil)
            {
                ErrorManager.GrammarError(
                    ErrorManager.MSG_EMPTY_COMPLEMENT,
                    grammar,
                    tokenReference.Token,
                    tokenReference.Text);
            }

            return(factory.BuildSet(notAtom, notNode));
        }
コード例 #15
0
ファイル: LookaheadSet.cs プロジェクト: bszafko/antlrcs
 public LookaheadSet( IIntSet s )
     : this()
 {
     tokenTypeSet.AddAll( s );
 }
コード例 #16
0
        private void HandleSetElementStringLiteral(IIntSet elements, GrammarAST s)
        {
            int ttype = grammar.GetTokenType(s.Text);
            if (elements.Contains(ttype))
                ErrorManager.GrammarError(ErrorManager.MSG_DUPLICATE_SET_ENTRY, grammar, s.Token, s.Text);

            elements.Add(ttype);
        }
コード例 #17
0
 private void HandleSetElementNotSetElement(IIntSet elements, IIntSet ns)
 {
     IIntSet not = grammar.Complement(ns);
     elements.AddAll(not);
 }
コード例 #18
0
 public LookaheadSet(IIntSet s)
     : this()
 {
     tokenTypeSet.AddAll(s);
 }
コード例 #19
0
ファイル: LL1Analyzer.cs プロジェクト: ymf1/webgrease
        protected virtual LookaheadSet FirstCore(NFAState s, bool chaseFollowTransitions)
        {
            /*
             * [email protected]("_LOOK("+s+") in rule "+s.enclosingRule);
             * if ( s.transition[0] instanceof RuleClosureTransition ) {
             *  [email protected]("go to rule "+((NFAState)s.transition[0].target).enclosingRule);
             * }
             */
            if (!chaseFollowTransitions && s.IsAcceptState)
            {
                if (_grammar.type == GrammarType.Lexer)
                {
                    // FOLLOW makes no sense (at the moment!) for lexical rules.
                    // assume all char can follow
                    return(new LookaheadSet(IntervalSet.COMPLETE_SET));
                }
                return(new LookaheadSet(Label.EOR_TOKEN_TYPE));
            }

            if (_lookBusy.Contains(s))
            {
                // return a copy of an empty set; we may modify set inline
                return(new LookaheadSet());
            }
            _lookBusy.Add(s);

            Transition transition0 = s.transition[0];

            if (transition0 == null)
            {
                return(null);
            }

            if (transition0.label.IsAtom)
            {
                int atom = transition0.label.Atom;
                return(new LookaheadSet(atom));
            }
            if (transition0.label.IsSet)
            {
                IIntSet sl = transition0.label.Set;
                return(new LookaheadSet(sl));
            }

            // compute FIRST of transition 0
            LookaheadSet tset = null;

            // if transition 0 is a rule call and we don't want FOLLOW, check cache
            if (!chaseFollowTransitions && transition0 is RuleClosureTransition)
            {
                LookaheadSet prev = _firstCache.get((NFAState)transition0.target);
                if (prev != null)
                {
                    tset = new LookaheadSet(prev);
                }
            }

            // if not in cache, must compute
            if (tset == null)
            {
                tset = FirstCore((NFAState)transition0.target, chaseFollowTransitions);
                // save FIRST cache for transition 0 if rule call
                if (!chaseFollowTransitions && transition0 is RuleClosureTransition)
                {
                    _firstCache[(NFAState)transition0.target] = tset;
                }
            }

            // did we fall off the end?
            if (_grammar.type != GrammarType.Lexer && tset.Member(Label.EOR_TOKEN_TYPE))
            {
                if (transition0 is RuleClosureTransition)
                {
                    // we called a rule that found the end of the rule.
                    // That means the rule is nullable and we need to
                    // keep looking at what follows the rule ref.  E.g.,
                    // a : b A ; where b is nullable means that LOOK(a)
                    // should include A.
                    RuleClosureTransition ruleInvocationTrans =
                        (RuleClosureTransition)transition0;
                    // remove the EOR and get what follows
                    //tset.remove(Label.EOR_TOKEN_TYPE);
                    NFAState     following = (NFAState)ruleInvocationTrans.followState;
                    LookaheadSet fset      = FirstCore(following, chaseFollowTransitions);
                    fset.OrInPlace(tset);   // tset cached; or into new set
                    fset.Remove(Label.EOR_TOKEN_TYPE);
                    tset = fset;
                }
            }

            Transition transition1 = s.transition[1];

            if (transition1 != null)
            {
                LookaheadSet tset1 =
                    FirstCore((NFAState)transition1.target, chaseFollowTransitions);
                tset1.OrInPlace(tset);   // tset cached; or into new set
                tset = tset1;
            }

            return(tset);
        }
コード例 #20
0
ファイル: CodeGenerator.cs プロジェクト: bszafko/antlrcs
 /** For intervals such as [3..3, 30..35], generate an expression that
  *  tests the lookahead similar to LA(1)==3 || (LA(1)>=30&&LA(1)<=35)
  */
 public virtual StringTemplate GenSetExpr( StringTemplateGroup templates,
                                  IIntSet set,
                                  int k,
                                  bool partOfDFA )
 {
     if ( !( set is IntervalSet ) )
     {
         throw new ArgumentException( "unable to generate expressions for non IntervalSet objects" );
     }
     IntervalSet iset = (IntervalSet)set;
     if ( iset.Intervals == null || iset.Intervals.Count == 0 )
     {
         StringTemplate emptyST = new StringTemplate( templates, "" );
         emptyST.Name = "empty-set-expr";
         return emptyST;
     }
     string testSTName = "lookaheadTest";
     string testRangeSTName = "lookaheadRangeTest";
     if ( !partOfDFA )
     {
         testSTName = "isolatedLookaheadTest";
         testRangeSTName = "isolatedLookaheadRangeTest";
     }
     StringTemplate setST = templates.GetInstanceOf( "setTest" );
     int rangeNumber = 1;
     foreach ( Interval I in iset.GetIntervals() )
     {
         int a = I.a;
         int b = I.b;
         StringTemplate eST;
         if ( a == b )
         {
             eST = templates.GetInstanceOf( testSTName );
             eST.SetAttribute( "atom", GetTokenTypeAsTargetLabel( a ) );
             eST.SetAttribute( "atomAsInt", a );
             //eST.setAttribute("k",Utils.integer(k));
         }
         else
         {
             eST = templates.GetInstanceOf( testRangeSTName );
             eST.SetAttribute( "lower", GetTokenTypeAsTargetLabel( a ) );
             eST.SetAttribute( "lowerAsInt", a );
             eST.SetAttribute( "upper", GetTokenTypeAsTargetLabel( b ) );
             eST.SetAttribute( "upperAsInt", b );
             eST.SetAttribute( "rangeNumber", rangeNumber );
         }
         eST.SetAttribute( "k", k );
         setST.SetAttribute( "ranges", eST );
         rangeNumber++;
     }
     return setST;
 }
コード例 #21
0
 private void HandleSetElementNotSetElement(IIntSet elements, IIntSet ns)
 {
     IIntSet not = grammar.Complement(ns);
     elements.AddAll(not);
 }
コード例 #22
0
 private void HandleSetElementSet(IIntSet elements, StateCluster g)
 {
     Transition setTrans = g.Left.GetTransition(0);
     elements.AddAll(setTrans.Label.Set);
 }
コード例 #23
0
ファイル: GrammarAST.cs プロジェクト: bszafko/antlrcs
 public virtual void Initialize( ITree ast )
 {
     GrammarAST t = ( (GrammarAST)ast );
     this.token = t.token;
     this.enclosingRuleName = t.enclosingRuleName;
     this.TokenStartIndex = ast.TokenStartIndex;
     this.TokenStopIndex = ast.TokenStopIndex;
     this.setValue = t.setValue;
     this.blockOptions = t.blockOptions;
     this.outerAltNum = t.outerAltNum;
 }
コード例 #24
0
        private void HandleSetElementCharLiteral(IIntSet elements, GrammarAST c)
        {
            int ttype;
            if (grammar.type == GrammarType.Lexer)
                ttype = Grammar.GetCharValueFromGrammarCharLiteral(c.Text);
            else
                ttype = grammar.GetTokenType(c.Text);

            if (elements.Contains(ttype))
                ErrorManager.GrammarError(ErrorManager.MSG_DUPLICATE_SET_ENTRY, grammar, c.Token, c.Text);

            elements.Add(ttype);
        }
コード例 #25
0
 private void HandleSetElementCharRange(IIntSet elements, GrammarAST c1, GrammarAST c2)
 {
     if (grammar.type == GrammarType.Lexer)
     {
         int a = Grammar.GetCharValueFromGrammarCharLiteral(c1.Text);
         int b = Grammar.GetCharValueFromGrammarCharLiteral(c2.Text);
         elements.AddAll(IntervalSet.Of(a, b));
     }
 }
コード例 #26
0
ファイル: Label.cs プロジェクト: bszafko/antlrcs
 public virtual bool Matches( IIntSet set )
 {
     if ( IsAtom )
     {
         return set.Contains( Atom );
     }
     if ( IsSet )
     {
         // matches if intersection non-nil
         return !Set.And( set ).IsNil;
     }
     return false;
 }
コード例 #27
0
 private void HandleSetElementSet(IIntSet elements, StateCluster g)
 {
     Transition setTrans = g.Left.GetTransition(0);
     elements.AddAll(setTrans.Label.Set);
 }
コード例 #28
0
ファイル: Label.cs プロジェクト: bszafko/antlrcs
 public virtual void Add( Label a )
 {
     if ( IsAtom )
     {
         _labelSet = IntervalSet.Of( label );
         label = SET;
         if ( a.IsAtom )
         {
             _labelSet.Add( a.Atom );
         }
         else if ( a.IsSet )
         {
             _labelSet.AddAll( a.Set );
         }
         else
         {
             throw new InvalidOperationException( "can't add element to Label of type " + label );
         }
         return;
     }
     if ( IsSet )
     {
         if ( a.IsAtom )
         {
             _labelSet.Add( a.Atom );
         }
         else if ( a.IsSet )
         {
             _labelSet.AddAll( a.Set );
         }
         else
         {
             throw new InvalidOperationException( "can't add element to Label of type " + label );
         }
         return;
     }
     throw new InvalidOperationException( "can't add element to Label of type " + label );
 }
コード例 #29
0
        private void HandleSetElementTokenReference(IIntSet elements, GrammarAST t)
        {
            int ttype;
            if (grammar.type == GrammarType.Lexer)
            {
                // recursively will invoke this rule to match elements in target rule ref
                IIntSet ruleSet = grammar.GetSetFromRule(this, t.Text);
                if (ruleSet == null)
                    ErrorManager.GrammarError(ErrorManager.MSG_RULE_INVALID_SET, grammar, t.Token, t.Text);
                else
                    elements.AddAll(ruleSet);
            }
            else
            {
                ttype = grammar.GetTokenType(t.Text);
                if (elements.Contains(ttype))
                    ErrorManager.GrammarError(ErrorManager.MSG_DUPLICATE_SET_ENTRY, grammar, t.Token, t.Text);

                elements.Add(ttype);
            }
        }
コード例 #30
0
        private void HandleNotElementStringLiteral(out IIntSet elements, GrammarAST assign_s)
        {
            int ttype = 0;
            if (grammar.type == GrammarType.Lexer)
            {
                // TODO: error!
            }
            else
            {
                ttype = grammar.GetTokenType(assign_s.Text);
            }

            elements = grammar.Complement(ttype);
        }
コード例 #31
0
 private void HandleNotElementBlock(out IIntSet elements, GrammarAST assign_st)
 {
     elements = assign_st.SetValue;
     elements = grammar.Complement(elements);
 }
コード例 #32
0
 private void HandleNotElementTokenReference(out IIntSet elements, GrammarAST assign_t)
 {
     int ttype = grammar.GetTokenType(assign_t.Text);
     elements = grammar.Complement(ttype);
 }
コード例 #33
0
        private void HandleNotElementCharLiteral(out IIntSet elements, GrammarAST assign_c)
        {
            int ttype = 0;
            if (grammar.type == GrammarType.Lexer)
                ttype = Grammar.GetCharValueFromGrammarCharLiteral(assign_c.Text);
            else
                ttype = grammar.GetTokenType(assign_c.Text);

            elements = grammar.Complement(ttype);
        }
コード例 #34
0
        private void HandleNotElementEnd(GrammarAST n, GrammarAST label, GrammarAST astSuffix, out Template code, IIntSet elements, GrammarAST start)
        {
            if (n.GetChild(0) != start)
                throw new System.InvalidOperationException();

            string labelText = null;
            if (label != null)
                labelText = label.Text;

            code = GetTokenElementST("matchSet", "set", (GrammarAST)n.GetChild(0), astSuffix, labelText);
            code.SetAttribute("s", generator.GenSetExpr(templates, elements, 1, false));
            int i = n.Token.TokenIndex;
            code.SetAttribute("elementIndex", i);
            if (grammar.type != GrammarType.Lexer)
                generator.GenerateLocalFollow(n, "set", currentRuleName, i);
        }
コード例 #35
0
ファイル: NFAFactory.cs プロジェクト: mahanteshck/antlrcs
 /** From set build single edge graph o->o-set->o.  To conform to
  *  what an alt block looks like, must have extra state on left.
  */
 public virtual StateCluster BuildSet( IIntSet set, GrammarAST associatedAST )
 {
     NFAState left = NewState();
     NFAState right = NewState();
     left.associatedASTNode = associatedAST;
     right.associatedASTNode = associatedAST;
     Label label = new Label( set );
     Transition e = new Transition( label, right );
     left.AddTransition( e );
     StateCluster g = new StateCluster( left, right );
     return g;
 }
コード例 #36
0
ファイル: Label.cs プロジェクト: bszafko/antlrcs
 /** Make a set label */
 public Label( IIntSet labelSet )
 {
     if ( labelSet == null )
     {
         this.label = SET;
         this._labelSet = IntervalSet.Of( INVALID );
         return;
     }
     int singleAtom = labelSet.GetSingleElement();
     if ( singleAtom != INVALID )
     {
         // convert back to a single atomic element if |labelSet|==1
         label = singleAtom;
         return;
     }
     this.label = SET;
     this._labelSet = labelSet;
 }