1 |
|
2 | Inline = require './Inline'
|
3 | Pattern = require './Pattern'
|
4 | Utils = require './Utils'
|
5 | ParseException = require './Exception/ParseException'
|
6 |
|
7 |
|
8 |
|
9 | class Parser
|
10 |
|
11 |
|
12 |
|
13 | PATTERN_FOLDED_SCALAR_ALL: new Pattern '^(?:(?<type>![^\\|>]*)\\s+)?(?<separator>\\||>)(?<modifiers>\\+|\\-|\\d+|\\+\\d+|\\-\\d+|\\d+\\+|\\d+\\-)?(?<comments> +#.*)?$'
|
14 | PATTERN_FOLDED_SCALAR_END: new Pattern '(?<separator>\\||>)(?<modifiers>\\+|\\-|\\d+|\\+\\d+|\\-\\d+|\\d+\\+|\\d+\\-)?(?<comments> +#.*)?$'
|
15 | PATTERN_SEQUENCE_ITEM: new Pattern '^\\-((?<leadspaces>\\s+)(?<value>.+?))?\\s*$'
|
16 | PATTERN_ANCHOR_VALUE: new Pattern '^&(?<ref>[^ ]+) *(?<value>.*)'
|
17 | PATTERN_COMPACT_NOTATION: new Pattern '^(?<key>'+Inline.REGEX_QUOTED_STRING+'|[^ \'"\\{\\[].*?) *\\:(\\s+(?<value>.+?))?\\s*$'
|
18 | PATTERN_MAPPING_ITEM: new Pattern '^(?<key>'+Inline.REGEX_QUOTED_STRING+'|[^ \'"\\[\\{].*?) *\\:(\\s+(?<value>.+?))?\\s*$'
|
19 | PATTERN_DECIMAL: new Pattern '\\d+'
|
20 | PATTERN_INDENT_SPACES: new Pattern '^ +'
|
21 | PATTERN_TRAILING_LINES: new Pattern '(\n*)$'
|
22 | PATTERN_YAML_HEADER: new Pattern '^\\%YAML[: ][\\d\\.]+.*\n'
|
23 | PATTERN_LEADING_COMMENTS: new Pattern '^(\\#.*?\n)+'
|
24 | PATTERN_DOCUMENT_MARKER_START: new Pattern '^\\-\\-\\-.*?\n'
|
25 | PATTERN_DOCUMENT_MARKER_END: new Pattern '^\\.\\.\\.\\s*$'
|
26 | PATTERN_FOLDED_SCALAR_BY_INDENTATION: {}
|
27 |
|
28 |
|
29 |
|
30 | CONTEXT_NONE: 0
|
31 | CONTEXT_SEQUENCE: 1
|
32 | CONTEXT_MAPPING: 2
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | constructor: (@offset = 0) ->
|
40 | @lines = []
|
41 | @currentLineNb = -1
|
42 | @currentLine = ''
|
43 | @refs = {}
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | parse: (value, exceptionOnInvalidType = false, objectDecoder = null) ->
|
57 | @currentLineNb = -1
|
58 | @currentLine = ''
|
59 | @lines = @cleanup(value).split "\n"
|
60 |
|
61 | data = null
|
62 | context = @CONTEXT_NONE
|
63 | allowOverwrite = false
|
64 | while @moveToNextLine()
|
65 | if @isCurrentLineEmpty()
|
66 | continue
|
67 |
|
68 |
|
69 | if "\t" is @currentLine[0]
|
70 | throw new ParseException 'A YAML file cannot contain tabs as indentation.', @getRealCurrentLineNb() + 1, @currentLine
|
71 |
|
72 | isRef = mergeNode = false
|
73 | if values = @PATTERN_SEQUENCE_ITEM.exec @currentLine
|
74 | if @CONTEXT_MAPPING is context
|
75 | throw new ParseException 'You cannot define a sequence item when in a mapping'
|
76 | context = @CONTEXT_SEQUENCE
|
77 | data ?= []
|
78 |
|
79 | if values.value? and matches = @PATTERN_ANCHOR_VALUE.exec values.value
|
80 | isRef = matches.ref
|
81 | values.value = matches.value
|
82 |
|
83 |
|
84 | if not(values.value?) or '' is Utils.trim(values.value, ' ') or Utils.ltrim(values.value, ' ').indexOf('#') is 0
|
85 | if @currentLineNb < @lines.length - 1 and not @isNextLineUnIndentedCollection()
|
86 | c = @getRealCurrentLineNb() + 1
|
87 | parser = new Parser c
|
88 | parser.refs = @refs
|
89 | data.push parser.parse(@getNextEmbedBlock(null, true), exceptionOnInvalidType, objectDecoder)
|
90 | else
|
91 | data.push null
|
92 |
|
93 | else
|
94 | if values.leadspaces?.length and matches = @PATTERN_COMPACT_NOTATION.exec values.value
|
95 |
|
96 |
|
97 | c = @getRealCurrentLineNb()
|
98 | parser = new Parser c
|
99 | parser.refs = @refs
|
100 |
|
101 | block = values.value
|
102 | indent = @getCurrentLineIndentation()
|
103 | if @isNextLineIndented(false)
|
104 | block += "\n"+@getNextEmbedBlock(indent + values.leadspaces.length + 1, true)
|
105 |
|
106 | data.push parser.parse block, exceptionOnInvalidType, objectDecoder
|
107 |
|
108 | else
|
109 | data.push @parseValue values.value, exceptionOnInvalidType, objectDecoder
|
110 |
|
111 | else if (values = @PATTERN_MAPPING_ITEM.exec @currentLine) and values.key.indexOf(' #') is -1
|
112 | if @CONTEXT_SEQUENCE is context
|
113 | throw new ParseException 'You cannot define a mapping item when in a sequence'
|
114 | context = @CONTEXT_MAPPING
|
115 | data ?= {}
|
116 |
|
117 |
|
118 | Inline.configure exceptionOnInvalidType, objectDecoder
|
119 | try
|
120 | key = Inline.parseScalar values.key
|
121 | catch e
|
122 | e.parsedLine = @getRealCurrentLineNb() + 1
|
123 | e.snippet = @currentLine
|
124 |
|
125 | throw e
|
126 |
|
127 | if '<<' is key
|
128 | mergeNode = true
|
129 | allowOverwrite = true
|
130 | if values.value?.indexOf('*') is 0
|
131 | refName = values.value[1..]
|
132 | unless @refs[refName]?
|
133 | throw new ParseException 'Reference "'+refName+'" does not exist.', @getRealCurrentLineNb() + 1, @currentLine
|
134 |
|
135 | refValue = @refs[refName]
|
136 |
|
137 | if typeof refValue isnt 'object'
|
138 | throw new ParseException 'YAML merge keys used with a scalar value instead of an object.', @getRealCurrentLineNb() + 1, @currentLine
|
139 |
|
140 | if refValue instanceof Array
|
141 |
|
142 | for value, i in refValue
|
143 | data[String(i)] ?= value
|
144 | else
|
145 |
|
146 | for key, value of refValue
|
147 | data[key] ?= value
|
148 |
|
149 | else
|
150 | if values.value? and values.value isnt ''
|
151 | value = values.value
|
152 | else
|
153 | value = @getNextEmbedBlock()
|
154 |
|
155 | c = @getRealCurrentLineNb() + 1
|
156 | parser = new Parser c
|
157 | parser.refs = @refs
|
158 | parsed = parser.parse value, exceptionOnInvalidType
|
159 |
|
160 | unless typeof parsed is 'object'
|
161 | throw new ParseException 'YAML merge keys used with a scalar value instead of an object.', @getRealCurrentLineNb() + 1, @currentLine
|
162 |
|
163 | if parsed instanceof Array
|
164 |
|
165 |
|
166 |
|
167 | for parsedItem in parsed
|
168 | unless typeof parsedItem is 'object'
|
169 | throw new ParseException 'Merge items must be objects.', @getRealCurrentLineNb() + 1, parsedItem
|
170 |
|
171 | if parsedItem instanceof Array
|
172 |
|
173 | for value, i in parsedItem
|
174 | k = String(i)
|
175 | unless data.hasOwnProperty(k)
|
176 | data[k] = value
|
177 | else
|
178 |
|
179 | for key, value of parsedItem
|
180 | unless data.hasOwnProperty(key)
|
181 | data[key] = value
|
182 |
|
183 | else
|
184 |
|
185 |
|
186 | for key, value of parsed
|
187 | unless data.hasOwnProperty(key)
|
188 | data[key] = value
|
189 |
|
190 | else if values.value? and matches = @PATTERN_ANCHOR_VALUE.exec values.value
|
191 | isRef = matches.ref
|
192 | values.value = matches.value
|
193 |
|
194 |
|
195 | if mergeNode
|
196 |
|
197 | else if not(values.value?) or '' is Utils.trim(values.value, ' ') or Utils.ltrim(values.value, ' ').indexOf('#') is 0
|
198 |
|
199 |
|
200 | if not(@isNextLineIndented()) and not(@isNextLineUnIndentedCollection())
|
201 |
|
202 |
|
203 | if allowOverwrite or data[key] is undefined
|
204 | data[key] = null
|
205 |
|
206 | else
|
207 | c = @getRealCurrentLineNb() + 1
|
208 | parser = new Parser c
|
209 | parser.refs = @refs
|
210 | val = parser.parse @getNextEmbedBlock(), exceptionOnInvalidType, objectDecoder
|
211 |
|
212 |
|
213 |
|
214 | if allowOverwrite or data[key] is undefined
|
215 | data[key] = val
|
216 |
|
217 | else
|
218 | val = @parseValue values.value, exceptionOnInvalidType, objectDecoder
|
219 |
|
220 |
|
221 |
|
222 | if allowOverwrite or data[key] is undefined
|
223 | data[key] = val
|
224 |
|
225 | else
|
226 |
|
227 | lineCount = @lines.length
|
228 | if 1 is lineCount or (2 is lineCount and Utils.isEmpty(@lines[1]))
|
229 | try
|
230 | value = Inline.parse @lines[0], exceptionOnInvalidType, objectDecoder
|
231 | catch e
|
232 | e.parsedLine = @getRealCurrentLineNb() + 1
|
233 | e.snippet = @currentLine
|
234 |
|
235 | throw e
|
236 |
|
237 | if typeof value is 'object'
|
238 | if value instanceof Array
|
239 | first = value[0]
|
240 | else
|
241 | for key of value
|
242 | first = value[key]
|
243 | break
|
244 |
|
245 | if typeof first is 'string' and first.indexOf('*') is 0
|
246 | data = []
|
247 | for alias in value
|
248 | data.push @refs[alias[1..]]
|
249 | value = data
|
250 |
|
251 | return value
|
252 |
|
253 | else if Utils.ltrim(value).charAt(0) in ['[', '{']
|
254 | try
|
255 | return Inline.parse value, exceptionOnInvalidType, objectDecoder
|
256 | catch e
|
257 | e.parsedLine = @getRealCurrentLineNb() + 1
|
258 | e.snippet = @currentLine
|
259 |
|
260 | throw e
|
261 |
|
262 | throw new ParseException 'Unable to parse.', @getRealCurrentLineNb() + 1, @currentLine
|
263 |
|
264 | if isRef
|
265 | if data instanceof Array
|
266 | @refs[isRef] = data[data.length-1]
|
267 | else
|
268 | lastKey = null
|
269 | for key of data
|
270 | lastKey = key
|
271 | @refs[isRef] = data[lastKey]
|
272 |
|
273 |
|
274 | if Utils.isEmpty(data)
|
275 | return null
|
276 | else
|
277 | return data
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | getRealCurrentLineNb: ->
|
286 | return @currentLineNb + @offset
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | getCurrentLineIndentation: ->
|
294 | return @currentLine.length - Utils.ltrim(@currentLine, ' ').length
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | getNextEmbedBlock: (indentation = null, includeUnindentedCollection = false) ->
|
306 | @moveToNextLine()
|
307 |
|
308 | if not indentation?
|
309 | newIndent = @getCurrentLineIndentation()
|
310 |
|
311 | unindentedEmbedBlock = @isStringUnIndentedCollectionItem @currentLine
|
312 |
|
313 | if not(@isCurrentLineEmpty()) and 0 is newIndent and not(unindentedEmbedBlock)
|
314 | throw new ParseException 'Indentation problem.', @getRealCurrentLineNb() + 1, @currentLine
|
315 |
|
316 | else
|
317 | newIndent = indentation
|
318 |
|
319 |
|
320 | data = [@currentLine[newIndent..]]
|
321 |
|
322 | unless includeUnindentedCollection
|
323 | isItUnindentedCollection = @isStringUnIndentedCollectionItem @currentLine
|
324 |
|
325 |
|
326 |
|
327 | removeCommentsPattern = @PATTERN_FOLDED_SCALAR_END
|
328 | removeComments = not removeCommentsPattern.test @currentLine
|
329 |
|
330 | while @moveToNextLine()
|
331 | indent = @getCurrentLineIndentation()
|
332 |
|
333 | if indent is newIndent
|
334 | removeComments = not removeCommentsPattern.test @currentLine
|
335 |
|
336 | if isItUnindentedCollection and not @isStringUnIndentedCollectionItem(@currentLine) and indent is newIndent
|
337 | @moveToPreviousLine()
|
338 | break
|
339 |
|
340 | if @isCurrentLineBlank()
|
341 | data.push @currentLine[newIndent..]
|
342 | continue
|
343 |
|
344 | if removeComments and @isCurrentLineComment()
|
345 | if indent is newIndent
|
346 | continue
|
347 |
|
348 | if indent >= newIndent
|
349 | data.push @currentLine[newIndent..]
|
350 | else if Utils.ltrim(@currentLine).charAt(0) is '#'
|
351 |
|
352 | else if 0 is indent
|
353 | @moveToPreviousLine()
|
354 | break
|
355 | else
|
356 | throw new ParseException 'Indentation problem.', @getRealCurrentLineNb() + 1, @currentLine
|
357 |
|
358 |
|
359 | return data.join "\n"
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 | moveToNextLine: ->
|
367 | if @currentLineNb >= @lines.length - 1
|
368 | return false
|
369 |
|
370 | @currentLine = @lines[++@currentLineNb];
|
371 |
|
372 | return true
|
373 |
|
374 |
|
375 |
|
376 |
|
377 | moveToPreviousLine: ->
|
378 | @currentLine = @lines[--@currentLineNb]
|
379 | return
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | parseValue: (value, exceptionOnInvalidType, objectDecoder) ->
|
393 | if 0 is value.indexOf('*')
|
394 | pos = value.indexOf '#'
|
395 | if pos isnt -1
|
396 | value = value.substr(1, pos-2)
|
397 | else
|
398 | value = value[1..]
|
399 |
|
400 | if @refs[value] is undefined
|
401 | throw new ParseException 'Reference "'+value+'" does not exist.', @currentLine
|
402 |
|
403 | return @refs[value]
|
404 |
|
405 |
|
406 | if matches = @PATTERN_FOLDED_SCALAR_ALL.exec value
|
407 | modifiers = matches.modifiers ? ''
|
408 |
|
409 | foldedIndent = Math.abs(parseInt(modifiers))
|
410 | if isNaN(foldedIndent) then foldedIndent = 0
|
411 | val = @parseFoldedScalar matches.separator, @PATTERN_DECIMAL.replace(modifiers, ''), foldedIndent
|
412 | if matches.type?
|
413 |
|
414 | Inline.configure exceptionOnInvalidType, objectDecoder
|
415 | return Inline.parseScalar matches.type+' '+val
|
416 | else
|
417 | return val
|
418 |
|
419 | try
|
420 | return Inline.parse value, exceptionOnInvalidType, objectDecoder
|
421 | catch e
|
422 |
|
423 | if value.charAt(0) in ['[', '{'] and e instanceof ParseException and @isNextLineIndented()
|
424 | value += "\n" + @getNextEmbedBlock()
|
425 | try
|
426 | return Inline.parse value, exceptionOnInvalidType, objectDecoder
|
427 | catch e
|
428 | e.parsedLine = @getRealCurrentLineNb() + 1
|
429 | e.snippet = @currentLine
|
430 |
|
431 | throw e
|
432 |
|
433 | else
|
434 | e.parsedLine = @getRealCurrentLineNb() + 1
|
435 | e.snippet = @currentLine
|
436 |
|
437 | throw e
|
438 |
|
439 | return
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 | parseFoldedScalar: (separator, indicator = '', indentation = 0) ->
|
451 | notEOF = @moveToNextLine()
|
452 | if not notEOF
|
453 | return ''
|
454 |
|
455 | isCurrentLineBlank = @isCurrentLineBlank()
|
456 | text = ''
|
457 |
|
458 |
|
459 | while notEOF and isCurrentLineBlank
|
460 |
|
461 | if notEOF = @moveToNextLine()
|
462 | text += "\n"
|
463 | isCurrentLineBlank = @isCurrentLineBlank()
|
464 |
|
465 |
|
466 |
|
467 | if 0 is indentation
|
468 | if matches = @PATTERN_INDENT_SPACES.exec @currentLine
|
469 | indentation = matches[0].length
|
470 |
|
471 |
|
472 | if indentation > 0
|
473 | pattern = @PATTERN_FOLDED_SCALAR_BY_INDENTATION[indentation]
|
474 | unless pattern?
|
475 | pattern = new Pattern '^ {'+indentation+'}(.*)$'
|
476 | Parser::PATTERN_FOLDED_SCALAR_BY_INDENTATION[indentation] = pattern
|
477 |
|
478 | while notEOF and (isCurrentLineBlank or matches = pattern.exec @currentLine)
|
479 | if isCurrentLineBlank
|
480 | text += @currentLine[indentation..]
|
481 | else
|
482 | text += matches[1]
|
483 |
|
484 |
|
485 | if notEOF = @moveToNextLine()
|
486 | text += "\n"
|
487 | isCurrentLineBlank = @isCurrentLineBlank()
|
488 |
|
489 | else if notEOF
|
490 | text += "\n"
|
491 |
|
492 |
|
493 | if notEOF
|
494 | @moveToPreviousLine()
|
495 |
|
496 |
|
497 |
|
498 | if '>' is separator
|
499 | newText = ''
|
500 | for line in text.split "\n"
|
501 | if line.length is 0 or line.charAt(0) is ' '
|
502 | newText = Utils.rtrim(newText, ' ') + line + "\n"
|
503 | else
|
504 | newText += line + ' '
|
505 | text = newText
|
506 |
|
507 | if '+' isnt indicator
|
508 |
|
509 | text = Utils.rtrim(text)
|
510 |
|
511 |
|
512 | if '' is indicator
|
513 | text = @PATTERN_TRAILING_LINES.replace text, "\n"
|
514 | else if '-' is indicator
|
515 | text = @PATTERN_TRAILING_LINES.replace text, ''
|
516 |
|
517 | return text
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 | isNextLineIndented: (ignoreComments = true) ->
|
525 | currentIndentation = @getCurrentLineIndentation()
|
526 | EOF = not @moveToNextLine()
|
527 |
|
528 | if ignoreComments
|
529 | while not(EOF) and @isCurrentLineEmpty()
|
530 | EOF = not @moveToNextLine()
|
531 | else
|
532 | while not(EOF) and @isCurrentLineBlank()
|
533 | EOF = not @moveToNextLine()
|
534 |
|
535 | if EOF
|
536 | return false
|
537 |
|
538 | ret = false
|
539 | if @getCurrentLineIndentation() > currentIndentation
|
540 | ret = true
|
541 |
|
542 | @moveToPreviousLine()
|
543 |
|
544 | return ret
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | isCurrentLineEmpty: ->
|
552 | trimmedLine = Utils.trim(@currentLine, ' ')
|
553 | return trimmedLine.length is 0 or trimmedLine.charAt(0) is '#'
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 | isCurrentLineBlank: ->
|
561 | return '' is Utils.trim(@currentLine, ' ')
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 | isCurrentLineComment: ->
|
569 |
|
570 | ltrimmedLine = Utils.ltrim(@currentLine, ' ')
|
571 |
|
572 | return ltrimmedLine.charAt(0) is '#'
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 | cleanup: (value) ->
|
582 | if value.indexOf("\r") isnt -1
|
583 | value = value.split("\r\n").join("\n").split("\r").join("\n")
|
584 |
|
585 |
|
586 | count = 0
|
587 | [value, count] = @PATTERN_YAML_HEADER.replaceAll value, ''
|
588 | @offset += count
|
589 |
|
590 |
|
591 | [trimmedValue, count] = @PATTERN_LEADING_COMMENTS.replaceAll value, '', 1
|
592 | if count is 1
|
593 |
|
594 | @offset += Utils.subStrCount(value, "\n") - Utils.subStrCount(trimmedValue, "\n")
|
595 | value = trimmedValue
|
596 |
|
597 |
|
598 | [trimmedValue, count] = @PATTERN_DOCUMENT_MARKER_START.replaceAll value, '', 1
|
599 | if count is 1
|
600 |
|
601 | @offset += Utils.subStrCount(value, "\n") - Utils.subStrCount(trimmedValue, "\n")
|
602 | value = trimmedValue
|
603 |
|
604 |
|
605 | value = @PATTERN_DOCUMENT_MARKER_END.replace value, ''
|
606 |
|
607 |
|
608 | lines = value.split("\n")
|
609 | smallestIndent = -1
|
610 | for line in lines
|
611 | continue if Utils.trim(line, ' ').length == 0
|
612 | indent = line.length - Utils.ltrim(line).length
|
613 | if smallestIndent is -1 or indent < smallestIndent
|
614 | smallestIndent = indent
|
615 | if smallestIndent > 0
|
616 | for line, i in lines
|
617 | lines[i] = line[smallestIndent..]
|
618 | value = lines.join("\n")
|
619 |
|
620 | return value
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 | isNextLineUnIndentedCollection: (currentIndentation = null) ->
|
628 | currentIndentation ?= @getCurrentLineIndentation()
|
629 | notEOF = @moveToNextLine()
|
630 |
|
631 | while notEOF and @isCurrentLineEmpty()
|
632 | notEOF = @moveToNextLine()
|
633 |
|
634 | if false is notEOF
|
635 | return false
|
636 |
|
637 | ret = false
|
638 | if @getCurrentLineIndentation() is currentIndentation and @isStringUnIndentedCollectionItem(@currentLine)
|
639 | ret = true
|
640 |
|
641 | @moveToPreviousLine()
|
642 |
|
643 | return ret
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 | isStringUnIndentedCollectionItem: ->
|
651 | return @currentLine is '-' or @currentLine[0...2] is '- '
|
652 |
|
653 |
|
654 | module.exports = Parser
|