Browse Source

escaped characters + unit tests, improved testing (multiline)

Kajetan Johannes Hammerle 4 years ago
parent
commit
035a04583d

+ 2 - 1
src/me/hammerle/snuviscript/test/TestLogger.java

@@ -3,6 +3,7 @@ package me.hammerle.snuviscript.test;
 import java.io.File;
 import java.nio.file.Files;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import me.hammerle.snuviscript.code.ISnuviLogger;
 import me.hammerle.snuviscript.code.Script;
@@ -16,7 +17,7 @@ public class TestLogger implements ISnuviLogger
     {
         if(ex == null)
         {
-            list.add(message);
+            list.addAll(Arrays.asList(message.split("\n")));
         }
         else
         {

+ 17 - 0
src/me/hammerle/snuviscript/tokenizer/Tokenizer.java

@@ -198,9 +198,14 @@ public class Tokenizer
     private void handleString()
     {
         StringBuilder sb = new StringBuilder();
+        int oldLine = line;
         while(true)
         {
             int data = next();
+            if(data == -1)
+            {
+                throw new PreScriptException("non closed string literal", oldLine);
+            }
             if(data == '"')
             {
                 add(STRING, sb.toString());
@@ -210,6 +215,18 @@ public class Tokenizer
             {
                 line++;
             }
+            if(data == '\\')
+            {
+                int escape = next();
+                switch(escape)
+                {
+                    case 'n': data = '\n'; break;
+                    case '\\': data = '\\'; break;
+                    case '"': data = '"'; break;
+                    default:
+                        throw new PreScriptException("invalid escaped character", line);
+                }
+            }
             sb.append((char) data);
         }
     }

+ 1 - 0
test/strings/string1

@@ -0,0 +1 @@
+print("This is a test.");

+ 2 - 0
test/strings/string1.cout

@@ -0,0 +1,2 @@
+push "This is a test."
+use print(1)

+ 1 - 0
test/strings/string1.out

@@ -0,0 +1 @@
+This is a test.

+ 6 - 0
test/strings/string1.tout

@@ -0,0 +1,6 @@
+(1, LITERAL, "print")
+(1, OPEN_BRACKET)
+(1, STRING, "This is a test.")
+(1, CLOSE_BRACKET)
+(1, SEMICOLON)
+(2, EOF)

+ 1 - 0
test/strings/string2

@@ -0,0 +1 @@
+print("This is \na test.");

+ 3 - 0
test/strings/string2.cout

@@ -0,0 +1,3 @@
+push "This is 
+a test."
+use print(1)

+ 2 - 0
test/strings/string2.out

@@ -0,0 +1,2 @@
+This is 
+a test.

+ 7 - 0
test/strings/string2.tout

@@ -0,0 +1,7 @@
+(1, LITERAL, "print")
+(1, OPEN_BRACKET)
+(1, STRING, "This is 
+a test.")
+(1, CLOSE_BRACKET)
+(1, SEMICOLON)
+(2, EOF)

+ 1 - 0
test/strings/string3

@@ -0,0 +1 @@
+print("This is \"a test.");

+ 2 - 0
test/strings/string3.cout

@@ -0,0 +1,2 @@
+push "This is "a test."
+use print(1)

+ 1 - 0
test/strings/string3.out

@@ -0,0 +1 @@
+This is "a test.

+ 6 - 0
test/strings/string3.tout

@@ -0,0 +1,6 @@
+(1, LITERAL, "print")
+(1, OPEN_BRACKET)
+(1, STRING, "This is "a test.")
+(1, CLOSE_BRACKET)
+(1, SEMICOLON)
+(2, EOF)

+ 1 - 0
test/strings/string4

@@ -0,0 +1 @@
+print("This is \\a test.");

+ 2 - 0
test/strings/string4.cout

@@ -0,0 +1,2 @@
+push "This is \a test."
+use print(1)

+ 1 - 0
test/strings/string4.out

@@ -0,0 +1 @@
+This is \a test.

+ 6 - 0
test/strings/string4.tout

@@ -0,0 +1,6 @@
+(1, LITERAL, "print")
+(1, OPEN_BRACKET)
+(1, STRING, "This is \a test.")
+(1, CLOSE_BRACKET)
+(1, SEMICOLON)
+(2, EOF)

+ 1 - 1
test/test.test

@@ -1 +1 @@
-print("Wusi");
+print("Wu\"si");