BackwardIterator.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Porting from
  2. // https://github.com/Borvik/vscode-postgres/blob/master/src/common/backwordIterator.ts
  3. const _NL = '\n'.charCodeAt(0)
  4. const _TAB = '\t'.charCodeAt(0)
  5. const _WSB = ' '.charCodeAt(0)
  6. const _LBracket = '['.charCodeAt(0)
  7. const _RBracket = ']'.charCodeAt(0)
  8. const _LCurly = '{'.charCodeAt(0)
  9. const _RCurly = '}'.charCodeAt(0)
  10. const _LParent = '('.charCodeAt(0)
  11. const _RParent = ')'.charCodeAt(0)
  12. const _Comma = ','.charCodeAt(0)
  13. const _Period = '.'.charCodeAt(0)
  14. const _Quote = "'".charCodeAt(0)
  15. const _DQuote = '"'.charCodeAt(0)
  16. const _USC = '_'.charCodeAt(0)
  17. // _a will give undefined... so rename to _aCode
  18. const _aCode = 'a'.charCodeAt(0)
  19. const _z = 'z'.charCodeAt(0)
  20. const _A = 'A'.charCodeAt(0)
  21. const _Z = 'Z'.charCodeAt(0)
  22. const _0 = '0'.charCodeAt(0)
  23. const _9 = '9'.charCodeAt(0)
  24. const BOF = 0
  25. class BackwardIterator {
  26. _line
  27. _text
  28. _lines
  29. model
  30. offset
  31. lineNumber
  32. constructor(model: any, offset: number, lineNumber: number) {
  33. this.model = model
  34. this.offset = offset
  35. this.lineNumber = lineNumber
  36. this._text = model.getValue()
  37. this._lines = this._text.split(/\r?\n/g)
  38. this._line = this._lines[lineNumber]
  39. }
  40. hasNext() {
  41. return this.lineNumber >= 0 || this.offset >= 0
  42. }
  43. isFowardDQuote() {
  44. if (!this.hasForward()) return false
  45. return this.peekForward() === _DQuote
  46. }
  47. isNextDQuote() {
  48. if (!this.hasNext()) return false
  49. return this.peekNext() === _DQuote
  50. }
  51. isNextPeriod() {
  52. if (!this.hasNext()) return false
  53. return this.peekNext() === _Period
  54. }
  55. peekNext() {
  56. if (this.offset < 0) {
  57. if (this.lineNumber > 0) {
  58. return _NL
  59. }
  60. return BOF
  61. }
  62. return this._line.charCodeAt(this.offset)
  63. }
  64. hasForward() {
  65. return this.lineNumber < this._lines.length || this.offset < this._line.length
  66. }
  67. peekForward() {
  68. if (this.offset === this._line.length) {
  69. if (this.lineNumber === this._lines.length) return BOF
  70. return _NL
  71. }
  72. return this._line.charCodeAt(this.offset + 1)
  73. }
  74. next() {
  75. if (this.offset < 0) {
  76. if (this.lineNumber > 0) {
  77. this.lineNumber--
  78. this._line = this._lines[this.lineNumber]
  79. this.offset = this._line.length - 1
  80. return _NL
  81. }
  82. this.lineNumber = -1
  83. return BOF
  84. }
  85. let ch = this._line.charCodeAt(this.offset)
  86. this.offset--
  87. return ch
  88. }
  89. readArguments() {
  90. let parentNesting = 0
  91. let bracketNesting = 0
  92. let curlyNesting = 0
  93. let paramCount = 0
  94. while (this.hasNext()) {
  95. let ch = this.next()
  96. switch (ch) {
  97. case _LParent:
  98. parentNesting--
  99. if (parentNesting < 0) {
  100. return paramCount
  101. }
  102. break
  103. case _RParent:
  104. parentNesting++
  105. break
  106. case _LCurly:
  107. curlyNesting--
  108. break
  109. case _RCurly:
  110. curlyNesting++
  111. break
  112. case _LBracket:
  113. bracketNesting--
  114. break
  115. case _RBracket:
  116. bracketNesting++
  117. break
  118. case _DQuote:
  119. case _Quote:
  120. while (this.hasNext() && ch !== this.next()) {
  121. // find the closing quote or double quote
  122. }
  123. break
  124. case _Comma:
  125. if (!parentNesting && !bracketNesting && !curlyNesting) {
  126. paramCount++
  127. }
  128. break
  129. }
  130. }
  131. return -1
  132. }
  133. readIdent() {
  134. let identStarted = false
  135. let isQuotedIdentifier = false
  136. let ident = ''
  137. while (this.hasNext()) {
  138. // Peek first and check if is part of identifier
  139. let ch = this.peekNext()
  140. if (identStarted && !isQuotedIdentifier && !this._isIdentPart(ch)) break
  141. ch = this.next()
  142. if (!identStarted && isQuotedIdentifier && ch === _DQuote) {
  143. identStarted = true
  144. continue
  145. }
  146. if (!identStarted && (ch === _WSB || ch === _TAB || ch == _NL)) continue
  147. if (!identStarted && (ch === _DQuote || this._isIdentPart(ch))) {
  148. identStarted = true
  149. isQuotedIdentifier = ch === _DQuote
  150. ident = String.fromCharCode(ch) + ident
  151. } else if (identStarted) {
  152. if (isQuotedIdentifier) {
  153. if (ch === BOF) break
  154. ident = String.fromCharCode(ch) + ident
  155. if (ch === _DQuote) break
  156. } else {
  157. ident = String.fromCharCode(ch) + ident
  158. }
  159. }
  160. }
  161. return ident
  162. }
  163. readIdents(maxlvl: number) {
  164. let idents = []
  165. while (maxlvl > 0) {
  166. maxlvl--
  167. let ident = this.readIdent()
  168. if (!ident) {
  169. break
  170. }
  171. idents.push(ident)
  172. if (!this.isNextPeriod()) {
  173. break
  174. }
  175. }
  176. return idents.reverse()
  177. }
  178. _isIdentPart(ch: number) {
  179. return (
  180. ch === _USC || // _
  181. (ch >= _aCode && ch <= _z) || // a-z
  182. (ch >= _A && ch <= _Z) || // A-Z
  183. (ch >= _0 && ch <= _9)
  184. ) // 0-9
  185. }
  186. }
  187. export default BackwardIterator