Beispiel #1
0
        public override IMatched <Unit> ParseStatement(ParseState state, Token[] tokens)
        {
            state.BeginTransaction();

            state.Colorize(tokens, Color.Keyword);

            var result =
                from skipped in state.SkipEndOfLine()
                from b in getBlock(state)
                from e in getUntil(state)
                select(b, e);

            if (result.ValueOrCast <Unit>(out var tuple, out var asUnit))
            {
                var(block, expression) = tuple;
                state.AddStatement(new Loop(block, expression));
                state.CommitTransaction();

                return(Unit.Matched());
            }
            else
            {
                state.RollBackTransaction();
                return(asUnit);
            }
        }
Beispiel #2
0
        public override IMatched <Unit> ParseStatement(ParseState state, Token[] tokens)
        {
            state.BeginTransaction();
            state.Colorize(tokens, Color.Keyword);

            var result =
                from expression in getExpression(state, ExpressionFlags.OmitIf)
                from @if in state.Scan("^ /(|s|) /'if' /b", Color.Whitespace, Color.Keyword)
                from condition in getExpression(state, ExpressionFlags.Standard)
                select(expression, condition);

            if (result.ValueOrCast <Unit>(out var tuple, out var asUnit))
            {
                var(expression, condition) = tuple;
                Condition  = condition;
                Expression = expression;

                state.CommitTransaction();

                return(Unit.Matched());
            }
            else
            {
                state.RollBackTransaction();
                return(asUnit);
            }
        }
        public override IMatched <Unit> Prefix(ParseState state, Token[] tokens)
        {
            state.BeginTransaction();

            mutable   = tokens[1].Text == "var";
            fieldName = tokens[3].Text;
            state.Colorize(tokens, Color.Keyword, Color.Whitespace, Color.Identifier);

            if (parseTypeConstraint(state).If(out _typeConstraint, out var _exception))
            {
            }
            else if (_exception.If(out var exception))
            {
                return(failedMatch <Unit>(exception));
            }
            else
            {
                _typeConstraint = none <TypeConstraint>();
            }

            if (state.Scan("^ /(|s|) /'='", Color.Whitespace, Color.Structure).If(out _, out _exception))
            {
                state.CommitTransaction();
                return(Unit.Matched());
            }
            else if (_exception.If(out var exception))
            {
                return(failedMatch <Unit>(exception));
            }
            else
            {
                state.RollBackTransaction();
                return(notMatched <Unit>());
            }
        }
Beispiel #4
0
 public override IMatched <Unit> Scan(ParseState state)
 {
     state.BeginTransaction();
     if (IgnoreIndentation || SingleLine || state.Scan($"^ /({state.Indentation.FriendlyString()})", Color.Whitespace).IsMatched)
     {
         if (base.Scan(state).If(out _, out var anyException))
         {
             state.CommitTransaction();
             return(Unit.Matched());
         }
         else if (anyException.If(out var exception))
         {
             ExceptionIndex = state.Index.Some();
             state.RollBackTransaction();
             return(failedMatch <Unit>(exception));
         }
         else
         {
             state.RollBackTransaction();
             return(notMatched <Unit>());
         }
     }
     else
     {
         state.RollBackTransaction();
         return(notMatched <Unit>());
     }
 }
        public override IMatched <Unit> ParseStatement(ParseState state, Token[] tokens)
        {
            state.BeginTransaction();

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

            var result =
                from comparisand in getExpression(state, ExpressionFlags.Comparisand)
                from scanned in state.Scan("^ /(|s|) /':='", Color.Whitespace, Color.Structure)
                from expression in getExpression(state, ExpressionFlags.Standard)
                from block in getBlock(state)
                select(comparisand, expression, block);

            if (result.ValueOrCast <Unit>(out var tuple, out var asUnit))
            {
                state.CommitTransaction();
                var(comparisand, expression, block) = tuple;
                state.AddStatement(new ConditionalWhile(comparisand, expression, block));

                return(Unit.Matched());
            }
            else
            {
                state.RollBackTransaction();
                return(asUnit);
            }
        }
Beispiel #6
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.BeginTransaction();
            state.Colorize(tokens, Color.Whitespace, Color.Operator);

            var innerBuilder = new ExpressionBuilder(ExpressionFlags.Standard);

            innerBuilder.Add(new FieldSymbol("__$0"));
            var operatorsParser = new OperatorsParser(innerBuilder);

            var result =
                from op in operatorsParser.Scan(state)
                from closing in state.Scan("^ /']'", Color.Operator)
                select op;

            if (result.ValueOrOriginal(out _, out var original))
            {
                innerBuilder.Add(new FieldSymbol("__$1"));
                if (innerBuilder.ToExpression().If(out var expression, out var exception))
                {
                    var lambda = new LambdaSymbol(2, expression);
                    builder.Add(new SendBinaryMessageSymbol("foldr", Precedence.ChainedOperator));
                    builder.Add(lambda);
                    state.CommitTransaction();

                    return(Unit.Matched());
                }
                else
                {
                    state.RollBackTransaction();
                    return(failedMatch <Unit>(exception));
                }
            }
Beispiel #7
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.Colorize(tokens, Color.Whitespace, Color.Operator);
            state.BeginTransaction();

            var expressionBuilder = new ExpressionBuilder(builder.Flags);
            var parser            = new AnyLambdaParser(expressionBuilder);
            var result            =
                from parsed in parser.Scan(state)
                from endToken in state.Scan("/']'", Color.Operator)
                select parsed;

            if (result.ValueOrOriginal(out _, out var original))
            {
                if (expressionBuilder.ToExpression().If(out var expression, out var exception))
                {
                    builder.Add(new ZipLambdaSymbol(expression));
                    state.CommitTransaction();

                    return(Unit.Matched());
                }
                else
                {
                    state.RollBackTransaction();
                    return(failedMatch <Unit>(exception));
                }
            }
Beispiel #8
0
        public override IMatched <Unit> ParseStatement(ParseState state, Token[] tokens)
        {
            state.BeginTransaction();

            var isNew     = tokens[1].Text.IsNotEmpty();
            var mutable   = tokens[1].Text == "var";
            var fieldName = tokens[3].Text;

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

            var result =
                from typeConstraint in parseTypeConstraint(state)
                from scanned in state.Scan($"^ /(|s|) /'=' /(|s|) /'loop' /({REGEX_EOL})", Color.Whitespace, Color.Structure,
                                           Color.Whitespace, Color.Keyword, Color.Whitespace)
                from block in getBlock(state)
                from pair in getReturn(state)
                select(typeConstraint, block, pair);

            if (result.If(out var tuple, out _))
            {
                var(typeConstraint, block, (condition, expression)) = tuple;
                state.AddStatement(new AssignFromLoop(isNew, mutable, fieldName, typeConstraint, block, condition, expression));
                state.CommitTransaction();

                return(Unit.Matched());
            }
            else
            {
                state.RollBackTransaction();
                return(notMatched <Unit>());
            }
        }
Beispiel #9
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.BeginTransaction();
            state.CreateReturnType();

            var result =
                from parameters in ParseParameters(state, tokens)
                from scanned in state.Scan("^ /(|s|) /'->'", Color.Whitespace, Color.Structure)
                from typeConstraint in parseTypeConstraint(state)
                from block in getLambdaBlock(!state.CurrentSource.IsMatch("^ (/r /n | /r | /n)"), state,
                                             builder.Flags & ~ExpressionFlags.Comparisand | ExpressionFlags.InLambda, typeConstraint)
                select new LambdaSymbol(parameters, block);

            if (result.ValueOrCast <Unit>(out var lambdaSymbol, out var asUnit))
            {
                builder.Add(lambdaSymbol);
                state.RemoveReturnType();
                state.CommitTransaction();

                return(Unit.Matched());
            }
            else
            {
                state.RollBackTransaction();
                state.RemoveReturnType();

                return(asUnit);
            }
        }
        public override IMatched <Unit> ParseStatement(ParseState state, Token[] tokens)
        {
            state.BeginTransaction();

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

            var result =
                from comparisand in getExpression(state, ExpressionFlags.Comparisand)
                from scanned in state.Scan("^ /(|s|) /':='", Color.Whitespace, Color.Structure)
                from expression in getExpression(state, ExpressionFlags.Standard)
                from and in getAnd(state)
                from block in getBlock(state)
                select(comparisand, expression, and, block);

            if (result.ValueOrCast <Unit>(out var tuple, out var asUnit))
            {
                var(comparisand, expression, and, block) = tuple;
                var elseBlock  = none <Block>();
                var elseParser = new ElseParser();
                if (elseParser.Scan(state).If(out _, out var anyException))
                {
                    elseBlock = elseParser.Block;
                }
                else if (anyException.If(out var exception))
                {
                    state.RollBackTransaction();
                    return(failedMatch <Unit>(exception));
                }

                if (and.If(out var a))
                {
                    var builder = new ExpressionBuilder(ExpressionFlags.Comparisand);
                    builder.Add(a);
                    if (builder.ToExpression().IfNot(out expression, out var exception))
                    {
                        state.RollBackTransaction();
                        return(failedMatch <Unit>(exception));
                    }
                }

                state.CommitTransaction();
                state.AddStatement(new ConditionalAssign(comparisand, expression, block, elseBlock));

                return(Unit.Matched());
            }
            else
            {
                state.RollBackTransaction();
                return(asUnit);
            }
        }
Beispiel #11
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.BeginTransaction();
            var monoTuple = tokens[3].Text == ",";

            state.Colorize(tokens, Color.Whitespace, Color.OpenParenthesis, Color.Structure);

            if (getExpression(state, "^ /')'", builder.Flags & ~ExpressionFlags.OmitComma, Color.CloseParenthesis)
                .If(out var expression, out var anyException))
            {
                builder.Add(new SubexpressionSymbol(expression, monoTuple));
                state.CommitTransaction();

                return(Unit.Matched());
            }
            else if (anyException.IsSome)
            {
                state.RollBackTransaction();
                state.BeginTransaction();
                if (getPartialLambda(state).ValueOrCast <Unit>(out var lambdaSymbol, out var asUnit))
                {
                    state.CommitTransaction();
                    builder.Add(lambdaSymbol);

                    return(Unit.Matched());
                }
                else
                {
                    state.RollBackTransaction();
                    return(asUnit);
                }
            }
            else
            {
                return(notMatched <Unit>());
            }
        }
Beispiel #12
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.BeginTransaction();
            state.Colorize(tokens, Color.Whitespace, Color.OpenParenthesis);
            if (getPartialLambda(state).ValueOrCast <Unit>(out var lambda, out var asUnit))
            {
                builder.Add(lambda);
                state.CommitTransaction();

                return(Unit.Matched());
            }
            else
            {
                state.RollBackTransaction();
                return(asUnit);
            }
        }
Beispiel #13
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.BeginTransaction();

            var whitespace = tokens[1].Text.IsNotEmpty();
            var source     = tokens[2].Text;

            state.Colorize(tokens, Color.Whitespace, Color.Operator, Color.Whitespace);

            if (getOperator(state, source, builder.Flags, whitespace).ValueOrCast <Unit>(out var symbol, out var asUnit))
            {
                builder.Add(symbol);
                state.CommitTransaction();

                return(Unit.Matched());
            }
            else
            {
                state.RollBackTransaction();
                return(asUnit);
            }
        }
Beispiel #14
0
        public override IMatched <Unit> ParseStatement(ParseState state, Token[] tokens)
        {
            state.BeginTransaction();
            var result =
                from comparisand in getExpression(state, ExpressionFlags.Comparisand | ExpressionFlags.OmitColon)
                from stem in state.Scan("^ /(|s|) /':='", Color.Whitespace, Color.Structure)
                from expression in getExpression(state, ExpressionFlags.Standard)
                select(comparisand, expression);

            if (result.If(out var tuple, out var anyException))
            {
                state.CommitTransaction();
                var(comparisand, expression) = tuple;
                state.AddStatement(new MatchAssign(comparisand, expression));

                return(Unit.Matched());
            }
            else if (anyException.If(out var exception) && exception.Message != "Invalid expression syntax")
            {
                state.RollBackTransaction();
                return(failedMatch <Unit>(exception));
            }
Beispiel #15
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.BeginTransaction();
            state.Colorize(tokens, Color.Structure);

            var result =
                from indexes in getExpression(state, "^ /(/s*) /'}' /(/s*) /'='", builder.Flags, Color.Whitespace, Color.Structure,
                                              Color.Whitespace, Color.Structure)
                from values in getExpression(state, builder.Flags)
                select new SliceAssignSymbol(indexes, values);

            if (result.ValueOrCast <Unit>(out var symbol, out var asUnit))
            {
                builder.Add(symbol);
                state.CommitTransaction();

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