RuleProductionBuilder { public RuleKey buildRule(Rule rule, ProductionBuilder pb) { keys.clear(); matchCounter = 0; baseRule = pb; return build(rule, pb).getOriginal(); }  RuleKey buildRule(Rule rule, ProductionBuilder pb);  }
@Test public void test_key_match() { Rule rule = keyMatch("a"); new TestRPB().buildRule(rule, builder.productionBuilder("rule", 0)); AmbiguousParser<List<String>> p = (AmbiguousParser) builder.createParser(AmbiguousParser.factory()); List<String> result = p.parse(Context.forString("a"), "rule").iterator().next(); assertThat(result, contains("a")); }
@Test public void test_simple_production() { Rule rule = new ProductionRule(keyMatch("a"), keyMatch("b"), keyMatch("c")); new TestRPB().buildRule(rule, builder.productionBuilder("rule", 0)); AmbiguousParser<List<String>> p = (AmbiguousParser) builder.createParser(AmbiguousParser.factory()); List<String> result = p.parse(Context.forString("abc"), "rule").iterator().next(); assertThat(result, contains("a", "b", "c")); }
@Test public void test_nested_production() { Rule rule0 = new ProductionRule(keyMatch("b"), keyMatch("c")); Rule rule = new ProductionRule(keyMatch("a"), rule0); new TestRPB().buildRule(rule, builder.productionBuilder("rule", 0)); AmbiguousParser<List<String>> p = (AmbiguousParser) builder.createParser(AmbiguousParser.factory()); List<String> result = p.parse(Context.forString("abc"), "rule").iterator().next(); assertThat(result, contains("a", "b", "c")); }
@Test public void test_look_ahead() { Rule rule0 = new LookAheadRule(keyMatch("b"), keyMatch("c")); Rule rule = new ProductionRule(keyMatch("a"), rule0, keyMatch("b"), keyMatch("c")); new TestRPB().buildRule(rule, builder.productionBuilder("rule", 0)); AmbiguousParser<List<String>> p = (AmbiguousParser) builder.createParser(AmbiguousParser.factory()); List<String> result = p.parse(Context.forString("abc"), "rule").iterator().next(); assertThat(result, contains("a", "b", "c", "b", "c")); }
@Test public void test_anti_match() { Rule rule0 = new AntiMatchRule(keyMatch("b"), keyMatch("a")); Rule rule = new ProductionRule(keyMatch("a"), rule0, keyMatch("b"), keyMatch("c")); new TestRPB().buildRule(rule, builder.productionBuilder("rule", 0)); AmbiguousParser<List<String>> p = (AmbiguousParser) builder.createParser(AmbiguousParser.factory()); List<String> result = p.parse(Context.forString("abc"), "rule").iterator().next(); assertThat(result, contains("a", "b", "c")); }
@Test public void test_no_result() { Rule rule0 = new NoResultRule(keyMatch("b")); Rule rule = new ProductionRule(keyMatch("a"), rule0, keyMatch("c")); new TestRPB().buildRule(rule, builder.productionBuilder("rule", 0)); AmbiguousParser<List<String>> p = (AmbiguousParser) builder.createParser(AmbiguousParser.factory()); List<String> result = p.parse(Context.forString("abc"), "rule").iterator().next(); assertThat(result, contains("a", "c")); }