diff options
author | Araq <rumpf_a@web.de> | 2011-11-27 19:01:55 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-11-27 19:01:55 +0100 |
commit | a489161b1629b2b7347521c8ec67a0e5e812ef6a (patch) | |
tree | eea6b32a8b598a613cdda006c9cbcfcedba7dccf | |
parent | 7832af187afa60fee37ab6e598f822b6a6ba6025 (diff) | |
download | Nim-a489161b1629b2b7347521c8ec67a0e5e812ef6a.tar.gz |
compiler uses new 'readLine'
-rwxr-xr-x | compiler/extccomp.nim | 4 | ||||
-rwxr-xr-x | compiler/filter_tmpl.nim | 4 | ||||
-rwxr-xr-x | compiler/filters.nim | 8 | ||||
-rw-r--r-- | compiler/idgen.nim | 13 | ||||
-rwxr-xr-x | compiler/llstream.nim | 71 | ||||
-rwxr-xr-x | compiler/syntaxes.nim | 9 | ||||
-rwxr-xr-x | lib/impure/rdstdin.nim | 24 | ||||
-rwxr-xr-x | lib/pure/streams.nim | 26 |
8 files changed, 86 insertions, 73 deletions
diff --git a/compiler/extccomp.nim b/compiler/extccomp.nim index 9184b3368..2d748c451 100755 --- a/compiler/extccomp.nim +++ b/compiler/extccomp.nim @@ -380,8 +380,8 @@ proc externalFileChanged(filename: string): bool = var currentCrc = int(footprint(filename)) var f: TFile if open(f, crcFile, fmRead): - var line = f.readLine() - if isNil(line) or line.len == 0: line = "0" + var line = newStringOfCap(40) + if not f.readLine(line): line = "0" close(f) var oldCrc = parseInt(line) result = oldCrc != currentCrc diff --git a/compiler/filter_tmpl.nim b/compiler/filter_tmpl.nim index e56a08552..785ad0cab 100755 --- a/compiler/filter_tmpl.nim +++ b/compiler/filter_tmpl.nim @@ -206,8 +206,8 @@ proc filterTmpl(stdin: PLLStream, filename: string, call: PNode): PLLStream = p.emit = strArg(call, "emit", 3, "result.add") p.conc = strArg(call, "conc", 4, " & ") p.toStr = strArg(call, "tostring", 5, "$") - while not LLStreamAtEnd(p.inp): - p.x = LLStreamReadLine(p.inp) + p.x = newStringOfCap(120) + while LLStreamReadLine(p.inp, p.x): p.info.line = p.info.line + int16(1) parseLine(p) newLine(p) diff --git a/compiler/filters.nim b/compiler/filters.nim index 43ac13c00..ca8c63c98 100755 --- a/compiler/filters.nim +++ b/compiler/filters.nim @@ -59,8 +59,8 @@ proc filterStrip(stdin: PLLStream, filename: string, call: PNode): PLLStream = var leading = boolArg(call, "leading", 2, true) var trailing = boolArg(call, "trailing", 3, true) result = LLStreamOpen("") - while not LLStreamAtEnd(stdin): - var line = LLStreamReadLine(stdin) + var line = newStringOfCap(80) + while LLStreamReadLine(stdin, line): var stripped = strip(line, leading, trailing) if (len(pattern) == 0) or startsWith(stripped, pattern): LLStreamWriteln(result, stripped) @@ -73,7 +73,7 @@ proc filterReplace(stdin: PLLStream, filename: string, call: PNode): PLLStream = if len(sub) == 0: invalidPragma(call) var by = strArg(call, "by", 2, "") result = LLStreamOpen("") - while not LLStreamAtEnd(stdin): - var line = LLStreamReadLine(stdin) + var line = newStringOfCap(80) + while LLStreamReadLine(stdin, line): LLStreamWriteln(result, replace(line, sub, by)) LLStreamClose(stdin) diff --git a/compiler/idgen.nim b/compiler/idgen.nim index b6d292574..0a0920c81 100644 --- a/compiler/idgen.nim +++ b/compiler/idgen.nim @@ -23,7 +23,7 @@ when debugIds: proc registerID*(id: PIdObj) = when debugIDs: - if (id.id == - 1) or ContainsOrIncl(usedIds, id.id): + if id.id == -1 or ContainsOrIncl(usedIds, id.id): InternalError("ID already used: " & $id.id) proc getID*(): int {.inline.} = @@ -55,9 +55,12 @@ proc saveMaxIds*(project: string) = proc loadMaxIds*(project: string) = var f: TFile if open(f, project.toGid, fmRead): - var frontEndId = parseInt(f.readLine) - var backEndId = parseInt(f.readLine) - gFrontEndId = max(gFrontEndId, frontEndId) - gBackEndId = max(gBackEndId, backEndId) + var line = newStringOfCap(20) + if f.readLine(line): + var frontEndId = parseInt(line) + if f.readLine(line): + var backEndId = parseInt(line) + gFrontEndId = max(gFrontEndId, frontEndId) + gBackEndId = max(gBackEndId, backEndId) f.close() diff --git a/compiler/llstream.nim b/compiler/llstream.nim index 2da56bccd..0461df993 100755 --- a/compiler/llstream.nim +++ b/compiler/llstream.nim @@ -16,11 +16,11 @@ when not defined(windows) and defined(useGnuReadline): import rdstdin type - TLLStreamKind* = enum # stream encapsulates stdin + 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 + llsStdIn # stream encapsulates stdin TLLStream* = object of TObject kind*: TLLStreamKind # accessible for low-level access (lexbase uses this) f*: tfile @@ -37,13 +37,12 @@ proc LLStreamOpen*(): PLLStream proc LLStreamOpenStdIn*(): PLLStream proc LLStreamClose*(s: PLLStream) proc LLStreamRead*(s: PLLStream, buf: pointer, bufLen: int): int -proc LLStreamReadLine*(s: PLLStream): string +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) -proc LLStreamAtEnd*(s: PLLStream): bool # implementation proc LLStreamOpen(data: string): PLLStream = @@ -80,9 +79,9 @@ proc LLStreamClose(s: PLLStream) = when not defined(ReadLineFromStdin): # fallback implementation: - proc ReadLineFromStdin(prompt: string): string = + proc ReadLineFromStdin(prompt: string, line: var string): bool = stdout.write(prompt) - result = readLine(stdin) + result = readLine(stdin, line) proc endsWith*(x: string, s: set[char]): bool = var i = x.len-1 @@ -95,8 +94,8 @@ const '|', '%', '&', '$', '@', '~', ','} AdditionalLineContinuationOprs = {'#', ':', '='} -proc endsWithOpr*(x: string): bool = - # also used be the standard template filter: +proc endsWithOpr*(x: string): bool = + # also used by the standard template filter: result = x.endsWith(LineContinuationOprs) proc continueLine(line: string, inTripleString: bool): bool {.inline.} = @@ -105,15 +104,11 @@ proc continueLine(line: string, inTripleString: bool): bool {.inline.} = line.endsWith(LineContinuationOprs+AdditionalLineContinuationOprs) proc LLreadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int = - var - line: string - L: int - inTripleString = false + var inTripleString = false s.s = "" s.rd = 0 - while true: - line = ReadLineFromStdin(if s.s.len == 0: ">>> " else: "... ") - L = len(line) + var line = newStringOfCap(120) + while ReadLineFromStdin(if s.s.len == 0: ">>> " else: "... ", line): add(s.s, line) add(s.s, "\n") if line.contains("\"\"\""): @@ -139,36 +134,30 @@ proc LLStreamRead(s: PLLStream, buf: pointer, bufLen: int): int = of llsStdIn: result = LLreadFromStdin(s, buf, bufLen) -proc LLStreamReadLine(s: PLLStream): string = +proc LLStreamReadLine(s: PLLStream, line: var string): bool = + setLen(line, 0) case s.kind - of llsNone: - result = "" - of llsString: - result = "" - while s.rd < len(s.s): - case s.s[s.rd + 0] - of '\x0D': + of llsNone: + result = true + of llsString: + while s.rd < len(s.s): + case s.s[s.rd] + of '\x0D': inc(s.rd) - if s.s[s.rd + 0] == '\x0A': inc(s.rd) - break - of '\x0A': + if s.s[s.rd] == '\x0A': inc(s.rd) + break + of '\x0A': inc(s.rd) - break - else: - add(result, s.s[s.rd + 0]) + break + else: + add(line, s.s[s.rd]) inc(s.rd) - of llsFile: - result = readLine(s.f) - of llsStdIn: - result = readLine(stdin) - -proc LLStreamAtEnd(s: PLLStream): bool = - case s.kind - of llsNone: result = true - of llsString: result = s.rd >= len(s.s) - of llsFile: result = endOfFile(s.f) - of llsStdIn: result = false - + result = line.len > 0 or s.rd < len(s.s) + of llsFile: + result = readLine(s.f, line) + of llsStdIn: + result = readLine(stdin, line) + proc LLStreamWrite(s: PLLStream, data: string) = case s.kind of llsNone, llsStdIn: diff --git a/compiler/syntaxes.nim b/compiler/syntaxes.nim index 14f7d7f03..9d4658a0b 100755 --- a/compiler/syntaxes.nim +++ b/compiler/syntaxes.nim @@ -89,12 +89,13 @@ proc parsePipe(filename: string, inputStream: PLLStream): PNode = result = ast.emptyNode var s = LLStreamOpen(filename, fmRead) if s != nil: - var line = LLStreamReadLine(s) + var line = newStringOfCap(80) + discard LLStreamReadLine(s, line) var i = UTF8_Bom(line) - if containsShebang(line, i): - line = LLStreamReadLine(s) + if containsShebang(line, i): + discard LLStreamReadLine(s, line) i = 0 - if (line[i] == '#') and (line[i + 1] == '!'): + if line[i] == '#' and line[i+1] == '!': inc(i, 2) while line[i] in WhiteSpace: inc(i) var q: TParser diff --git a/lib/impure/rdstdin.nim b/lib/impure/rdstdin.nim index bc523187a..5d39186ce 100755 --- a/lib/impure/rdstdin.nim +++ b/lib/impure/rdstdin.nim @@ -14,15 +14,25 @@ ## wanted functionality. when defined(Windows): - proc ReadLineFromStdin*(prompt: string): TaintedString = + proc ReadLineFromStdin*(prompt: string): TaintedString {.deprecated.} = ## Reads a line from stdin. stdout.write(prompt) result = readLine(stdin) + proc ReadLineFromStdin*(prompt: string, line: var TaintedString): bool = + ## Reads a `line` from stdin. `line` must not be + ## ``nil``! May throw an IO exception. + ## A line of text may be delimited by ``CR``, ``LF`` or + ## ``CRLF``. The newline character(s) are not part of the returned string. + ## Returns ``false`` if the end of the file has been reached, ``true`` + ## otherwise. If ``false`` is returned `line` contains no new data. + stdout.write(prompt) + result = readLine(stdin, line) + else: import readline, history - proc ReadLineFromStdin*(prompt: string): TaintedString = + proc ReadLineFromStdin*(prompt: string): TaintedString {.deprecated.} = var buffer = readline.readLine(prompt) if isNil(buffer): quit(0) result = TaintedString($buffer) @@ -30,6 +40,16 @@ else: add_history(buffer) readline.free(buffer) + proc ReadLineFromStdin*(prompt: string, line: var TaintedString): bool = + var buffer = readline.readLine(prompt) + if isNil(buffer): quit(0) + line = TaintedString($buffer) + if line.string.len > 0: + add_history(buffer) + readline.free(buffer) + # XXX how to determine CTRL+D? + result = true + # initialization: # disable auto-complete: proc doNothing(a, b: cint): cint {.cdecl, procvar.} = nil diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 480d25250..3a254dc50 100755 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -150,19 +150,6 @@ proc readStr*(s: PStream, length: int): TaintedString = var L = readData(s, addr(string(result)[0]), length) if L != length: setLen(result.string, L) -proc readLine*(s: PStream): TaintedString {.deprecated.} = - ## Reads a line from a stream `s`. Note: This is not very efficient. Raises - ## `EIO` if an error occured. - ## **Deprecated since version 0.8.14**: Because Posix supports it poorly. - result = TaintedString"" - while not atEnd(s): - var c = readChar(s) - if c == '\c': - c = readChar(s) - break - elif c == '\L' or c == '\0': break - result.string.add(c) - proc readLine*(s: PStream, line: var TaintedString): bool = ## reads a line of text from the stream `s` into `line`. `line` must not be ## ``nil``! May throw an IO exception. @@ -183,6 +170,19 @@ proc readLine*(s: PStream, line: var TaintedString): bool = line.string.add(c) result = true +proc readLine*(s: PStream): TaintedString {.deprecated.} = + ## Reads a line from a stream `s`. Note: This is not very efficient. Raises + ## `EIO` if an error occured. + ## **Deprecated since version 0.8.14**: Because Posix supports it poorly. + result = TaintedString"" + while true: + var c = readChar(s) + if c == '\c': + c = readChar(s) + break + elif c == '\L' or c == '\0': break + result.string.add(c) + type PStringStream* = ref TStringStream ## a stream that encapsulates a string TStringStream* = object of TStream |