Skip to content

Commit bb38ae2

Browse files
CIAvashalecthomas
authored andcommitted
Raku: Fix detecting adverbs, plus a little cleanup
1 parent 2eba3ce commit bb38ae2

File tree

3 files changed

+15
-28
lines changed

3 files changed

+15
-28
lines changed

lexers/r/raku.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ func rakuRules() Rules {
4949
const (
5050
colonPairOpeningBrackets = `(?:<<|<|«|\(|\[|\{)`
5151
colonPairClosingBrackets = `(?:>>|>|»|\)|\]|\})`
52-
colonPairPattern = `(?<colon>:)(?<key>\w[\w'-]*)(?<opening_delimiters>` + colonPairOpeningBrackets + `)`
53-
namePattern = `((?:(?!` + colonPairPattern + `)[\w':-])+)`
52+
colonPairPattern = `(?<!:)(?<colon>:)(?<key>\w[\w'-]*)(?<opening_delimiters>` + colonPairOpeningBrackets + `)`
53+
colonPairLookahead = `(?=(:['\w-]+` +
54+
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `)?`
55+
namePattern = `((?:(?!` + colonPairPattern + `)(?:::|[\w':-]))+)`
5456
variablePattern = `[$@%&]+[.^:?=!~]?` + namePattern
5557
globalVariablePattern = `[$@%&]+\*` + namePattern
5658
)
@@ -601,18 +603,10 @@ func rakuRules() Rules {
601603
Include("colon-pair"),
602604
// Token
603605
{
604-
// Token with adverbs
605-
`(?<=(?:^|\s)(?:regex|token|rule)(\s+))(['\w:-]+)(?=:['\w-]+` +
606-
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `\s*[({])`,
606+
`(?<=(?:^|\s)(?:regex|token|rule)(\s+))` + namePattern + colonPairLookahead + `\s*[({])`,
607607
NameFunction,
608608
Push("token", "name-adverb"),
609609
},
610-
{
611-
// Token without adverbs
612-
`(?<=(?:^|\s)(?:regex|token|rule)(?:\s+))(['\w:-]+)`,
613-
NameFunction,
614-
Push("token"),
615-
},
616610
// Substitution
617611
{`(?<=^|\b|\s)(?<!\.)(ss|S|s|TR|tr)\b(\s*)`, ByGroups(Keyword, Text), Push("substitution")},
618612
{keywordsPattern, Keyword, nil},
@@ -626,18 +620,10 @@ func rakuRules() Rules {
626620
},
627621
// Routine
628622
{
629-
// Routine with adverbs
630-
`(?<=(?:^|\s)(?:sub|method|multi sub|multi)\s+)!?['\w:-]+(?=:['\w-]+` +
631-
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `\s*[({])`,
623+
`(?<=(?:^|\s)(?:sub|method|multi sub|multi)\s+)!?` + namePattern + colonPairLookahead + `\s*[({])`,
632624
NameFunction,
633625
Push("name-adverb"),
634626
},
635-
{
636-
// Routine without adverbs
637-
`(?<=(?:^|\s)(?:sub|submethod|method|multi)\s+)!?['\w:-]+`,
638-
NameFunction,
639-
nil,
640-
},
641627
// Constant
642628
{`(?<=\bconstant\s+)` + namePattern, NameConstant, Push("name-adverb")},
643629
// Namespace
@@ -655,22 +641,16 @@ func rakuRules() Rules {
655641
},
656642
// Function
657643
{
658-
`\b(?:\w['\w:-]*)(?=:['\w-]+` +
659-
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `\()`,
644+
`\b` + namePattern + colonPairLookahead + `\()`,
660645
NameFunction,
661646
Push("name-adverb"),
662647
},
663-
{`\b(?:\w['\w:-]*)(?=\()`, NameFunction, nil},
664648
// Method
665-
// Method with adverb
666649
{
667-
`(?<!\.\.[?^*+]?)(?<=(?:\.[?^*+&]?)|self!)['\w:-]+(?=:['\w-]+` +
668-
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `$)`,
650+
`(?<!\.\.[?^*+]?)(?<=(?:\.[?^*+&]?)|self!)` + namePattern + colonPairLookahead + `\b)`,
669651
NameFunction,
670652
Push("name-adverb"),
671653
},
672-
// Method without adverb
673-
{`(?<!\.\.[?^*+]?)(?<=(?:\.[?^*+&]?)|self!)['\w:-]+`, NameFunction, nil},
674654
// Indirect invocant
675655
{namePattern + `(?=\s+\W?['\w:-]+:\W)`, NameFunction, Push("name-adverb")},
676656
{`(?<=\W)(?:∅|i|e|𝑒|tau|τ|pi|π|Inf|∞)(?=\W)`, NameConstant, nil},

lexers/testdata/raku.actual

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ sub f2($a,$b) {
3232

3333
2.&f2(3);
3434

35+
Module::function($var);
36+
3537
#| Fibonacci with Multiple dispatch
3638
multi sub fib (0 --> 0) {}
3739
multi sub fib (1 --> 1) {}

lexers/testdata/raku.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@
102102
{"type":"LiteralNumberInteger","value":"3"},
103103
{"type":"Punctuation","value":");"},
104104
{"type":"Text","value":"\n\n"},
105+
{"type":"NameFunction","value":"Module::function"},
106+
{"type":"Punctuation","value":"("},
107+
{"type":"NameVariable","value":"$var"},
108+
{"type":"Punctuation","value":");"},
109+
{"type":"Text","value":"\n\n"},
105110
{"type":"Keyword","value":"#| "},
106111
{"type":"LiteralStringDoc","value":"Fibonacci with Multiple dispatch\n"},
107112
{"type":"Keyword","value":"multi"},

0 commit comments

Comments
 (0)