/
desugar-string-quotations.str
167 lines (145 loc) · 4.1 KB
/
desugar-string-quotations.str
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Desugar $[ string quotations. ]
*
* @author Lennart Kats
*/
module desugar-string-quotations
strategies
Desugar :
StringQuotation4(s, t*) -> <Desugar> StringQuotation1(s, t*)
Desugar :
StringQuotation3(s, t*) -> <Desugar> StringQuotation1(s, t*)
Desugar :
StringQuotation2(s, t*) -> <Desugar> StringQuotation1(s, t*)
Desugar :
StringQuotation1(s, t*) ->
(Term |[ <concat-strings(|)> [t''*] ]|)
with
t'* := <normalize-quoted(|s)> t*
; t''* := t'*{"quoted"}
normalize-quoted(|padding) =
map(try(DesugarToQStr))
; if ?[QStr(_) | _] then
// Combine padding before the initial string with the padding inside it
[QStr(<conc-strings> (padding, <id>) => initial) | id]
; where(
padding' := <string-as-chars(get-padding-chars)> initial
; padding-length := <padding-length> padding'
)
// Unpadd the initial string
; [QStr(
string-as-chars(
remove-padding-single-line(|<padding-length> initial)
)
)
| id
]
else
where(
padding-length := <padding-length> padding
)
end
; map(try(DesugarQuoted))
; string-quotation-padd(|padding-length)
; try(at-last([QStr(string-as-chars(rtrim(' ' + '\t')))]))
; map(try(DesugarQStr))
get-padding-chars =
[' ' + '\t' | get-padding-chars]
<+
![]
padding-length =
string-replace(|"\t", " "); string-length
/**
* Remove balanced padding from quoted strings,
* and expand anti-quotations with padding.
*
* @param max Maximum number of padding characters to remove.
*/
string-quotation-padd(|max) =
[string-literal-unpadd(|max) => (<id>, max') | string-quotation-padd(|max')]
<+
[string-escape-padd(|max) | string-quotation-padd(|max)]
<+
[]
<+
with(fail)
/**
* Remove padding from quoted string literals.
*
* @type List(List(Char)) -> (List(List(Char)), Int)
*/
string-literal-unpadd(|max) =
QStr(string-as-chars(
split-at-newlines
; try([id | remove-padding(|max, max) => (<id>, max')])
; separate-by(|['\n'])
; concat
))
; !(<id>, max')
/**
* Remove padding characters from a list of character lists
* (each representing a line).
*
* @param max Maximum number of padding characters to remove.
* @param n Current number of padding characters to remove
* (decreases with recursion).
*
* @type List(List(Char)) -> (List(List(Char)), Int)
*/
remove-padding(|max, n) =
where(<leq> (n, 0) <+ ?[[] | _]) // finished with current line
; [id | remove-padding(|max, max) => (<id>, max')]
; !(<id>, max')
<+
[?[' ' | <id>] | id]
; remove-padding(|max, <dec> n)
<+
?[['\t' | _] | _]
; if <lt> (n, 4) then
[id | remove-padding(|max, max) => (<id>, max')]
; !(<id>, max')
else
[?[_ | <id>] | id]
; remove-padding(|max, <subt> (n, 4))
end
<+
?[_ | _] // uh oh, non-whitespace character with n > 0; reduce max
; where(max' := <subt> (max, n))
; [id | remove-padding(|max', max') => (<id>, max'')]
; !(<id>, max'')
<+
[]
; !([], max)
remove-padding-single-line(|n):
s -> s'
with
([s'], _) := <remove-padding(|n, n)> [s]
split-at-newlines =
at-suffix(
(['\n' | tl] -> [])
<+ (['\r', '\n' | tl] -> [])
<+ (['\r' | tl] -> [])
)
; ![<id> | <split-at-newlines> tl]
<+ ![<id>]
/**
* Adjust padding of quoted string escapes.
*/
string-escape-padd(|n) :
StringEscape1(s, t) -> <desugar> Term |[ <internal-string-interpolation(|t, ~str:s_padded)> ]|
with
s_padded := <string-as-chars(!['\n' | <remove-padding-single-line(|n)>])> s
DesugarQuoted :
StringEscape2(s, t) -> StringEscape1(s, t)
DesugarQuoted :
StringEscape3(s, t) -> StringEscape1(s, t)
DesugarQuoted :
StringEscape4(s, t) -> StringEscape1(s, t)
DesugarQStr :
QStr(s) -> Anno(Str(s'), Op("Nil", []))
with
s' := <conc-strings> ("\"", <string-replace(|"\\", "\\\\")> s, "\"")
DesugarToQStr :
QBr(s) -> QStr(s)
DesugarToQStr :
QDollar(s) -> QStr(s)