branch: externals/phps-mode commit a629baba89b3f4da9fc9ef53d09e61f4b0131946 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
More work on bookkeeping via AST for arrow function variables --- phps-mode-ast-bookkeeping.el | 58 +++++++++++++++++++++++++++++++++++++++----- phps-mode-parser-sdt.el | 2 +- test/phps-mode-test-ast.el | 2 +- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/phps-mode-ast-bookkeeping.el b/phps-mode-ast-bookkeeping.el index eeeab5f929..4e2a713399 100644 --- a/phps-mode-ast-bookkeeping.el +++ b/phps-mode-ast-bookkeeping.el @@ -78,6 +78,16 @@ scope-string scope-name))) + ((and + (equal scope-type 'arrow-function) + scope-name) + (setq + scope-string + (format + "%s arrow function %s" + scope-string + scope-name))) + ((and (equal scope-type 'static) (setq @@ -96,8 +106,8 @@ scope-string)) (defun phps-mode-ast-bookkeeping--generate-variable-scope-string - (scope name) - "Generate variable scope string from SCOPE and NAME." + (scope name &optional read-only) + "Generate variable scope string from SCOPE and NAME and optionally READ-ONLY." (let ((scope-string "") (namespace)) (dolist (bubble (reverse scope)) @@ -150,6 +160,17 @@ scope-string scope-name))) + ((and + (equal scope-type 'arrow-function) + scope-name + (not read-only)) + (setq + scope-string + (format + "%s arrow function %s" + scope-string + scope-name))) + ((and (equal scope-type 'static) (setq @@ -171,7 +192,8 @@ "Generate AST for current buffer." (let ((bookkeeping (make-hash-table :test 'equal)) (bookkeeping-stack phps-mode-ast--tree) - (inline-function-count 0)) + (inline-function-count 0) + (arrow-function-count 0)) (while bookkeeping-stack (let ((item-raw (pop bookkeeping-stack)) (item) @@ -625,11 +647,35 @@ ((equal type 'static-inline-function) (push `(,scope ,(plist-get item 'inline-function)) bookkeeping-stack)) + ((equal type 'arrow-function) + (let ((sub-scope scope)) + (setq arrow-function-count (1+ arrow-function-count)) + (push `(type arrow-function name ,arrow-function-count) sub-scope) + (when-let ((inner-statement-list (reverse (plist-get item 'inner-statement-list)))) + (dolist (inner-statement inner-statement-list) + (push `(,sub-scope ,inner-statement) bookkeeping-stack))) + (when-let ((parameter-list (plist-get item 'parameter-list))) + (dolist (parameter parameter-list) + (let ((id + (phps-mode-ast-bookkeeping--generate-variable-scope-string + sub-scope + (plist-get parameter 'name))) + (object + (list + (plist-get parameter 'start) + (plist-get parameter 'end)))) + (puthash + id + 1 + bookkeeping) + (puthash + object + 1 + bookkeeping)))))) + ((equal type 'inline-function) - (setq - inline-function-count - (1+ inline-function-count)) (let ((sub-scope scope)) + (setq inline-function-count (1+ inline-function-count)) (push `(type inline-function name ,inline-function-count) sub-scope) (when-let ((inner-statement-list (reverse (plist-get item 'inner-statement-list)))) (dolist (inner-statement inner-statement-list) diff --git a/phps-mode-parser-sdt.el b/phps-mode-parser-sdt.el index 1112367852..d37f16630d 100644 --- a/phps-mode-parser-sdt.el +++ b/phps-mode-parser-sdt.el @@ -748,7 +748,7 @@ (let ((ast-object (list 'ast-type - 'inline-function + 'arrow-function 'start (car (cdr (nth 9 terminals))) 'end diff --git a/test/phps-mode-test-ast.el b/test/phps-mode-test-ast.el index 883d2b8eb2..77332896c2 100644 --- a/test/phps-mode-test-ast.el +++ b/test/phps-mode-test-ast.el @@ -537,7 +537,7 @@ (should (equal (phps-mode-test--hash-to-list phps-mode-ast-bookkeeping--index t) - '((" id $y" 1) ((7 9) 1) (" id $fn1" 1) ((15 19) 1) (" anonymous function 1 id $x" 1) ((25 27) 1) ((32 34) 1) ((37 39) 1) (" id $z" 1) ((41 43) 1) (" id $fn" 1) ((49 52) 1) (" anonymous function 2 id $x2" 1) ((58 61) 1) (" anonymous function 2 anonymous function 3 id $y2" 1) ((69 72) 1) ((77 80) 1) ((83 86) 1) ((89 91) 1) (" anonymous function 3 id $x3" 1) ((102 105) 1) ((110 113) 1) (" id $x4" 1) ((115 118) 1) ((144 147) 1) (" anonymous function 6 id $x5" 1) ((152 155) 1) ((165 1 [...] + '((" id $y" 1) ((7 9) 1) (" id $fn1" 1) ((15 19) 1) (" arrow function 1 id $x" 1) ((25 27) 1) ((32 34) 1) ((37 39) 1) (" id $z" 1) ((41 43) 1) (" id $fn" 1) ((49 52) 1) (" arrow function 2 id $x2" 1) ((58 61) 1) (" arrow function 2 arrow function 3 id $y2" 1) ((69 72) 1) ((77 80) 1) ((83 86) 1) ((89 91) 1) (" arrow function 3 id $x3" 1) ((102 105) 1) ((110 113) 1) (" id $x4" 1) ((115 118) 1) ((144 147) 1) (" arrow function 6 id $x5" 1) ((152 155) 1) ((165 168) 1) (" arrow function [...] (phps-mode-test-ast--buffer-contents "<?php\n$z = (object) array('name' => 'random');\nif ($z->name) {\n echo 'Hit';\n}"