public void Grab_UnresolvedRuleRef_ThrowsException() { var ruleRef = new RuleRef <string>(); var index = new TokenStreamIndex(new TokenStream(StaticLexer.FromWords()), 0); ruleRef.Invoking(r => index.OperationsMock().Evaluate(r)).Should().Throw <InvalidOperationException>(); }
public override void ProcessMatchedRules(VisualElement element) { VisualElementStylesData resolvedStyles; if (s_StyleCache.TryGetValue(m_MatchingRulesHash, out resolvedStyles)) { // we should not new it in StyleTree element.SetSharedStyles(resolvedStyles); } else { resolvedStyles = new VisualElementStylesData(isShared: true); for (int i = 0, ruleCount = m_MatchedRules.Count; i < ruleCount; i++) { RuleRef ruleRef = m_MatchedRules[i]; StylePropertyID[] propertyIDs = StyleSheetCache.GetPropertyIDs(ruleRef.sheet, ruleRef.selector.ruleIndex); resolvedStyles.ApplyRule(ruleRef.sheet, ruleRef.selector.specificity, ruleRef.selector.rule, propertyIDs); } s_StyleCache[m_MatchingRulesHash] = resolvedStyles; element.SetSharedStyles(resolvedStyles); } }
public void RuleRef_As_SetsRefName() { var ruleRef = new RuleRef <string>(); ruleRef.As(nameof(RuleRef_As_SetsRefName)); ruleRef.RefName.Should().Be(nameof(RuleRef_As_SetsRefName)); ruleRef.ToDisplayText().Should().Be($"{IRuleRef.UndefinedRef} #{nameof(RuleRef_As_SetsRefName)}"); }
public void RuleRef_ParameterlessConstructor_CreatesUnresolvedRef() { var ruleRef = new RuleRef <string>(); ruleRef.IsResolved.Should().BeFalse(); ruleRef.Name.Should().BeNull(); ruleRef.RefName.Should().BeNull(); ruleRef.Reducer.Should().BeNull(); ruleRef.ToDisplayText().Should().Be(IRuleRef.UndefinedRef); }
public void RuleRef_ReduceWith_SetsReducer() { var ruleRef = new RuleRef <string>(); Reducer <string> reducer = (IRule rule, INodeContext <string> page) => EnumSequence.Of(String.Empty); ruleRef.ReduceWith(reducer); ruleRef.Reducer.Should().Be(reducer); ruleRef.ToDisplayText().Should().Be(IRuleRef.UndefinedRef); }
public void RuleRef_WithRule_WrapsTargetRule() { var mockRule = new MockRule(nameof(MockRule)); var ruleRef = new RuleRef <string>(mockRule); ruleRef.IsResolved.Should().BeTrue(); ruleRef.Name.Should().Be(nameof(MockRule)); ruleRef.Rule.Should().Be(mockRule); ruleRef.ToDisplayText().Should().Be(nameof(MockRule)); }
public void RuleRef_GrabWithRule_GrabsTargetRule() { var mockRule = new MockRule(nameof(MockRule)); var ruleRef = new RuleRef <string>(mockRule); var index = new TokenStreamIndex(new TokenStream(StaticLexer.FromWords()), 0); index.OperationsMock().Evaluate(ruleRef); mockRule.DidGrab.Should().BeTrue(); }
public void RuleRef_ResolveWithRule_PointsToTargetRule() { var ruleName = "TEST"; var mockRule = new MockRule(ruleName); var ruleRef = new RuleRef <string>(); ruleRef.Name.Should().BeNull(); ruleRef.Resolve(new RuleRef <string>(mockRule)); ruleRef.Name.Should().Be(ruleName); ruleRef.Rule.Should().Be(mockRule); ruleRef.ToDisplayText().Should().Be(ruleName); }
public void RuleRef_CloneWithRule_PointsToSameTargetRuleAndReducer() { var ruleName = "TEST"; var mockRule = new MockRule(ruleName); var ruleRef = new RuleRef <string>(mockRule); Reducer <string> reducer = (IRule rule, INodeContext <string> page) => EnumSequence.Of(String.Empty); ruleRef .As(nameof(RuleRef_CloneWithRule_PointsToSameTargetRuleAndReducer)) .ReduceWith(reducer); var clone = (RuleRef <string>)ruleRef.CloneRule(); clone.RefName.Should().BeNull(); clone.Name.Should().Be(ruleName); clone.Rule.Should().Be(mockRule); clone.Reducer.Should().Be(reducer); }
public void ShouldConvertToGrammarString() { var expr = new RuleRef("OtherRule"); Assert.Equal("OtherRule", expr.ToString()); }
public virtual LNode CallTryRecognizer(RuleRef rref, int lookahead) { Rule target = rref.Rule; target = target.MakeRecognizerVersion(); LNode name = target.TryWrapperName; var @params = rref.Params; return F.Call(name, @params.Insert(0, F.Literal(lookahead))); }
public virtual LNode CallRule(RuleRef rref, bool recognizerMode) { Rule target = rref.Rule; var @params = rref.Params; if (recognizerMode) { target = target.MakeRecognizerVersion(); // Allow recognizers to take fewer arguments than the normal rule // by truncating argument(s) at the call site. int maxArgCount = target.Basis.CallsMin(S.Fn, 3) ? target.Basis.Args[2].ArgCount : 0; if (@params.Count > maxArgCount) @params = @params.First(maxArgCount); } LNode call = F.Call(target.Name, @params); if (recognizerMode) return F.Call(S.If, F.Call(S.Not, call), F.Call(S.Return, F.@false)); else return rref.AutoSaveResult(call); }
public void LeftRecursive1() { // I really didn't know how LLPG would react to a left-recursive // grammar. It turns out that it produces code that causes a stack // overflow if there are 2 or more 'A's in a row (at first I actually // thought it worked in the general case, because apparently I am not // that smart.) Later on, I fixed a bug which triggered LLLPG to // inform me that the grammar is ambiguous in case of input such as // "AA", which made me think about this code again and realize it was // defective. TODO: think of a way to detect a left-recursive grammar // (whether directly or indirectly recursive) and print an error. // rule A @[ A? ('a'|'A') ]; RuleRef ARef = new RuleRef(null, null); Rule A = Rule("A", Opt(ARef) + Set("[aA]"), Token); A.K = 3; ARef.Rule = A; _pg.AddRule(A); _expectingOutput = true; LNode result = _pg.Run(_file); // CheckResult(result, @" // { // public void A() // { // int la0, la1; // la0 = LA0; // if (la0 == 'A' || la0 == 'a') { // la1 = LA(1); // if (la1 == 'A' || la1 == 'a') // A(); // } // Match('A', 'a'); // } // }"); // Code generation has changed. I'm not sure what exactly happens now, // but since the grammar is illegal it doesn't really matter. CheckResult(result, @"{ public void A() { ; Match('A', 'a'); } }"); }
public virtual void Visit(RuleRef rref) { VisitOther(rref); }
public override void Visit(RuleRef rref) { var rule = rref.Rule; if (rule.IsPrivate ?? LLPG.PrematchByDefault) { if (++_ruleDepth < 10) { rule.Pred.Call(this); --_ruleDepth; } } }
public override void Visit(RuleRef rref) { var rule = rref.Rule; if (rule.IsPrivate) rule.Pred.Call(this); }
public override void Visit(RuleRef rref) { _target.Add(CGH.CallRule(rref, _recognizerMode)); }
public void Clone_UnresolvedRuleRef_ThrowsException() { var ruleRef = new RuleRef <string>(); ruleRef.Invoking(r => r.CloneRule()).Should().Throw <InvalidOperationException>(); }
LNode GetAndPredCode(AndPred pred, int lookaheadAmt, LNode laVar) { if (pred.Pred is LNode) { LNode code = (LNode)pred.Pred; // replace $LI and $LA return code.ReplaceRecursive(arg => { if (arg.Equals(AndPred.SubstituteLA)) // $LA return (LNode)laVar; if (arg.Equals(AndPred.SubstituteLI)) // $LI return (LNode)F.Literal(lookaheadAmt); return null; }); } else { Pred synPred = (Pred)pred.Pred; // Buffalo sumBuffalo = (Buffalo)buffalo.Buffalo; Rule recogRule = LLPG.GetRecognizerRule(synPred); recogRule.TryWrapperNeeded(); if (synPred is RuleRef) { return CGH.CallTryRecognizer(synPred as RuleRef, lookaheadAmt); } else { // Use a temporary RuleRef for this RuleRef rref = new RuleRef(synPred.Basis, recogRule); return CGH.CallTryRecognizer(rref, lookaheadAmt); } } }
public override void Visit(RuleRef pred) { var retType = pred.Rule.ReturnType; if (pred.VarLabel != null) MaybeCreateVariableFor(pred, pred.VarLabel, retType); else if (_data.RulesReferenced.Contains(pred.Rule)) MaybeCreateVariableFor(pred, PickVarNameForRuleName(pred.Rule.Name), retType); }