[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode dab0cf5562 1/2: Improved support for escaped
From: |
Christian Johansson |
Subject: |
[elpa] externals/phps-mode dab0cf5562 1/2: Improved support for escaped characters in double quoted strings |
Date: |
Thu, 3 Nov 2022 01:13:05 -0400 (EDT) |
branch: externals/phps-mode
commit dab0cf55621f7ab21c076faafadf8f7facd395a1
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>
Improved support for escaped characters in double quoted strings
---
phps-mode-lexer.el | 30 +++++++++++++-----------------
test/phps-mode-test-lexer.el | 7 +++++++
2 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/phps-mode-lexer.el b/phps-mode-lexer.el
index 9b29e7d1d8..6754ce5cc1 100644
--- a/phps-mode-lexer.el
+++ b/phps-mode-lexer.el
@@ -123,7 +123,7 @@
(defvar phps-mode-lexer--lambdas-by-state #s(hash-table size 65 test equal
rehash-size 1.5 rehash-threshold 0.8125 data (ST_IN_SCRIPTING (((lambda nil
(looking-at "exit")) (lambda nil (phps-mode-lexer--return-token-with-indent
'T_EXIT))) ((lambda nil (looking-at "die")) (lambda nil
(phps-mode-lexer--return-token-with-indent 'T_EXIT))) ((lambda nil (looking-at
"fn")) (lambda nil (phps-mode-lexer--return-token-with-indent 'T_FN))) ((lambda
nil (looking-at "function")) (lambda nil (phps-mod [...]
]*("))) (lambda nil (phps-mode-lexer--yyless (length "readonly"))
(phps-mode-lexer--return-token-with-str 'T_STRING 0))) ((lambda nil (looking-at
"unset")) (lambda nil (phps-mode-lexer--return-token-with-indent 'T_UNSET)))
((lambda nil (looking-at "=>")) (lambda nil (phps-mode-lexer--return-token
'T_DOUBLE_ARROW))) ((lambda nil (looking-at "list")) (lambda nil
(phps-mode-lexer--return-token-with-indent 'T_LIST))) ((lambda nil (looking-at
"array")) (lambda nil (phps-mode-lexer--return-tok [...]
-]*" "\\(\\$\\|\\.\\.\\.\\)"))) (lambda nil (phps-mode-lexer--yyless 1)
(phps-mode-lexer--return-token 'T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG
(match-beginning 0) (- (match-end 0) 1)))) ((lambda nil (looking-at "&"))
(lambda nil (phps-mode-lexer--return-token
'T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG))) ((lambda nil (looking-at (concat
"\\(" "]" "\\|" ")" "\\)"))) (lambda nil
(phps-mode-lexer--return-exit-nesting-token))) ((lambda nil (looking-at (concat
"\\(" "\\[" "\\|" "(" "\\)"))) (la [...]
+]*" "\\(\\$\\|\\.\\.\\.\\)"))) (lambda nil (phps-mode-lexer--yyless 1)
(phps-mode-lexer--return-token 'T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG
(match-beginning 0) (- (match-end 0) 1)))) ((lambda nil (looking-at "&"))
(lambda nil (phps-mode-lexer--return-token
'T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG))) ((lambda nil (looking-at (concat
"\\(" "]" "\\|" ")" "\\)"))) (lambda nil
(phps-mode-lexer--return-exit-nesting-token))) ((lambda nil (looking-at (concat
"\\(" "\\[" "\\|" "(" "\\)"))) (la [...]
[ ]*" phps-mode-lexer--heredoc-label ";?
\\|\\$" phps-mode-lexer--label "\\|{\\$" phps-mode-lexer--label "\\|\\${"
phps-mode-lexer--label "\\)") nil t))) (if string-start (let* ((start
(match-beginning 0)) (end (match-end 0)) (data (buffer-substring-no-properties
start end))) (cond ((string-match-p (concat "
[ ]*" phps-mode-lexer--heredoc-label ";?
@@ -1682,31 +1682,27 @@
;; Do we find a ending double quote or starting variable?
(if string-start
(let ((string-start (match-beginning 0))
- (is-escaped nil)
- (is-escaped-1 nil)
- (is-escaped-2 nil))
+ (last-character-is-escape-character t)
+ (is-escaped nil))
- ;; Check whether one character back is escape character
- (goto-char (1- string-start))
- (setq is-escaped-1 (looking-at-p "\\\\"))
-
- ;; Check whether two characters back is escape character
- (goto-char (- string-start 2))
- (setq is-escaped-2 (looking-at-p "\\\\"))
+ ;; Do we find variable inside quote?
+ (goto-char string-start)
- (setq is-escaped
- (and
- is-escaped-1
- (not is-escaped-2)))
+ ;; Backtrack until we find a character that is not a escape
character
+ (while last-character-is-escape-character
+ (forward-char -1)
+ (if (looking-at-p "\\\\")
+ (setq is-escaped (not is-escaped))
+ (setq last-character-is-escape-character nil)))
;; Do we find variable inside quote?
(goto-char string-start)
- ;; Process character if it's not escaped
+ ;; Process character but only if it's not escaped
(if is-escaped
(forward-char 1)
(setq open-quote nil)
- (if (looking-at "\"")
+ (if (looking-at-p "\"")
(let ((_double-quoted-string
(buffer-substring-no-properties start (+
string-start 1))))
;; (message "Double quoted string: %s"
_double-quoted-string)
diff --git a/test/phps-mode-test-lexer.el b/test/phps-mode-test-lexer.el
index d123af88e9..32a5911a0d 100644
--- a/test/phps-mode-test-lexer.el
+++ b/test/phps-mode-test-lexer.el
@@ -65,6 +65,13 @@
phps-mode-lex-analyzer--tokens
'((T_OPEN_TAG 1 . 7) (T_VARIABLE 8 . 10) ("=" 11 . 12)
(T_CONSTANT_ENCAPSED_STRING 13 . 18) (";" 18 . 19) (T_ECHO 21 . 25) ("\"" 26 .
27) (T_ENCAPSED_AND_WHITESPACE 27 . 32) (T_CURLY_OPEN 32 . 33) (T_VARIABLE 33 .
35) ("}" 35 . 36) (T_ENCAPSED_AND_WHITESPACE 36 . 38) ("\"" 38 . 39) (";" 39 .
40)))))
+ (phps-mode-test--with-buffer
+ "<?php echo \"\\\\\\\"\";"
+ "Another tricky case where escape character is escaped"
+ (should (equal
+ phps-mode-lex-analyzer--tokens
+ '((T_OPEN_TAG 1 . 7) (T_ECHO 7 . 11) (T_CONSTANT_ENCAPSED_STRING
12 . 18) (";" 18 . 19)))))
+
)
(defun phps-mode-test-lexer--simple-tokens ()