lexbase.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2009 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a base object of a lexer with efficient buffer
  10. ## handling. Only at line endings checks are necessary if the buffer
  11. ## needs refilling.
  12. import
  13. strutils, streams
  14. const
  15. EndOfFile* = '\0' ## end of file marker
  16. NewLines* = {'\c', '\L'}
  17. # Buffer handling:
  18. # buf:
  19. # "Example Text\n ha!" bufLen = 17
  20. # ^pos = 0 ^ sentinel = 12
  21. #
  22. type
  23. BaseLexer* = object of RootObj ## the base lexer. Inherit your lexer from
  24. ## this object.
  25. bufpos*: int ## the current position within the buffer
  26. buf*: string ## the buffer itself
  27. input: Stream ## the input stream
  28. lineNumber*: int ## the current line number
  29. sentinel: int
  30. lineStart: int # index of last line start in buffer
  31. offsetBase*: int # use `offsetBase + bufpos` to get the offset
  32. refillChars: set[char]
  33. proc close*(L: var BaseLexer) =
  34. ## closes the base lexer. This closes `L`'s associated stream too.
  35. close(L.input)
  36. proc fillBuffer(L: var BaseLexer) =
  37. var
  38. charsRead, toCopy, s: int # all are in characters,
  39. # not bytes (in case this
  40. # is not the same)
  41. oldBufLen: int
  42. # we know here that pos == L.sentinel, but not if this proc
  43. # is called the first time by initBaseLexer()
  44. assert(L.sentinel + 1 <= L.buf.len)
  45. toCopy = L.buf.len - (L.sentinel + 1)
  46. assert(toCopy >= 0)
  47. if toCopy > 0:
  48. when defined(js) or defined(nimscript):
  49. # nimscript has to be here to avoid compiling other branch (moveMem)
  50. for i in 0 ..< toCopy:
  51. L.buf[i] = L.buf[L.sentinel + 1 + i]
  52. else:
  53. when nimvm:
  54. for i in 0 ..< toCopy:
  55. L.buf[i] = L.buf[L.sentinel + 1 + i]
  56. else:
  57. # "moveMem" handles overlapping regions
  58. moveMem(addr L.buf[0], addr L.buf[L.sentinel + 1], toCopy)
  59. charsRead = L.input.readDataStr(L.buf, toCopy ..< toCopy + L.sentinel + 1)
  60. s = toCopy + charsRead
  61. if charsRead < L.sentinel + 1:
  62. L.buf[s] = EndOfFile # set end marker
  63. L.sentinel = s
  64. else:
  65. # compute sentinel:
  66. dec(s) # BUGFIX (valgrind)
  67. while true:
  68. assert(s < L.buf.len)
  69. while s >= 0 and L.buf[s] notin L.refillChars: dec(s)
  70. if s >= 0:
  71. # we found an appropriate character for a sentinel:
  72. L.sentinel = s
  73. break
  74. else:
  75. # rather than to give up here because the line is too long,
  76. # double the buffer's size and try again:
  77. oldBufLen = L.buf.len
  78. L.buf.setLen(L.buf.len * 2)
  79. charsRead = readDataStr(L.input, L.buf, oldBufLen ..< L.buf.len)
  80. if charsRead < oldBufLen:
  81. L.buf[oldBufLen + charsRead] = EndOfFile
  82. L.sentinel = oldBufLen + charsRead
  83. break
  84. s = L.buf.len - 1
  85. proc fillBaseLexer(L: var BaseLexer, pos: int): int =
  86. assert(pos <= L.sentinel)
  87. if pos < L.sentinel:
  88. result = pos + 1 # nothing to do
  89. else:
  90. fillBuffer(L)
  91. L.offsetBase += pos
  92. L.bufpos = 0
  93. result = 0
  94. proc handleCR*(L: var BaseLexer, pos: int): int =
  95. ## Call this if you scanned over '\c' in the buffer; it returns the
  96. ## position to continue the scanning from. `pos` must be the position
  97. ## of the '\c'.
  98. assert(L.buf[pos] == '\c')
  99. inc(L.lineNumber)
  100. result = fillBaseLexer(L, pos)
  101. if L.buf[result] == '\L':
  102. result = fillBaseLexer(L, result)
  103. L.lineStart = result
  104. proc handleLF*(L: var BaseLexer, pos: int): int =
  105. ## Call this if you scanned over '\L' in the buffer; it returns the
  106. ## position to continue the scanning from. `pos` must be the position
  107. ## of the '\L'.
  108. assert(L.buf[pos] == '\L')
  109. inc(L.lineNumber)
  110. result = fillBaseLexer(L, pos) #L.lastNL := result-1; // BUGFIX: was: result;
  111. L.lineStart = result
  112. proc handleRefillChar*(L: var BaseLexer, pos: int): int =
  113. ## Call this if a terminator character other than a new line is scanned
  114. ## at `pos`; it returns the position to continue the scanning from.
  115. assert(L.buf[pos] in L.refillChars)
  116. result = fillBaseLexer(L, pos) #L.lastNL := result-1; // BUGFIX: was: result;
  117. proc skipUtf8Bom(L: var BaseLexer) =
  118. if (L.buf[0] == '\xEF') and (L.buf[1] == '\xBB') and (L.buf[2] == '\xBF'):
  119. inc(L.bufpos, 3)
  120. inc(L.lineStart, 3)
  121. proc open*(L: var BaseLexer, input: Stream, bufLen: int = 8192;
  122. refillChars: set[char] = NewLines) =
  123. ## inits the BaseLexer with a stream to read from.
  124. assert(bufLen > 0)
  125. assert(input != nil)
  126. L.input = input
  127. L.bufpos = 0
  128. L.offsetBase = 0
  129. L.refillChars = refillChars
  130. L.buf = newString(bufLen)
  131. L.sentinel = bufLen - 1
  132. L.lineStart = 0
  133. L.lineNumber = 1 # lines start at 1
  134. fillBuffer(L)
  135. skipUtf8Bom(L)
  136. proc getColNumber*(L: BaseLexer, pos: int): int =
  137. ## retrieves the current column.
  138. result = abs(pos - L.lineStart)
  139. proc getCurrentLine*(L: BaseLexer, marker: bool = true): string =
  140. ## retrieves the current line.
  141. var i: int
  142. result = ""
  143. i = L.lineStart
  144. while not (L.buf[i] in {'\c', '\L', EndOfFile}):
  145. add(result, L.buf[i])
  146. inc(i)
  147. add(result, "\n")
  148. if marker:
  149. add(result, spaces(getColNumber(L, L.bufpos)) & "^\n")