Skip to content

Commit

Permalink
🔥 Remove IndexMap.merge (#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Jul 8, 2024
1 parent 36a9866 commit 1234d6d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 129 deletions.
10 changes: 10 additions & 0 deletions __snapshots__/packages/core/test-out/parser/string.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 0 additions & 65 deletions __snapshots__/packages/core/test-out/source/IndexMap.spec.js

This file was deleted.

19 changes: 14 additions & 5 deletions packages/core/src/parser/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ export function string(options: StringOptions): InfallibleParser<StringNode> {
value: '',
valueMap: [],
}
let start = src.cursor
let start: number

if (options.quotes?.length && (src.peek() === '"' || src.peek() === "'")) {
const currentQuote = src.read() as Quote
ans.quote = currentQuote
const contentStart = src.cursor
let cStart = src.cursor
start = cStart
while (src.canRead() && src.peek() !== currentQuote) {
const c = src.peek()
if (options.escapable && c === '\\') {
const cStart = src.cursor
src.skip()
const c2 = src.read()
if (
Expand Down Expand Up @@ -73,9 +73,18 @@ export function string(options: StringOptions): InfallibleParser<StringNode> {
})
ans.value += c2
}
cStart = src.cursor
} else {
src.skip()
const cEnd = src.cursor
if (cEnd - cStart > 1) {
ans.valueMap.push({
inner: Range.create(ans.value.length, ans.value.length + 1),
outer: Range.create(cStart, cEnd),
})
}
ans.value += c
cStart = cEnd
}
}

Expand All @@ -86,16 +95,16 @@ export function string(options: StringOptions): InfallibleParser<StringNode> {
if (!options.quotes.includes(currentQuote)) {
ctx.err.report(localize('parser.string.illegal-quote', options.quotes), ans)
}

start = contentStart
} else if (options.unquotable) {
start = src.cursor
while (src.canRead() && isAllowedCharacter(src.peek(), options.unquotable)) {
ans.value += src.read()
}
if (!ans.value && !options.unquotable.allowEmpty) {
ctx.err.report(localize('expected', localize('string')), src)
}
} else {
start = src.cursor
ctx.err.report(localize('expected', options.quotes!), src)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/parser/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { AstNode, ErrorNode } from '../node/index.js'
import { SequenceUtil, SequenceUtilDiscriminator } from '../node/index.js'
import type { ParserContext } from '../service/index.js'
import { ErrorReporter } from '../service/index.js'
import type { ErrorSeverity, ReadonlySource } from '../source/index.js'
import { IndexMap, Range, Source } from '../source/index.js'
import type { ErrorSeverity, IndexMap, ReadonlySource } from '../source/index.js'
import { Range, Source } from '../source/index.js'
import type { InfallibleParser, Parser, Result, Returnable } from './Parser.js'
import { Failure } from './Parser.js'

Expand Down Expand Up @@ -504,7 +504,7 @@ export function concatOnTrailingBackslash<N extends Returnable>(parser: Parser<N
indexMap.push({ inner: Range.create(wrappedStr.length), outer: Range.span(from, to) })
}

const wrappedSrc = new Source(wrappedStr, IndexMap.merge(src.indexMap, indexMap))
const wrappedSrc = new Source(wrappedStr, indexMap)
wrappedSrc.innerCursor = wrappedSrcCursor
const ans = parser(wrappedSrc, ctx)
src.cursor = wrappedSrc.cursor
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/source/IndexMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,4 @@ export namespace IndexMap {
export function toOuterRange(map: IndexMap, inner: Range): Range {
return Range.create(toOuterOffset(map, inner.start), toOuterOffset(map, inner.end))
}

export function merge(outerMap: IndexMap, innerMap: IndexMap): IndexMap {
return innerMap.map((p) => ({ inner: p.inner, outer: toOuterRange(outerMap, p.outer) }))
}
}
61 changes: 9 additions & 52 deletions packages/core/test/source/IndexMap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { strict as assert } from 'assert'
import { describe, it } from 'mocha'
import snapshot from 'snap-shot-it'
import { IndexMap, Range } from '../../lib/index.js'

describe('IndexMap', () => {
Expand All @@ -11,10 +10,11 @@ describe('IndexMap', () => {
* Outer - "foo\"bar\u00a7qux"
* Inner - foo"bar§qux
*/
const map: IndexMap = [{ outer: Range.create(13, 13), inner: Range.create(0, 0) }, {
outer: Range.create(16, 18),
inner: Range.create(3, 4),
}, { outer: Range.create(21, 27), inner: Range.create(7, 8) }]
const map: IndexMap = [
{ outer: Range.create(13, 13), inner: Range.create(0, 0) },
{ outer: Range.create(16, 18), inner: Range.create(3, 4) },
{ outer: Range.create(21, 27), inner: Range.create(7, 8) },
]
const toInnerCases: { input: number; expected: number }[] = [
{ input: 13, expected: 0 },
{ input: 14, expected: 1 },
Expand Down Expand Up @@ -65,49 +65,6 @@ describe('IndexMap', () => {
}
})

describe('merge()', () => {
it('Should merge correctly', () => {
/*
* Index Tens - 0000000000111111111122222222223333333333
* Index Ones - 0123456789012345678901234567890123456789
* Outer - [helloworld::"foo\"bar\u00a7qux"]
* Middle - helloworld::"foo\"bar\u00a7qux"
* Inner - foo"bar§qux
*/
const outerMap: IndexMap = [{ inner: Range.create(0, 0), outer: Range.create(8, 8) }]
const innerMap: IndexMap = [{ inner: Range.create(0, 0), outer: Range.create(13, 13) }, {
inner: Range.create(3, 4),
outer: Range.create(16, 18),
}, { inner: Range.create(7, 8), outer: Range.create(21, 27) }]
const mergedMap = IndexMap.merge(outerMap, innerMap)
snapshot(mergedMap)
})
it('Should merge nested pairs correctly', () => {
/*
* Index Tens - 0000000000111111111122222222223333333333
* Index Ones - 0123456789012345678901234567890123456789
* Outer - "{Command: \"say \\\"hi\\\"\"}"
* Middle - {Command: "say \"hi\""}
* Inner - say "hi"
*/
const outerMap: IndexMap = [
{ inner: Range.create(0, 0), outer: Range.create(8, 8) },
{ inner: Range.create(10, 11), outer: Range.create(18, 20) },
{ inner: Range.create(15, 16), outer: Range.create(24, 26) },
{ inner: Range.create(16, 17), outer: Range.create(26, 28) },
{ inner: Range.create(19, 20), outer: Range.create(30, 32) },
{ inner: Range.create(20, 21), outer: Range.create(32, 34) },
{ inner: Range.create(21, 22), outer: Range.create(34, 36) },
]
const innerMap: IndexMap = [{ inner: Range.create(0, 0), outer: Range.create(11, 11) }, {
inner: Range.create(4, 5),
outer: Range.create(15, 17),
}, { inner: Range.create(7, 8), outer: Range.create(19, 21) }]
const mergedMap = IndexMap.merge(outerMap, innerMap)
snapshot(mergedMap)
})
})

describe('to<Inner/Outer>Range', () => {
describe('foo"bar§qux <=> foo\\"bar\\u00a7qux', () => {
/*
Expand All @@ -116,10 +73,10 @@ describe('IndexMap', () => {
* Outer - foo\"bar\u00a7qux
* Inner - foo"bar§qux
*/
const indexMap = [{ inner: Range.create(3, 4), outer: Range.create(3, 5) }, {
inner: Range.create(7, 8),
outer: Range.create(8, 14),
}]
const indexMap = [
{ inner: Range.create(3, 4), outer: Range.create(3, 5) },
{ inner: Range.create(7, 8), outer: Range.create(8, 14) },
]
const toInnerCases = [
{ input: Range.create(0, 1), expected: Range.create(0, 1), name: '`f` -> `f`' },
{ input: Range.create(3, 5), expected: Range.create(3, 4), name: '`\\"` -> `"`' },
Expand Down

0 comments on commit 1234d6d

Please sign in to comment.