filters.nim 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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:
  31. result = default(char)
  32. invalidPragma(conf, n)
  33. proc strArg*(conf: ConfigRef; n: PNode, name: string, pos: int, default: string): string =
  34. var x = getArg(conf, n, name, pos)
  35. if x == nil: result = default
  36. elif x.kind in {nkStrLit..nkTripleStrLit}: result = x.strVal
  37. else:
  38. result = ""
  39. invalidPragma(conf, n)
  40. proc boolArg*(conf: ConfigRef; n: PNode, name: string, pos: int, default: bool): bool =
  41. var x = getArg(conf, n, name, pos)
  42. if x == nil: result = default
  43. elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "true") == 0: result = true
  44. elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "false") == 0: result = false
  45. else:
  46. result = false
  47. invalidPragma(conf, n)
  48. proc filterStrip*(conf: ConfigRef; stdin: PLLStream, filename: AbsoluteFile, call: PNode): PLLStream =
  49. var pattern = strArg(conf, call, "startswith", 1, "")
  50. var leading = boolArg(conf, call, "leading", 2, true)
  51. var trailing = boolArg(conf, call, "trailing", 3, true)
  52. result = llStreamOpen("")
  53. var line = newStringOfCap(80)
  54. while llStreamReadLine(stdin, line):
  55. var stripped = strip(line, leading, trailing)
  56. if pattern.len == 0 or startsWith(stripped, pattern):
  57. llStreamWriteln(result, stripped)
  58. else:
  59. llStreamWriteln(result, line)
  60. llStreamClose(stdin)
  61. proc filterReplace*(conf: ConfigRef; stdin: PLLStream, filename: AbsoluteFile, call: PNode): PLLStream =
  62. var sub = strArg(conf, call, "sub", 1, "")
  63. if sub.len == 0: invalidPragma(conf, call)
  64. var by = strArg(conf, call, "by", 2, "")
  65. result = llStreamOpen("")
  66. var line = newStringOfCap(80)
  67. while llStreamReadLine(stdin, line):
  68. llStreamWriteln(result, replace(line, sub, by))
  69. llStreamClose(stdin)