Skip to content

Commit

Permalink
Merge pull request #80 from ar-shao/multiline
Browse files Browse the repository at this point in the history
Multiline compact sequence/mapping/string
  • Loading branch information
jeremyfa committed Feb 11, 2017
2 parents 0ce1c7e + 1e8d6dd commit 5e74781
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ task 'build', 'build project', ->
fs.mkdirSync libDir
unless fs.existsSync libDir+'/Exception'
fs.mkdirSync libDir+'/Exception'
toCompile = 'Yaml Utils Unescaper Pattern Parser Inline Escaper Dumper Exception/ParseException Exception/DumpException'.split ' '
toCompile = 'Yaml Utils Unescaper Pattern Parser Inline Escaper Dumper Exception/ParseException Exception/ParseMore Exception/DumpException'.split ' '
do compileOne = ->
name = toCompile.shift()
outputDir = (if '/' in name then libDir+'/Exception' else libDir)
Expand All @@ -40,7 +40,7 @@ task 'build', 'build project', ->
fs.mkdirSync libDebugDir
unless fs.existsSync libDebugDir+'/Exception'
fs.mkdirSync libDebugDir+'/Exception'
toCompile = 'Yaml Utils Unescaper Pattern Parser Inline Escaper Dumper Exception/ParseException Exception/DumpException'.split ' '
toCompile = 'Yaml Utils Unescaper Pattern Parser Inline Escaper Dumper Exception/ParseException Exception/ParseMore Exception/DumpException'.split ' '
do compileOne = ->
name = toCompile.shift()
outputDir = (if '/' in name then libDebugDir+'/Exception' else libDebugDir)
Expand Down
12 changes: 12 additions & 0 deletions src/Exception/ParseMore.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

class ParseMore extends Error

constructor: (@message, @parsedLine, @snippet) ->

toString: ->
if @parsedLine? and @snippet?
return '<ParseMore> ' + @message + ' (line ' + @parsedLine + ': \'' + @snippet + '\')'
else
return '<ParseMore> ' + @message

module.exports = ParseMore
13 changes: 7 additions & 6 deletions src/Inline.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Unescaper = require './Unescaper'
Escaper = require './Escaper'
Utils = require './Utils'
ParseException = require './Exception/ParseException'
ParseMore = require './Exception/ParseMore'
DumpException = require './Exception/DumpException'

# Inline YAML parsing and dumping
Expand Down Expand Up @@ -211,13 +212,13 @@ class Inline
#
# @return [String] A YAML string
#
# @throw [ParseException] When malformed inline YAML string is parsed
# @throw [ParseMore] When malformed inline YAML string is parsed
#
@parseQuotedScalar: (scalar, context) ->
{i} = context

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

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

Expand All @@ -239,7 +240,7 @@ class Inline
#
# @return [String] A YAML string
#
# @throw [ParseException] When malformed inline YAML string is parsed
# @throw [ParseMore] When malformed inline YAML string is parsed
#
@parseSequence: (sequence, context) ->
output = []
Expand Down Expand Up @@ -282,7 +283,7 @@ class Inline

++i

throw new ParseException 'Malformed inline YAML string '+sequence
throw new ParseMore 'Malformed inline YAML string '+sequence


# Parses a mapping to a YAML string.
Expand All @@ -292,7 +293,7 @@ class Inline
#
# @return [String] A YAML string
#
# @throw [ParseException] When malformed inline YAML string is parsed
# @throw [ParseMore] When malformed inline YAML string is parsed
#
@parseMapping: (mapping, context) ->
output = {}
Expand Down Expand Up @@ -364,7 +365,7 @@ class Inline
if done
break

throw new ParseException 'Malformed inline YAML string '+mapping
throw new ParseMore 'Malformed inline YAML string '+mapping


# Evaluates scalars and replaces magic values.
Expand Down
30 changes: 14 additions & 16 deletions src/Parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Inline = require './Inline'
Pattern = require './Pattern'
Utils = require './Utils'
ParseException = require './Exception/ParseException'
ParseMore = require './Exception/ParseMore'

# Parser parses YAML strings to convert them to JavaScript objects.
#
Expand Down Expand Up @@ -416,25 +417,22 @@ class Parser
else
return val

try
return Inline.parse value, exceptionOnInvalidType, objectDecoder
catch e
# Try to parse multiline compact sequence or mapping
if value.charAt(0) in ['[', '{'] and e instanceof ParseException and @isNextLineIndented()
value += "\n" + @getNextEmbedBlock()
# Value can be multiline compact sequence or mapping or string
if value.charAt(0) in ['[', '{', '"', "'"]
while true
try
return Inline.parse value, exceptionOnInvalidType, objectDecoder
catch e
e.parsedLine = @getRealCurrentLineNb() + 1
e.snippet = @currentLine

throw e

else
e.parsedLine = @getRealCurrentLineNb() + 1
e.snippet = @currentLine

throw e
if e instanceof ParseMore and @moveToNextLine()
value += "\n" + Utils.trim(@currentLine, ' ')
else
e.parsedLine = @getRealCurrentLineNb() + 1
e.snippet = @currentLine
throw e
else
if @isNextLineIndented()
value += "\n" + @getNextEmbedBlock()
return Inline.parse value, exceptionOnInvalidType, objectDecoder

return

Expand Down

0 comments on commit 5e74781

Please sign in to comment.