filters.nim 2.9 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, os, wordrecg, idents, strutils, ast, astalgo, msgs, options,
  12. renderer
  13. proc filterReplace*(stdin: PLLStream, filename: string, call: PNode): PLLStream
  14. proc filterStrip*(stdin: PLLStream, filename: string, call: PNode): PLLStream
  15. # helpers to retrieve arguments:
  16. proc charArg*(n: PNode, name: string, pos: int, default: char): char
  17. proc strArg*(n: PNode, name: string, pos: int, default: string): string
  18. proc boolArg*(n: PNode, name: string, pos: int, default: bool): bool
  19. # implementation
  20. proc invalidPragma(n: PNode) =
  21. localError(n.info, errXNotAllowedHere, renderTree(n, {renderNoComments}))
  22. proc getArg(n: PNode, name: string, pos: int): PNode =
  23. result = nil
  24. if n.kind in {nkEmpty..nkNilLit}: return
  25. for i in countup(1, sonsLen(n) - 1):
  26. if n.sons[i].kind == nkExprEqExpr:
  27. if n.sons[i].sons[0].kind != nkIdent: invalidPragma(n)
  28. if cmpIgnoreStyle(n.sons[i].sons[0].ident.s, name) == 0:
  29. return n.sons[i].sons[1]
  30. elif i == pos:
  31. return n.sons[i]
  32. proc charArg(n: PNode, name: string, pos: int, default: char): char =
  33. var x = getArg(n, name, pos)
  34. if x == nil: result = default
  35. elif x.kind == nkCharLit: result = chr(int(x.intVal))
  36. else: invalidPragma(n)
  37. proc strArg(n: PNode, name: string, pos: int, default: string): string =
  38. var x = getArg(n, name, pos)
  39. if x == nil: result = default
  40. elif x.kind in {nkStrLit..nkTripleStrLit}: result = x.strVal
  41. else: invalidPragma(n)
  42. proc boolArg(n: PNode, name: string, pos: int, default: bool): bool =
  43. var x = getArg(n, name, pos)
  44. if x == nil: result = default
  45. elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "true") == 0: result = true
  46. elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "false") == 0: result = false
  47. else: invalidPragma(n)
  48. proc filterStrip(stdin: PLLStream, filename: string, call: PNode): PLLStream =
  49. var pattern = strArg(call, "startswith", 1, "")
  50. var leading = boolArg(call, "leading", 2, true)
  51. var trailing = boolArg(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 len(pattern) == 0 or startsWith(stripped, pattern):
  57. llStreamWriteln(result, stripped)
  58. else:
  59. llStreamWriteln(result, line)
  60. llStreamClose(stdin)
  61. proc filterReplace(stdin: PLLStream, filename: string, call: PNode): PLLStream =
  62. var sub = strArg(call, "sub", 1, "")
  63. if len(sub) == 0: invalidPragma(call)
  64. var by = strArg(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)