diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-06 14:16:45 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-06 14:16:45 -0400 |
| commit | 7b57090564e6f2fe4e2de686f6c76eb0d5744274 (patch) | |
| tree | 8600e43b3c147d3e8f6ce1b9ebf85d55a8e03cda /examples/ini.tm | |
| parent | 11fa4f548ca71baa96a9dba4aa9a24051de265d3 (diff) | |
Use 'when' expression
Diffstat (limited to 'examples/ini.tm')
| -rw-r--r-- | examples/ini.tm | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/examples/ini.tm b/examples/ini.tm index f8f801d0..38ccb624 100644 --- a/examples/ini.tm +++ b/examples/ini.tm @@ -9,27 +9,28 @@ HELP := " file := use ./file.tm func parse_ini(filename:Text)->{Text:{Text:Text}}: - when file.read(filename) is Failure(err): fail(err) - is Success(text): - sections := {:Text:@{Text:Text}} - current_section := @{:Text:Text} - sections:set("", current_section) - - # Line wraps: - text = text:replace($/\{1 nl}{0+space}/, " ") - - for line in text:lines(): - line = line:trim() - if line:matches($/{0+space}[{..}]/): - section_name := line:replace($/{0+space}[{..}]{..}/, "\2"):trim():lower() - current_section = @{:Text:Text} - sections:set(section_name, current_section) - else if line:matches($/{..}={..}/): - key := line:replace($/{..}={..}/, "\1"):trim():lower() - value := line:replace($/{..}={..}/, "\2"):trim() - current_section:set(key, value) - - return {k:v[] for k,v in sections} + text := when file.read(filename) is Failure(err): fail(err) + is Success(text): text + + sections := {:Text:@{Text:Text}} + current_section := @{:Text:Text} + sections:set("", current_section) + + # Line wraps: + text = text:replace($/\{1 nl}{0+space}/, " ") + + for line in text:lines(): + line = line:trim() + if line:matches($/{0+space}[{..}]/): + section_name := line:replace($/{0+space}[{..}]{..}/, "\2"):trim():lower() + current_section = @{:Text:Text} + sections:set(section_name, current_section) + else if line:matches($/{..}={..}/): + key := line:replace($/{..}={..}/, "\1"):trim():lower() + value := line:replace($/{..}={..}/, "\2"):trim() + current_section:set(key, value) + + return {k:v[] for k,v in sections} func main(filename:Text, key:Text): keys := key:split($Pattern"/") |
