hoisting.nim 579 B

123456789101112131415161718192021222324
  1. type
  2. Regex = distinct string
  3. const maxSubpatterns = 10
  4. proc re(x: string): Regex =
  5. result = Regex(x)
  6. proc match(s: string, pattern: Regex, captures: var openArray[string]): bool =
  7. true
  8. template optRe{re(x)}(x: string{lit}): Regex =
  9. var g {.global.} = re(x)
  10. g
  11. template `=~`(s: string, pattern: Regex): bool =
  12. when not declaredInScope(matches):
  13. var matches {.inject.}: array[maxSubPatterns, string]
  14. match(s, pattern, matches)
  15. for line in lines("input.txt"):
  16. if line =~ re"(\w+)=(\w+)":
  17. echo "key-value pair; key: ", matches[0], " value: ", matches[1]