From e9eb6d087d0aaac8e9b25e467a9a609d43920a6e Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy <mmstickman@gmail.com> Date: Sun, 10 Dec 2017 19:08:27 -0500 Subject: [PATCH] Fix Issues w/ Multiline Array Assignments This fix will ensure that comments within multi-line array assignments are properly handled within both the REPL and script executions. Closes #642 --- examples/multiline-arrays.ion | 16 ++++++++++++++++ examples/multiline-arrays.out | 2 ++ src/shell/binary/terminate.rs | 26 +++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 examples/multiline-arrays.ion create mode 100644 examples/multiline-arrays.out diff --git a/examples/multiline-arrays.ion b/examples/multiline-arrays.ion new file mode 100644 index 00000000..ce9174a3 --- /dev/null +++ b/examples/multiline-arrays.ion @@ -0,0 +1,16 @@ +let x = [ + a # a comment + b # another comment + ab#cd #with a comment +] + +echo @x + +let y = [ + a + # a comment + #another comment +] + +echo @x + diff --git a/examples/multiline-arrays.out b/examples/multiline-arrays.out new file mode 100644 index 00000000..ddf27e8e --- /dev/null +++ b/examples/multiline-arrays.out @@ -0,0 +1,2 @@ +a b ab#cd +a b ab#cd diff --git a/src/shell/binary/terminate.rs b/src/shell/binary/terminate.rs index 2cb932d9..c288c206 100644 --- a/src/shell/binary/terminate.rs +++ b/src/shell/binary/terminate.rs @@ -11,8 +11,26 @@ pub(crate) fn terminate_script_quotes<I: Iterator<Item = String>>( while !buffer.is_terminated() { loop { if let Some(command) = lines.next() { - buffer.append(&command); - break; + if !command.starts_with('#') { + let mut start = 0; + let cmd: &str = loop { + if start >= command.len() { + break &command; + } + + match command[start..].find('#').map(|x| x + start) { + Some(pos) if command.as_bytes()[pos-1] != b' ' => { + start = pos + 1; + } + Some(pos) => { + break &command[..pos] + } + None => break &command + } + }; + buffer.append(cmd); + break; + } } else { eprintln!("ion: unterminated quote in script"); return FAILURE; @@ -40,7 +58,9 @@ pub(crate) fn terminate_quotes(shell: &mut Shell, command: String) -> Result<Str shell.flow_control.level += 1; while !buffer.is_terminated() { if let Some(command) = shell.readln() { - buffer.append(&command); + if !command.starts_with('#') { + buffer.append(&command); + } } else { return Err(()); } -- GitLab