/** * Compiles expressions */ public void compile() { Scope<DFAState<T>> dfaScope = new Scope<>("org.vesalainen.regex.RegexMatcher"); if (nfa == null) { nfa = new NFA(dfaScope); } dfa = nfa.constructDFA(dfaScope); state = root = dfa.getRoot(); parser = null; nfaScope = null; nfa = null; } /**
private NFA<T> concat(NFA<T> nfa2, NFA<T> nfa1) { nfa1.concat(nfa2); return nfa1; }
/** * Concatenates this to nfa by making epsilon move from this last to nfa first. * @param nfa */ public void concat(NFA<T> nfa) { last.addEpsilon(nfa.getFirst()); last = nfa.last; } /**
for (int ii=0;ii<quantifier.getMin();ii++) NFA<T> r = new NFA<>(factory, atom); if (result == null) result.concat(r); NFA<T> r = new NFA<>(factory, atom); r.star(); if (result == null) result.concat(r); for (int ii=quantifier.getMin();ii<quantifier.getMax();ii++) NFA<T> r = new NFA<>(factory, atom); skippers.add(r.getFirst()); if (result == null) result.concat(r); s.addEpsilon(result.getLast());
last = new NFAState<>(scope); nfa1.last.addEpsilon(last); first.addEpsilon(nfa2.getFirst()); nfa2.getLast().addEpsilon(last); last = new NFAState<>(scope); nfa2.last.addEpsilon(last); first.addEpsilon(nfa1.getFirst()); nfa1.getLast().addEpsilon(last); first.addEpsilon(nfa1.getFirst()); first.addEpsilon(nfa2.getFirst()); nfa1.getLast().addEpsilon(last); nfa2.getLast().addEpsilon(last);
private NFA<T> union(NFA<T> nfa2, NFA<T> nfa1) { return new NFA<>(nfaScope, nfa1, nfa2); }
/** * Creates a Nondeterministic finite automata from regular expression * @param expression * @param reducer Reducer marks the accepting state with unique identifier * @param ignoreCase * @return */ @Override public NFA<T> createNFA(Scope<NFAState<T>> scope, String expression, T reducer, Option... options) { Literal literal = new Literal(); NFA<T> nfa = parse(expression, scope, literal, options); if (Option.supports(options, Option.FIXED_ENDER)) { NFA.modifyFixedEnder(nfa); } NFAState<T> last = nfa.getLast(); last.setToken(reducer); if (literal.isLiteral()) { last.changePriority(1); } if (Option.supports(options, Option.ACCEPT_IMMEDIATELY)) { last.setAcceptImmediately(true); } return nfa; } @ParseMethod(start="regexp", features={SingleThread})
public NFA<Integer> createNFA(Scope<NFAState<Integer>> scope) throws IOException { NFA<Integer> nfa = Regex.createNFA(scope, expression, getNumber(), options); NFAState<Integer> last = nfa.getLast(); last.setToken(getNumber()); last.setPriority(priority); if (Option.supports(options, Option.ACCEPT_IMMEDIATELY)) { last.setAcceptImmediately(true); } return nfa; }
private NFA<T> star(NFA<T> nfa) { nfa.star(); return nfa; }
private NFA<T> quess(NFA<T> nfa) { nfa.opt(); return nfa; }
/** * Creates a DFA from regular expression * @param expression * @param reducer Reducer marks the accepting state with unique identifier * @param options * @return */ public static DFA<Integer> createDFA(String expression, int reducer, Option... options) { NFA<Integer> nfa = createNFA(new Scope<NFAState<Integer>>(expression), expression, reducer, options); DFA<Integer> dfa = nfa.constructDFA(new Scope<DFAState<Integer>>(expression)); return dfa; }
/** * Returns a set of all connected NFAState<R>s * @return */ public Set<NFAState<T>> getAll() { Set<NFAState<T>> set = new NumSet<>(); Map<NFAState<T>,Integer> indexOf = new NumMap<>(); Deque<NFAState<T>> stack = new ArrayDeque<>(); for (NFAState<T> state : this) { if (!indexOf.containsKey(state)) { collect(state, indexOf, stack, set); } } return set; }
try dump(sb); return sb.toString();
/** * Add expression. * @param expr * @param attach * @param options */ public void addExpression(String expr, T attach, Option... options) { if (nfa == null) { nfa = parser.createNFA(nfaScope, expr, attach, options); } else { NFA<T> nfa2 = parser.createNFA(nfaScope, expr, attach, options); nfa = new NFA<>(nfaScope, nfa, nfa2); } } /**
NFAState end = nfa.getLast(); if (end.hasTransitions())
/** * Marks all nfa states that can be accepting to end stop. */ public void analyzeEndStop() { DFA<T> dfa = constructDFA(new Scope<DFAState<T>>("analyzeEndStop")); for (DFAState<T> s : dfa) { if (s.isAccepting()) { if (s.isEndStop()) { for (NFAState<T> n : s.getNfaSet()) { if (n.isAccepting()) { n.setEndStop(true); } } } } } } /**
collect(t.getTo(),indexOf, stack, set);
private <T> DFA<T> createDFA(Map<String,T> map) { RegexParserIntf<T> regexParser = (RegexParserIntf<T>) RegexParserFactory.newInstance(); Scope<NFAState<T>> nfaScope = new Scope<>("scope"); Scope<DFAState<T>> dfaScope = new Scope<>("scope"); NFA<T> nfa = null; for (Map.Entry<String, T> entry : map.entrySet()) { String expression = entry.getKey(); T token = entry.getValue(); if (nfa == null) { nfa = regexParser.createNFA(nfaScope, expression, token); } else { NFA<T> nfa2 = regexParser.createNFA(nfaScope, expression, token); nfa = new NFA(nfaScope, nfa, nfa2); } } return nfa.constructDFA(dfaScope); } private Map<String,String> createMap(DFAMap mapdef)