diff options
author | Jérémy Faivre <contact@jeremyfa.com> | 2017-02-12 00:39:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-12 00:39:06 +0100 |
commit | 5e74781976ad23645970eb192f2d83a324e7c95f (patch) | |
tree | a8860fc3255992224b5567190bc3a8043b7d2121 | |
parent | 0ce1c7eb73474a1c0ad5edeec112427b095b154f (diff) | |
parent | 1e8d6dd845e0f59dd88c28615ad8702e4a59c0d9 (diff) | |
download | yamljs-5e74781976ad23645970eb192f2d83a324e7c95f.tar.gz |
Merge pull request #80 from ar-shao/multiline
Multiline compact sequence/mapping/string
-rw-r--r-- | Cakefile | 4 | ||||
-rw-r--r-- | src/Exception/ParseMore.coffee | 12 | ||||
-rw-r--r-- | src/Inline.coffee | 13 | ||||
-rw-r--r-- | src/Parser.coffee | 30 |
4 files changed, 35 insertions, 24 deletions
@@ -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 d196b27..546c02f 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 |