Skip to content

Commit 5e74781

Browse files
authored
Merge pull request #80 from ar-shao/multiline
Multiline compact sequence/mapping/string
2 parents 0ce1c7e + 1e8d6dd commit 5e74781

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

Cakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ task 'build', 'build project', ->
2121
fs.mkdirSync libDir
2222
unless fs.existsSync libDir+'/Exception'
2323
fs.mkdirSync libDir+'/Exception'
24-
toCompile = 'Yaml Utils Unescaper Pattern Parser Inline Escaper Dumper Exception/ParseException Exception/DumpException'.split ' '
24+
toCompile = 'Yaml Utils Unescaper Pattern Parser Inline Escaper Dumper Exception/ParseException Exception/ParseMore Exception/DumpException'.split ' '
2525
do compileOne = ->
2626
name = toCompile.shift()
2727
outputDir = (if '/' in name then libDir+'/Exception' else libDir)
@@ -40,7 +40,7 @@ task 'build', 'build project', ->
4040
fs.mkdirSync libDebugDir
4141
unless fs.existsSync libDebugDir+'/Exception'
4242
fs.mkdirSync libDebugDir+'/Exception'
43-
toCompile = 'Yaml Utils Unescaper Pattern Parser Inline Escaper Dumper Exception/ParseException Exception/DumpException'.split ' '
43+
toCompile = 'Yaml Utils Unescaper Pattern Parser Inline Escaper Dumper Exception/ParseException Exception/ParseMore Exception/DumpException'.split ' '
4444
do compileOne = ->
4545
name = toCompile.shift()
4646
outputDir = (if '/' in name then libDebugDir+'/Exception' else libDebugDir)

src/Exception/ParseMore.coffee

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
class ParseMore extends Error
3+
4+
constructor: (@message, @parsedLine, @snippet) ->
5+
6+
toString: ->
7+
if @parsedLine? and @snippet?
8+
return '<ParseMore> ' + @message + ' (line ' + @parsedLine + ': \'' + @snippet + '\')'
9+
else
10+
return '<ParseMore> ' + @message
11+
12+
module.exports = ParseMore

src/Inline.coffee

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Unescaper = require './Unescaper'
44
Escaper = require './Escaper'
55
Utils = require './Utils'
66
ParseException = require './Exception/ParseException'
7+
ParseMore = require './Exception/ParseMore'
78
DumpException = require './Exception/DumpException'
89

910
# Inline YAML parsing and dumping
@@ -211,13 +212,13 @@ class Inline
211212
#
212213
# @return [String] A YAML string
213214
#
214-
# @throw [ParseException] When malformed inline YAML string is parsed
215+
# @throw [ParseMore] When malformed inline YAML string is parsed
215216
#
216217
@parseQuotedScalar: (scalar, context) ->
217218
{i} = context
218219

219220
unless match = @PATTERN_QUOTED_SCALAR.exec scalar[i..]
220-
throw new ParseException 'Malformed inline YAML string ('+scalar[i..]+').'
221+
throw new ParseMore 'Malformed inline YAML string ('+scalar[i..]+').'
221222

222223
output = match[0].substr(1, match[0].length - 2)
223224

@@ -239,7 +240,7 @@ class Inline
239240
#
240241
# @return [String] A YAML string
241242
#
242-
# @throw [ParseException] When malformed inline YAML string is parsed
243+
# @throw [ParseMore] When malformed inline YAML string is parsed
243244
#
244245
@parseSequence: (sequence, context) ->
245246
output = []
@@ -282,7 +283,7 @@ class Inline
282283

283284
++i
284285

285-
throw new ParseException 'Malformed inline YAML string '+sequence
286+
throw new ParseMore 'Malformed inline YAML string '+sequence
286287

287288

288289
# Parses a mapping to a YAML string.
@@ -292,7 +293,7 @@ class Inline
292293
#
293294
# @return [String] A YAML string
294295
#
295-
# @throw [ParseException] When malformed inline YAML string is parsed
296+
# @throw [ParseMore] When malformed inline YAML string is parsed
296297
#
297298
@parseMapping: (mapping, context) ->
298299
output = {}
@@ -364,7 +365,7 @@ class Inline
364365
if done
365366
break
366367

367-
throw new ParseException 'Malformed inline YAML string '+mapping
368+
throw new ParseMore 'Malformed inline YAML string '+mapping
368369

369370

370371
# Evaluates scalars and replaces magic values.

src/Parser.coffee

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Inline = require './Inline'
33
Pattern = require './Pattern'
44
Utils = require './Utils'
55
ParseException = require './Exception/ParseException'
6+
ParseMore = require './Exception/ParseMore'
67

78
# Parser parses YAML strings to convert them to JavaScript objects.
89
#
@@ -416,25 +417,22 @@ class Parser
416417
else
417418
return val
418419

419-
try
420-
return Inline.parse value, exceptionOnInvalidType, objectDecoder
421-
catch e
422-
# Try to parse multiline compact sequence or mapping
423-
if value.charAt(0) in ['[', '{'] and e instanceof ParseException and @isNextLineIndented()
424-
value += "\n" + @getNextEmbedBlock()
420+
# Value can be multiline compact sequence or mapping or string
421+
if value.charAt(0) in ['[', '{', '"', "'"]
422+
while true
425423
try
426424
return Inline.parse value, exceptionOnInvalidType, objectDecoder
427425
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
426+
if e instanceof ParseMore and @moveToNextLine()
427+
value += "\n" + Utils.trim(@currentLine, ' ')
428+
else
429+
e.parsedLine = @getRealCurrentLineNb() + 1
430+
e.snippet = @currentLine
431+
throw e
432+
else
433+
if @isNextLineIndented()
434+
value += "\n" + @getNextEmbedBlock()
435+
return Inline.parse value, exceptionOnInvalidType, objectDecoder
438436

439437
return
440438

0 commit comments

Comments
 (0)