| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use ascii; |
| 5 |
use fmt; |
| 6 |
use io; |
| 7 |
use memio; |
| 8 |
use strings; |
| 9 |
|
| 10 |
export type literal = str; |
| 11 |
export type variable = str; |
| 12 |
export type instruction = (literal | variable); |
| 13 |
export type template = []instruction; |
| 14 |
|
| 15 |
// Parameters to execute with a template, a tuple of a variable name and a |
| 16 |
// formattable object. |
| 17 |
export type param = (str, fmt::formattable); |
| 18 |
|
| 19 |
// The template string has an invalid format. |
| 20 |
export type invalid = !void; |
| 21 |
|
| 22 |
// Returns a human-friendly string for [[invalid]]. |
| 23 |
export fn strerror(err: invalid) str = "Template string has invalid format"; |
| 24 |
|
| 25 |
// Compiles a template string. The return value must be freed with [[finish]] |
| 26 |
// after use. |
| 27 |
export fn compile(input: str) (template | invalid | nomem) = { |
| 28 |
let buf = memio::dynamic(); |
| 29 |
defer io::close(&buf)!; |
| 30 |
|
| 31 |
let instrs: []instruction = []; |
| 32 |
let ok = false; |
| 33 |
defer if (!ok) finish(&instrs); |
| 34 |
|
| 35 |
const iter = strings::iter(input); |
| 36 |
for (let rn => strings::next(&iter)) { |
| 37 |
if (rn == '$') { |
| 38 |
match (strings::next(&iter)) { |
| 39 |
case let next_rn: rune => |
| 40 |
if (next_rn == '$') { |
| 41 |
memio::appendrune(&buf, rn)!; |
| 42 |
} else { |
| 43 |
strings::prev(&iter); |
| 44 |
const lit = memio::string(&buf)!; |
| 45 |
append(instrs, strings::dup(lit)?: literal)?; |
| 46 |
memio::reset(&buf); |
| 47 |
|
| 48 |
parse_variable(&instrs, &iter, &buf)?; |
| 49 |
}; |
| 50 |
case => |
| 51 |
return invalid; |
| 52 |
}; |
| 53 |
} else { |
| 54 |
memio::appendrune(&buf, rn)!; |
| 55 |
}; |
| 56 |
}; |
| 57 |
|
| 58 |
if (len(memio::string(&buf)!) != 0) { |
| 59 |
const lit = memio::string(&buf)!; |
| 60 |
append(instrs, strings::dup(lit)?: literal)?; |
| 61 |
}; |
| 62 |
|
| 63 |
ok = true; |
| 64 |
return instrs; |
| 65 |
}; |
| 66 |
|
| 67 |
// Frees resources associated with a [[template]]. |
| 68 |
export fn finish(tmpl: *template) void = { |
| 69 |
for (let instr .. *tmpl) { |
| 70 |
match (instr) { |
| 71 |
case let lit: literal => |
| 72 |
free(lit); |
| 73 |
case let var: variable => |
| 74 |
free(var); |
| 75 |
}; |
| 76 |
}; |
| 77 |
free(*tmpl); |
| 78 |
}; |
| 79 |
|
| 80 |
// Executes a template, writing the output to the given [[io::handle]]. If the |
| 81 |
// template calls for a parameter which is not provided, an assertion will be |
| 82 |
// fired. |
| 83 |
export fn execute( |
| 84 |
tmpl: *template, |
| 85 |
out: io::handle, |
| 86 |
params: param... |
| 87 |
) (size | io::error) = { |
| 88 |
let z = 0z; |
| 89 |
for (let instr .. *tmpl) { |
| 90 |
match (instr) { |
| 91 |
case let lit: literal => |
| 92 |
z += fmt::fprint(out, lit)?; |
| 93 |
case let var: variable => |
| 94 |
const value = get_param(var, params...); |
| 95 |
z += fmt::fprint(out, value)?; |
| 96 |
}; |
| 97 |
}; |
| 98 |
return z; |
| 99 |
}; |
| 100 |
|
| 101 |
fn get_param(name: str, params: param...) fmt::formattable = { |
| 102 |
// TODO: Consider preparing a parameter map or something |
| 103 |
for (let (var_name, obj) .. params) { |
| 104 |
if (var_name == name) { |
| 105 |
return obj; |
| 106 |
}; |
| 107 |
}; |
| 108 |
fmt::errorfln("strings::template: required parameter ${} was not provided", name)!; |
| 109 |
abort(); |
| 110 |
}; |
| 111 |
|
| 112 |
fn parse_variable( |
| 113 |
instrs: *[]instruction, |
| 114 |
iter: *strings::iterator, |
| 115 |
buf: *memio::stream, |
| 116 |
) (void | invalid | nomem) = { |
| 117 |
let brace = false; |
| 118 |
match (strings::next(iter)) { |
| 119 |
case let rn: rune => |
| 120 |
if (rn == '{') { |
| 121 |
brace = true; |
| 122 |
} else { |
| 123 |
strings::prev(iter); |
| 124 |
}; |
| 125 |
case => |
| 126 |
return invalid; |
| 127 |
}; |
| 128 |
|
| 129 |
for (true) { |
| 130 |
const rn = match (strings::next(iter)) { |
| 131 |
case let rn: rune => |
| 132 |
yield rn; |
| 133 |
case => |
| 134 |
return invalid; |
| 135 |
}; |
| 136 |
|
| 137 |
if (brace) { |
| 138 |
if (rn == '{') { |
| 139 |
return invalid; |
| 140 |
} else if (rn != '}') { |
| 141 |
memio::appendrune(buf, rn)!; |
| 142 |
} else { |
| 143 |
break; |
| 144 |
}; |
| 145 |
} else { |
| 146 |
if (ascii::isalnum(rn) || rn == '_') { |
| 147 |
memio::appendrune(buf, rn)!; |
| 148 |
} else { |
| 149 |
strings::prev(iter); |
| 150 |
break; |
| 151 |
}; |
| 152 |
}; |
| 153 |
}; |
| 154 |
|
| 155 |
const var = memio::string(buf)!; |
| 156 |
append(instrs, strings::dup(var)?: variable)?; |
| 157 |
memio::reset(buf); |
| 158 |
}; |
| 159 |
|
| 160 |
def test_input: str = `Dear ${recipient}, |
| 161 |
|
| 162 |
I am the crown prince of $country. Your brother, $brother, has recently passed |
| 163 |
away in my country. I am writing to you to facilitate the transfer of his |
| 164 |
foreign bank account balance of $$1,000,000 to you.`; |
| 165 |
|
| 166 |
def test_output: str = `Dear Mrs. Johnson, |
| 167 |
|
| 168 |
I am the crown prince of South Africa. Your brother, Elon Musk, has recently passed |
| 169 |
away in my country. I am writing to you to facilitate the transfer of his |
| 170 |
foreign bank account balance of $1,000,000 to you.`; |
| 171 |
|
| 172 |
@test fn template() void = { |
| 173 |
const tmpl = compile(test_input)!; |
| 174 |
defer finish(&tmpl); |
| 175 |
|
| 176 |
let buf = memio::dynamic(); |
| 177 |
defer io::close(&buf)!; |
| 178 |
|
| 179 |
execute(&tmpl, &buf, |
| 180 |
("recipient", "Mrs. Johnson"), |
| 181 |
("country", "South Africa"), |
| 182 |
("brother", "Elon Musk"), |
| 183 |
)!; |
| 184 |
|
| 185 |
assert(memio::string(&buf)! == test_output); |
| 186 |
}; |