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

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

[elpa] externals/phps-mode beb5b0b: Optimized lexer regex and added supp


From: Christian Johansson
Subject: [elpa] externals/phps-mode beb5b0b: Optimized lexer regex and added support for PSR-12
Date: Thu, 5 Dec 2019 01:16:01 -0500 (EST)

branch: externals/phps-mode
commit beb5b0ba765ea4de1ed7d06a5ed414133583a938
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>

    Optimized lexer regex and added support for PSR-12
---
 Makefile                     |  6 ++++-
 README.md                    |  5 ++--
 phps-mode-analyzer.el        |  4 +--
 phps-mode.el                 | 62 ++++++++++++++++++++++++++++++++++++++++----
 test/phps-mode-test-lexer.el | 23 ++++++++++++++--
 5 files changed, 88 insertions(+), 12 deletions(-)

diff --git a/Makefile b/Makefile
index 6b143f1..3a85a6b 100644
--- a/Makefile
+++ b/Makefile
@@ -28,7 +28,11 @@ test-integration:
 
 .PHONY: test-lexer
 test-lexer:
-       $(EMACS_CMD) -l test/phps-mode-test-lexer.el
+       $(EMACS_CMD) -l test/phps-mode-test-lexer.el -f "phps-mode-test-lexer"
+
+.PHONY: benchmark-lexer
+benchmark-lexer:
+       $(EMACS_CMD) -l test/phps-mode-test-lexer.el -f 
"phps-mode-test-lexer-benchmark"
 
 .PHONY: test-parser
 test-parser:
diff --git a/README.md b/README.md
index 693c2a6..cbcbc91 100644
--- a/README.md
+++ b/README.md
@@ -11,9 +11,10 @@ This mode does not require PHP installed on your computer 
because it has a built
 
 * GPLv3 license
 * Flycheck support with `(phps-mode-flycheck-setup)`
-* Semantic lexer based on official PHP re2c lexer
+* Semantic lexer based on official PHP 7.4 re2c lexer
 * Syntax coloring based on lexer tokens, makes it easier to spot invalid code
-* PSR-1 and PSR-2 indentation based on lexer tokens
+* PSR-1, PSR-2 and PSR-12 indentation based on lexer tokens
+* PSR-1, PSR-2 and PSR-12 supported white-space
 * Integration with `(electric-pair)`
 * Incremental lexer and syntax coloring after buffer changes
 * Incremental indentation and imenu generation after buffer changes
diff --git a/phps-mode-analyzer.el b/phps-mode-analyzer.el
index 94df331..7b2ee74 100644
--- a/phps-mode-analyzer.el
+++ b/phps-mode-analyzer.el
@@ -155,10 +155,10 @@
 ;; NOTE Original is [;:,.\[\]()|^&+-/*=%!~$<>?@]
 ;; NOTE The hyphen moved last since it has special meaning and to avoid it 
being interpreted as a range.
 
-(defvar phps-mode-lexer-ANY_CHAR ".\\|\n"
+(defvar phps-mode-lexer-ANY_CHAR "[^z-a]"
   "Any character.  The Zend equivalent is [^] but is not possible in Emacs 
Lisp.")
 
-(defvar phps-mode-lexer-NEWLINE "\\(\r\n\\|\r\\|\n\\)"
+(defvar phps-mode-lexer-NEWLINE "[\n\r]"
   "Newline characters.  The Zend equivalent is (\"\r\"|\"\n\"|\"\r\n\").")
 
 
diff --git a/phps-mode.el b/phps-mode.el
index 7e6bc8d..e68b260 100644
--- a/phps-mode.el
+++ b/phps-mode.el
@@ -5,8 +5,8 @@
 ;; Author: Christian Johansson <address@hidden>
 ;; Maintainer: Christian Johansson <address@hidden>
 ;; Created: 3 Mar 2018
-;; Modified: 2 Dec 2019
-;; Version: 0.3.20
+;; Modified: 5 Dec 2019
+;; Version: 0.3.21
 ;; Keywords: tools, convenience
 ;; URL: https://github.com/cjohansson/emacs-phps-mode
 
@@ -56,6 +56,9 @@
 (defvar phps-mode-use-psr-2 t
   "Whether to use PSR-2 guidelines for white-space or not.")
 
+(defvar phps-mode-use-psr-12 t
+  "Whether to use PSR-12 guidelines for white-space or not.")
+
 (defvar phps-mode-map
   (let ((map (make-sparse-keymap)))
     (define-key map (kbd "C-c C-r") #'phps-mode-lexer-run)
@@ -73,6 +76,16 @@
     (flycheck-add-mode 'php-phpmd 'phps-mode)
     (flycheck-add-mode 'php-phpcs 'phps-mode)))
 
+(defun phps-mode-add-trailing-newline ()
+  "Add a trailing newline to buffer if missing."
+  (let ((max (point-max)))
+    (when (> max 1)
+      (let ((last-character (buffer-substring-no-properties (1- max) max)))
+        (unless (string= last-character "\n")
+          (save-excursion
+            (goto-char (point-max))
+            (insert "\n")))))))
+
 ;;;###autoload
 (defun phps-mode-format-buffer ()
   "Format current buffer according to PHPs mode."
@@ -81,6 +94,19 @@
       (progn
         (when phps-mode-use-psr-2
           (untabify (point-min) (point-max)))
+        (when phps-mode-use-psr-12
+
+          ;; All PHP files MUST use the Unix LF (linefeed) line ending only.
+          (set-buffer-file-coding-system 'unix t t)
+
+          ;; There MUST NOT be trailing whitespace at the end of lines.
+          (delete-trailing-whitespace (point-min) (point-max))
+          (whitespace-cleanup)
+
+          ;; All PHP files MUST end with a non-blank line, terminated with a 
single LF.
+          (phps-mode-add-trailing-newline)
+          )
+          
         (phps-mode-analyzer-process-changes)
         (phps-mode-functions-process-current-buffer)
         (indent-region (point-min) (point-max)))
@@ -95,6 +121,19 @@
         (phps-mode)
         (when phps-mode-use-psr-2
           (untabify (point-min) (point-max)))
+        (when phps-mode-use-psr-12
+
+          ;; All PHP files MUST use the Unix LF (linefeed) line ending only.
+          (set-buffer-file-coding-system 'unix t t)
+
+          ;; There MUST NOT be trailing whitespace at the end of lines.
+          (delete-trailing-whitespace (point-min) (point-max))
+          (whitespace-cleanup)
+
+          ;; All PHP files MUST end with a non-blank line, terminated with a 
single LF.
+          (phps-mode-add-trailing-newline)
+
+          )
         (indent-region (point-min) (point-max))
         (setq
          new-buffer-contents
@@ -117,10 +156,10 @@
   (setq-local font-lock-keywords-only nil)
   (setq-local font-lock-defaults '(nil t))
 
-  ;; Flymake TODO
+  ;; Flymake TODO?
   ;; (phps-mode-flymake-init)
 
-  ;; Custom indentation
+  ;; Indentation
   ;; Indent-region will call this on each line of selected region
   (setq-local indent-line-function #'phps-mode-functions-indent-line)
 
@@ -136,7 +175,19 @@
     ;; MUST NOT use tabs for indenting
     (setq-local indent-tabs-mode nil))
 
-  ;; Reset flags
+  (when phps-mode-use-psr-12
+
+    ;; All PHP files MUST use the Unix LF (linefeed) line ending only.
+    (set-buffer-file-coding-system 'unix t t)
+
+    ;; TODO There MUST NOT be trailing whitespace at the end of lines.
+    
+    ;; All PHP files MUST end with a non-blank line, terminated with a single 
LF.
+    (setq require-final-newline t)
+
+    )
+
+  ;; Reset buffer-local variables
   (setq-local phps-mode-functions-allow-after-change t)
   (setq-local phps-mode-analyzer-change-min nil)
   (setq-local phps-mode-functions-idle-timer nil)
@@ -179,6 +230,7 @@
 
   ;; Wisent LALR parser TODO
   ;; (phps-mode-tags-init)
+
   )
 
 (provide 'phps-mode)
diff --git a/test/phps-mode-test-lexer.el b/test/phps-mode-test-lexer.el
index 0355617..a24b4cc 100644
--- a/test/phps-mode-test-lexer.el
+++ b/test/phps-mode-test-lexer.el
@@ -429,10 +429,30 @@
             -2)))
   )
 
+(defun phps-mode-test-lexer-benchmark ()
+  "Benchmark the lexer tests."
+  (require 'benchmark)
+  (let ((iteration 1)
+        (iterations 50))
+    (message "Benchmarking %s iterations" iterations)
+    (let ((elapsed
+           (benchmark-run
+               iterations
+             (progn
+               (phps-mode-test-lexer-script-boundaries)
+               (phps-mode-test-lexer-simple-tokens)
+               (phps-mode-test-lexer-complex-tokens)
+               (phps-mode-test-lexer-namespaces)
+               (phps-mode-test-lexer-errors)
+               (message "Finished iteration %s" iteration)
+               (setq iteration (1+ iteration))))))
+      (message "Lexer tests completed in: %ss." elapsed))))
+
 (defun phps-mode-test-lexer ()
   "Run test for lexer."
   ;; (message "-- Running all tests for lexer... --\n")
   ;; (setq debug-on-error t)
+  
   (phps-mode-test-lexer-script-boundaries)
   (phps-mode-test-lexer-simple-tokens)
   (phps-mode-test-lexer-complex-tokens)
@@ -441,9 +461,8 @@
   (phps-mode-test-lexer-get-moved-lexer-tokens)
   (phps-mode-test-lexer-get-moved-lexer-states)
   ;; (message "\n-- Ran all tests for lexer. --")
-  )
 
-(phps-mode-test-lexer)
+  )
 
 (provide 'phps-mode-test-lexer)
 



reply via email to

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