branch: externals/phps-mode
commit a034b63d561deea7a83e880118ea20976ec7516d
Author: Christian Johansson <christ...@cvj.se>
Commit: Christian Johansson <christ...@cvj.se>

    Started on bookkeeping global constants
---
 phps-mode-parser-sdt.el    | 55 ++++++++++++++++++++++++++++++++++++++++------
 test/phps-mode-test-ast.el | 15 ++++++++-----
 2 files changed, 58 insertions(+), 12 deletions(-)

diff --git a/phps-mode-parser-sdt.el b/phps-mode-parser-sdt.el
index 18d51d34c5..20bb3b93af 100644
--- a/phps-mode-parser-sdt.el
+++ b/phps-mode-parser-sdt.el
@@ -4259,10 +4259,18 @@
 (puthash 283 (lambda(_args _terminals) 'T_ELLIPSIS) 
phps-mode-parser--table-translations)
 
 ;; 284 ((non_empty_argument_list) (argument))
-(puthash 284 (lambda(args _terminals) (list (nth 0 args))) 
phps-mode-parser--table-translations)
+(puthash
+ 284
+ (lambda(args _terminals)
+   (list args))
+ phps-mode-parser--table-translations)
 
 ;; 285 ((non_empty_argument_list) (non_empty_argument_list "," argument))
-(puthash 285 (lambda(args _terminals) (append (nth 0 args) (list (nth 2 
args)))) phps-mode-parser--table-translations)
+(puthash
+ 285
+ (lambda(args _terminals)
+   (append (nth 0 args) (list (nth 2 args))))
+ phps-mode-parser--table-translations)
 
 ;; 286 ((argument) (expr))
 (puthash
@@ -4271,8 +4279,6 @@
    `(
      ast-type
      argument
-     type
-     nil
      value
      ,args
      )
@@ -4285,8 +4291,8 @@
  (lambda(args _terminals)
    `(
      ast-type
-     argument
-     type
+     named-argument
+     name
      ,(nth 0 args)
      value
      ,(nth 2 args)
@@ -6734,7 +6740,42 @@
 ;; 454 ((function_call) (name argument_list))
 (puthash
  454
- (lambda(args _terminals)
+ (lambda(args terminals)
+   (when (string= (downcase (nth 0 args)) "define")
+     (let* ((arguments (nth 1 args))
+            (key-argument (nth 0 arguments))
+            (key-argument-type (plist-get key-argument 'ast-type)))
+       (when (equal key-argument-type 'argument))
+       (let* ((key-argument-value (plist-get key-argument 'value))
+              (key-argument-value-type (plist-get key-argument-value 
'ast-type)))
+         (when (equal key-argument-value-type 'expr-scalar)
+           (let* ((key-scalar (plist-get key-argument-value 'scalar))
+                  (key-scalar-type (plist-get key-scalar 'ast-type)))
+             (when (equal key-scalar-type 'scalar-dereferencable-scalar)
+               (let* ((dereferenced-scalar (plist-get key-scalar 
'dereferenceable-scalar))
+                      (dereferenced-scalar-type (plist-get dereferenced-scalar 
'ast-type)))
+                 (when
+                     (equal
+                      dereferenced-scalar-type
+                      'dereferencable-scalar-constant-encapsed-string)
+                   (let ((constant-name
+                          (substring
+                           (plist-get
+                            dereferenced-scalar
+                            'constant-encapsed-string)
+                           1
+                           -1))
+                         (constant-start
+                          (1+ (car (cdr (nth 0 (nth 1 (nth 1 terminals)))))))
+                         (constant-end
+                          (1- (cdr (cdr (nth 0 (nth 1 (nth 1 terminals))))))))
+                     (push
+                      (list
+                       constant-name
+                       nil
+                       constant-start
+                       constant-end)
+                      
phps-mode-parser-sdt--bookkeeping-symbol-assignment-stack))))))))))
    `(
      ast-type
      function-call
diff --git a/test/phps-mode-test-ast.el b/test/phps-mode-test-ast.el
index 147b654abc..618a2e2f87 100644
--- a/test/phps-mode-test-ast.el
+++ b/test/phps-mode-test-ast.el
@@ -37,15 +37,14 @@
      (message "\n")
      (phps-mode-ast--generate)
 
-     ;; (message "symbol-table: \n%S\n"
-     ;;          (phps-mode-test--hash-to-list
-     ;;           phps-mode-parser-sdt-symbol-table))
-     ;; (message "phps-mode-parser-sdt-symbol-table-by-uri: \n%S\n" 
phps-mode-parser-sdt-symbol-table-by-uri)
-
      (unless (equal
               (phps-mode-test--hash-to-list
                phps-mode-parser-sdt-bookkeeping)
               expected-bookkeeping)
+       (message "symbol-table: \n%S\n"
+                (phps-mode-test--hash-to-list
+                 phps-mode-parser-sdt-symbol-table))
+       (message "phps-mode-parser-sdt-symbol-table-by-uri: \n%S\n" 
phps-mode-parser-sdt-symbol-table-by-uri)
        (message
         "expected-bookkeeping:\n%S\n"
         expected-bookkeeping)
@@ -135,6 +134,12 @@
   ;; TODO v2 Should properly bookkeep inside potentially endlessly nested 
anonymous functions / arrow functions / anonymous classes
   ;; TODO v2 bookkeep and include all kind of constants in imenu
 
+  (phps-mode-test-ast--should-bookkeep
+   "<?php\n\nnamespace mySpace\n{\n    define('MY_CONSTANT', 'abc123');\n    
const MY_CONSTANT2 = 'def456';\n\n    if (\\MY_CONSTANT) {\n        echo 
'hit';\n    }\n    if (MY_CONSTANT) {\n        echo 'hit';\n    }\n    if 
(MY_CONSTANT2) {\n        echo 'hit';\n    }\n    if (\\mySpace\\MY_CONSTANT2) 
{\n        echo 'hit';\n    }\n\n    if (\\YOUR_CONSTANT) {\n        echo 
'miss';\n    }\n    if (YOUR_CONSTANT) {\n        echo 'miss';\n    }\n    if 
(\\MY_CONSTANT2) {\n        echo 'mis [...]
+   "Constants in all possible scopes"
+   '(("abc"))
+   '(("abc")))
+
   (phps-mode-test-ast--should-bookkeep
    "<?php\n\n$var = 'abc';\n\nif ($var2) {\n    echo 'This never 
happens';\n}\nif ($var) {\n    echo 'This happens';\n}"
    "Bookkeeping in root level variable assignments #1"

Reply via email to