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

Allow multi-block expressions in seval #452

Merged
merged 3 commits into from
Feb 13, 2024
Merged

Conversation

MilesCranmer
Copy link
Contributor

@MilesCranmer MilesCranmer commented Feb 8, 2024

Simpler than I thought!

Fixes #433

@LilithHafner @cjdoris

The only difference between these is that Meta.parse checks for multiple blocks and throws an error:

    ex, pos = parse(str, 1; filename, greedy=true, raise, depwarn)
    if isexpr(ex, :error)
        return ex
    end
    if pos <= ncodeunits(str)
        raise && throw(ParseError("extra token after end of expression"))
        return Expr(:error, "extra token after end of expression")
    end
    return ex

whereas Meta.parseall ignores the number of blocks:

    ex,_ = _parse_string(text, String(filename), lineno, 1, :all)
    return ex

Note that otherwise the behavior is identical.

@MilesCranmer MilesCranmer changed the title Allow multi-line strings in seval Allow multi-block expressions in seval Feb 8, 2024
@LilithHafner
Copy link
Contributor

It's worth noting that Meta.parseall is undocumented, but this still looks like a good fix to me.

I suppose it would be possible to do this with public API by repeatedly parsing with the two-arg parse method and then manually joining them into a single expr, but I like this approach better.

@MilesCranmer
Copy link
Contributor Author

MilesCranmer commented Feb 8, 2024

Is it public since it is parseall rather than _parseall? (I don't know; just wondering). There are even some exported functions from Julia that don't have docs IIRC.

Just for posterity: I just tried to do a 2-arg Meta.parse version and ran into some sharp edges of the interface. For this reason I think parseall is safer even if not documented given it's unit-tested in Base and calls the same underlying _parse_str but with a different option.

@cjdoris cjdoris merged commit b84c3cb into JuliaPy:main Feb 13, 2024
13 checks passed
@cjdoris
Copy link
Collaborator

cjdoris commented Feb 13, 2024

Great thanks! Given Meta.parseall is used directly for code loading it seems unlikely to go away. Would be good to get it documented in Julia though, since there's no other documented reliable way to parse top-level expressions.

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.

Main.seval can't parse multiple toplevel blocks
3 participants