Skip to content

Commit 397dfb1

Browse files
zufuliunyamatongwe
authored andcommitted
#124 Add new lexical classes for % literals in Ruby.
SCE_RB_STRING_W (%w non-interpolable string array), SCE_RB_STRING_I (%i non-interpolable symbol array), SCE_RB_STRING_QI (%I interpolable symbol array), and SCE_RB_STRING_QS (%s symbol).
1 parent b62dcea commit 397dfb1

File tree

7 files changed

+95
-24
lines changed

7 files changed

+95
-24
lines changed

doc/LexillaHistory.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,13 @@ <h3>
596596
Add scripts/PromoteNew.bat script to promote .new files after checking.
597597
</li>
598598
<li>
599+
Ruby: Add new lexical classes for % literals SCE_RB_STRING_W (%w non-interpolable string array),
600+
SCE_RB_STRING_I (%i non-interpolable symbol array),
601+
SCE_RB_STRING_QI (%I interpolable symbol array),
602+
and SCE_RB_STRING_QS (%s symbol).
603+
<a href="https://github.com/ScintillaOrg/lexilla/issues/124">Issue #124</a>.
604+
</li>
605+
<li>
599606
Ruby: Fix additional fold level for single character in SCE_RB_STRING_QW.
600607
<a href="https://github.com/ScintillaOrg/lexilla/issues/132">Issue #132</a>.
601608
</li>

include/LexicalStyles.iface

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,11 @@ val SCE_RB_WORD_DEMOTED=29
467467
val SCE_RB_STDIN=30
468468
val SCE_RB_STDOUT=31
469469
val SCE_RB_STDERR=40
470-
val SCE_RB_UPPER_BOUND=41
470+
val SCE_RB_STRING_W=41
471+
val SCE_RB_STRING_I=42
472+
val SCE_RB_STRING_QI=43
473+
val SCE_RB_STRING_QS=44
474+
val SCE_RB_UPPER_BOUND=45
471475
# Lexical states for SCLEX_VB, SCLEX_VBSCRIPT, SCLEX_POWERBASIC, SCLEX_BLITZBASIC, SCLEX_PUREBASIC, SCLEX_FREEBASIC
472476
lex VB=SCLEX_VB SCE_B_
473477
lex VBScript=SCLEX_VBSCRIPT SCE_B_

include/SciLexer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,11 @@
432432
#define SCE_RB_STDIN 30
433433
#define SCE_RB_STDOUT 31
434434
#define SCE_RB_STDERR 40
435-
#define SCE_RB_UPPER_BOUND 41
435+
#define SCE_RB_STRING_W 41
436+
#define SCE_RB_STRING_I 42
437+
#define SCE_RB_STRING_QI 43
438+
#define SCE_RB_STRING_QS 44
439+
#define SCE_RB_UPPER_BOUND 45
436440
#define SCE_B_DEFAULT 0
437441
#define SCE_B_COMMENT 1
438442
#define SCE_B_NUMBER 2

lexers/LexRuby.cxx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,25 @@ class QuoteCls {
292292
}
293293
};
294294

295+
constexpr bool isPercentLiteral(int state) noexcept {
296+
return state == SCE_RB_STRING_Q
297+
|| state == SCE_RB_STRING_QQ
298+
// excluded SCE_RB_STRING_QR
299+
|| state == SCE_RB_STRING_W
300+
|| state == SCE_RB_STRING_QW
301+
|| state == SCE_RB_STRING_I
302+
|| state == SCE_RB_STRING_QI
303+
|| state == SCE_RB_STRING_QS
304+
|| state == SCE_RB_STRING_QX;
305+
}
306+
307+
constexpr bool isInterpolableLiteral(int state) noexcept {
308+
return state != SCE_RB_STRING_Q
309+
&& state != SCE_RB_STRING_W
310+
&& state != SCE_RB_STRING_I
311+
&& state != SCE_RB_STRING_QS
312+
&& state != SCE_RB_CHARACTER;
313+
}
295314

296315
void enterInnerExpression(int *p_inner_string_types,
297316
int *p_inner_expn_brace_counts,
@@ -756,11 +775,14 @@ void ColouriseRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
756775
SCE_RB_STRING_Q,
757776
SCE_RB_STRING_QQ,
758777
SCE_RB_STRING_QR,
778+
SCE_RB_STRING_W,
759779
SCE_RB_STRING_QW,
760-
SCE_RB_STRING_QW,
761-
SCE_RB_STRING_QX
780+
SCE_RB_STRING_QX,
781+
SCE_RB_STRING_I,
782+
SCE_RB_STRING_QI,
783+
SCE_RB_STRING_QS,
762784
};
763-
constexpr const char *q_chars = "qQrwWx";
785+
constexpr const char *q_chars = "qQrwWxiIs";
764786

765787
// In most cases a value of 2 should be ample for the code in the
766788
// Ruby library, and the code the user is likely to enter.
@@ -1479,8 +1501,7 @@ void ColouriseRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
14791501
}
14801502
}
14811503
// Quotes of all kinds...
1482-
} else if (state == SCE_RB_STRING_Q || state == SCE_RB_STRING_QQ ||
1483-
state == SCE_RB_STRING_QX || state == SCE_RB_STRING_QW ||
1504+
} else if (isPercentLiteral(state) ||
14841505
state == SCE_RB_STRING || state == SCE_RB_CHARACTER ||
14851506
state == SCE_RB_BACKTICKS) {
14861507
if (!Quote.Down && !isspacechar(ch)) {
@@ -1499,8 +1520,7 @@ void ColouriseRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
14991520
Quote.Count++;
15001521
} else if (ch == '#' && chNext == '{'
15011522
&& inner_string_count < INNER_STRINGS_MAX_COUNT
1502-
&& state != SCE_RB_CHARACTER
1503-
&& state != SCE_RB_STRING_Q) {
1523+
&& isInterpolableLiteral(state)) {
15041524
// process #{ ... }
15051525
styler.ColourTo(i - 1, state);
15061526
styler.ColourTo(i + 1, SCE_RB_OPERATOR);

test/examples/ruby/AllStyles.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Enumerate all styles where possible: 0..31,40..41
2-
# 22,23,30,31,40,41 are never set and 1 switches rest of file to error state
1+
# Enumerate all styles where possible: 0..31,40..45
2+
# 22,23,30,31,40,45 are never set and 1 switches rest of file to error state
33

44
#0 whitespace
55
#
@@ -87,13 +87,25 @@ module Module15 end
8787
#27:regex
8888
%r(27[a-z]/[A-Z]+)
8989

90-
#28:string array
91-
%w(28 cgi.rb complex.rb date.rb)
90+
#28:interpolable string array
91+
%W(28 cgi.rb complex.rb date.rb #{1} )
9292

9393
#29:demoted keyword do
9494
while 1 do end
9595

96-
# 30,31,40,41 never set
96+
# 30,31,40,45 never set
97+
98+
#41:non-interpolable string array
99+
%w(#{1 + 1})
100+
101+
#42:non-interpolable symbol array
102+
%i(#{1 + 1})
103+
104+
#43:interpolable symbol array
105+
%I(#{1 + 1})
106+
107+
#44:symbol
108+
%s(#{1 + 1})
97109

98110
#19:data section
99111
__END__

test/examples/ruby/AllStyles.rb.folded

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
0 400 0 # Enumerate all styles where possible: 0..31,40..41
2-
0 400 0 # 22,23,30,31,40,41 are never set and 1 switches rest of file to error state
1+
0 400 0 # Enumerate all styles where possible: 0..31,40..45
2+
0 400 0 # 22,23,30,31,40,45 are never set and 1 switches rest of file to error state
33
1 400 0
44
0 400 0 #0 whitespace
55
0 400 0 #
@@ -87,13 +87,25 @@
8787
0 400 0 #27:regex
8888
0 400 0 %r(27[a-z]/[A-Z]+)
8989
1 400 0
90-
0 400 0 #28:string array
91-
0 400 0 %w(28 cgi.rb complex.rb date.rb)
90+
0 400 0 #28:interpolable string array
91+
0 400 0 %W(28 cgi.rb complex.rb date.rb #{1} )
9292
1 400 0
9393
0 400 0 #29:demoted keyword do
9494
0 400 0 while 1 do end
9595
1 400 0
96-
0 400 0 # 30,31,40,41 never set
96+
0 400 0 # 30,31,40,45 never set
97+
1 400 0
98+
0 400 0 #41:non-interpolable string array
99+
0 400 0 %w(#{1 + 1})
100+
1 400 0
101+
0 400 0 #42:non-interpolable symbol array
102+
0 400 0 %i(#{1 + 1})
103+
1 400 0
104+
0 400 0 #43:interpolable symbol array
105+
0 400 0 %I(#{1 + 1})
106+
1 400 0
107+
0 400 0 #44:symbol
108+
0 400 0 %s(#{1 + 1})
97109
1 400 0
98110
0 400 0 #19:data section
99111
0 400 0 __END__

test/examples/ruby/AllStyles.rb.styled

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{2}# Enumerate all styles where possible: 0..31,40..41{0}
2-
{2}# 22,23,30,31,40,41 are never set and 1 switches rest of file to error state{0}
1+
{2}# Enumerate all styles where possible: 0..31,40..45{0}
2+
{2}# 22,23,30,31,40,45 are never set and 1 switches rest of file to error state{0}
33

44
{2}#0 whitespace{0}
55
{2}#{0}
@@ -87,13 +87,25 @@
8787
{2}#27:regex{0}
8888
{27}%r(27[a-z]/[A-Z]+){0}
8989

90-
{2}#28:string array{0}
91-
{28}%w(28 cgi.rb complex.rb date.rb){0}
90+
{2}#28:interpolable string array{0}
91+
{28}%W(28 cgi.rb complex.rb date.rb {10}#{{4}1{10}}{28} ){0}
9292

9393
{2}#29:demoted keyword do{0}
9494
{5}while{0} {4}1{0} {29}do{0} {5}end{0}
9595

96-
{2}# 30,31,40,41 never set{0}
96+
{2}# 30,31,40,45 never set{0}
97+
98+
{2}#41:non-interpolable string array{0}
99+
{41}%w(#{1 + 1}){0}
100+
101+
{2}#42:non-interpolable symbol array{0}
102+
{42}%i(#{1 + 1}){0}
103+
104+
{2}#43:interpolable symbol array{0}
105+
{43}%I({10}#{{4}1{0} {10}+{0} {4}1{10}}{43}){0}
106+
107+
{2}#44:symbol{0}
108+
{44}%s(#{1 + 1}){0}
97109

98110
{2}#19:data section{0}
99111
{19}__END__

0 commit comments

Comments
 (0)