-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
directives.ts
191 lines (170 loc) · 5.7 KB
/
directives.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { isNode } from '../nodes/identity.js'
import { visit } from '../visit.js'
import type { Document } from './Document.js'
const escapeChars: Record<string, string> = {
'!': '%21',
',': '%2C',
'[': '%5B',
']': '%5D',
'{': '%7B',
'}': '%7D'
}
const escapeTagName = (tn: string) =>
tn.replace(/[!,[\]{}]/g, ch => escapeChars[ch])
export class Directives {
static defaultYaml: Directives['yaml'] = { explicit: false, version: '1.2' }
static defaultTags: Directives['tags'] = { '!!': 'tag:yaml.org,2002:' }
yaml: { version: '1.1' | '1.2' | 'next'; explicit?: boolean }
tags: Record<string, string>
/**
* The directives-end/doc-start marker `---`. If `null`, a marker may still be
* included in the document's stringified representation.
*/
docStart: true | null = null
/** The doc-end marker `...`. */
docEnd = false
/**
* Used when parsing YAML 1.1, where:
* > If the document specifies no directives, it is parsed using the same
* > settings as the previous document. If the document does specify any
* > directives, all directives of previous documents, if any, are ignored.
*/
private atNextDocument?: boolean
constructor(yaml?: Directives['yaml'], tags?: Directives['tags']) {
this.yaml = Object.assign({}, Directives.defaultYaml, yaml)
this.tags = Object.assign({}, Directives.defaultTags, tags)
}
clone(): Directives {
const copy = new Directives(this.yaml, this.tags)
copy.docStart = this.docStart
return copy
}
/**
* During parsing, get a Directives instance for the current document and
* update the stream state according to the current version's spec.
*/
atDocument() {
const res = new Directives(this.yaml, this.tags)
switch (this.yaml.version) {
case '1.1':
this.atNextDocument = true
break
case '1.2':
this.atNextDocument = false
this.yaml = {
explicit: Directives.defaultYaml.explicit,
version: '1.2'
}
this.tags = Object.assign({}, Directives.defaultTags)
break
}
return res
}
/**
* @param onError - May be called even if the action was successful
* @returns `true` on success
*/
add(
line: string,
onError: (offset: number, message: string, warning?: boolean) => void
) {
if (this.atNextDocument) {
this.yaml = { explicit: Directives.defaultYaml.explicit, version: '1.1' }
this.tags = Object.assign({}, Directives.defaultTags)
this.atNextDocument = false
}
const parts = line.trim().split(/[ \t]+/)
const name = parts.shift()
switch (name) {
case '%TAG': {
if (parts.length !== 2) {
onError(0, '%TAG directive should contain exactly two parts')
if (parts.length < 2) return false
}
const [handle, prefix] = parts
this.tags[handle] = prefix
return true
}
case '%YAML': {
this.yaml.explicit = true
if (parts.length !== 1) {
onError(0, '%YAML directive should contain exactly one part')
return false
}
const [version] = parts
if (version === '1.1' || version === '1.2') {
this.yaml.version = version
return true
} else {
const isValid = /^\d+\.\d+$/.test(version)
onError(6, `Unsupported YAML version ${version}`, isValid)
return false
}
}
default:
onError(0, `Unknown directive ${name}`, true)
return false
}
}
/**
* Resolves a tag, matching handles to those defined in %TAG directives.
*
* @returns Resolved tag, which may also be the non-specific tag `'!'` or a
* `'!local'` tag, or `null` if unresolvable.
*/
tagName(source: string, onError: (message: string) => void) {
if (source === '!') return '!' // non-specific tag
if (source[0] !== '!') {
onError(`Not a valid tag: ${source}`)
return null
}
if (source[1] === '<') {
const verbatim = source.slice(2, -1)
if (verbatim === '!' || verbatim === '!!') {
onError(`Verbatim tags aren't resolved, so ${source} is invalid.`)
return null
}
if (source[source.length - 1] !== '>')
onError('Verbatim tags must end with a >')
return verbatim
}
const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/) as string[]
if (!suffix) onError(`The ${source} tag has no suffix`)
const prefix = this.tags[handle]
if (prefix) return prefix + decodeURIComponent(suffix)
if (handle === '!') return source // local tag
onError(`Could not resolve tag: ${source}`)
return null
}
/**
* Given a fully resolved tag, returns its printable string form,
* taking into account current tag prefixes and defaults.
*/
tagString(tag: string) {
for (const [handle, prefix] of Object.entries(this.tags)) {
if (tag.startsWith(prefix))
return handle + escapeTagName(tag.substring(prefix.length))
}
return tag[0] === '!' ? tag : `!<${tag}>`
}
toString(doc?: Document) {
const lines = this.yaml.explicit
? [`%YAML ${this.yaml.version || '1.2'}`]
: []
const tagEntries = Object.entries(this.tags)
let tagNames: string[]
if (doc && tagEntries.length > 0 && isNode(doc.contents)) {
const tags: Record<string, boolean> = {}
visit(doc.contents, (_key, node) => {
if (isNode(node) && node.tag) tags[node.tag] = true
})
tagNames = Object.keys(tags)
} else tagNames = []
for (const [handle, prefix] of tagEntries) {
if (handle === '!!' && prefix === 'tag:yaml.org,2002:') continue
if (!doc || tagNames.some(tn => tn.startsWith(prefix)))
lines.push(`%TAG ${handle} ${prefix}`)
}
return lines.join('\n')
}
}