Skip to content

Commit b3c7dfa

Browse files
committed
Perl: Handle 'method' the same as 'sub' for special cases
Fixes #342.
1 parent 7c27985 commit b3c7dfa

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

lexers/LexPerl.cxx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace {
8787
#define SUB_HAS_PROTO 1 // only 'prototype' attribute allows prototypes
8888
#define SUB_HAS_ATTRIB 2 // other attributes can exist leftward
8989
#define SUB_HAS_MODULE 3 // sub name can have a ::identifier part
90-
#define SUB_HAS_SUB 4 // 'sub' keyword
90+
#define SUB_HAS_SUB 4 // 'sub' (or 'method') keyword
9191

9292
// all interpolated styles are different from their parent styles by a constant difference
9393
// we also assume SCE_PL_STRING_VAR is the interpolated style with the smallest value
@@ -131,8 +131,9 @@ int disambiguateBareword(LexAccessor &styler, Sci_PositionU bk, Sci_PositionU fw
131131
// ->bareword: part of variable spec
132132
|| styler.Match(bk - 1, "::")
133133
// ::bareword: part of module spec
134-
|| styler.Match(bk - 2, "sub")) {
135-
// sub bareword: subroutine declaration
134+
|| styler.Match(bk - 2, "sub")
135+
|| styler.Match(bk - 5, "method")) {
136+
// 'sub' or 'method' bareword: subroutine declaration
136137
// (implied BACK_KEYWORD, no keywords end in 'sub'!)
137138
result |= 1;
138139
}
@@ -299,9 +300,10 @@ bool styleCheckSubPrototype(LexAccessor &styler, Sci_PositionU bk) {
299300
state = SUB_HAS_MODULE;
300301
} else
301302
break;
302-
} else if (style1 == SCE_PL_WORD && len1 == 3 &&
303-
styler.Match(pos1, "sub")) { // 'sub'
304-
if (style2 == SCE_PL_IDENTIFIER) { // 'sub' <identifier>
303+
} else if ((style1 == SCE_PL_WORD || style1 == SCE_PL_IDENTIFIER) &&
304+
((len1 == 3 && styler.Match(pos1, "sub")) || // 'sub'
305+
(len1 == 6 && styler.Match(pos1, "method")))) { // or 'method
306+
if (style2 == SCE_PL_IDENTIFIER) { // ('sub' | 'method') <identifier>
305307
state = SUB_HAS_SUB;
306308
} else
307309
break;

test/examples/perl/class.pl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env perl
2+
use v5.38;
3+
use feature 'class';
4+
5+
class MyClass::SubClass {
6+
method inClass { return 1 }
7+
method inClassProto($) { return $_[0] }
8+
method inClassAttrib :prototype($) { return $_[0] }
9+
}

test/examples/perl/class.pl.folded

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
0 400 400 #!/usr/bin/env perl
2+
0 400 400 use v5.38;
3+
0 400 400 use feature 'class';
4+
1 400 400
5+
2 400 401 + class MyClass::SubClass {
6+
0 401 401 | method inClass { return 1 }
7+
0 401 401 | method inClassProto($) { return $_[0] }
8+
0 401 401 | method inClassAttrib :prototype($) { return $_[0] }
9+
0 401 400 | }
10+
0 400 0

test/examples/perl/class.pl.styled

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{2}#!/usr/bin/env perl
2+
{5}use{0} {6}v5.38{10};{0}
3+
{5}use{0} {11}feature{0} {7}'class'{10};{0}
4+
5+
{11}class{0} {11}MyClass{10}::{11}SubClass{0} {10}{{0}
6+
{11}method{0} {11}inClass{0} {10}{{0} {5}return{0} {4}1{0} {10}}{0}
7+
{11}method{0} {11}inClassProto{40}($){0} {10}{{0} {5}return{0} {12}$_{10}[{4}0{10}]{0} {10}}{0}
8+
{11}method{0} {11}inClassAttrib{0} {10}:{5}prototype{40}($){0} {10}{{0} {5}return{0} {12}$_{10}[{4}0{10}]{0} {10}}{0}
9+
{10}}{0}

0 commit comments

Comments
 (0)