llstream.nim 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. ## Low-level streams for high performance.
  10. import
  11. pathutils
  12. when defined(nimPreviewSlimSystem):
  13. import std/syncio
  14. # support `useGnuReadline`, `useLinenoise` for backwards compatibility
  15. const hasRstdin = (defined(nimUseLinenoise) or defined(useLinenoise) or defined(useGnuReadline)) and
  16. not defined(windows)
  17. when hasRstdin: import rdstdin
  18. type
  19. TLLRepl* = proc (s: PLLStream, buf: pointer, bufLen: int): int
  20. OnPrompt* = proc() {.closure.}
  21. TLLStreamKind* = enum # enum of different stream implementations
  22. llsNone, # null stream: reading and writing has no effect
  23. llsString, # stream encapsulates a string
  24. llsFile, # stream encapsulates a file
  25. llsStdIn # stream encapsulates stdin
  26. TLLStream* = object of RootObj
  27. kind*: TLLStreamKind # accessible for low-level access (lexbase uses this)
  28. f*: File
  29. s*: string
  30. rd*, wr*: int # for string streams
  31. lineOffset*: int # for fake stdin line numbers
  32. repl*: TLLRepl # gives stdin control to clients
  33. onPrompt*: OnPrompt
  34. PLLStream* = ref TLLStream
  35. proc llStreamOpen*(data: string): PLLStream =
  36. new(result)
  37. result.s = data
  38. result.kind = llsString
  39. proc llStreamOpen*(f: File): PLLStream =
  40. new(result)
  41. result.f = f
  42. result.kind = llsFile
  43. proc llStreamOpen*(filename: AbsoluteFile, mode: FileMode): PLLStream =
  44. new(result)
  45. result.kind = llsFile
  46. if not open(result.f, filename.string, mode): result = nil
  47. proc llStreamOpen*(): PLLStream =
  48. new(result)
  49. result.kind = llsNone
  50. proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int
  51. proc llStreamOpenStdIn*(r: TLLRepl = llReadFromStdin, onPrompt: OnPrompt = nil): PLLStream =
  52. new(result)
  53. result.kind = llsStdIn
  54. result.s = ""
  55. result.lineOffset = -1
  56. result.repl = r
  57. result.onPrompt = onPrompt
  58. proc llStreamClose*(s: PLLStream) =
  59. case s.kind
  60. of llsNone, llsString, llsStdIn:
  61. discard
  62. of llsFile:
  63. close(s.f)
  64. when not declared(readLineFromStdin):
  65. # fallback implementation:
  66. proc readLineFromStdin(prompt: string, line: var string): bool =
  67. stdout.write(prompt)
  68. result = readLine(stdin, line)
  69. if not result:
  70. stdout.write("\n")
  71. quit(0)
  72. proc endsWith*(x: string, s: set[char]): bool =
  73. var i = x.len-1
  74. while i >= 0 and x[i] == ' ': dec(i)
  75. if i >= 0 and x[i] in s:
  76. result = true
  77. const
  78. LineContinuationOprs = {'+', '-', '*', '/', '\\', '<', '>', '!', '?', '^',
  79. '|', '%', '&', '$', '@', '~', ','}
  80. AdditionalLineContinuationOprs = {'#', ':', '='}
  81. proc endsWithOpr*(x: string): bool =
  82. result = x.endsWith(LineContinuationOprs)
  83. proc continueLine(line: string, inTripleString: bool): bool {.inline.} =
  84. result = inTripleString or line.len > 0 and (
  85. line[0] == ' ' or
  86. line.endsWith(LineContinuationOprs+AdditionalLineContinuationOprs))
  87. proc countTriples(s: string): int =
  88. var i = 0
  89. while i+2 < s.len:
  90. if s[i] == '"' and s[i+1] == '"' and s[i+2] == '"':
  91. inc result
  92. inc i, 2
  93. inc i
  94. proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int =
  95. s.s = ""
  96. s.rd = 0
  97. var line = newStringOfCap(120)
  98. var triples = 0
  99. while readLineFromStdin(if s.s.len == 0: ">>> " else: "... ", line):
  100. s.s.add(line)
  101. s.s.add("\n")
  102. inc triples, countTriples(line)
  103. if not continueLine(line, (triples and 1) == 1): break
  104. inc(s.lineOffset)
  105. result = min(bufLen, s.s.len - s.rd)
  106. if result > 0:
  107. copyMem(buf, addr(s.s[s.rd]), result)
  108. inc(s.rd, result)
  109. proc llStreamRead*(s: PLLStream, buf: pointer, bufLen: int): int =
  110. case s.kind
  111. of llsNone:
  112. result = 0
  113. of llsString:
  114. result = min(bufLen, s.s.len - s.rd)
  115. if result > 0:
  116. copyMem(buf, addr(s.s[0 + s.rd]), result)
  117. inc(s.rd, result)
  118. of llsFile:
  119. result = readBuffer(s.f, buf, bufLen)
  120. of llsStdIn:
  121. if s.onPrompt!=nil: s.onPrompt()
  122. result = s.repl(s, buf, bufLen)
  123. proc llStreamReadLine*(s: PLLStream, line: var string): bool =
  124. setLen(line, 0)
  125. case s.kind
  126. of llsNone:
  127. result = true
  128. of llsString:
  129. while s.rd < s.s.len:
  130. case s.s[s.rd]
  131. of '\r':
  132. inc(s.rd)
  133. if s.s[s.rd] == '\n': inc(s.rd)
  134. break
  135. of '\n':
  136. inc(s.rd)
  137. break
  138. else:
  139. line.add(s.s[s.rd])
  140. inc(s.rd)
  141. result = line.len > 0 or s.rd < s.s.len
  142. of llsFile:
  143. result = readLine(s.f, line)
  144. of llsStdIn:
  145. result = readLine(stdin, line)
  146. proc llStreamWrite*(s: PLLStream, data: string) =
  147. case s.kind
  148. of llsNone, llsStdIn:
  149. discard
  150. of llsString:
  151. s.s.add(data)
  152. inc(s.wr, data.len)
  153. of llsFile:
  154. write(s.f, data)
  155. proc llStreamWriteln*(s: PLLStream, data: string) =
  156. llStreamWrite(s, data)
  157. llStreamWrite(s, "\n")
  158. proc llStreamWrite*(s: PLLStream, data: char) =
  159. var c: char
  160. case s.kind
  161. of llsNone, llsStdIn:
  162. discard
  163. of llsString:
  164. s.s.add(data)
  165. inc(s.wr)
  166. of llsFile:
  167. c = data
  168. discard writeBuffer(s.f, addr(c), sizeof(c))
  169. proc llStreamWrite*(s: PLLStream, buf: pointer, buflen: int) =
  170. case s.kind
  171. of llsNone, llsStdIn:
  172. discard
  173. of llsString:
  174. if buflen > 0:
  175. setLen(s.s, s.s.len + buflen)
  176. copyMem(addr(s.s[0 + s.wr]), buf, buflen)
  177. inc(s.wr, buflen)
  178. of llsFile:
  179. discard writeBuffer(s.f, buf, buflen)
  180. proc llStreamReadAll*(s: PLLStream): string =
  181. const
  182. bufSize = 2048
  183. case s.kind
  184. of llsNone, llsStdIn:
  185. result = ""
  186. of llsString:
  187. if s.rd == 0: result = s.s
  188. else: result = substr(s.s, s.rd)
  189. s.rd = s.s.len
  190. of llsFile:
  191. result = newString(bufSize)
  192. var bytes = readBuffer(s.f, addr(result[0]), bufSize)
  193. var i = bytes
  194. while bytes == bufSize:
  195. setLen(result, i + bufSize)
  196. bytes = readBuffer(s.f, addr(result[i + 0]), bufSize)
  197. inc(i, bytes)
  198. setLen(result, i)