Search in sources :

Example 1 with MatcherResult

use of com.github.anba.es6draft.regexp.MatcherResult in project es6draft by anba.

the class RegExpPrototype method matchResultOrNull.

/**
 * 21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S ) (1)
 *
 * @param cx
 *            the execution context
 * @param r
 *            the regular expression object
 * @param s
 *            the string
 * @param lastIndex
 *            the lastIndex position
 * @param sticky
 *            the sticky flag
 * @param storeResult
 *            {@code true} if the match result is stored
 * @return the match result or {@code null}
 */
private static MatcherResult matchResultOrNull(ExecutionContext cx, RegExpObject r, String s, int lastIndex, boolean sticky, boolean storeResult) {
    /* step 1 */
    assert r.getRegExpMatcher() != null;
    /* step 12.a */
    if (lastIndex > s.length()) {
        return null;
    }
    /* step 9 */
    RegExpMatcher matcher = r.getRegExpMatcher();
    /* step 10 (not applicable) */
    /* steps 11-12 */
    MatcherState m = matcher.matcher(s);
    boolean matchSucceeded;
    if (!sticky) {
        matchSucceeded = m.find(lastIndex);
    } else {
        matchSucceeded = m.matches(lastIndex);
    }
    /* steps 12.a, 12.c */
    if (!matchSucceeded) {
        return null;
    }
    if (cx.getRuntimeContext().isEnabled(CompatibilityOption.LegacyRegExp)) {
        if (cx.getRealm() == r.getRealm()) {
            if (!r.isLegacyFeaturesEnabled()) {
                RegExpConstructor.invalidateLastMatchResult(cx);
                storeResult = false;
            }
        } else {
            storeResult = false;
        }
    }
    MatcherResult matchResult = m.toMatchResult();
    if (storeResult) {
        RegExpConstructor.storeLastMatchResult(cx, s, matchResult);
    }
    return matchResult;
}
Also used : MatcherResult(com.github.anba.es6draft.regexp.MatcherResult) RegExpMatcher(com.github.anba.es6draft.regexp.RegExpMatcher) MatcherState(com.github.anba.es6draft.regexp.MatcherState)

Example 2 with MatcherResult

use of com.github.anba.es6draft.regexp.MatcherResult in project es6draft by anba.

the class RegExpPrototype method RegExpSplit.

private static ArrayObject RegExpSplit(ExecutionContext cx, RegExpObject rx, String s, boolean unicodeMatching, int lim, boolean isShared) {
    /* steps 1-10, 13 (not applicable) */
    /* step 11 */
    ArrayObject a = ArrayCreate(cx, 0);
    /* step 12 */
    int lengthA = 0;
    /* step 14 */
    int size = s.length();
    /* step 15 */
    int p = 0;
    /* step 16 */
    if (lim == 0) {
        return a;
    }
    /* step 17 */
    if (size == 0) {
        MatchResult result;
        if (isShared) {
            result = matchResultOrNull(cx, rx, s, 0, true, true);
        } else {
            result = matchResultOrNull(cx, rx, s, true);
        }
        if (result != null) {
            return a;
        }
        CreateDataProperty(cx, a, 0, s);
        return a;
    }
    if (!isShared) {
        /* step 19.a */
        RegExpSetLastIndex(cx, rx, 0);
    }
    /* step 18 */
    int q = p;
    /* step 19 */
    int lastStart = -1;
    MatcherState matcher = rx.getRegExpMatcher().matcher(s);
    boolean storeResult = true, invalidateResult = false;
    if (cx.getRuntimeContext().isEnabled(CompatibilityOption.LegacyRegExp)) {
        if (cx.getRealm() == rx.getRealm()) {
            if (!rx.isLegacyFeaturesEnabled()) {
                invalidateResult = true;
                storeResult = false;
            }
        } else {
            storeResult = false;
        }
    }
    while (q != size) {
        /* steps 19.a-c */
        boolean match = matcher.find(q);
        if (!match) {
            break;
        }
        MatcherResult result = matcher.toMatchResult();
        if (storeResult) {
            RegExpConstructor.storeLastMatchResult(cx, s, result);
        } else if (invalidateResult) {
            RegExpConstructor.invalidateLastMatchResult(cx);
            invalidateResult = false;
        }
        /* steps 19.d.i-ii */
        int e = result.end();
        /* steps 19.d.iii-iv */
        if (e == p) {
            /* step 19.d.iii */
            q = AdvanceStringIndex(s, q, unicodeMatching);
        } else {
            /* step 19.d.iv */
            String t = s.substring(p, lastStart = result.start());
            CreateDataProperty(cx, a, lengthA, t);
            lengthA += 1;
            if (lengthA == lim) {
                if (!isShared) {
                    RegExpSetLastIndex(cx, rx, e);
                }
                return a;
            }
            int groupCount = result.groupCount();
            for (int i = 1; i <= groupCount; ++i) {
                String cap = result.group(i);
                CreateDataProperty(cx, a, lengthA, cap != null ? cap : UNDEFINED);
                lengthA += 1;
                if (lengthA == lim) {
                    if (!isShared) {
                        RegExpSetLastIndex(cx, rx, e);
                    }
                    return a;
                }
            }
            p = e;
            q = p;
        }
    }
    if (lastStart == size) {
        return a;
    }
    /* step 20 */
    String t = s.substring(p, size);
    /* step 21 */
    CreateDataProperty(cx, a, lengthA, t);
    /* step 22 */
    return a;
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) MatcherResult(com.github.anba.es6draft.regexp.MatcherResult) MatcherState(com.github.anba.es6draft.regexp.MatcherState) MatchResult(java.util.regex.MatchResult)

Example 3 with MatcherResult

use of com.github.anba.es6draft.regexp.MatcherResult in project es6draft by anba.

the class RegExpPrototype method namedGroups.

private static NamedGroups namedGroups(ExecutionContext cx, String s, MatchResult matchResult) {
    if (matchResult instanceof ScriptObjectMatchResult) {
        ScriptObjectMatchResult scriptMatchResult = (ScriptObjectMatchResult) matchResult;
        Object groups = Get(cx, scriptMatchResult.object, "groups");
        if (Type.isUndefined(groups)) {
            return null;
        }
        return new ScriptNamedGroups(cx, ToObject(cx, groups));
    }
    assert matchResult instanceof MatcherResult;
    MatcherResult result = (MatcherResult) matchResult;
    if (result.groups().isEmpty()) {
        return null;
    }
    return new MatchNamedGroups(result);
}
Also used : MatcherResult(com.github.anba.es6draft.regexp.MatcherResult) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 4 with MatcherResult

use of com.github.anba.es6draft.regexp.MatcherResult in project es6draft by anba.

the class RegExpPrototype method RegExpMatch.

private static Object RegExpMatch(ExecutionContext cx, RegExpObject rx, String s, boolean global, boolean fullUnicode) {
    /* step 5 */
    if (!global) {
        MatcherResult result = matchResultOrNull(cx, rx, s, true);
        if (result == null) {
            return NULL;
        }
        return toMatchArray(cx, s, result);
    }
    /* step 6 */
    /* step 6.a (not applicable) */
    /* step 6.b */
    RegExpSetLastIndex(cx, rx, 0);
    int lastIndex = 0;
    /* step 6.c */
    ArrayObject array = null;
    MatcherResult lastResult = null;
    /* steps 6.d-e */
    MatcherState matcher = rx.getRegExpMatcher().matcher(s);
    boolean sticky = rx.isSet(RegExpObject.Flags.Sticky);
    for (int n = 0; ; ++n) {
        /* step 6.e.i */
        boolean matchSucceeded = matchOrFind(matcher, lastIndex, sticky);
        /* step 6.e.ii */
        if (!matchSucceeded) {
            break;
        }
        if (array == null) {
            array = ArrayCreate(cx, 0);
        }
        lastResult = matcher.toMatchResult();
        lastIndex = lastResult.end();
        /* step 6.e.iii */
        CreateDataProperty(cx, array, n, lastResult.group());
        if (lastResult.start() == lastIndex) {
            lastIndex = AdvanceStringIndex(s, lastIndex, fullUnicode);
        }
        if (lastIndex > s.length()) {
            break;
        }
    }
    if (lastResult == null) {
        return NULL;
    }
    boolean storeResult = true;
    if (cx.getRuntimeContext().isEnabled(CompatibilityOption.LegacyRegExp)) {
        if (cx.getRealm() == rx.getRealm()) {
            if (!rx.isLegacyFeaturesEnabled()) {
                RegExpConstructor.invalidateLastMatchResult(cx);
                storeResult = false;
            }
        } else {
            storeResult = false;
        }
    }
    if (storeResult) {
        RegExpConstructor.storeLastMatchResult(cx, s, lastResult);
    }
    return array;
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) MatcherResult(com.github.anba.es6draft.regexp.MatcherResult) MatcherState(com.github.anba.es6draft.regexp.MatcherState)

Example 5 with MatcherResult

use of com.github.anba.es6draft.regexp.MatcherResult in project es6draft by anba.

the class RegExpPrototype method matchResultOrNull.

/**
 * 21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S ) (1)
 *
 * @param cx
 *            the execution context
 * @param r
 *            the regular expression object
 * @param s
 *            the string
 * @param storeResult
 *            {@code true} if the match result is stored
 * @return the match result or {@code null}
 */
private static MatcherResult matchResultOrNull(ExecutionContext cx, RegExpObject r, String s, boolean storeResult) {
    /* step 1 */
    assert r.getRegExpMatcher() != null;
    /* steps 2-3 (not applicable) */
    /* step 4 */
    int lastIndex = RegExpGetLastIndex(cx, r);
    /* step 5 (not applicable) */
    /* step 6 */
    boolean global = r.isSet(RegExpObject.Flags.Global);
    /* step 7 */
    boolean sticky = r.isSet(RegExpObject.Flags.Sticky);
    /* step 8 */
    if (!global && !sticky) {
        lastIndex = 0;
    }
    /* steps 9-15 */
    MatcherResult m = matchResultOrNull(cx, r, s, lastIndex, sticky, storeResult);
    /* steps 12.a, 12.c */
    if (m == null) {
        if (global || sticky) {
            RegExpSetLastIndex(cx, r, 0);
        }
        return null;
    }
    /* steps 13-15 */
    if (global || sticky) {
        RegExpSetLastIndex(cx, r, m.end());
    }
    return m;
}
Also used : MatcherResult(com.github.anba.es6draft.regexp.MatcherResult)

Aggregations

MatcherResult (com.github.anba.es6draft.regexp.MatcherResult)5 MatcherState (com.github.anba.es6draft.regexp.MatcherState)3 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)3 RegExpMatcher (com.github.anba.es6draft.regexp.RegExpMatcher)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1 MatchResult (java.util.regex.MatchResult)1