|
| 1 | +/** |
| 2 | + * @file Fixtures - Reader |
| 3 | + * @module fixtures/Reader |
| 4 | + */ |
| 5 | + |
| 6 | +import type { Point } from '@flex-development/docast' |
| 7 | +import { at, clamp, fallback, isInteger } from '@flex-development/tutils' |
| 8 | +import { ok } from 'devlop' |
| 9 | +import type { Code } from 'micromark-util-types' |
| 10 | +import type { VFile } from 'vfile' |
| 11 | + |
| 12 | +/** |
| 13 | + * Source document reader. |
| 14 | + * |
| 15 | + * @class |
| 16 | + */ |
| 17 | +class Reader { |
| 18 | + /** |
| 19 | + * Source document. |
| 20 | + * |
| 21 | + * @public |
| 22 | + * @readonly |
| 23 | + * @instance |
| 24 | + * @member {string} document |
| 25 | + */ |
| 26 | + public readonly document: string |
| 27 | + |
| 28 | + /** |
| 29 | + * List, where each index is a line number (`0`-based), and each value is the |
| 30 | + * number of columns in the line. |
| 31 | + * |
| 32 | + * @protected |
| 33 | + * @readonly |
| 34 | + * @instance |
| 35 | + * @member {number[]} indices |
| 36 | + */ |
| 37 | + protected readonly indices: number[] |
| 38 | + |
| 39 | + /** |
| 40 | + * Current position in {@linkcode document}. |
| 41 | + * |
| 42 | + * @protected |
| 43 | + * @instance |
| 44 | + * @member {number} position |
| 45 | + */ |
| 46 | + protected position: number |
| 47 | + |
| 48 | + /** |
| 49 | + * Create a new source document reader. |
| 50 | + * |
| 51 | + * @see {@linkcode VFile} |
| 52 | + * |
| 53 | + * @param {VFile | string} source - Source document or file |
| 54 | + */ |
| 55 | + constructor(source: VFile | string) { |
| 56 | + this.document = source = String(source) |
| 57 | + this.indices = [...source.matchAll(/^.*/gm)].map(([m]) => m.length + 1) |
| 58 | + this.position = 0 |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Check if the reader is at the end of the document. |
| 63 | + * |
| 64 | + * @example |
| 65 | + * ```ts |
| 66 | + * while (!reader.eof) { |
| 67 | + * // do something |
| 68 | + * } |
| 69 | + * ``` |
| 70 | + * |
| 71 | + * @public |
| 72 | + * @instance |
| 73 | + * |
| 74 | + * @return {boolean} `true` if at end of document |
| 75 | + */ |
| 76 | + public get eof(): boolean { |
| 77 | + return this.index >= this.document.length |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get the current position in the document. |
| 82 | + * |
| 83 | + * @public |
| 84 | + * @instance |
| 85 | + * |
| 86 | + * @return {number} Current position in document |
| 87 | + */ |
| 88 | + public get index(): number { |
| 89 | + return this.position |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the next `k`-th character code from the document without changing the |
| 94 | + * position of the reader, with `null` denoting the end of the document. |
| 95 | + * |
| 96 | + * @see {@linkcode Code} |
| 97 | + * |
| 98 | + * @public |
| 99 | + * @instance |
| 100 | + * |
| 101 | + * @param {number} [k=1] - Difference between index of next `k`-th character |
| 102 | + * code and current position in document |
| 103 | + * @return {Code} Peeked character code or `null` |
| 104 | + */ |
| 105 | + public peek(k: number = 1): Code { |
| 106 | + ok(isInteger(k), 'expected k to be an integer') |
| 107 | + return fallback(this.document.codePointAt(this.index + k), null) |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get a [*point*][1] in the document by `offset`. |
| 112 | + * |
| 113 | + * The given `offset` must be an integer in the range `[0, document.length]`.\ |
| 114 | + * If invalid, `point.column` and `point.line` will have a value of `-1`. |
| 115 | + * |
| 116 | + * > **Point** represents one place in a source [*file*][2]. |
| 117 | + * > |
| 118 | + * > The `line` field (`1`-indexed integer) represents a line in a source |
| 119 | + * > file. The `column` field (`1`-indexed integer) represents a column in a |
| 120 | + * > source file. The `offset` field (`0`-indexed integer) represents a |
| 121 | + * > character in a source file. |
| 122 | + * > |
| 123 | + * > The term character means a (UTF-16) code unit which is defined in the |
| 124 | + * > [Web IDL][3] specification. |
| 125 | + * |
| 126 | + * [1]: https://github.com/syntax-tree/unist#point |
| 127 | + * [2]: https://github.com/syntax-tree/unist#file |
| 128 | + * [3]: https://webidl.spec.whatwg.org/ |
| 129 | + * |
| 130 | + * @see {@linkcode Point} |
| 131 | + * @see https://github.com/syntax-tree/unist#point |
| 132 | + * |
| 133 | + * @public |
| 134 | + * @instance |
| 135 | + * |
| 136 | + * @param {number?} [offset=this.index] - Index of character in document |
| 137 | + * @return {Point} Point in document |
| 138 | + */ |
| 139 | + public point(offset: number = this.index): Point { |
| 140 | + /** |
| 141 | + * Current offset (`0`-indexed integer). |
| 142 | + * |
| 143 | + * @var {number} index |
| 144 | + */ |
| 145 | + let index: number = 0 |
| 146 | + |
| 147 | + /** |
| 148 | + * Current line (`1`-indexed integer). |
| 149 | + * |
| 150 | + * @var {number} line |
| 151 | + */ |
| 152 | + let line: number = -1 |
| 153 | + |
| 154 | + /** |
| 155 | + * Current {@linkcode Point} in source document. |
| 156 | + * |
| 157 | + * @const {Point} point |
| 158 | + */ |
| 159 | + const point: Point = { column: -1, line, offset } |
| 160 | + |
| 161 | + // convert offset to point |
| 162 | + if (isInteger(offset) && offset > -1 && offset <= this.document.length) { |
| 163 | + while (++line < this.indices.length) { |
| 164 | + for (let j = 0; j < at(this.indices, line)!; j++) { |
| 165 | + if (index++ === offset) { |
| 166 | + point.column = j + 1 |
| 167 | + point.line = line + 1 |
| 168 | + break |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return point |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Get the next `k`-th character code from the document, with `null` denoting |
| 179 | + * the end of the document. |
| 180 | + * |
| 181 | + * Unlike {@linkcode peek}, this method changes the position of the reader. |
| 182 | + * |
| 183 | + * @see {@linkcode Code} |
| 184 | + * |
| 185 | + * @public |
| 186 | + * @instance |
| 187 | + * |
| 188 | + * @param {number} [k=1] - Difference between index of next `k`-th character |
| 189 | + * code and current position in document |
| 190 | + * @return {Code} Next `k`-th character or `null` |
| 191 | + */ |
| 192 | + public read(k: number = 1): Code { |
| 193 | + ok(isInteger(k), 'expected k to be an integer') |
| 194 | + ok(!this.eof, 'cannot read past end of document') |
| 195 | + this.position = clamp(this.position + k, 0, this.document.length) |
| 196 | + return fallback(this.document.codePointAt(this.position), null) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +export default Reader |
0 commit comments