示例#1
0
        protected internal override object GetInstanceIdValue(int id)
        {
            int shifted = id - base.MaxInstanceId;

            if (1 <= shifted && shifted <= MAX_INSTANCE_ID)
            {
                RegExpImpl impl = Impl;
                object     stringResult;
                switch (shifted)
                {
                case Id_multiline:
                case Id_STAR:
                    return(impl.multiline);


                case Id_input:
                case Id_UNDERSCORE:
                    stringResult = impl.input;
                    break;


                case Id_lastMatch:
                case Id_AMPERSAND:
                    stringResult = impl.lastMatch;
                    break;


                case Id_lastParen:
                case Id_PLUS:
                    stringResult = impl.lastParen;
                    break;


                case Id_leftContext:
                case Id_BACK_QUOTE:
                    stringResult = impl.leftContext;
                    break;


                case Id_rightContext:
                case Id_QUOTE:
                    stringResult = impl.rightContext;
                    break;


                default: {
                    // Must be one of $1..$9, convert to 0..8
                    int substring_number = shifted - DOLLAR_ID_BASE - 1;
                    stringResult = impl.getParenSubString(substring_number);
                    break;
                }
                }
                return((stringResult == null) ? "" : stringResult.ToString());
            }
            return(base.GetInstanceIdValue(id));
        }
示例#2
0
        private static SubString interpretDollar (Context cx, RegExpImpl res, string da, int dp, int [] skip)
        {
            char dc;
            int num, tmp;

            if (da [dp] != '$')
                Context.CodeBug ();

            /* Allow a real backslash (literal "\\") to escape "$1" etc. */
            Context.Versions version = cx.Version;
            if (version != Context.Versions.Default && version <= Context.Versions.JS1_4) {
                if (dp > 0 && da [dp - 1] == '\\')
                    return null;
            }
            int daL = da.Length;
            if (dp + 1 >= daL)
                return null;
            /* Interpret all Perl match-induced dollar variables. */
            dc = da [dp + 1];
            if (BuiltinRegExp.isDigit (dc)) {
                int cp;
                if (version != Context.Versions.Default && version <= Context.Versions.JS1_4) {
                    if (dc == '0')
                        return null;
                    /* Check for overflow to avoid gobbling arbitrary decimal digits. */
                    num = 0;
                    cp = dp;
                    while (++cp < daL && BuiltinRegExp.isDigit (dc = da [cp])) {
                        tmp = 10 * num + (dc - '0');
                        if (tmp < num)
                            break;
                        num = tmp;
                    }
                }
                else {
                    /* ECMA 3, 1-9 or 01-99 */
                    int parenCount = (res.parens == null) ? 0 : res.parens.Length;
                    num = dc - '0';
                    if (num > parenCount)
                        return null;
                    cp = dp + 2;
                    if ((dp + 2) < daL) {
                        dc = da [dp + 2];
                        if (BuiltinRegExp.isDigit (dc)) {
                            tmp = 10 * num + (dc - '0');
                            if (tmp <= parenCount) {
                                cp++;
                                num = tmp;
                            }
                        }
                    }
                    if (num == 0)
                        return null; /* $0 or $00 is not valid */
                }
                /* Adjust num from 1 $n-origin to 0 array-index-origin. */
                num--;
                skip [0] = cp - dp;
                return res.getParenSubString (num);
            }

            skip [0] = 2;
            switch (dc) {

                case '$':
                    return new SubString ("$");

                case '&':
                    return res.lastMatch;

                case '+':
                    return res.lastParen;

                case '`':
                    if (version == Context.Versions.JS1_2) {
                        /*
                        * JS1.2 imitated the Perl4 bug where left context at each step
                        * in an iterative use of a global regexp started from last match,
                        * not from the start of the target string.  But Perl4 does start
                        * $` at the beginning of the target string when it is used in a
                        * substitution, so we emulate that special case here.
                        */
                        res.leftContext.index = 0;
                        res.leftContext.length = res.lastMatch.index;
                    }
                    return res.leftContext;

                case '\'':
                    return res.rightContext;
            }
            return null;
        }
示例#3
0
        private static SubString interpretDollar(Context cx, RegExpImpl res, string da, int dp, int [] skip)
        {
            char dc;
            int  num, tmp;

            if (da [dp] != '$')
            {
                Context.CodeBug();
            }

            /* Allow a real backslash (literal "\\") to escape "$1" etc. */
            Context.Versions version = cx.Version;
            if (version != Context.Versions.Default && version <= Context.Versions.JS1_4)
            {
                if (dp > 0 && da [dp - 1] == '\\')
                {
                    return(null);
                }
            }
            int daL = da.Length;

            if (dp + 1 >= daL)
            {
                return(null);
            }
            /* Interpret all Perl match-induced dollar variables. */
            dc = da [dp + 1];
            if (BuiltinRegExp.isDigit(dc))
            {
                int cp;
                if (version != Context.Versions.Default && version <= Context.Versions.JS1_4)
                {
                    if (dc == '0')
                    {
                        return(null);
                    }
                    /* Check for overflow to avoid gobbling arbitrary decimal digits. */
                    num = 0;
                    cp  = dp;
                    while (++cp < daL && BuiltinRegExp.isDigit(dc = da [cp]))
                    {
                        tmp = 10 * num + (dc - '0');
                        if (tmp < num)
                        {
                            break;
                        }
                        num = tmp;
                    }
                }
                else
                {
                    /* ECMA 3, 1-9 or 01-99 */
                    int parenCount = (res.parens == null) ? 0 : res.parens.Length;
                    num = dc - '0';
                    if (num > parenCount)
                    {
                        return(null);
                    }
                    cp = dp + 2;
                    if ((dp + 2) < daL)
                    {
                        dc = da [dp + 2];
                        if (BuiltinRegExp.isDigit(dc))
                        {
                            tmp = 10 * num + (dc - '0');
                            if (tmp <= parenCount)
                            {
                                cp++;
                                num = tmp;
                            }
                        }
                    }
                    if (num == 0)
                    {
                        return(null); /* $0 or $00 is not valid */
                    }
                }
                /* Adjust num from 1 $n-origin to 0 array-index-origin. */
                num--;
                skip [0] = cp - dp;
                return(res.getParenSubString(num));
            }

            skip [0] = 2;
            switch (dc)
            {
            case '$':
                return(new SubString("$"));

            case '&':
                return(res.lastMatch);

            case '+':
                return(res.lastParen);

            case '`':
                if (version == Context.Versions.JS1_2)
                {
                    /*
                     * JS1.2 imitated the Perl4 bug where left context at each step
                     * in an iterative use of a global regexp started from last match,
                     * not from the start of the target string.  But Perl4 does start
                     * $` at the beginning of the target string when it is used in a
                     * substitution, so we emulate that special case here.
                     */
                    res.leftContext.index  = 0;
                    res.leftContext.length = res.lastMatch.index;
                }
                return(res.leftContext);

            case '\'':
                return(res.rightContext);
            }
            return(null);
        }