branch: externals/phps-mode commit c83b1c264eb83e0ba6e13afe0f2f53362bf3b36c Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Fixed SDT for isset_variables --- phps-mode-ast-bookkeeping.el | 87 ++++++++++++++++++++++++++++++++++---------- phps-mode-parser-sdt.el | 11 +++++- test/phps-mode-test-ast.el | 2 +- 3 files changed, 78 insertions(+), 22 deletions(-) diff --git a/phps-mode-ast-bookkeeping.el b/phps-mode-ast-bookkeeping.el index ab798591aa..1054f87282 100644 --- a/phps-mode-ast-bookkeeping.el +++ b/phps-mode-ast-bookkeeping.el @@ -105,8 +105,6 @@ name)) scope-string)) -;; TODO Should return a list of strings to support the case with a read-only -;; variable inside a arrow function that should check outer scopes as well (defun phps-mode-ast-bookkeeping--generate-variable-scope-string (scope name &optional read-only) "Generate variable scope string from SCOPE and NAME and optionally READ-ONLY." @@ -170,6 +168,16 @@ scope-string scope-name))) + ((and + (equal scope-type 'defined) + scope-name) + (setq + scope-string + (format + "%s defined %s" + scope-string + scope-name))) + ((and (equal scope-type 'arrow-function) scope-name) @@ -212,7 +220,8 @@ (let ((bookkeeping (make-hash-table :test 'equal)) (bookkeeping-stack phps-mode-ast--tree) (inline-function-count 0) - (arrow-function-count 0)) + (arrow-function-count 0) + (defined-count 0)) (while bookkeeping-stack (let ((item-raw (pop bookkeeping-stack)) (item) @@ -346,12 +355,58 @@ (push `(,sub-scope ,child) bookkeeping-stack))))) ((equal type 'if) - (when-let ((children (reverse (plist-get item 'children)))) - (dolist (child children) - (push `(,scope, child) bookkeeping-stack))) - (when-let ((conditions (reverse (plist-get item 'condition)))) + (let ((conditions (reverse (plist-get item 'condition))) + (found-defined-scope) + (sub-scope scope)) (dolist (condition conditions) - (push `(,scope ,condition) bookkeeping-stack)))) + (when-let ((condition-type (plist-get condition 'ast-type))) + (cond + + ((equal condition-type 'isset-variables) + (let ((sub-scope scope)) + (unless found-defined-scope + (setq defined-count (1+ defined-count)) + (setq found-defined-scope t)) + (push `(type defined name ,defined-count) sub-scope) + (let ((isset-variables (plist-get condition 'variables))) + (dolist (isset-variable isset-variables) + (let ((id + (phps-mode-ast-bookkeeping--generate-variable-scope-string + sub-scope + (plist-get isset-variable 'name)))) + (puthash + id + 1 + bookkeeping)))))) + + ((and + (equal condition-type 'negated-expression) + (equal (plist-get (plist-get condition 'expression) 'ast-type) 'empty-expression)) + (let ((sub-scope scope)) + (unless found-defined-scope + (setq defined-count (1+ defined-count)) + (setq found-defined-scope t)) + (push `(type defined name ,defined-count) sub-scope) + (let ((not-empty-variables (plist-get (plist-get condition 'expression) 'variables))) + (dolist (not-empty-variable not-empty-variables) + (let ((id + (phps-mode-ast-bookkeeping--generate-variable-scope-string + sub-scope + (plist-get not-empty-variable 'name)))) + (puthash + id + 1 + bookkeeping)))))) + + ))) + (when found-defined-scope + (push `(type defined name ,defined-count) sub-scope)) + (when-let ((children (reverse (plist-get item 'children)))) + (dolist (child children) + (push `(,sub-scope, child) bookkeeping-stack))) + (when conditions + (dolist (condition conditions) + (push `(,sub-scope ,condition) bookkeeping-stack))))) ((equal type 'foreach) (when-let ((children (reverse (plist-get item 'children)))) @@ -568,13 +623,7 @@ (push `(,scope ,(plist-get item 'variable)) bookkeeping-stack)) ((equal type 'negated-expression) - (let ((expression (plist-get item 'expression))) - ;; TODO Define sub-scope here - (when (equal (plist-get expression 'ast-type) 'empty-expression) - (let ((not-empty-variables (reverse (plist-get expression 'variables)))) - ;; TODO Define variable here - )) - (push `(,scope ,expression) bookkeeping-stack))) + (push `(,scope ,(plist-get item 'expression)) bookkeeping-stack)) ((equal type 'try) (when-let ((children (reverse (plist-get item 'inner-statement-list)))) @@ -636,10 +685,10 @@ (phps-mode-ast-bookkeeping--generate-symbol-scope-string sub-scope property-name)) - (bookkeeping-object - (list - (plist-get item 'property-start) - (plist-get item 'property-end)))) + (bookkeeping-object + (list + (plist-get item 'property-start) + (plist-get item 'property-end)))) ;; (message "dereferenced: %S %S" variable-id symbol-id) (when (gethash symbol-id bookkeeping) (setq diff --git a/phps-mode-parser-sdt.el b/phps-mode-parser-sdt.el index c1f022d112..0a20e3414e 100644 --- a/phps-mode-parser-sdt.el +++ b/phps-mode-parser-sdt.el @@ -1030,7 +1030,7 @@ (puthash 538 (lambda(args _terminals) - `(ast-type isset-variables variables ,(nth 2 args))) + `(ast-type isset-variables variables ,(phps-mode-parser-sdt--get-list-of-object (nth 2 args)))) phps-mode-parser--table-translations) ;; internal_functions_in_yacc -> (T_EMPTY "(" expr ")") @@ -1044,7 +1044,7 @@ (puthash 545 (lambda(args _terminals) - (list args)) + args) phps-mode-parser--table-translations) ;; isset_variables -> (isset_variables "," isset_variable) @@ -1054,5 +1054,12 @@ (append (nth 0 args) (nth 2 args))) phps-mode-parser--table-translations) +;; isset_variable -> (expr) +(puthash + 547 + (lambda(args _terminals) + (list args)) + phps-mode-parser--table-translations) + (provide 'phps-mode-parser-sdt) ;;; phps-mode-parser-sdt.el ends here diff --git a/test/phps-mode-test-ast.el b/test/phps-mode-test-ast.el index 43a557a682..e58001c914 100644 --- a/test/phps-mode-test-ast.el +++ b/test/phps-mode-test-ast.el @@ -603,7 +603,7 @@ (should (equal (phps-mode-test--hash-to-list phps-mode-ast-bookkeeping--index t) - '((" defined 1 id $x" 1) ((18 20) 1) ((33 35) 1) (" defined 2 id $i" 1) ((77 79) 1) (" defined 2 id $u" 1) ((81 83) 1) ((104 106) 1) ((168 170) 1) ((232 234) 1) ((302 304) 0) ((355 357) 0) ((408 410) 0) (" defined 3 id $y" 1) ((445 447) 1) ((460 462) 1) (" defined 4 id $k" 1) ((505 507) 1) (" defined 4 id $L" 1) ((519 521) 1) ((542 544) 1) ((606 608) 1) ((670 672) 1) ((740 742) 0) ((793 795) 0) ((846 848) 0)))))) + '((" defined 1 id $x" 1) ((18 20) 1) ((33 35) 1) (" defined 2 id $i" 1) (" defined 2 id $u" 1) ((77 79) 1) ((81 83) 1) ((104 106) 1) ((168 170) 1) ((232 234) 1) ((302 304) 0) ((355 357) 0) ((408 410) 0) (" defined 3 id $y" 1) ((445 447) 1) ((460 462) 1) (" defined 4 id $k" 1) ((505 507) 1) (" defined 4 id $L" 1) ((519 521) 1) ((542 544) 1) ((606 608) 1) ((670 672) 1) ((740 742) 0) ((793 795) 0) ((846 848) 0)))))) (phps-mode-test-ast--buffer-contents "<?php\ninterface myInterface\n{\n function myFunction1();\n function myFunction2($x);\n}\n"