Skip to content

Commit 29e85b4

Browse files
authored
Separate indicator processing (#3)
Allows for processing to be done at parse time instead of runtime when interpolation is present.
1 parent 6071847 commit 29e85b4

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

src/MultilineStrings.jl

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,53 +43,7 @@ Revise a multiline string according to the provided style and chomp encoded in t
4343
- "lk" / "|+": literal and keep
4444
"""
4545
function multiline(str::AbstractString, indicators::AbstractString)
46-
indicators_len = length(indicators)
47-
indicators_len > 2 && throw(ArgumentError("Too many indicators provided"))
48-
49-
# Note: Using '\0` to indicate undefined
50-
yaml_chomp = false
51-
style_char, chomp_char = if indicators_len == 2
52-
indicators
53-
elseif indicators_len == 1
54-
ind = indicators[1]
55-
if ind in ('f', 'l')
56-
ind, '\0'
57-
elseif ind in ('>', '|')
58-
yaml_chomp = true
59-
ind, '\0'
60-
else
61-
'\0', ind
62-
end
63-
else
64-
'\0', '\0'
65-
end
66-
67-
if style_char != '\0' && chomp_char != '\0' && isletter(style_char) isletter(chomp_char)
68-
throw(ArgumentError("Can't mix YAML style block indicators with letter indicators"))
69-
end
70-
71-
style = if style_char == 'f' || style_char == '>'
72-
:folded
73-
elseif style_char == 'l' || style_char == '|'
74-
:literal
75-
elseif style_char == '\0'
76-
DEFAULT_STYLE
77-
else
78-
throw(ArgumentError("Unknown style indicator: $(repr(style_char))"))
79-
end
80-
81-
chomp = if chomp_char == 'c' || yaml_chomp
82-
:clip
83-
elseif chomp_char == 's' || chomp_char == '-'
84-
:strip
85-
elseif chomp_char == 'k' || chomp_char == '+'
86-
:keep
87-
elseif chomp_char == '\0'
88-
DEFAULT_CHOMP
89-
else
90-
throw(ArgumentError("Unknown chomping indicator: $(repr(chomp_char))"))
91-
end
92-
46+
style, chomp = _process_indicators(indicators)
9347
return multiline(str, style, chomp)
9448
end
9549

@@ -148,16 +102,17 @@ julia> m\"\"\"
148102
```
149103
"""
150104
macro m_str(str::AbstractString, indicators::AbstractString="")
105+
style, chomp = _process_indicators(indicators)
151106
parsed = interpolate(str)
152107

153108
# When no string interpolation needs to take place we can just process the multiline
154109
# string during parse time. If string interpolation needs to take place we'll evaluate
155110
# the multiline string at runtime so that we can process after interpolation has taken
156111
# place.
157112
result = if parsed isa String
158-
multiline(unescape_string(parsed), indicators)
113+
multiline(unescape_string(parsed), style, chomp)
159114
else
160-
Expr(:call, :(MultilineStrings.multiline), parsed, indicators)
115+
Expr(:call, :(MultilineStrings.multiline), parsed, QuoteNode(style), QuoteNode(chomp))
161116
end
162117

163118
return esc(result)
@@ -193,4 +148,55 @@ function interpolate(str::AbstractString)
193148
return Expr(:string, components...)
194149
end
195150

151+
function _process_indicators(indicators::AbstractString)
152+
indicators_len = length(indicators)
153+
indicators_len > 2 && throw(ArgumentError("Too many indicators provided"))
154+
155+
# Note: Using '\0` to indicate undefined
156+
yaml_chomp = false
157+
style_char, chomp_char = if indicators_len == 2
158+
indicators
159+
elseif indicators_len == 1
160+
ind = indicators[1]
161+
if ind in ('f', 'l')
162+
ind, '\0'
163+
elseif ind in ('>', '|')
164+
yaml_chomp = true
165+
ind, '\0'
166+
else
167+
'\0', ind
168+
end
169+
else
170+
'\0', '\0'
171+
end
172+
173+
if style_char != '\0' && chomp_char != '\0' && isletter(style_char) isletter(chomp_char)
174+
throw(ArgumentError("Can't mix YAML style block indicators with letter indicators"))
175+
end
176+
177+
style = if style_char == 'f' || style_char == '>'
178+
:folded
179+
elseif style_char == 'l' || style_char == '|'
180+
:literal
181+
elseif style_char == '\0'
182+
DEFAULT_STYLE
183+
else
184+
throw(ArgumentError("Unknown style indicator: $(repr(style_char))"))
185+
end
186+
187+
chomp = if chomp_char == 'c' || yaml_chomp
188+
:clip
189+
elseif chomp_char == 's' || chomp_char == '-'
190+
:strip
191+
elseif chomp_char == 'k' || chomp_char == '+'
192+
:keep
193+
elseif chomp_char == '\0'
194+
DEFAULT_CHOMP
195+
else
196+
throw(ArgumentError("Unknown chomping indicator: $(repr(chomp_char))"))
197+
end
198+
199+
return style, chomp
200+
end
201+
196202
end

0 commit comments

Comments
 (0)