# # # The Nimrod Compiler # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # ## Low-level streams for high performance. import strutils when not defined(windows) and defined(useGnuReadline): import rdstdin type TLLStreamKind* = enum # enum of different stream implementations llsNone, # null stream: reading and writing has no effect llsString, # stream encapsulates a string llsFile, # stream encapsulates a file llsStdIn # stream encapsulates stdin TLLStream* = object of TObject kind*: TLLStreamKind # accessible for low-level access (lexbase uses this) f*: tfile s*: string rd*, wr*: int # for string streams lineOffset*: int # for fake stdin line numbers PLLStream* = ref TLLStream proc LLStreamOpen*(data: string): PLLStream proc LLStreamOpen*(f: var tfile): PLLStream proc LLStreamOpen*(filename: string, mode: TFileMode): PLLStream proc LLStreamOpen*(): PLLStream proc LLStreamOpenStdIn*(): PLLStream proc LLStreamClose*(s: PLLStream) proc LLStreamRead*(s: PLLStream, buf: pointer, bufLen: int): int proc LLStreamReadLine*(s: PLLStream, line: var string): bool proc LLStreamReadAll*(s: PLLStream): string proc LLStreamWrite*(s: PLLStream, data: string) proc LLStreamWrite*(s: PLLStream, data: Char) proc LLStreamWrite*(s: PLLStream, buf: pointer, buflen: int) proc LLStreamWriteln*(s: PLLStream, data: string) # implementation proc LLStreamOpen(data: string): PLLStream = new(result) result.s = data result.kind = llsString proc LLStreamOpen(f: var tfile): PLLStream = new(result) result.f = f result.kind = llsFile proc LLStreamOpen(filename: string, mode: TFileMode): PLLStream = new(result) result.kind = llsFile if not open(result.f, filename, mode): result = nil proc LLStreamOpen(): PLLStream = new(result) result.kind = llsNone proc LLStreamOpenStdIn(): PLLStream = new(result) result.kind = llsStdIn result.s = "" result.lineOffset = -1 proc LLStreamClose(s: PLLStream) = case s.kind of llsNone, llsString, llsStdIn: nil of llsFile: close(s.f) when not defined(ReadLineFromStdin): # fallback implementation: proc ReadLineFromStdin(prompt: string, line: var string): bool = stdout.write(prompt) result = readLine(stdin, line) proc endsWith*(x: string, s: set[char]): bool = var i = x.len-1 while i >= 0 and x[i] == ' ': dec(i) if i >= 0 and x[i] in s: result = true const LineContinuationOprs = {'+', '-', '*', '/', '\\', '<', '>', '!', '?', '^', '|', '%', '&', '$', '@', '~', ','} AdditionalLineContinuationOprs = {'#', ':', '='} proc endsWithOpr*(x: string): bool = # also used by the standard template filter: result = x.endsWith(LineContinuationOprs) proc continueLine(line: string, inTripleString: bool): bool {.inline.} = result = inTriplestring or line[0] == ' ' or line.endsWith(LineContinuationOprs+AdditionalLineContinuationOprs) proc countTriples(s: string): int = var i = 0 while i < s.len: if s[i] == '"' and s[i+1] == '"' and s[i+2] == '"': inc result inc i, 2 inc i proc LLreadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int = s.s = "" s.rd = 0 var line = newStringOfCap(120) var triples = 0 while ReadLineFromStdin(if s.s.len == 0: ">>> " else: "... ", line): add(s.s, line) add(s.s, "\n") inc triples, countTriples(line) if not continueLine(line, (triples and 1) == 1): break inc(s.lineOffset) result = min(bufLen, len(s.s) - s.rd) if result > 0: copyMem(buf, addr(s.s[s.rd]), result) inc(s.rd, result) proc LLStreamRead(s: PLLStream, buf: pointer, bufLen: int): int = case s.kind of llsNone: result = 0 of llsString: result = min(bufLen, len(s.s) - s.rd) if result > 0: copyMem(buf, addr(s.s[0 + s.rd]), result) inc(s.rd, resu