filters.nim 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module implements Nim's simple filters and helpers for filters.
  10. import
  11. llstream, idents, ast, msgs, options,
  12. renderer, pathutils
  13. import std/strutils
  14. proc invalidPragma(conf: ConfigRef; n: PNode) =
  15. localError(conf, n.info,
  16. "'$1' not allowed here" % renderTree(n, {renderNoComments}))
  17. proc getArg(conf: ConfigRef; n: PNode, name: string, pos: int): PNode =
  18. result = nil
  19. if n.kind in {nkEmpty..nkNilLit}: return
  20. for i in 1..<n.len:
  21. if n[i].kind == nkExprEqExpr:
  22. if n[i][0].kind != nkIdent: invalidPragma(conf, n)
  23. if cmpIgnoreStyle(n[i][0].ident.s, name) == 0:
  24. return n[i][1]
  25. elif i == pos:
  26. return n[i]
  27. proc charArg*(conf: ConfigRef; n: PNode, name: string, pos: int, default: char): char =
  28. var x = getArg(conf, n, name, pos)
  29. if x == nil: result = default
  30. elif x.kind == nkCharLit: result = chr(int(x.intVal))
  31. else:
  32. result = default(char)
  33. invalidPragma(conf, n)
  34. proc strArg*(conf: ConfigRef; n: PNode, name: string, pos: int, default: string): string =
  35. var x = getArg(conf, n, name, pos)
  36. if x == nil: result = default
  37. elif x.kind in {nkStrLit..nkTripleStrLit}: result = x.strVal
  38. else:
  39. result = ""
  40. invalidPragma(conf, n)
  41. proc boolArg*(conf: ConfigRef; n: PNode, name: string, pos: int, default: bool): bool =
  42. var x = getArg(conf, n, name, pos)
  43. if x == nil: result = default
  44. elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "true") == 0: result = true
  45. elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "false") == 0: result = false
  46. else:
  47. result = false
  48. invalidPragma(conf, n)
  49. proc filterStrip*(conf: ConfigRef; stdin: PLLStream, filename: AbsoluteFile, call: PNode): PLLStream =
  50. var pattern = strArg(conf, call, "startswith", 1, "")
  51. var leading = boolArg(conf, call, "leading", 2, true)
  52. var trailing = boolArg(conf, call, "trailing", 3, true)
  53. result = llStreamOpen("")
  54. var line = newStringOfCap(80)
  55. while llStreamReadLine(stdin, line):
  56. var stripped = strip(line, leading, trailing)
  57. if pattern.len == 0 or startsWith(stripped, pattern):
  58. llStreamWriteln(result, stripped)
  59. else:
  60. llStreamWriteln(result, line)
  61. llStreamClose(stdin)
  62. proc filterReplace*(conf: ConfigRef; stdin: PLLStream, filename: AbsoluteFile, call: PNode): PLLStream =
  63. var sub = strArg(conf, call, "sub", 1, "")
  64. if sub.len == 0: invalidPragma(conf, call)
  65. var by = strArg(conf, call, "by", 2, "")
  66. result = llStreamOpen("")
  67. var line = newStringOfCap(80)
  68. while llStreamReadLine(stdin, line):
  69. llStreamWriteln(result, replace(line, sub, by))
  70. llStreamClose(stdin)