@@ -7,18 +7,16 @@ Licensed under MIT License, see LICENSE.md
77"""
88module StrFormat
99
10- using Format, StrLiterals
10+ using APITools, Format
11+ @api init
1112
12- parse_error (s) = throw (StrLiterals. _ParseError (s))
13-
14- _check_exp (ex) =
15- isa (ex, Expr) && (ex. head === :continue ) && parse_error (" Incomplete expression" )
13+ @api extend StrAPI, CharSetEncodings, Chars, StrBase, StrLiterals
1614
1715function _parse_format (str, pos, fun)
18- ex, j = StrLiterals . _parse ( str, pos; greedy= false )
19- _check_exp (ex)
20- ex, k = StrLiterals . _parse ( string (" a" , str[pos: j- 1 ]), 1 , greedy= true )
21- _check_exp (ex)
16+ ex, j = parse (Expr, str, pos; greedy= false )
17+ check_expr (ex)
18+ ex, k = parse (Expr, string (" a" , str[pos: j- 1 ]), 1 , greedy= true )
19+ check_expr (ex)
2220 isa (ex, Symbol) && (println (string (" a" , str[pos: j- 1 ])) ; dump (ex))
2321 ex. args[1 ] = fun
2422 ex, j
@@ -28,7 +26,7 @@ function _parse_fmt(sx::Vector{Any}, s::AbstractString, unescape::Function,
2826 i:: Integer , j:: Integer , k:: Integer )
2927 # Move past \\, k should point to '%'
3028 c, k = next (s, k)
31- done (s, k) && parse_error ( " Incomplete % expression" )
29+ check_done (s, k, " Incomplete % expression" )
3230 # Handle interpolation
3331 isempty (s[i: j- 1 ]) || push! (sx, unescape (s[i: j- 1 ]))
3432 if s[k] == ' ('
@@ -39,7 +37,7 @@ function _parse_fmt(sx::Vector{Any}, s::AbstractString, unescape::Function,
3937 beg = k
4038 while true
4139 c, k = next (s, k)
42- done (s, k) && parse_error ( " Incomplete % expression" )
40+ check_done (s, k, " Incomplete % expression" )
4341 s[k] == ' (' && break
4442 end
4543 ex, j = _parse_format (s, k, Format. cfmt)
@@ -53,16 +51,16 @@ function _parse_pyfmt(sx::Vector{Any}, s::AbstractString, unescape::Function,
5351 i:: Integer , j:: Integer , k:: Integer )
5452 # Move past \\, k should point to '{'
5553 c, k = next (s, k)
56- done (s, k) && parse_error ( " Incomplete {...} Python format expression" )
54+ check_done (s, k, " Incomplete {...} Python format expression" )
5755 # Handle interpolation
5856 isempty (s[i: j- 1 ]) || push! (sx, unescape (s[i: j- 1 ]))
5957 beg = k # start location
6058 c, k = next (s, k)
6159 while c != ' }'
62- done (s, k) && parse_error ( string (" \\ { missing closing } in " , c))
60+ check_done (s, k, string (" \\ { missing closing } in " , c))
6361 c, k = next (s, k)
6462 end
65- done (s, k) && parse_error ( " Missing (expr) in Python format expression" )
63+ check_done (s, k, " Missing (expr) in Python format expression" )
6664 c, k = next (s, k)
6765 c == ' (' || parse_error (string (" Missing (expr) in Python format expression: " , c))
6866 # Need to find end to parse to
@@ -73,8 +71,10 @@ function _parse_pyfmt(sx::Vector{Any}, s::AbstractString, unescape::Function,
7371end
7472
7573function __init__ ()
76- StrLiterals . interpolate[' %' ] = _parse_fmt
77- StrLiterals . interpolate[' {' ] = _parse_pyfmt
74+ interpolate[' %' ] = _parse_fmt
75+ interpolate[' {' ] = _parse_pyfmt
7876end
7977
78+ @api freeze
79+
8080end # module StrFormat
0 commit comments