Skip to content

Commit

Permalink
Raku: Fix detecting adverbs, plus a little cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CIAvash authored and alecthomas committed Aug 28, 2021
1 parent 2eba3ce commit bb38ae2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
36 changes: 8 additions & 28 deletions lexers/r/raku.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ func rakuRules() Rules {
const (
colonPairOpeningBrackets = `(?:<<|<|«|\(|\[|\{)`
colonPairClosingBrackets = `(?:>>|>|»|\)|\]|\})`
colonPairPattern = `(?<colon>:)(?<key>\w[\w'-]*)(?<opening_delimiters>` + colonPairOpeningBrackets + `)`
namePattern = `((?:(?!` + colonPairPattern + `)[\w':-])+)`
colonPairPattern = `(?<!:)(?<colon>:)(?<key>\w[\w'-]*)(?<opening_delimiters>` + colonPairOpeningBrackets + `)`
colonPairLookahead = `(?=(:['\w-]+` +
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `)?`
namePattern = `((?:(?!` + colonPairPattern + `)(?:::|[\w':-]))+)`
variablePattern = `[$@%&]+[.^:?=!~]?` + namePattern
globalVariablePattern = `[$@%&]+\*` + namePattern
)
Expand Down Expand Up @@ -601,18 +603,10 @@ func rakuRules() Rules {
Include("colon-pair"),
// Token
{
// Token with adverbs
`(?<=(?:^|\s)(?:regex|token|rule)(\s+))(['\w:-]+)(?=:['\w-]+` +
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `\s*[({])`,
`(?<=(?:^|\s)(?:regex|token|rule)(\s+))` + namePattern + colonPairLookahead + `\s*[({])`,
NameFunction,
Push("token", "name-adverb"),
},
{
// Token without adverbs
`(?<=(?:^|\s)(?:regex|token|rule)(?:\s+))(['\w:-]+)`,
NameFunction,
Push("token"),
},
// Substitution
{`(?<=^|\b|\s)(?<!\.)(ss|S|s|TR|tr)\b(\s*)`, ByGroups(Keyword, Text), Push("substitution")},
{keywordsPattern, Keyword, nil},
Expand All @@ -626,18 +620,10 @@ func rakuRules() Rules {
},
// Routine
{
// Routine with adverbs
`(?<=(?:^|\s)(?:sub|method|multi sub|multi)\s+)!?['\w:-]+(?=:['\w-]+` +
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `\s*[({])`,
`(?<=(?:^|\s)(?:sub|method|multi sub|multi)\s+)!?` + namePattern + colonPairLookahead + `\s*[({])`,
NameFunction,
Push("name-adverb"),
},
{
// Routine without adverbs
`(?<=(?:^|\s)(?:sub|submethod|method|multi)\s+)!?['\w:-]+`,
NameFunction,
nil,
},
// Constant
{`(?<=\bconstant\s+)` + namePattern, NameConstant, Push("name-adverb")},
// Namespace
Expand All @@ -655,22 +641,16 @@ func rakuRules() Rules {
},
// Function
{
`\b(?:\w['\w:-]*)(?=:['\w-]+` +
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `\()`,
`\b` + namePattern + colonPairLookahead + `\()`,
NameFunction,
Push("name-adverb"),
},
{`\b(?:\w['\w:-]*)(?=\()`, NameFunction, nil},
// Method
// Method with adverb
{
`(?<!\.\.[?^*+]?)(?<=(?:\.[?^*+&]?)|self!)['\w:-]+(?=:['\w-]+` +
colonPairOpeningBrackets + `.+?` + colonPairClosingBrackets + `$)`,
`(?<!\.\.[?^*+]?)(?<=(?:\.[?^*+&]?)|self!)` + namePattern + colonPairLookahead + `\b)`,
NameFunction,
Push("name-adverb"),
},
// Method without adverb
{`(?<!\.\.[?^*+]?)(?<=(?:\.[?^*+&]?)|self!)['\w:-]+`, NameFunction, nil},
// Indirect invocant
{namePattern + `(?=\s+\W?['\w:-]+:\W)`, NameFunction, Push("name-adverb")},
{`(?<=\W)(?:∅|i|e|𝑒|tau|τ|pi|π|Inf|∞)(?=\W)`, NameConstant, nil},
Expand Down
2 changes: 2 additions & 0 deletions lexers/testdata/raku.actual
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ sub f2($a,$b) {

2.&f2(3);

Module::function($var);

#| Fibonacci with Multiple dispatch
multi sub fib (0 --> 0) {}
multi sub fib (1 --> 1) {}
Expand Down
5 changes: 5 additions & 0 deletions lexers/testdata/raku.expected
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
{"type":"LiteralNumberInteger","value":"3"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n\n"},
{"type":"NameFunction","value":"Module::function"},
{"type":"Punctuation","value":"("},
{"type":"NameVariable","value":"$var"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n\n"},
{"type":"Keyword","value":"#| "},
{"type":"LiteralStringDoc","value":"Fibonacci with Multiple dispatch\n"},
{"type":"Keyword","value":"multi"},
Expand Down

0 comments on commit bb38ae2

Please sign in to comment.