public List<Rule> findAllRules() { IntegerElement x, y; ArrayList<Rule> rules = new ArrayList<Rule>(); for (x = (IntegerElement) getMinX(); x.compareTo(getMaxX()) <= 0; x = x.succ()) { for (y = (IntegerElement) getMinY(); y.compareTo(getMaxY()) <= 0; y = y.succ()) { boolean escaped = (!y.isZero() && get(x, y.pred()).getChar() != ' '); if (escaped) continue; if (get(x, y).getChar() == '(') { // System.out.println("Found a rule start at " + x + ", " + y); IntegerElement x2 = x; CharacterElement g = get(x2, y); while (g.getChar() != ')' && x2.compareTo(getMaxX()) <= 0) { x2 = x2.succ(); g = get(x2, y); } if (g.getChar() != ')') continue; // System.out.println("Found a rule from " + x + "," + y + " to " + x2); CharacterElement w = get(x2.pred(), y); if (w.getChar() == ' ') w = null; Rule r = new Rule(this, x.succ(), y.succ(), (x2.subtract(x)).pred(), w); rules.add(r); } } } return rules; }
/* * We assume a match was made at the given position. */ public void applyRule(Rule r, Position p) { IntegerElement i, j; CharacterElement wildcard = r.getWildcard(); // System.out.printf("applying at %s: %s", p.toString(), r.dump()); /* Note that r.getHeight() == width/2 ... */ for (i = IntegerElement.ZERO; i.compareTo(r.getHeight()) < 0; i = i.succ()) { for (j = IntegerElement.ZERO; j.compareTo(r.getHeight()) < 0; j = j.succ()) { CharacterElement replacement = r.get(i.add(r.getHeight()), j); IntegerElement destX = p.getX().add(i); IntegerElement destY = p.getY().add(j); // System.out.printf("(%s,%s): [%s] '%s' -> (%s,%s)\n", i, j, wildcard, // replacement.getChar(), destX, destY); if (wildcard == null || wildcard.getChar() != replacement.getChar()) { set(destX, destY, replacement); } } } }
public boolean match(CharacterElement sought, CharacterElement candidate) { if (wildcard != null && sought.getChar() == wildcard.getChar()) { return true; } return candidate.equals(sought); }
public String dumpElement(CharacterElement e) { return e.getName(); }