added UTF-8 pattern support for the interpolation markers
diff -r 8b3c98d57d7b -r f0f2584a4dc0 lib/scopes/string-interpolation.sc --- a/lib/scopes/string-interpolation.sc Wed Aug 12 02:44:54 2020 -0500 +++ b/lib/scopes/string-interpolation.sc Wed Aug 12 18:58:17 2020 -0500 @@ -10,31 +10,42 @@ expressions inside them.
spice escape-pattern (str)
fold (result = "") for c in (str as string)
local c = c
switch c # characters in "[]\\^$.|?*+(){}"
pass 91:i8
pass 93:i8
pass 92:i8
pass 94:i8
pass 36:i8
pass 46:i8
pass 124:i8
pass 63:i8
pass 42:i8
pass 43:i8
pass 40:i8
pass 41:i8
pass 123:i8
pass 125:i8
do
.. result "\\" (string &c 1:usize)
default
.. result (string &c 1:usize)
->> str UTF-8.decoder
retain
inline (c)
switch c
pass (UTF-8.char "[")
pass (UTF-8.char "]")
pass (UTF-8.char "\\")
pass (UTF-8.char "^")
pass (UTF-8.char "$")
pass (UTF-8.char ".")
pass (UTF-8.char "|")
pass (UTF-8.char "?")
pass (UTF-8.char "*")
pass (UTF-8.char "+")
pass (UTF-8.char "(")
pass (UTF-8.char ")")
pass (UTF-8.char "{")
pass (UTF-8.char "}")
do
UTF-8.char "\\"
default
-1
map
inline (code-point)
code-point
flatten
filter
(x) -> (x != -1)
UTF-8.encoder
string.collector 256
`[new-pattern]run-stage; diff -r 8b3c98d57d7b -r f0f2584a4dc0 testing/test_string_interpolation.sc --- a/testing/test_string_interpolation.sc Wed Aug 12 02:44:54 2020 -0500 +++ b/testing/test_string_interpolation.sc Wed Aug 12 18:58:17 2020 -0500 @@ -34,3 +34,12 @@ == fAlt"this is not a very smart interpolation marker abravarargs...cadabra" "this is not a very smart interpolation marker 1 2 3 4" + +let prefix:f🤔 = (gen-interpolation-macro "🤔" "🤔") +run-stage; +let encoding = "UTF-8" +test
f🤔"we also support 🤔encoding🤔 markers!"
"we also support UTF-8 markers!"
+print f🤔"we also support 🤔encoding🤔 markers!"small adjustments to string-interpolation and its test
diff -r f0f2584a4dc0 -r 7e6b1b843bcd lib/scopes/string-interpolation.sc --- a/lib/scopes/string-interpolation.sc Wed Aug 12 18:58:17 2020 -0500 +++ b/lib/scopes/string-interpolation.sc Wed Aug 19 15:44:22 2020 -0500 @@ -15,7 +15,6 @@
str as:= string
let new-pattern =
# TODO: support UTF-8
->> str UTF-8.decoder
retain
inline (c)
f🤔"we also support 🤔encoding🤔 markers!"
"we also support UTF-8 markers!"
-print f🤔"we also support 🤔encoding🤔 markers!"added a check for illegal UTF-8 bytes in the regexp escape
diff -r 7e6b1b843bcd -r 0e745ccc1a16 lib/scopes/string-interpolation.sc --- a/lib/scopes/string-interpolation.sc Wed Aug 19 15:44:22 2020 -0500 +++ b/lib/scopes/string-interpolation.sc Thu Aug 20 08:01:26 2020 -0500 @@ -39,6 +39,8 @@ -1 map inline (code-point)
if (code-point < 0)
error "illegal byte in UTF-8 stream"
code-point
flatten
filter
First version of string-interpolation.sc for evaluation
diff -r 7173c4a4d444 -r 8b3c98d57d7b lib/scopes/string-interpolation.sc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/scopes/string-interpolation.sc Wed Aug 12 02:44:54 2020 -0500 @@ -0,0 +1,90 @@ +#
# TODO: support UTF-8
fold (result = "") for c in (str as string)
local c = c
switch c # characters in "[]\\^$.|?*+(){}"
pass 91:i8
pass 93:i8
pass 92:i8
pass 94:i8
pass 36:i8
pass 46:i8
pass 124:i8
pass 63:i8
pass 42:i8
pass 43:i8
pass 40:i8
pass 41:i8
pass 123:i8
pass 125:i8
do
.. result "\\" (string &c 1:usize)
default
.. result (string &c 1:usize)
error "interpolation markers must be at least one character long"
inline convert (value)
static-if ((typeof value) == string)
value
else
tostring value
let first rest = (va-split 1 values...)
va-lfold (convert (first))
inline (__ v result)
.. result " " (convert v)
(rest)
..
escape-pattern begin-pattern
".*?"
escape-pattern end-pattern
str as:= string
let chunks =
loop (str chunks = str '())
let match? start end = ('match? pattern str)
if (not match?)
break ('reverse (cons str chunks))
let lhs = (lslice str start)
let code =
slice str
start + (countof begin-pattern)
end - (countof end-pattern)
let parsed-code = (sc_parse_from_string code)
_
rslice str end
cons
cons (qq [any->string]) (parsed-code as list)
cons lhs chunks
if ((countof chunks) > 1)
cons (qq [..]) chunks
else
cons (qq [embed]) chunks
f"ABC is {ABC}, CDE is {CDE}, and the sum is {(+ ABC CDE)}. This other string is {str}"
"ABC is 123, CDE is 345, and the sum is 468. This other string is banana"
f"there is nothing to interpolate here, so the string should remain unchanged."
"there is nothing to interpolate here, so the string should remain unchanged."
f"{ an expression start token without a corresponding end is ignored"
"{ an expression start token without a corresponding end is ignored"
f"some ... {varargs...} for you!"
"some ... 1 2 3 4 for you!"
fAlt"this is not a very smart interpolation marker abravarargs...cadabra"
"this is not a very smart interpolation marker 1 2 3 4"