Exemplo n.º 1
0
        protected static IMatched <Unit> getMatchFunction(ParseState state, string functionName, Parameters parameters, bool overriding,
                                                          string className)
        {
            var list = new List <If>();

            if (state.Advance().ValueOrOriginal(out _, out var original))
            {
                state.CreateReturnType();
                while (state.More)
                {
                    var caseParser = new CaseParser(parameters[0].Name);
                    state.SkipEndOfLine();
                    if (caseParser.Scan(state).If(out _, out var anyException))
                    {
                        caseParser.If.AddReturnIf();
                        list.Add(caseParser.If);
                    }
                    else if (anyException.If(out var exception))
                    {
                        state.Regress();
                        return(failedMatch <Unit>(exception));
                    }
                    else
                    {
                        break;
                    }
                }

                if (list.Count == 0)
                {
                    state.Regress();
                    state.RemoveReturnType();
                    return(notMatched <Unit>());
                }
                else
                {
                    var stack = new Stack <If>();
                    foreach (var ifStatement in list)
                    {
                        stack.Push(ifStatement);
                    }

                    var previousIf = stack.Pop();
                    while (stack.Count > 0)
                    {
                        var current = stack.Pop();
                        current.ElseIf = previousIf.Some();
                        previousIf     = current;
                    }

                    state.AddStatement(new MatchFunction(functionName, parameters, previousIf, overriding, className));
                    state.Regress();
                    state.RemoveReturnType();

                    return(Unit.Matched());
                }
            }
Exemplo n.º 2
0
        public override IMatched <Unit> ParseStatement(ParseState state, Token[] tokens)
        {
            var mutable    = tokens[1].Text == "var";
            var fieldName  = tokens[3].Text;
            var assignment = fieldName.IsNotEmpty();

            state.Colorize(tokens, Color.Keyword, Color.Whitespace, Color.Identifier, Color.Whitespace, Color.Structure, Color.Whitespace,
                           Color.Keyword, Color.Whitespace);

            if (getExpression(state, ExpressionFlags.Standard).ValueOrCast <Unit>(out var expression, out var asUnit))
            {
                var matchField = newLabel("match");
                state.AddStatement(new AssignToNewField(false, matchField, expression));

                state.Advance();
                var caseParser = new CaseParser(fieldName, mutable, assignment, matchField, true, CaseType.Statement);
                if (caseParser.Scan(state).ValueOrOriginal(out _, out asUnit))
                {
                    var ifStatement = caseParser.If;
                    addMatchElse(ifStatement);
                    state.AddStatement(ifStatement);
                    state.Regress();

                    return(Unit.Matched());
                }
                else
                {
                    state.Regress();
                    return(asUnit);
                }
            }
            else
            {
                return(asUnit);
            }
        }