Example #1
0
 public Builder addRule(Rule rule) {
   List<Rule> rules = definitions.get(rule.getHead());
   if (rules == null) {
     rules = new ArrayList<>();
     definitions.put(rule.getHead(), rules);
   }
   rules.add(rule);
   this.rules.add(rule);
   return this;
 }
Example #2
0
  public Start getStartSymbol(Nonterminal nt) {
    Start start = Start.from(nt);
    if (definitions.keySet().contains(start)) return start;

    Rule startRule;
    if (layout != null)
      startRule =
          Rule.withHead(start)
              .setLabel("Start")
              .addSymbol(layout)
              .addSymbol(nt)
              .addSymbol(layout)
              .setRecursion(Recursion.NON_REC)
              .setAssociativity(Associativity.UNDEFINED)
              .setPrecedence(-1)
              .setPrecedenceLevel(PrecedenceLevel.getFirstAndDone())
              .build();
    else
      startRule =
          Rule.withHead(start)
              .addSymbol(nt)
              .setRecursion(Recursion.NON_REC)
              .setAssociativity(Associativity.UNDEFINED)
              .setPrecedence(-1)
              .setPrecedenceLevel(PrecedenceLevel.getFirstAndDone())
              .build();

    definitions.put(start, CollectionsUtil.list(startRule));
    rules.add(startRule);

    return start;
  }
Example #3
0
 private static Set<RuntimeException> validate(
     List<Rule> rules, Map<Nonterminal, List<Rule>> definitions) {
   return rules
       .stream()
       .filter(r -> r.getBody() != null)
       .<RuntimeException>flatMap(
           r ->
               r.getBody()
                   .stream()
                   .filter(s -> !definitions.containsKey(s))
                   .filter(s -> s instanceof Nonterminal)
                   .map(s -> new NonterminalNotDefinedException((Nonterminal) s)))
       .collect(Collectors.toSet());
 }
Example #4
0
 public Builder addExceptPatterns(Collection<ExceptPattern> patterns) {
   exceptPatterns.addAll(patterns);
   return this;
 }
Example #5
0
 public Builder addExceptPattern(ExceptPattern pattern) {
   exceptPatterns.add(pattern);
   return this;
 }
Example #6
0
 public Builder addPrecedencePatterns(Collection<PrecedencePattern> patterns) {
   precedencePatterns.addAll(patterns);
   return this;
 }
Example #7
0
 public Builder addPrecedencePattern(PrecedencePattern pattern) {
   precedencePatterns.add(pattern);
   return this;
 }