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

Proc -> NoReturn with controlled recursion #2284

Closed
umurgdk opened this issue Mar 7, 2016 · 8 comments
Closed

Proc -> NoReturn with controlled recursion #2284

umurgdk opened this issue Mar 7, 2016 · 8 comments

Comments

@umurgdk
Copy link

umurgdk commented Mar 7, 2016

Hi im working on a parser combinator library and i have small issues with proc return types. To be able to handle recursion for parsers i have to make it lazy, to be able to do it I have a ParserClosure class which accepts a proc to be ran when it is needed. You can see see the codes below.

In case of you want to see all the codes please check https://github.com/umurgdk/haydari

Error in ./samples/parser_new.cr:677: no overload matches 'ParserClosure(T)#initialize' with type ( -> NoReturn)
Overloads are:
 - ParserClosure(T)#initialize(generator : Proc(Parser(T)))
class ParserClosure(T) < Parser(T)
    def initialize(generator : Proc(Parser(T)))
        @generator = generator
        @output = [] of T
    end

    def reset
        @output = [] of T
    end

    def output
        @output[0]
    end

    def parse(input)
        if generator = @generator
            parser = generator.call()

            if parser.parse(input)
                @output << parser.output.not_nil!
                true
            end

            false
        end

        false
    end
end
class BrainfuckParser
    include Haydari

    def parse_command
        parser = one_of("><+-.,")
        return parser.select { |t| BrainfuckTokens.from_string(t) }
    end

    def parse_program : Parser(BrainfuckProgram)
        start_t = string("[")

        commands_1   = parse_command.many
        sub_programs = ParserClosure.new ->self.parse_program
        commands_2   = parse_command.many

        program      = commands_1 + sub_programs + commands_2
        program      = program.select { |p| BrainfuckProgram.new p.concat }

        end_t = string("]")

        (start_t > program < end_t)
    end

    def parse(input : String)
        parser = parse_program
        if parser.run(input)
            parser.output
        end
    end
end

This code doesn't work also for another issue about parse_program's return type. Please see #2283

@umurgdk
Copy link
Author

umurgdk commented Mar 7, 2016

Or any better idea how to handle this recursion/lazyness? 💃

@asterite
Copy link
Member

asterite commented Mar 7, 2016

Hi,

Just like in #2283, if you don't provide code that we can copy and paste to immediately reproduce the problem you are having, so we can debug and fix the bug, there's not much we can do.

@asterite
Copy link
Member

asterite commented Mar 7, 2016

Oh, sorry, I see you provided a link to the github repo. That's good. In any case, it's better if you try to reduce the code to the minimum that exhibits the problem.

@asterite
Copy link
Member

asterite commented Mar 7, 2016

Hmmm... Just ran crystal spec on your project and everything's green. I don't know what else to do.

@umurgdk
Copy link
Author

umurgdk commented Mar 7, 2016

@asterite I just created another sample which uses recursive parser. could you please try to run it.

https://github.com/umurgdk/haydari/blob/master/samples/brainfuck_recursive.cr#L85

recurse macro defined in here:
https://github.com/umurgdk/haydari/blob/master/src/haydari.cr#L77

@jhass
Copy link
Member

jhass commented May 27, 2016

Ping.

@asterite
Copy link
Member

Please also try to provide reduced code that reproduces this. The problem with links to repos is that they eventually change and then the issue becomes harder to reproduce.

@asterite
Copy link
Member

Two weeks afterwards, I'm closing this. Please reopen if you have reproducible code for this. Thanks!

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

No branches or pull requests

3 participants