summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAiran Shao <shaoairan@gmail.com>2017-02-11 14:08:46 +0800
committerAiran Shao <shaoairan@gmail.com>2017-02-11 14:08:46 +0800
commit1e8d6dd845e0f59dd88c28615ad8702e4a59c0d9 (patch)
tree814b2d38f37987273f44858e5125f3b067680b32
parent97d39b6c2bf533d33538205a399638b63f6baa66 (diff)
downloadyamljs-1e8d6dd845e0f59dd88c28615ad8702e4a59c0d9.tar.gz
Multiline compact sequence/mapping/string
Fix following issues: 1) Multiline compact sequence/mapping/quoted string can begin without indentation. 2) Unquoted string can extend to multiline with proper indentation.
-rw-r--r--Cakefile4
-rw-r--r--src/Exception/ParseMore.coffee12
-rw-r--r--src/Inline.coffee13
-rw-r--r--src/Parser.coffee30
4 files changed, 35 insertions, 24 deletions
diff --git a/Cakefile b/Cakefile
index 0449803..edc0878 100644
--- a/Cakefile
+++ b/Cakefile
@@ -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)
@@ -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)
diff --git a/src/Exception/ParseMore.coffee b/src/Exception/ParseMore.coffee
new file mode 100644
index 0000000..faeb946
--- /dev/null
+++ b/src/Exception/ParseMore.coffee
@@ -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
diff --git a/src/Inline.coffee b/src/Inline.coffee
index a0f8434..4620e3f 100644
--- a/src/Inline.coffee
+++ b/src/Inline.coffee
@@ -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
@@ -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)
@@ -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 = []
@@ -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.
@@ -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 = {}
@@ -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.
diff --git a/src/Parser.coffee b/src/Parser.coffee
index c0299d9..610bc3f 100644
--- a/src/Parser.coffee
+++ b/src/Parser.coffee
@@ -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.
#
@@ -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