Skip to content

Revert to the use of StringMacroName/CmdMacroName kinds #583

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

Merged
merged 4 commits into from
Aug 12, 2025

Conversation

c42f
Copy link
Member

@c42f c42f commented Aug 11, 2025

Trying out macro_name_str / macro_name_cmd in JuliaLowering it turns out to be inconvenient to work with identifiers which change their meaning due to being nested inside another construct.

This change revert to the previous use of StringMacroName / CmdMacroName identifier kinds. macro_name is left as-is because it faithfully represents the position of the @.

This isn't a complete reversion to the previous JuliaSyntax behavior. Previously, SyntaxNode would contain the symbol @x_str for the string macro x in x"hi" despite having kind set to SringMacroName. However, appending the _str is best seen as a symbolic lowering (/name mangling) step which isn't reflected in the source code and shouldn't be the business of the parser or parser-related tools. Thus, in the code here we defer this name mangling to the Expr conversion step instead and introduce lower_identifier_name() as a way to do this conversion for cases where is_identifier returns true.

Given the subtle change of semantics (in terms of the SyntaxNode .val field) we could also take this opportunity to use MacroNameStr and MacroNameCmd if those seem like better names?

I considered keeping macro_name_str and macro_name_cmd as wrappers, but that's actually more difficult implementation-wise if we only want to wrap an identifier (Eg, when dealing with A.B.x"hi" - it's awkward to avoid wrapping the whole path with macro_name_str. Additionally, less nesting is simpler for consumers of the AST structure.

Fix #582

Trying out `macro_name_str` / `macro_name_cmd` in JuliaLowering it turns
out to be inconvenient to work with identifiers which change their
meaning due to being nested inside another construct.

This change revert to the previous use of StringMacroName / CmdMacroName
identifier kinds. macro_name is left as-is because it faithfully
represents the position of the `@`.

This isn't a complete reversion to the previous JuliaSyntax behavior.
Previously, SyntaxNode would contain the symbol `@x_str` for the string
macro `x` in `x"hi"` despite having kind set to `SringMacroName`.
However, appending the `_str` is best seen as a symbolic lowering (/name
mangling) step which isn't reflected in the source code and shouldn't be
the business of the parser or parser-related tools. Thus, in the code
here we defer this name mangling to the `Expr` conversion step instead,
and introduce `lower_identifier_name()` as a standard way to do this
conversion.
@c42f c42f requested a review from Keno August 11, 2025 03:23
test/expr.jl Outdated
@@ -554,7 +554,6 @@

# var""
@test parsestmt("@var\"#\" a") == Expr(:macrocall, Symbol("@#"), LineNumberNode(1), :a)
@test parsestmt("@var\"\\\"\" a") == Expr(:macrocall, Symbol("@\""), LineNumberNode(1), :a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be kept. This is JuliaLang/julia#58885

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. My mistake in doing the partial-revert I'll do a closer self-review of the test changes.

test/parser.jl Outdated
# · and · normalize to ⋅
@test parse_to_sexpr_str(JuliaSyntax.parse_eq, "a \u00B7 b") == "(call-i a \u22C5 b)"
@test parse_to_sexpr_str(JuliaSyntax.parse_eq, "a \u0387 b") == "(call-i a \u22C5 b)"
# − ('\u2212') normalizes to - ('\u002d')
@test parse_to_sexpr_str(JuliaSyntax.parse_expr, "a \u2212 b") == "(call-i a - b)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no idea what happened here, yikes

@Keno
Copy link
Member

Keno commented Aug 11, 2025

Fine by me in general, though it precludes enabling var"foo""bar", which is something that @JeffBezanson wanted last we spoke about this.

@c42f
Copy link
Member Author

c42f commented Aug 11, 2025

I feel it's sufficient that @var"##_str" "bar" work rather than trying to make var"##""bar" work, under the principle of "ugly syntax is ok for ugly corner cases" 😅 (And I'm honestly just unsure why any user would want to write this - it's so ugly that not having it work is kind of a feature?)

Given the subtle change of semantics (in terms of the SyntaxNode .val field) we could also take this opportunity to use MacroNameStr and MacroNameCmd if those seem like better names?

Do you have thoughts on this?

@Keno
Copy link
Member

Keno commented Aug 11, 2025

I do like MacroNameStr better. In the fullness of time, I think, I would also like to rename Identifier to RawIdentifier and introduce a special KSet"Identifier" that refers to all of them (including Operator after #575).

@c42f
Copy link
Member Author

c42f commented Aug 11, 2025

I tried the Kind renaming StringMacroName->MacroNameStr (and CmdMacroName->MacroNameCmd) but in hindsight I think it's clearer to use StrMacroName and CmdMacroName because the Str and Cmd are adjectives describing the type of macro name. Using Str rather than String seems nice though to match the abbreviated _str suffix used when lowering these.

I think I'm happy with this now.

@c42f c42f merged commit 1320b6e into main Aug 12, 2025
36 checks passed
@c42f c42f deleted the caf/simplify-str-cmd-macro-name branch August 12, 2025 12:28
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.

macro_name_str should not wrap around full macro path
2 participants