Skip to content

Commit cb4e0eb

Browse files
b4nnyamatongwe
authored andcommitted
#343 Perl: Handle 'method' the same as 'sub' for special cases
Fixes #342.
1 parent e74f4d0 commit cb4e0eb

File tree

7 files changed

+43
-8
lines changed

7 files changed

+43
-8
lines changed

doc/LexillaHistory.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,11 @@ <h3>
616616
Makefile: Recognize comments in more situations.
617617
<a href="https://github.com/ScintillaOrg/lexilla/issues/40">Issue #40</a>.
618618
</li>
619+
<li>
620+
Perl: Handle 'method' the same as 'sub' for special cases.
621+
<a href="https://github.com/ScintillaOrg/lexilla/issues/342">Issue #342</a>,
622+
<a href="https://github.com/ScintillaOrg/lexilla/pull/343">Pull request #343</a>.
623+
</li>
619624
</ul>
620625
<h3>
621626
<a href="https://www.scintilla.org/lexilla546.zip">Release 5.4.6</a>

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} {5}feature{0} {7}'class'{10};{0}
4+
5+
{5}class{0} {11}MyClass{10}::{11}SubClass{0} {10}{{0}
6+
{5}method{0} {11}inClass{0} {10}{{0} {5}return{0} {4}1{0} {10}}{0}
7+
{5}method{0} {11}inClassProto{40}($){0} {10}{{0} {5}return{0} {12}$_{10}[{4}0{10}]{0} {10}}{0}
8+
{5}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}

test/examples/perl/perl-test-5220delta.pl.styled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
{5}sub{0} {11}Y{10}::{11}bar{0} {10}:{0} {11}lvalue{0} {10};{0}
116116

117117
{2}# built-in attributes for subroutines:
118-
{11}lvalue{0} {5}method{0} {5}prototype{10}(..){0} {11}locked{0} {11}const{0}
118+
{11}lvalue{0} {5}method{0} {11}prototype{10}(..){0} {11}locked{0} {11}const{0}
119119

120120
{2}#--------------------------------------------------------------------------
121121
# Repetition in list assignment

test/examples/perl/perl-test-sub-prototypes.pl.styled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
{5}sub{0} {11}Y{10}::{11}bar{0} {10}:{0} {11}lvalue{0} {10};{0}
233233

234234
{2}# built-in attributes for subroutines:
235-
{11}lvalue{0} {5}method{0} {5}prototype{10}(..){0} {11}locked{0} {11}const{0}
235+
{11}lvalue{0} {5}method{0} {11}prototype{10}(..){0} {11}locked{0} {11}const{0}
236236

237237
{2}#--------------------------------------------------------------------------
238238
# end of test file

0 commit comments

Comments
 (0)