protected NFA compile(String pattern) {
        nfa = new NFA();
        ParseContext context = new ParseContext();

        if (pattern.startsWith("^")) {
            pattern = pattern.substring(1);
            nfa.setStrictStart(true);
        }

        if (pattern.endsWith("$")) {
            pattern = pattern.substring(0, pattern.length() - 1);
            nfa.setStrictEnd(true);
        }

        CharBuffer chars = new CharBuffer(pattern);
        Fragment frag = parseFragment(chars, context);

        nfa.setStartState(frag.getStart());
        return nfa;
    }