summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xbuild.sh4
-rw-r--r--src/bin/printtokens.lua29
-rw-r--r--src/cratera/luatokens.lua11
-rw-r--r--test/line-numbers.cratera45
-rw-r--r--test/strings.cratera6
5 files changed, 86 insertions, 9 deletions
diff --git a/build.sh b/build.sh
index 325b3a3..717b2d1 100755
--- a/build.sh
+++ b/build.sh
@@ -65,7 +65,9 @@ do_test() {
 		test_wrapper 'out/bin/cratera test/interp-error.cratera nil 2>&1 | grep '\''nil value'\'
 		test_wrapper 'out/bin/cratera test/interp-error.cratera table 2>&1 | grep '\''table value'\'
 		test_wrapper 'out/bin/cratera test/interp-error.cratera metaerror 2>&1 | grep '\''overflow\|handling'\'
-		test_wrapper 'printf '\''_PROMPT=setmetatable({}, {__tostring=error})\n'\'' | out/bin/cratera -i 2>&1 ' # | grep '\''overflow\|handling'\'
+		test_wrapper 'printf '\''_PROMPT=setmetatable({}, {__tostring=error})\n'\'' | out/bin/cratera -i 2>&1 | grep '\''overflow\|handling'\'
+		test_wrapper 'out/bin/cratera test/line-numbers.cratera'
+		test_wrapper 'out/bin/cratera test/strings.cratera'
 	) || exit 2
 }
 
diff --git a/src/bin/printtokens.lua b/src/bin/printtokens.lua
index 49827da..316b439 100644
--- a/src/bin/printtokens.lua
+++ b/src/bin/printtokens.lua
@@ -50,18 +50,37 @@ defs[-1] = function(state, token, rule)
         error("Unknown option: " .. token)
     end
 end
+defs['-l'] = function(state, token, rule)
+    state.print_lineno = true
+    return "self"
+end
 defs['--'] = parser.selfify({[parser.FALLBACK] = defs[parser.FALLBACK], [parser.EOZ] = defs[parser.EOZ]})
 
 local state = parser.parse(defs, arg)
 local luatokens = require "cratera.luatokens"
 local file = state.file
 local tokens = luatokens.defs
-local state, err, etoken, estate = parser.parse(tokens, function() return file:read(8192) end)
-if state then
+local print_lineno = state.print_lineno
+--local state, err, etoken, estate = parser.parse(tokens, function() return file:read(8192) end)
+local f, s, i = parser.stream(tokens, function() return file:read(8192) end)
+local lastline
+local lasttoken = 0
+for pos, state, transemsg, etoken, estate in f, s, i do
+    if not state then
+        print("Parse error")
+        break
+    end
+    if (state.line or 1) ~= lastline then
+        lastline = state.line or 1
+        print("line", lastline)
+    end
     for i,v in ipairs(state) do
+        lasttoken = lasttoken + 1
         v = luatokens.reverse_keywords[v] or luatokens.reverse_tokens[v] or v
-        print(i, v) -- TODO formatting
+        print(lasttoken, v) -- TODO formatting
+        state[i] = nil
+    end
+    if not transemsg then
+        break
     end
-else
-    print("Parse error")
 end
diff --git a/src/cratera/luatokens.lua b/src/cratera/luatokens.lua
index 90d0c61..632cf27 100644
--- a/src/cratera/luatokens.lua
+++ b/src/cratera/luatokens.lua
@@ -1,6 +1,6 @@
 --[[
     This file is part of Cratera Compiler
-    Copyright (C) 2019  Soni L.
+    Copyright (C) 2019, 2024  Soni L.
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU Affero General Public License as published by
@@ -387,7 +387,12 @@ end
 do local tlongstring = {}
     defs.longstring = tlongstring
     do local tllongstring_proper = selfify({[parser.FALLBACK] = "self", ["]"] = function(state, token) state.longstring_close = 0 return "maybe_end" end})
-        tllongstring_proper[1] = false -- placeholder for newline handling
+        tlongstring[1] = false -- first newline is skipped(!)
+        tllongstring_proper[1] = function(state, token, rule)
+            if token == "\n" or token == "\r" then
+                collect_fallback(state, "\n")
+            end
+        end
         tllongstring_proper[2] = collect_fallback
 
         do local tllmaybe_end = selfify({defs = defs}, "maybe_end")
@@ -425,7 +430,7 @@ do local tlongstring = {}
         end
 
         tlongstring.longstring_proper = tllongstring_proper
-        mknewline(tlongstring, 1, tllongstring_proper)
+        mknewline(tllongstring_proper, 1)
         setmetatable(tlongstring, {__index=tllongstring_proper})
     end
 end
diff --git a/test/line-numbers.cratera b/test/line-numbers.cratera
new file mode 100644
index 0000000..f2f977b
--- /dev/null
+++ b/test/line-numbers.cratera
@@ -0,0 +1,45 @@
+local function mkerror(key)
+    if key == "a" then
+        error("test failed")
+    elseif key == "b" then
+        -- hello
+        error("test failed")
+    elseif key == "c" then
+
+
+        error("test failed")
+    elseif key == "d" then
+-- hmm...
+        error("test failed")
+    elseif key == "e" then
+
+-- hmm...
+        error("test failed")
+    elseif key == "f" then
+-- increasingly confused about this
+-- hmm...
+        error("test failed")
+    elseif key == "g" then
+local foo = [[
+Test.
+Test.
+]]
+        error("test failed")
+    end
+end
+
+local ok, msg = pcall(mkerror,"a")
+assert(not ok and msg:match(":3"), msg)
+local ok, msg = pcall(mkerror,"b")
+assert(not ok and msg:match(":6"), msg)
+local ok, msg = pcall(mkerror,"c")
+assert(not ok and msg:match(":10"), msg)
+local ok, msg = pcall(mkerror,"d")
+assert(not ok and msg:match(":13"), msg)
+local ok, msg = pcall(mkerror,"e")
+assert(not ok and msg:match(":17"), msg)
+local ok, msg = pcall(mkerror,"f")
+assert(not ok and msg:match(":21"), msg)
+local ok, msg = pcall(mkerror,"g")
+assert(not ok and msg:match(":27"), msg)
+print("line number tests pass")
diff --git a/test/strings.cratera b/test/strings.cratera
new file mode 100644
index 0000000..d2189a5
--- /dev/null
+++ b/test/strings.cratera
@@ -0,0 +1,6 @@
+local longstring = [[
+Test.
+Test.
+]]
+
+assert(longstring == "Test.\nTest.\n", longstring)