filters.nim 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, strutils, ast, msgs, options,
  12. renderer, pathutils
  13. proc invalidPragma(conf: ConfigRef; n: PNode) =
  14. localError(conf, n.info,
  15. "'$1' not allowed here" % renderTree(n, {renderNoComments}))
  16. proc getArg(conf: ConfigRef; n: PNode, name: string, pos: int): PNode =
  17. result = nil
  18. if n.kind in {nkEmpty..nkNilLit}: return
  19. for i in 1..<n.len:
  20. if n[i].kind == nkExprEqExpr:
  21. if n[i][0].kind != nkIdent: invalidPragma(conf, n)
  22. if cmpIgnoreStyle(n[i][0].ident.s, name) == 0:
  23. return n[i][1]
  24. elif i == pos:
  25. return n[i]
  26. proc charArg*(conf: ConfigRef; n: PNode, name: string, pos: int, default: char): char =
  27. var x = getArg(conf, n, name, pos)
  28. if x == nil: result = default
  29. elif x.kind == nkCharLit: result = chr(int(x.intVal))
  30. else: invalidPragma(conf, n)
  31. proc strArg*(conf: ConfigRef; n: PNode, name: string, pos: int, default: string): string =
  32. var x = getArg(conf, n, name, pos)
  33. if x == nil: result = default
  34. elif x.kind in {nkStrLit..nkTripleStrLit}: result = x.strVal
  35. else: invalidPragma(conf, n)
  36. proc boolArg*(conf: ConfigRef; n: PNode, name: string, pos: int, default: bool): bool =
  37. var x = getArg(conf, n, name, pos)
  38. if x == nil: result = default
  39. elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "true") == 0: result = true
  40. elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "false") == 0: result = false
  41. else: invalidPragma(conf, n)
  42. proc filterStrip*(conf: ConfigRef; stdin: PLLStream, filename: AbsoluteFile, call: PNode): PLLStream =
  43. var pattern = strArg(conf, call, "startswith", 1, "")
  44. var leading = boolArg(conf, call, "leading", 2, true)
  45. var trailing = boolArg(conf, call, "trailing", 3, true)
  46. result = llStreamOpen("")
  47. var line = newStringOfCap(80)
  48. while llStreamReadLine(stdin, line):
  49. var stripped = strip(line, leading, trailing)
  50. if pattern.len == 0 or startsWith(stripped, pattern):
  51. llStreamWriteln(result, stripped)
  52. else:
  53. llStreamWriteln(result, line)
  54. llStreamClose(stdin)
  55. proc filterReplace*(conf: ConfigRef; stdin: PLLStream, filename: AbsoluteFile, call: PNode): PLLStream =
  56. var sub = strArg(conf, call, "sub", 1, "")
  57. if sub.len == 0: invalidPragma(conf, call)
  58. var by = strArg(conf, call, "by", 2, "")
  59. result = llStreamOpen("")
  60. var line = newStringOfCap(80)
  61. while llStreamReadLine(stdin, line):
  62. llStreamWriteln(result, replace(line, sub, by))
  63. llStreamClose(stdin)