forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyamlParser07.ts
More file actions
52 lines (47 loc) · 2.07 KB
/
yamlParser07.ts
File metadata and controls
52 lines (47 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Copyright (c) Adam Voss. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { Parser, Composer, Document, LineCounter, ParseOptions, DocumentOptions, SchemaOptions } from 'yaml';
import { YAMLDocument, SingleYAMLDocument } from './yaml-documents';
import { getCustomTags } from './custom-tag-provider';
export { YAMLDocument, SingleYAMLDocument };
export type YamlVersion = '1.1' | '1.2';
export interface ParserOptions {
customTags: string[];
yamlVersion: YamlVersion;
}
export const defaultOptions: ParserOptions = {
customTags: [],
yamlVersion: '1.2',
};
/**
* `yaml-ast-parser-custom-tags` parses the AST and
* returns YAML AST nodes, which are then formatted
* for consumption via the language server.
*/
export function parse(text: string, parserOptions: ParserOptions = defaultOptions): YAMLDocument {
const options: ParseOptions & DocumentOptions & SchemaOptions = {
strict: false,
customTags: getCustomTags(parserOptions.customTags),
version: parserOptions.yamlVersion,
keepSourceTokens: true,
};
const composer = new Composer(options);
const lineCounter = new LineCounter();
const parser = new Parser(lineCounter.addNewLine);
const tokens = parser.parse(text);
const tokensArr = Array.from(tokens);
const docs = composer.compose(tokensArr, true, text.length);
// Generate the SingleYAMLDocs from the AST nodes
const yamlDocs: SingleYAMLDocument[] = Array.from(docs, (doc) => parsedDocToSingleYAMLDocument(doc, lineCounter));
// Consolidate the SingleYAMLDocs
return new YAMLDocument(yamlDocs, tokensArr);
}
function parsedDocToSingleYAMLDocument(parsedDoc: Document, lineCounter: LineCounter): SingleYAMLDocument {
const syd = new SingleYAMLDocument(lineCounter);
syd.internalDocument = parsedDoc;
return syd;
}