diff options
| -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"/") |
