Skip to content

Commit 60c956f

Browse files
committed
Revert reservation of ":" within styled regions
In 5dd8b9e we reserved the use of ":" in styled regions in anticipation of the potential use of a style and format construction "{style:content:format}" inspired by Python's f-strings. It has since become apparent that this causes more headaches than anticipated in Julia. The primary reason for this is the different syntactic meaning that ":" has in Julia compared to Python. While in Python ":" serves as a block terminator (used after: if, for, while, def, etc.), in Julia ":" can appear /within/ expressions (namely with ranges and indexing), for example: 1:2:9 m[1, :] :(1 + 2) This means that when formatting an expression, copy-paste is now subtly error-prone as not just quotes but colons will need to be escaped. This adds to the minor, but present, annoyance of having to escape ":" in "{style:content}" constructions, and in my mind tips the scales towards the prospective "{style:content:fmt}" syntax no longer being worth it. A more promising alternative is to go with something like "${fmt:expr}". By both this and "{style:content}" having a single delimiting colon, we can simply read everything until the paired closing curly parentheses at the end of the form as expr/content. The use of "$" in "${fmt:expr}" is also evocative of Julia's $-interpolation syntax, which is appropriate given that expr would be evaluated. While a formating syntax, let alone system, is not decided on at this stage, I think we can safely un-reserve the colon.
1 parent 302a0d0 commit 60c956f

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/stylemacro.jl

+3-7
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ escaped = '\\\\', specialchar ;
3636
interpolated = '\$', ? expr ? | '\$(', ? expr ?, ')' ;
3737
3838
styled = '{', ws, annotations, ':', content, '}' ;
39-
content = { interpolated | colonescaped | almostplain | styled } ;
40-
colonescaped = escaped | '\\\\:' ;
41-
almostplain = { plain - ':' } ;
39+
content = { interpolated | plain | escaped | styled } ;
4240
annotations = annotation | annotations, ws, ',', ws, annotation ;
4341
annotation = face | inlineface | keyvalue ;
4442
ws = { ' ' | '\\t' | '\\n' } ; (* whitespace *)
@@ -109,7 +107,7 @@ macro styled_str(raw_content::String)
109107
end =#
110108

111109
# Instead we'll just use a `NamedTuple`
112-
state = let content = unescape_string(raw_content, ('{', '}', ':', '$', '\n', '\r'))
110+
state = let content = unescape_string(raw_content, ('{', '}', '$', '\n', '\r'))
113111
(; content, bytes = Vector{UInt8}(content),
114112
s = Iterators.Stateful(pairs(content)),
115113
parts = Any[],
@@ -208,7 +206,7 @@ macro styled_str(raw_content::String)
208206
end
209207

210208
function escaped!(state, i, char)
211-
if char in ('{', '}', ':', '$', '\\')
209+
if char in ('{', '}', '$', '\\')
212210
deleteat!(state.bytes, i + state.offset[] - 1)
213211
state.offset[] -= ncodeunits('\\')
214212
elseif char ('\n', '\r')
@@ -651,8 +649,6 @@ macro styled_str(raw_content::String)
651649
else
652650
styerr!(state, "Contains extraneous style terminations", -2, "right here")
653651
end
654-
elseif char == ':' && !isempty(state.active_styles)
655-
styerr!(state, "Colons within styled regions need to be escaped", -2, "right here")
656652
end
657653
end
658654
# Ensure that any trailing unstyled content is added

0 commit comments

Comments
 (0)