Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new overloads to LeftAssociative, RightAssociative and Unary #128

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gumbarros
Copy link
Contributor

@gumbarros gumbarros commented Jun 8, 2024

This closes #127

@gumbarros gumbarros changed the title Added new overloads to LeftAssociative and RightAssociative Added new overloads to LeftAssociative, RightAssociative and Unary Jun 8, 2024
@gumbarros
Copy link
Contributor Author

gumbarros commented Jun 8, 2024

Unary W.I.P, I'm still stuck:

        /// <summary>
        /// Builds a parser that creates a unary operation.
        /// </summary>
        /// <typeparam name="T">The type of the returned parser.</typeparam>
        /// <typeparam name="TInput">The type of the symbol parsers.</typeparam>
        /// <param name="parser">The higher-priority parser the symbols are separating.</param>
        /// <param name="ops">The array of operators that can be parsed.</param>
        /// <param name="factory">The factory method to create the result of the operation.</param>
        /// <returns>A parser that creates a unary operation.</returns>
        public static Parser<T> Unary<T, TInput>(this Parser<T> parser, Parser<TInput>[] ops, Func<TInput, T, T> factory)
        {
            var choices = ops.Select(op => new Then<TInput, Func<T, T>>(op, operation => a => factory(operation, a)));

            return Recursive<T>(u =>
            {
                var unaryChoices = choices.Select(choice => choice.Then(u));
                return new OneOf<T>(unaryChoices.ToArray()).Or(parser);
            });
        }
        [Theory]
        [InlineData("2", 2)]
        [InlineData("-2", -2)]
        [InlineData("--2", 2)]
        public void ShouldParsePrefixWithAlternativeOverload(string expression, double result)
        {
            var primary = Terms.Double();

            var negate = Terms.Char('-').Then(_ => (Func<double, double>)(a => 0 - a));

            var unary = primary.Unary([negate], (operation, a) => operation(a));

            Assert.Equal(result, unary.Parse(expression));
        }

Argument type 'Parlot.Fluent.Parser<Parlot.Fluent.Deferred>[]' is not assignable to parameter type 'Parlot.Fluent.Parser[]'

@sebastienros do you know how can I use T instead of Deferred<T> in this case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create LeftAssociative overload with more friendly factory
1 participant