emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/lua-mode 6f7800f 258/468: Merge (and squash) pull request


From: Philip Kaludercic
Subject: [nongnu] elpa/lua-mode 6f7800f 258/468: Merge (and squash) pull request #66 from rolpereira/master
Date: Thu, 5 Aug 2021 04:58:49 -0400 (EDT)

branch: elpa/lua-mode
commit 6f7800f24c337f3f96845619a132ebc4e22f96f8
Author: Rolando Pereira <rolando_pereira@sapo.pt>
Commit: immerrr <immerrr+lua@gmail.com>

    Merge (and squash) pull request #66 from rolpereira/master
    
    Add support for Lua5.2 goto keyword
    
    * font-lock: add new keyword, highlight labels as constants
    * indentation: fix labels being treated as continuation lines
---
 lua-mode.el                  |  17 ++++--
 test/test-defun-font-lock.el |  32 ++++++++++++
 test/test-indentation.el     | 120 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 166 insertions(+), 3 deletions(-)

diff --git a/lua-mode.el b/lua-mode.el
index 5c631c7..ad10382 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -192,8 +192,8 @@ element is itself expanded with `lua-rx-to-string'. "
                    ">" "=" ";" ":" "," "." ".." "..."))
           (lua-keyword
            :rx (symbol "and" "break" "do" "else" "elseif" "end"  "for" 
"function"
-                       "if" "in" "local" "not" "or" "repeat" "return" "then"
-                       "until" "while")))
+                       "goto" "if" "in" "local" "not" "or" "repeat" "return"
+                       "then" "until" "while")))
         ))
 
 
@@ -592,6 +592,17 @@ Groups 6-9 can be used in any of argument regexps."
     (,(lua-rx lua-keyword)
      . font-lock-keyword-face)
 
+    ;; Labels used by the "goto" statement
+    ;; Highlights the following syntax:  ::label::
+    (,(lua-rx "::" ws lua-name ws "::")
+      . font-lock-constant-face)
+
+    ;; Hightlights the name of the label in the "goto" statement like
+    ;; "goto label"
+    (,(lua-rx (symbol (seq "goto" ws+ (group-n 1 lua-name))))
+      nil nil
+      (1 font-lock-constant-face))
+
     ;; Highlight lua builtin functions and variables
     (,lua--builtins
      (1 font-lock-builtin-face) (2 font-lock-builtin-face nil noerror))
@@ -1105,7 +1116,7 @@ Returns final value of point as integer or nil if 
operation failed."
 
 (eval-when-compile
   (defconst lua-operator-class
-    "-+*/^.=<>~"))
+    "-+*/^.=<>~:"))
 
 (defconst lua-cont-eol-regexp
   (eval-when-compile
diff --git a/test/test-defun-font-lock.el b/test/test-defun-font-lock.el
index 125ebf7..28072ab 100644
--- a/test/test-defun-font-lock.el
+++ b/test/test-defun-font-lock.el
@@ -77,3 +77,35 @@ end"
      ("qux_qux" function-name "function" keyword "end" keyword)
      ("local" keyword "quux_quux" function-name "function" keyword "end" 
keyword)
      ("end" keyword)))  )
+
+(ert-deftest lua-font-lock-labels ()
+  (should-lua-font-lock-equal
+    "\
+goto foo
+::foo::"
+   '(("goto" keyword "foo" constant)
+     ("::foo::" constant)))
+
+  (should-lua-font-lock-equal
+    "\
+local foo = 'test' ::f12o::
+goto f12o"
+   '(("local" keyword "foo" variable-name "'test'" string "::f12o::" constant)
+     ("goto" keyword "f12o" constant)))
+
+  ;; With spaces after and before "::"
+  (should-lua-font-lock-equal
+    "\
+goto foo
+:: foo ::"
+   '(("goto" keyword "foo" constant)
+     (":: foo ::" constant)))
+
+  ;; Don't font lock labels when substring "goto" appears as a suffix
+  ;; of another variable
+  (should-lua-font-lock-equal
+    "\
+JUNKgoto foo
+:: foo ::"
+   '(nil ;; don't font lock "foo"
+     (":: foo ::" constant))))
diff --git a/test/test-indentation.el b/test/test-indentation.el
index 53810db..aefbf53 100644
--- a/test/test-indentation.el
+++ b/test/test-indentation.el
@@ -493,3 +493,123 @@ for k, v
 a = 0")
 
   )
+
+(ert-deftest lua-indentation-labels ()
+  (should-lua-indent "\
+::foo::")
+
+  (should-lua-indent "\
+::foo::
+::bar::
+
+::baz::")
+
+  ;; The following tests come from the LuaWiki goto page:
+  ;; http://lua-users.org/wiki/GotoStatement
+  (should-lua-indent "\
+for z=1,10 do
+   for y=1,10 do
+      for x=1,10 do
+         if x^2 + y^2 == z^2 then
+            print('found a Pythagorean triple:', x, y, z)
+            goto done
+            goto done2
+         end
+      end
+      ::done2::
+   end
+end
+
+::done::")
+
+  (should-lua-indent "\
+-- 5.2.0-beta-rc2
+for z=1,10 do
+   for y=1,10 do
+      for x=1,10 do
+         if x^2 + y^2 == z^2 then
+            print('found a Pythagorean triple:', x, y, z)
+            print('now trying next z...')
+            goto zcontinue
+         end
+      end
+   end
+   ::zcontinue::
+end")
+
+  (should-lua-indent "\
+-- Lua 5.2.0-beta-rc2
+for x=1,5 do ::redo::
+   print(x .. ' + 1 = ?')
+   local y = tonumber(io.read'*l')
+   if y ~= x + 1 then goto redo end
+end")
+
+  (should-lua-indent "\
+-- 5.2.0-beta-rc1
+::a::
+print 'A'
+if math.random() < 0.3 then goto c end
+::b::
+print 'B'
+if math.random() < 0.5 then goto a end
+::c::
+print 'C'
+if math.random() < 0.1 then goto a else goto b end
+")
+
+  (should-lua-indent "\
+-- 5.2.0-beta-rc2 - factorial with tail recursion simulated with goto's
+-- (warning: there's no need to do this)
+function fact_(n, ans)
+   ::call::
+   if n == 0 then
+      return ans
+   else
+      n, ans = n - 1, ans * n
+      goto call
+   end
+end
+print(fact_(5, 1)) --> 120")
+
+  (should-lua-indent "\
+-- 5.2.0-beta-rc2
+function f()
+   if not g() then goto fail end
+   if not h() then goto cleanup_g end
+   if not i() then goto cleanup_h end
+   do return true end    -- need do/end?
+
+   ::cleanup_h::
+   undo_h()
+   ::cleanup_g::
+   undo_g()
+   ::fail::
+   return false
+end")
+
+  (should-lua-indent "\
+-- 5.2.0-beta-rc2
+::redo:: 
+for x=1,10 do
+   for y=1,10 do
+      if not f(x,y) then goto continue end
+      if not g(x,y) then goto skip end
+      if not h(x,y) then goto redo end
+      ::continue::
+   end
+end ::skip::
+print('foo')"))
+
+(ert-deftest lua-indentation-indent-labels-inside-multiline-string ()
+  :expected-result :failed
+  ;; TODO: It seems that `should-lua-indent' tries to indent text
+  ;; inside the "[[ ]]" multiline string?
+  (should-lua-indent "\
+local b, bt = [[
+      if not x then goto a end
+           f()
+      goto b; ::a::
+           g()
+         ::b::
+]]"))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]