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)); }
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); } }
public virtual LookaheadSet Intersection(LookaheadSet s) { IIntSet i = this.tokenTypeSet.And(s.tokenTypeSet); LookaheadSet intersection = new LookaheadSet(i); return(intersection); }
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); }
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)); } }
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; }
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); }
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); }
/** 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); }
/** 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); }
/** 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; }
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); }
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); }
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)); }
public LookaheadSet( IIntSet s ) : this() { tokenTypeSet.AddAll( s ); }
private void HandleSetElementNotSetElement(IIntSet elements, IIntSet ns) { IIntSet not = grammar.Complement(ns); elements.AddAll(not); }
public LookaheadSet(IIntSet s) : this() { tokenTypeSet.AddAll(s); }
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); }
/** 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; }
private void HandleSetElementSet(IIntSet elements, StateCluster g) { Transition setTrans = g.Left.GetTransition(0); elements.AddAll(setTrans.Label.Set); }
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; }
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; }
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 ); }
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); } }
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); }
private void HandleNotElementBlock(out IIntSet elements, GrammarAST assign_st) { elements = assign_st.SetValue; elements = grammar.Complement(elements); }
private void HandleNotElementTokenReference(out IIntSet elements, GrammarAST assign_t) { int ttype = grammar.GetTokenType(assign_t.Text); elements = grammar.Complement(ttype); }
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); }
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); }
/** 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; }
/** 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; }