summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cakefile4
-rw-r--r--src/Exception/ParseMore.coffee12
-rw-r--r--src/Inline.coffee13
-rw-r--r--src/Parser.coffee38
-rw-r--r--src/Pattern.coffee2
5 files changed, 40 insertions, 29 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 48d3030..812d23f 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.
#
@@ -19,10 +20,10 @@ class Parser
PATTERN_DECIMAL: new Pattern '\\d+'
PATTERN_INDENT_SPACES: new Pattern '^ +'
PATTERN_TRAILING_LINES: new Pattern '(\n*)$'
- PATTERN_YAML_HEADER: new Pattern '^\\%YAML[: ][\\d\\.]+.*\n'
- PATTERN_LEADING_COMMENTS: new Pattern '^(\\#.*?\n)+'
- PATTERN_DOCUMENT_MARKER_START: new Pattern '^\\-\\-\\-.*?\n'
- PATTERN_DOCUMENT_MARKER_END: new Pattern '^\\.\\.\\.\\s*$'
+ PATTERN_YAML_HEADER: new Pattern '^\\%YAML[: ][\\d\\.]+.*\n', 'm'
+ PATTERN_LEADING_COMMENTS: new Pattern '^(\\#.*?\n)+', 'm'
+ PATTERN_DOCUMENT_MARKER_START: new Pattern '^\\-\\-\\-.*?\n', 'm'
+ PATTERN_DOCUMENT_MARKER_END: new Pattern '^\\.\\.\\.\\s*$', 'm'
PATTERN_FOLDED_SCALAR_BY_INDENTATION: {}
# Context types
@@ -415,25 +416,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
diff --git a/src/Pattern.coffee b/src/Pattern.coffee
index e65fa79..82f96e7 100644
--- a/src/Pattern.coffee
+++ b/src/Pattern.coffee
@@ -134,7 +134,7 @@ class Pattern
count = 0
while @regex.test(str) and (limit is 0 or count < limit)
@regex.lastIndex = 0
- str = str.replace @regex, ''
+ str = str.replace @regex, replacement
count++
return [str, count]