syntax: ignore first '\n' in triple-quoted strings
authorJean Privat <jean@pryen.org>
Fri, 7 Mar 2014 20:33:28 +0000 (15:33 -0500)
committerJean Privat <jean@pryen.org>
Sat, 8 Mar 2014 02:12:40 +0000 (21:12 -0500)
This detail of the specification makes triple-quoted strings more
readable and usable in code.

Before, programmer was required to starts content just after the """
thus writing unaligned piece of text

~~~
var x = """Roses are red,
Violets are blue,
Triple-quoted strings,
Are easier to write.
"""
~~~

This patch ignore the end of line iff it just follows the """,
thus programmer can write

~~~
var x = """
Roses are red,
Violets are blue,
Triple-quoted strings,
Are easier to write.
"""
~~~

This commit will improve the usability of the new/future template library.

Signed-off-by: Jean Privat <jean@pryen.org>

src/literal.nit
tests/sav/test_string_triple.res
tests/sav/test_string_triple3.res [new file with mode: 0644]
tests/test_string_triple3.nit [new file with mode: 0644]

index 9daefcb..9ea79f0 100644 (file)
@@ -100,8 +100,13 @@ redef class AStringFormExpr
        redef fun accept_literal(v)
        do
                var txt = self.n_string.text
-               var skip = 1
-               if txt.chars[0] == txt.chars[1] and txt.length >= 6 then skip = 3
-               self.value = txt.substring(skip, txt.length-(2*skip)).unescape_nit
+               var behead = 1
+               var betail = 1
+               if txt.chars[0] == txt.chars[1] and txt.length >= 6 then
+                       behead = 3
+                       betail = 3
+                       if txt.chars[0] == '"' and txt.chars[3] == '\n' then behead = 4 # ignore first \n in """
+               end
+               self.value = txt.substring(behead, txt.length - behead - betail).unescape_nit
        end
 end
index bf73df1..0ade744 100644 (file)
@@ -9,6 +9,4 @@ c
 1111b{b}bc{{{ {{{ {{{ 1
 
 
-
-
 *1**2***3***$4$
diff --git a/tests/sav/test_string_triple3.res b/tests/sav/test_string_triple3.res
new file mode 100644 (file)
index 0000000..5630e3b
--- /dev/null
@@ -0,0 +1,10 @@
+hello
+word
+hello
+world
+hello
+word
+
+hello
+world
+
diff --git a/tests/test_string_triple3.nit b/tests/test_string_triple3.nit
new file mode 100644 (file)
index 0000000..22fac8a
--- /dev/null
@@ -0,0 +1,29 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+print """hello
+word"""
+
+print """
+hello
+world"""
+
+print """hello
+word
+"""
+
+print """
+hello
+world
+"""