You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is possible to go through all matches using StringLiteral#gsub, but doing that is needlessly cumbersome and... wrong. That's not what it's supposed to do, at the very least. StringLiteral#scan, on the other hand, should simplify this / fill in the gap, which would be very nice. Instead of exposing Regex::MatchData on the macro side we can expose Regex::MatchData#to_h, leaving us with StringLiteral#scan(regex : RegexLiteral) : ArrayLiteral(HashLiteral(NumberLiteral | StringLiteral), StringLiteral | NilLiteral)
In my particular case, I am relying on it heavily -- I have a case macro for my own pattern matching system and to reduce typing on the Crystal side, I want it to be able to interact with Crystal at compile-time a tiny bit, through mutual understanding of certain constructs.
Term.case(...) do
matchpi %[(%terminal successor_)], %[(%'%let _ successor_)]do
specificity0?(successor)
# ^^^# Note the use of successor on the Crystal side, extracted from the string above# using StringLiteral#scanendend
A very similar use case to mine would be a macro that takes a regex, extracts named captures, and gives them to a block to reduce typing. The captures can be accessed in the block as variables. Or for example, a macro that takes a URL pattern, extracts parameter names from it, and gives them to the block in parsed form, so they can be accessed as properly typed variables. Similarly, one can imagine a macro that inspects the source code of the program itself for commands or directions, and so on. And much more!
The text was updated successfully, but these errors were encountered:
Feature Request
It is possible to go through all matches using
StringLiteral#gsub
, but doing that is needlessly cumbersome and... wrong. That's not what it's supposed to do, at the very least.StringLiteral#scan
, on the other hand, should simplify this / fill in the gap, which would be very nice. Instead of exposingRegex::MatchData
on the macro side we can exposeRegex::MatchData#to_h
, leaving us withStringLiteral#scan(regex : RegexLiteral) : ArrayLiteral(HashLiteral(NumberLiteral | StringLiteral), StringLiteral | NilLiteral)
In my particular case, I am relying on it heavily -- I have a case macro for my own pattern matching system and to reduce typing on the Crystal side, I want it to be able to interact with Crystal at compile-time a tiny bit, through mutual understanding of certain constructs.
A very similar use case to mine would be a macro that takes a regex, extracts named captures, and gives them to a block to reduce typing. The captures can be accessed in the block as variables. Or for example, a macro that takes a URL pattern, extracts parameter names from it, and gives them to the block in parsed form, so they can be accessed as properly typed variables. Similarly, one can imagine a macro that inspects the source code of the program itself for commands or directions, and so on. And much more!
The text was updated successfully, but these errors were encountered: