Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add document composition, with test wrapper at compose/parse-docs
- Loading branch information
Showing
6 changed files
with
84 additions
and
10 deletions.
There are no files selected for viewing
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,39 @@ | ||
import { Document } from '../doc/Document.js' | ||
import type { Options } from '../options.js' | ||
import type * as Parser from '../parse/parser.js' | ||
import { composeNode } from './compose-node.js' | ||
import { resolveEnd } from './resolve-end.js' | ||
import { resolveProps } from './resolve-props.js' | ||
import { StreamDirectives } from './stream-directives.js' | ||
|
||
export function composeDoc( | ||
options: Options | null, | ||
directives: StreamDirectives, | ||
{ offset, start, value, end }: Parser.Document, | ||
onError: (offset: number, message: string, warning?: boolean) => void | ||
) { | ||
const doc = new Document(undefined, options || undefined) as Document.Parsed | ||
doc.version = directives.yaml.version | ||
doc.setSchema() // FIXME: always do this in the constructor | ||
|
||
const props = resolveProps(start, 'doc-start', offset, onError) | ||
if (props.found) doc.directivesEndMarker = true | ||
|
||
let to = offset + props.length | ||
const token = value || { type: 'scalar', offset: to, indent: -1, source: '' } | ||
doc.contents = composeNode(doc, token, props, onError) | ||
if (doc.contents.range) to = doc.contents.range[1] | ||
else { | ||
// FIXME: remove once verified never happens | ||
onError(to, 'Resolved child node has no range') | ||
if (value) { | ||
if ('offset' in value) to = value.offset | ||
if ('source' in value && value.source) to += value.source.length | ||
} | ||
} | ||
const { comment, length } = resolveEnd(end) | ||
if (comment) doc.comment = comment | ||
|
||
doc.range = [offset, to + length] | ||
return doc | ||
} |
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,33 @@ | ||
import { Document } from '../doc/Document' | ||
import { Parser } from '../parse/parser' | ||
import { composeDoc } from './compose-doc' | ||
import { StreamDirectives } from './stream-directives' | ||
|
||
export function parseDocs(source: string) { | ||
const directives = new StreamDirectives() | ||
const docs: Document.Parsed[] = [] | ||
const lines: number[] = [] | ||
|
||
const onError = (offset: number, message: string, warning?: boolean) => { | ||
console.error(warning ? '???' : '!!!', { offset, message }) | ||
} | ||
|
||
const parser = new Parser( | ||
token => { | ||
switch (token.type) { | ||
case 'directive': | ||
directives.add(token.source, onError) | ||
break | ||
case 'document': | ||
docs.push(composeDoc(null, directives, token, onError)) | ||
break | ||
default: | ||
console.log('###', token) | ||
} | ||
}, | ||
n => lines.push(n) | ||
) | ||
parser.parse(source) | ||
|
||
return docs | ||
} |
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