branch: elpa/raku-mode
commit 4f1e2928d2c56732dea509a758a065acf477f72c
Author: Tom Browder <[email protected]>
Commit: Tom Browder <[email protected]>
add more generalization with more defined regexes, remove commented out old
code
---
perl6-imenu.el | 71 +++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 51 insertions(+), 20 deletions(-)
diff --git a/perl6-imenu.el b/perl6-imenu.el
index 9681820e56..68f1a9bfc3 100644
--- a/perl6-imenu.el
+++ b/perl6-imenu.el
@@ -2,45 +2,76 @@
;; Imenu functions and variables are defined here.
-;; TODO: tighten regexes to distinguish correct naming for Perl 6
-;; vs. NQP (make a minor mode?)
+;; Definition of "identifiers" (names) from
+;; https://docs.perl6.org/language/syntax#Identifiers
+;;
+;; Identifiers are a grammatical building block that occur in several
+;; places. An identifier is a primitive name, and must start with an
+;; alphabetic character (or an underscore), followed by zero or more
+;; word characters (alphabetic, underscore or number). You can also
+;; embed dashes - or single quotes ' in the middle, but not two in a
+;; row, and only if followed immediately by an alphabetic character.
+;;
+;; For NQP names, no embedded hyphens or single quotes are allowed.
;; Regex definitions:
+(defvar perl6-name-regex
+ (concat
+ "[_[:alpha:]]" ; mandatory leading character
+ "\\(?:[-']?[[:alpha:]]\\|[_[:alnum:]]\\)*" ; rest of the name allowing
embedded hyphens or single quotes
+ ))
+
+(defvar nqp-name-regex
+ (concat
+ "[_[:alpha:]]" ; mandatory leading character
+ "[_[:alnum:]]*" ; rest of the name (stricter than Perl 6 name)
+ ))
+
(defvar perl6-vars-regex
(concat
- "^\\s-*" ; leading ws allowed
- "\\(?:my\\|our\\)\\s-+" ; scope of var, followed by mandatory ws
- "\\(" ; start capture group 1 for the var name
- "\\(?:\\$\\|@\\|%\\)" ; sigil for type of var
- "\\(?:[-_[:alnum:]]+\\)" ; the var name ends with ws
- "\\)" ; end of capture group 1
+ "^\\s-*" ; leading ws allowed
+ "\\(?:my\\|our\\)\\s-+" ; scope of var, followed by mandatory ws
+ "\\(" ; start capture group 1 for the var name
+ "\\(?:\\$\\|@\\|%\\)" ; sigil for type of var
+ "\\(?:" ; start shy group for choice of one type name
+ perl6-name-regex
+ "\\|"
+ nqp-name-regex
+ "\\)" ; end shy group
+ "\\)" ; end of capture group 1
))
(defvar perl6-subs-regex
(concat
- "^\\s-*" ; leading ws allowed
- "\\(?:my\\s-+\\|our\\s-+\\)?" ; optional specific scope followed by at
least one space
- ; must have one of the five type identifiers followed by at least one
space:
-
"\\(?:multi\\s-+sub\\|multi\\s-+method\\|sub\\|method\\|multi\\|proto\\)\\s-+"
- "\\([-_[:alnum:]]+\\)" ; the capture group of the sub name
+ "^\\s-*" ; leading ws allowed
+ "\\(?:my\\s-+\\|our\\s-+\\)?" ; optional specific scope followed by at
least one space
+ ; must have one of the five type identifiers
+ ; followed by at least one space:
+
"\\(?:multi\\s-+sub\\|multi\\s-+method\\|sub\\|method\\|multi\\|proto\\)\\s-+"
+ "\\(" ; start capture group 1 for the sub name
+ perl6-name-regex
+ "\\|"
+ nqp-name-regex
+ "\\)" ; end of capture group 1
))
(defvar perl6-classes-regex
(concat
- "^\\s-*" ; leading ws allowed
- ; must have one of the four type identifiers followed by at least one
space:
- "class\\s-+"
- "\\([-_[:alnum:]]+\\)" ; the capture group of the sub
name
- ;"[\\n\\s\\-{]+" ; ended by whitespace or an opening curly
brace'
+ "^\\s-*" ; leading ws allowed
+ ; must have one of the four type identifiers followed by
at least one space:
+ "class\\s-+"
+ "\\(" ; start capture group 1 of the class name
+ perl6-name-regex
+ "\\|"
+ nqp-name-regex
+ "\\)" ; end of capture group 1
))
(defvar perl6-imenu-generic-expression
`(
;; the names are in reverse desired order since they are evaluated here
last first
- ;("Variables"
"^\\s-*\\(?:my\\|our\\)\\s-+\\(\\(?:\\$\\|@\\|%\\)\\(?:[_[:alnum:]]+\\)\\)" 1)
("Classes" ,perl6-classes-regex 1)
("Variables" ,perl6-vars-regex 1)
- ;;("Subs/Methods"
"^\\s-*\\(?:my\\s-+\\|our\\s-+\\)?\\(?:multi\\s-+sub\\|multi\\s-+method\\|sub\\|method\\|multi\\)\\s-+\\(.+)\\)"
1)
("Subs/Methods" ,perl6-subs-regex 1)
)
"Define interesting points in the Perl 6 buffer for `imenu'.