@@ -2,13 +2,15 @@ import * as YAML from "yaml";
22import { attachComments } from "./attach.ts" ;
33import { createRoot } from "./factories/root.ts" ;
44import Context from "./transforms/context.ts" ;
5+ import { transformDocuments } from "./transforms/document.ts" ;
56import type { ParseOptions , Root } from "./types.ts" ;
67import { removeFakeNodes } from "./utils/remove-fake-nodes.ts" ;
78import { updatePositions } from "./utils/update-positions.ts" ;
89import { YAMLSyntaxError } from "./yaml-syntax-error.ts" ;
910
1011export function parse ( text : string , options ?: ParseOptions ) : Root {
1112 const lineCounter = new YAML . LineCounter ( ) ;
13+ const context = new Context ( text , lineCounter ) ;
1214 const parser = new YAML . Parser ( lineCounter . addNewLine ) ;
1315 const composer = new YAML . Composer ( {
1416 keepSourceTokens : true ,
@@ -17,24 +19,21 @@ export function parse(text: string, options?: ParseOptions): Root {
1719 uniqueKeys : options ?. uniqueKeys ,
1820 lineCounter,
1921 } ) ;
20- const documentNodes : YAML . Document . Parsed [ ] = [ ] ;
21- const cstTokens : YAML . CST . Token [ ] = [ ] ;
22- const context = new Context ( text , lineCounter ) ;
22+ const parsedDocuments : YAML . Document . Parsed [ ] = [ ] ;
23+ const cstTokens = [ ...parser . parse ( text ) ] ;
2324
24- for ( const cst of parser . parse ( text ) ) {
25- cstTokens . push ( cst ) ;
26- for ( const doc of composer . next ( cst ) ) {
27- documentNodes . push ( throwParseError ( doc , context ) ) ;
25+ for ( const parsedDocument of composer . compose ( cstTokens , true , text . length ) ) {
26+ const { errors } = parsedDocument ;
27+ if ( errors . length > 0 ) {
28+ throw new YAMLSyntaxError ( context , errors [ 0 ] ) ;
2829 }
29- }
3030
31- for ( const doc of composer . end ( ) ) {
32- documentNodes . push ( throwParseError ( doc , context ) ) ;
31+ parsedDocuments . push ( parsedDocument ) ;
3332 }
3433
3534 const root = createRoot (
3635 context . transformRange ( [ 0 , text . length ] ) ,
37- context . transformDocuments ( documentNodes , cstTokens ) ,
36+ transformDocuments ( parsedDocuments , cstTokens , context ) ,
3837 context . comments ,
3938 ) ;
4039
@@ -44,11 +43,3 @@ export function parse(text: string, options?: ParseOptions): Root {
4443
4544 return root ;
4645}
47-
48- function throwParseError ( document : YAML . Document . Parsed , context : Context ) {
49- const { errors } = document ;
50- if ( errors . length > 0 ) {
51- throw new YAMLSyntaxError ( context , errors [ 0 ] ) ;
52- }
53- return document ;
54- }
0 commit comments