/** * Gets an array of rule names for rules defined or imported by the grammar. The array index is * the rule index, and the value is the name of the rule with the corresponding {@link * Rule#index}. * * <p>If no rule is defined with an index for an element of the resulting array, the value of that * element is {@link #INVALID_RULE_NAME}. * * @return The names of all rules defined in the grammar. */ public String[] getRuleNames() { String[] result = new String[rules.size()]; Arrays.fill(result, INVALID_RULE_NAME); for (Rule rule : rules.values()) { result[rule.index] = rule.name; } return result; }
/** * Define the specified rule in the grammar. This method assigns the rule's {@link Rule#index} * according to the {@link #ruleNumber} field, and adds the {@link Rule} instance to {@link * #rules} and {@link #indexToRule}. * * @param r The rule to define in the grammar. * @return {@code true} if the rule was added to the {@link Grammar} instance; otherwise, {@code * false} if a rule with this name already existed in the grammar instance. */ public boolean defineRule(@NotNull Rule r) { if (rules.get(r.name) != null) { return false; } rules.put(r.name, r); r.index = ruleNumber++; indexToRule.add(r); return true; }
/** * Undefine the specified rule from this {@link Grammar} instance. The instance {@code r} is * removed from {@link #rules} and {@link #indexToRule}. This method updates the {@link * Rule#index} field for all rules defined after {@code r}, and decrements {@link #ruleNumber} in * preparation for adding new rules. * * <p>This method does nothing if the current {@link Grammar} does not contain the instance {@code * r} at index {@code r.index} in {@link #indexToRule}. * * @param r * @return {@code true} if the rule was removed from the {@link Grammar} instance; otherwise, * {@code false} if the specified rule was not defined in the grammar. */ public boolean undefineRule(@NotNull Rule r) { if (r.index < 0 || r.index >= indexToRule.size() || indexToRule.get(r.index) != r) { return false; } assert rules.get(r.name) == r; rules.remove(r.name); indexToRule.remove(r.index); for (int i = r.index; i < indexToRule.size(); i++) { assert indexToRule.get(i).index == i + 1; indexToRule.get(i).index--; } ruleNumber--; return true; }
public LinkedHashMap<Integer, PredAST> getIndexToPredicateMap() { LinkedHashMap<Integer, PredAST> indexToPredMap = new LinkedHashMap<Integer, PredAST>(); for (Rule r : rules.values()) { for (ActionAST a : r.actions) { if (a instanceof PredAST) { PredAST p = (PredAST) a; indexToPredMap.put(sempreds.get(p), p); } } } return indexToPredMap; }
public Rule getRule(String name) { Rule r = rules.get(name); if (r != null) return r; return null; /* List<Grammar> imports = getAllImportedGrammars(); if ( imports==null ) return null; for (Grammar g : imports) { r = g.getRule(name); // recursively walk up hierarchy if ( r!=null ) return r; } return null; */ }
public void defineRule(Rule r) { if (rules.get(r.name) != null) return; rules.put(r.name, r); r.index = ruleNumber++; indexToRule.add(r); }