@evaluate macro#24
Conversation
|
Just fyi -- the tests actually seem to work, the CI failure is related to something different (will fix). It's going to need some changes (e.g. the huge module-finding hack is unnecessary because you already hold the module :) ) but should be doable. Same case for the other PR I guess. |
|
Sounds good! Very open to feedback, and glad that the test suite runs! Regarding testing - I've written an ODL parser via these macros over the last two days (the grammar + evaluator is ~120 LOC); it's part of a larger codebase which isn't ready for release, but I would be happy to share it with you, as it's a reasonable stress-test for these macros. Could you shoot me an email at henry.eshbaugh@physics.ox.ac.uk so I can link you the repo? (It's on a secret self-hosted server). So - I'll admit that this code is somewhat hacky, but I'm not sure what the best option is, and the decision is up to you. I'll try to summarise it here and what the options are for it. TL;DR about the hackWhat we do depends on whether within the macro we'd like to write clauses as e.g. Those problems relate to the gap between the evaluation context of the code in the macro, and the code produced by the macro; we need to know about the caller's context to carry out this functionality. It's precisely the second context that we need to get information about to make this functionality work. As I see it, we have five options:
I went with the first option as it's the first one that occurred to me and got the tests to pass; however,
so the second option is probably better - fewer lines of code, less hacky, and also works if you import via Specifics around the problemThe issue is that the Check out this code at the REPL: eshbaugh_pu @ atmlxdaq03 ~/src/MoonTools meshing
❯ julia -q
Activating project at `~/src/MoonTools`
julia> import PikaParser as P
julia> names(@__MODULE__; imported=true)
7-element Vector{Symbol}:
:Base
:Core
:Main
:P
:Pkg
:Revise
:StyledStrings
julia> PikaParser
ERROR: UndefVarError: `PikaParser` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Hint: PikaParser is loaded but not imported in the active module Main.
Stacktrace:
[1] top-level scope
@ REPL:1
julia> nameof(P)
:PikaParserBecause we have If we hardcode julia> import PikaParser
julia> test1 = PikaParser.@grammar :top begin
:ws => first(satisfy(isspace), epsilon),
:ident => r"[a-z_]+[a-z0-9_]*"i,
:decl => seq(:ident, :ws),
:top => some(:decl)
end
#3 (generic function with 1 method)
julia> test2 = PikaParser.@evaluate :top m v begin
:ident => Symbol(m.view)
:decl => v[1]
end
#9 (generic function with 1 method)
julia> test2(test1("foo bar baz"))
3-element Vector{Any}:
:foo
:bar
:bazBut if we change what PikaParser is called, we have issues: julia> import PikaParser as P
julia> test1 = PikaParser.@grammar :top begin
:ws => first(satisfy(isspace), epsilon),
:ident => r"[a-z_]+[a-z0-9_]*"i,
:decl => seq(:ident, :ws),
:top => some(:decl)
end
ERROR: LoadError: UndefVarError: `PikaParser` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Hint: PikaParser is loaded but not imported in the active module Main.
Stacktrace:
[1] top-level scope
@ REPL:1
in expression starting at REPL[2]:1
julia> nameof(P)
:PikaParserSo we need to know what PikaParser is called in The trick used in the code at the moment is that the
Of course, there are other ways to do this: we can just re-import (which should be instant) and bind PikaParser to a gensym, so that we know we have a reference to it (and this also won't break if you import via I'm happy to do whatever you think is best! This turned out to be an interesting problem yesterday when I was baking the PRs and trying to ensure the tests ran. There also might be more elegant ways of solving this problem, but I haven't managed to think up any yet. |
Carrying on the work from #22 - this is rebased on top of the
@grammarpull, though, as most of the nasty bits are the same!Likewise - I still need to write the docs, so probably not ready for merging yet, but good to track it here :-).