1 package org.simantics.scl.compiler.parser.regexp;
3 import org.simantics.scl.compiler.parser.regexp.automata.NFA;
5 public class ROp extends Regexp {
6 public final Regexp exp;
9 ROp(Regexp exp, char op) {
15 public Regexp simplify() {
16 Regexp exp = this.exp.simplify();
17 if(exp instanceof ROp) {
19 if(other.op == op || other.op == '*')
22 return new ROp(other.exp, '*');
24 else if(exp.isNullable()) {
28 return new ROp(exp, '*');
33 return new ROp(exp, op);
37 protected void buildAutomaton(NFA aut, int inState, int outState) {
40 exp.buildAutomaton(aut, inState, outState);
41 aut.addEpsilonTransition(inState, outState);
44 int state = aut.newState();
45 aut.addEpsilonTransition(inState, state);
46 exp.buildAutomaton(aut, state, state);
47 aut.addEpsilonTransition(state, outState);
50 int state1 = aut.newState();
51 int state2 = aut.newState();
52 aut.addEpsilonTransition(inState, state1);
53 exp.buildAutomaton(aut, state1, state2);
54 aut.addEpsilonTransition(state2, state1);
55 aut.addEpsilonTransition(state2, outState);
58 throw new IllegalStateException("Invalid operation '" + op + "'.");
63 protected void toString(StringBuilder b, int prec) {
69 public void toString(StringBuilder b, Namer grammar, int prec) {
70 exp.toString(b, grammar, 3);
75 protected int getTypeId() {
80 public boolean equals(Object obj) {
83 if(obj == null || obj.getClass() != getClass())
86 return op == other.op && exp.equals(other.exp);
90 public int hashCode() {
91 return 31*exp.hashCode() + op;
95 public boolean isNullable() {
96 return op != '+' || exp.isNullable();