From 1ed6801137ede253844a6d798d5f9f8327dc0090 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 12 May 2016 11:28:32 -0700 Subject: [PATCH] Add option to enable old behavior Cargo will use this in the interim. --- src/parser.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 8c689f8..60767eb 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -89,6 +89,7 @@ impl Value { pub struct Parser<'a> { input: &'a str, cur: str::CharIndices<'a>, + require_newline_after_table: bool, /// A list of all errors which have occurred during parsing. /// @@ -138,6 +139,7 @@ impl<'a> Parser<'a> { input: s, cur: s.char_indices(), errors: Vec::new(), + require_newline_after_table: true, } } @@ -155,6 +157,16 @@ impl<'a> Parser<'a> { (self.input.lines().count(), 0) } + /// Historical versions of toml-rs accidentally allowed a newline after a + /// table definition, but the TOML spec requires a newline after a table + /// definition header. + /// + /// This option can be set to `false` (the default is `true`) to emulate + /// this behavior for backwards compatibility with older toml-rs versions. + pub fn set_require_newline_after_table(&mut self, require: bool) { + self.require_newline_after_table = require; + } + fn next_pos(&self) -> usize { self.cur.clone().next().map(|p| p.0).unwrap_or(self.input.len()) } @@ -271,15 +283,17 @@ impl<'a> Parser<'a> { values: BTreeMap::new(), defined: true, }; - self.ws(); - self.comment(); - if !self.newline() { - self.errors.push(ParserError { - lo: start, - hi: start, - desc: format!("expected a newline after table definition"), - }); - return None + if self.require_newline_after_table { + self.ws(); + self.comment(); + if !self.newline() { + self.errors.push(ParserError { + lo: start, + hi: start, + desc: format!("expected a newline after table definition"), + }); + return None + } } if !self.values(&mut table) { return None } if array {