Пример #1
0
        public override bool Parse(SyntacticState state)
        {
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            // null-coalescing-expression is required
            if (!NullCoalescingExpression.S.Parse(state))
                return false;

            // if captured expression ends with "?"
            LexicalEntry entry = state.GetInner(state.InnerPosition - 1);
            if (state.GetOuter(entry) == "?")
            {
                // check whether "? and :" part goes after
                bool full = ParseAll(
                    state,
                    QuestionTerminal.S,
                    Expression.S,
                    ColonTerminal.S,
                    Expression.S);

                // if so, everything is OK
                if (full)
                {
                    state.AddBack(Key, innerIndex, outerIndex);
                    return true;
                }

                // if not, make another attempt to parse conditional expression
                // handling null-coalescing-expression without trailing "?"
                state.Reset(innerIndex, outerIndex);

                // if we could do that, we can return
                if (ParseAll(
                        state,
                        NullCoalescingExpressionShorten.S,
                        QuestionTerminal.S,
                        Expression.S,
                        ColonTerminal.S,
                        Expression.S))
                {
                    return true;
                }

                // if not, parse initial null-coalescing-expression once again
                NullCoalescingExpression.S.Parse(state);
            }

            ParseAll(
                state,
                QuestionTerminal.S,
                Expression.S,
                ColonTerminal.S,
                Expression.S);

            state.AddBack(Key, innerIndex, outerIndex);
            return true;
        }
Пример #2
0
        public override bool Parse(SyntacticState state)
        {
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            // non-assignment-expression is required
            if (!NonAssignmentExpression.S.Parse(state))
            {
                return(false);
            }

            // if unary-expression has been captured
            // we should try to capture rest parts of assignment
            if (state.CheckEntry(UnaryExpression.S.Key, innerIndex))
            {
                int inner = state.InnerPosition;
                int outer = state.OuterPosition;

                // the rest parts of assigment are assignment-operator
                if (AssignmentOperator.S.Parse(state))
                {
                    // and another expression (recursive call here)
                    if (S.Parse(state))
                    {
                    }
                    else
                    {
                        state.Reset(inner, outer);
                    }
                }
            }

            state.AddBack(Key, innerIndex, outerIndex);
            return(true);
        }
Пример #3
0
        public override bool Parse(SyntacticState state)
        {
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            // non-assignment-expression is required
            if (!NonAssignmentExpression.S.Parse(state))
                return false;

            // if unary-expression has been captured
            // we should try to capture rest parts of assignment
            if (state.CheckEntry(UnaryExpression.S.Key, innerIndex))
            {
                int inner = state.InnerPosition;
                int outer = state.OuterPosition;

                // the rest parts of assigment are assignment-operator
                if (AssignmentOperator.S.Parse(state))
                {
                    // and another expression (recursive call here)
                    if (S.Parse(state))
                    {
                    }
                    else
                    {
                        state.Reset(inner, outer);
                    }
                }
            }

            state.AddBack(Key, innerIndex, outerIndex);
            return true;
        }
Пример #4
0
        public override bool Parse(SyntacticState state)
        {
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            // null-coalescing-expression is required
            if (!NullCoalescingExpression.S.Parse(state))
            {
                return(false);
            }

            // if captured expression ends with "?"
            LexicalEntry entry = state.GetInner(state.InnerPosition - 1);

            if (state.GetOuter(entry) == "?")
            {
                // check whether "? and :" part goes after
                bool full = ParseAll(
                    state,
                    QuestionTerminal.S,
                    Expression.S,
                    ColonTerminal.S,
                    Expression.S);

                // if so, everything is OK
                if (full)
                {
                    state.AddBack(Key, innerIndex, outerIndex);
                    return(true);
                }

                // if not, make another attempt to parse conditional expression
                // handling null-coalescing-expression without trailing "?"
                state.Reset(innerIndex, outerIndex);

                // if we could do that, we can return
                if (ParseAll(
                        state,
                        NullCoalescingExpressionShorten.S,
                        QuestionTerminal.S,
                        Expression.S,
                        ColonTerminal.S,
                        Expression.S))
                {
                    return(true);
                }

                // if not, parse initial null-coalescing-expression once again
                NullCoalescingExpression.S.Parse(state);
            }

            ParseAll(
                state,
                QuestionTerminal.S,
                Expression.S,
                ColonTerminal.S,
                Expression.S);

            state.AddBack(Key, innerIndex, outerIndex);
            return(true);
        }
Пример #5
0
        /// <summary>
        /// Tries to parse a batch of similar syntactic items with specified delimiter.
        /// </summary>
        private bool ParseManyInternal(SyntacticState state, SyntacticItem part, SyntacticItem delimiter)
        {
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            if (!part.Parse(state))
                return false;

            while (true)
            {
                int lastInnerIndex = state.InnerPosition;
                int lastOuterIndex = state.OuterPosition;

                if (delimiter != null)
                {
                    if (!delimiter.Parse(state))
                        break;
                }

                if (!part.Parse(state))
                {
                    state.Reset(lastInnerIndex, lastOuterIndex);
                    break;
                }
            }

            state.AddBack(Key, innerIndex, outerIndex);
            return true;
        }
Пример #6
0
        /// <summary>
        /// Tries to parse any of specified syntactic items.
        /// </summary>
        private bool ParseAnyInternal(SyntacticState state, params SyntacticItem[] parts)
        {
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            foreach (SyntacticItem part in parts)
            {
                if (part.Parse(state))
                {
                    state.AddBack(Key, innerIndex, outerIndex);
                    return true;
                }
            }

            return false;
        }
Пример #7
0
        /// <summary>
        /// Tries to parse a consequent number of specified syntactic items.
        /// </summary>
        private bool ParseAllInternal(SyntacticState state, params SyntacticItem[] parts)
        {
            int innerIndex = state.InnerPosition;
            int outerIndex = state.OuterPosition;

            bool parsed = true;
            foreach (SyntacticItem part in parts)
            {
                if (!part.Parse(state))
                {
                    parsed = false;
                    break;
                }
            }

            if (!parsed)
            {
                state.Reset(innerIndex, outerIndex);
                return false;
            }

            state.AddBack(Key, innerIndex, outerIndex);
            return true;
        }