diff --git a/src/parse/lexer.ts b/src/parse/lexer.ts index f0e60552..af06ee54 100644 --- a/src/parse/lexer.ts +++ b/src/parse/lexer.ts @@ -310,14 +310,11 @@ export class Lexer { if (ch === '-' || ch === '.') { if (!this.atEnd && !this.hasChars(4)) return this.setNext('line-start') const s = this.peek(3) - if (s === '---' && isEmpty(this.charAt(3))) { + if ((s === '---' || s === '...') && isEmpty(this.charAt(3))) { yield* this.pushCount(3) this.indentValue = 0 this.indentNext = 0 - return 'doc' - } else if (s === '...' && isEmpty(this.charAt(3))) { - yield* this.pushCount(3) - return 'stream' + return s === '---' ? 'doc' : 'stream' } } this.indentValue = yield* this.pushSpaces(false) diff --git a/tests/lexer.ts b/tests/lexer.ts index c4efb581..58f473c3 100644 --- a/tests/lexer.ts +++ b/tests/lexer.ts @@ -1,4 +1,6 @@ -import { Lexer } from 'yaml' +import { CST, Lexer } from 'yaml' + +const { DOCUMENT: DOC, SCALAR } = CST test('unexpected unindent in quoted string with CRLF', () => { const src = '- "\r\nx"' @@ -8,23 +10,29 @@ test('unexpected unindent in quoted string with CRLF', () => { res.push(lex) if (++n === 10) break } - expect(res).toEqual(['\u0002', '-', ' ', '"', '\r\n', '\u001f', 'x"']) + expect(res).toEqual([DOC, '-', ' ', '"', '\r\n', SCALAR, 'x"']) }) test('plain scalar + CRLF + comment', () => { const src = 'foo\r\n# bar' const res = Array.from(new Lexer().lex(src)) - expect(res).toEqual(['\u0002', '\u001f', 'foo', '\r\n', '# bar']) + expect(res).toEqual([DOC, SCALAR, 'foo', '\r\n', '# bar']) }) test('Scalar starting with : after flow-ish key in preceding node in flow collection', () => { const src = '[[], :@]' const res = Array.from(new Lexer().lex(src)) - expect(res).toEqual(['\u0002', '[', '[', ']', ',', ' ', '\u001f', ':@', ']']) + expect(res).toEqual([DOC, '[', '[', ']', ',', ' ', SCALAR, ':@', ']']) }) test('unindented comment in flow collection', () => { const src = '- {\n#c\n }' const res = Array.from(new Lexer().lex(src)) - expect(res).toEqual(['\u0002', '-', ' ', '{', '\n', '#c', '\n', ' ', '}']) + expect(res).toEqual([DOC, '-', ' ', '{', '\n', '#c', '\n', ' ', '}']) +}) + +test('indented map + ... + unindented flow collection', () => { + const src = ' :\n...\n{\n}' + const res = Array.from(new Lexer().lex(src)) + expect(res).toEqual([DOC, ' ', ':', '\n', '...', '\n', DOC, '{', '\n', '}']) })