-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from MikeRalphson/lineOffsets
Optionally compute lineOffsets, fixes #67
- Loading branch information
Showing
10 changed files
with
181 additions
and
33 deletions.
There are no files selected for viewing
Submodule docs
updated
8 files
+34 −1 | source/includes/content_nodes.md | |
+4 −0 | source/includes/cst_parser.md | |
+31 −19 | source/includes/documents.md | |
+3 −3 | source/includes/intro.md | |
+1 −1 | source/includes/options.md | |
+1 −1 | source/includes/parse_stringify.md | |
+2 −2 | source/includes/yaml_syntax.md | |
+3 −3 | source/index.html.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
function findLineStarts(src) { | ||
const ls = [0] | ||
let offset = src.indexOf('\n') | ||
while (offset !== -1) { | ||
offset += 1 | ||
ls.push(offset) | ||
offset = src.indexOf('\n', offset) | ||
} | ||
return ls | ||
} | ||
|
||
/** | ||
* Determine the line/col position matching a character offset. | ||
* | ||
* Accepts a source string or a CST document as the second parameter. With | ||
* the latter, starting indices for lines are cached in the document as | ||
* `lineStarts: number[]`. | ||
* | ||
* Returns a zero-indexed `{ line, col }` location if found, or | ||
* `undefined` otherwise. | ||
* | ||
* @param {number} offset | ||
* @param {string|Document|Document[]} cst | ||
* @returns {{ line: number, col: number }|undefined} | ||
*/ | ||
export default function getLinePos(offset, cst) { | ||
if (typeof offset === 'number' && offset >= 0) { | ||
let lineStarts, srcLength | ||
if (typeof cst === 'string') { | ||
lineStarts = findLineStarts(cst) | ||
srcLength = cst.length | ||
} else { | ||
if (Array.isArray(cst)) cst = cst[0] | ||
if (cst) { | ||
if (!cst.lineStarts) cst.lineStarts = findLineStarts(cst.context.src) | ||
lineStarts = cst.lineStarts | ||
srcLength = cst.context.src.length | ||
} | ||
} | ||
if (lineStarts && offset <= srcLength) { | ||
for (let i = 0; i < lineStarts.length; ++i) { | ||
const start = lineStarts[i] | ||
if (offset < start) { | ||
const line = i - 1 | ||
return { line, col: offset - lineStarts[line] } | ||
} | ||
if (offset === start) return { line: i, col: 0 } | ||
} | ||
const line = lineStarts.length - 1 | ||
return { line, col: offset - lineStarts[line] } | ||
} | ||
} | ||
return undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import getLinePos from '../../src/cst/getLinePos' | ||
import parse from '../../src/cst/parse' | ||
|
||
test('lineStarts for empty document', () => { | ||
const src = '' | ||
const cst = parse(src) | ||
expect(() => getLinePos(0, cst)).not.toThrow() | ||
expect(cst[0].lineStarts).toMatchObject([0]) | ||
}) | ||
|
||
test('lineStarts for multiple documents', () => { | ||
const src = 'foo\n...\nbar\n' | ||
const cst = parse(src) | ||
expect(() => getLinePos(0, cst)).not.toThrow() | ||
expect(cst[0].lineStarts).toMatchObject([0, 4, 8, 12]) | ||
}) | ||
|
||
test('lineStarts for malformed document', () => { | ||
const src = '- foo\n\t- bar\n' | ||
const cst = parse(src) | ||
expect(() => getLinePos(0, cst)).not.toThrow() | ||
expect(cst[0].lineStarts).toMatchObject([0, 6, 13]) | ||
}) | ||
|
||
test('getLinePos()', () => { | ||
const src = '- foo\n- bar\n' | ||
const cst = parse(src) | ||
expect(cst[0].lineStarts).toBeUndefined() | ||
expect(getLinePos(0, cst)).toMatchObject({ line: 0, col: 0 }) | ||
expect(getLinePos(1, cst)).toMatchObject({ line: 0, col: 1 }) | ||
expect(getLinePos(2, cst)).toMatchObject({ line: 0, col: 2 }) | ||
expect(getLinePos(5, cst)).toMatchObject({ line: 0, col: 5 }) | ||
expect(getLinePos(6, cst)).toMatchObject({ line: 1, col: 0 }) | ||
expect(getLinePos(7, cst)).toMatchObject({ line: 1, col: 1 }) | ||
expect(getLinePos(11, cst)).toMatchObject({ line: 1, col: 5 }) | ||
expect(getLinePos(12, cst)).toMatchObject({ line: 2, col: 0 }) | ||
expect(cst[0].lineStarts).toMatchObject([0, 6, 12]) | ||
}) | ||
|
||
test('invalid args for getLinePos()', () => { | ||
const src = '- foo\n- bar\n' | ||
const cst = parse(src) | ||
expect(getLinePos()).toBeUndefined() | ||
expect(getLinePos(0)).toBeUndefined() | ||
expect(getLinePos(1)).toBeUndefined() | ||
expect(getLinePos(-1, cst)).toBeUndefined() | ||
expect(getLinePos(13, cst)).toBeUndefined() | ||
expect(getLinePos(Math.MAXINT, cst)).toBeUndefined() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Node from '../../src/cst/Node' | ||
import YAML from '../../src/index' | ||
|
||
test('eemeli/yaml#6', () => { | ||
const src = 'abc: 123\ndef' | ||
const doc = YAML.parseDocument(src) | ||
expect(doc.errors).toMatchObject([{ name: 'YAMLSemanticError' }]) | ||
const node = doc.errors[0].source | ||
expect(node).toBeInstanceOf(Node) | ||
expect(node.rangeAsLinePos).toMatchObject({ | ||
start: { line: 1, col: 0 }, | ||
end: { line: 1, col: 3 } | ||
}) | ||
}) | ||
|
||
describe('eemeli/yaml#7', () => { | ||
test('map', () => { | ||
const src = '{ , }\n---\n{ 123,,, }\n' | ||
const docs = YAML.parseAllDocuments(src) | ||
expect(docs[0].errors).toMatchObject([{ name: 'YAMLSyntaxError' }]) | ||
expect(docs[1].errors).toMatchObject([ | ||
{ name: 'YAMLSyntaxError' }, | ||
{ name: 'YAMLSyntaxError' } | ||
]) | ||
const node = docs[0].errors[0].source | ||
expect(node).toBeInstanceOf(Node) | ||
expect(node.rangeAsLinePos).toMatchObject({ | ||
start: { line: 0, col: 0 }, | ||
end: { line: 0, col: 5 } | ||
}) | ||
}) | ||
test('seq', () => { | ||
const src = '[ , ]\n---\n[ 123,,, ]\n' | ||
const docs = YAML.parseAllDocuments(src) | ||
expect(docs[0].errors).toMatchObject([{ name: 'YAMLSyntaxError' }]) | ||
expect(docs[1].errors).toMatchObject([ | ||
{ name: 'YAMLSyntaxError' }, | ||
{ name: 'YAMLSyntaxError' } | ||
]) | ||
const node = docs[1].errors[0].source | ||
expect(node).toBeInstanceOf(Node) | ||
expect(node.rangeAsLinePos).toMatchObject({ | ||
start: { line: 2, col: 0 }, | ||
end: { line: 2, col: 10 } | ||
}) | ||
}) | ||
}) | ||
|
||
test('eemeli/yaml#8', () => { | ||
const src = '{' | ||
const doc = YAML.parseDocument(src) | ||
expect(doc.errors).toMatchObject([{ name: 'YAMLSemanticError' }]) | ||
const node = doc.errors[0].source | ||
expect(node).toBeInstanceOf(Node) | ||
expect(node.rangeAsLinePos).toMatchObject({ | ||
start: { line: 0, col: 0 }, | ||
end: { line: 0, col: 1 } | ||
}) | ||
}) |