Skip to content

Commit 8947524

Browse files
Ekopalypsenyamatongwe
authored andcommitted
#239 #240 Rust: Add raw identifier lexing
1 parent 3796591 commit 8947524

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

doc/LexillaHistory.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ <h3>
633633
Ruby: Allow modifier if, unless, while and until after heredoc delimiter.
634634
<a href="https://github.com/ScintillaOrg/lexilla/issues/236">Issue #236</a>.
635635
</li>
636+
<li>
637+
Rust: Recognize raw identifiers.
638+
<a href="https://github.com/ScintillaOrg/lexilla/issues/239">Issue #239</a>,
639+
<a href="https://github.com/ScintillaOrg/lexilla/pull/240">Pull request #240</a>.
640+
</li>
636641
</ul>
637642
<h3>
638643
<a href="https://www.scintilla.org/lexilla531.zip">Release 5.3.1</a>

lexers/LexRust.cxx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@ static void GrabString(char* s, Accessor& styler, Sci_Position start, Sci_Positi
207207
s[len] = '\0';
208208
}
209209

210+
static void ScanRawIdentifier(Accessor& styler, Sci_Position& pos) {
211+
Sci_Position start = pos;
212+
while (IsIdentifierContinue(styler.SafeGetCharAt(pos, '\0')))
213+
pos++;
214+
215+
char s[MAX_RUST_IDENT_CHARS + 1];
216+
Sci_Position len = pos - start;
217+
len = len > MAX_RUST_IDENT_CHARS ? MAX_RUST_IDENT_CHARS : len;
218+
GrabString(s, styler, start, len);
219+
// restricted values https://doc.rust-lang.org/reference/identifiers.html#raw-identifiers
220+
if (strcmp(s, "crate") != 0 && strcmp(s, "self") != 0 &&
221+
strcmp(s, "super") != 0 && strcmp(s, "Self") != 0) {
222+
styler.ColourTo(pos - 1, SCE_RUST_IDENTIFIER);
223+
} else {
224+
styler.ColourTo(pos - 1, SCE_RUST_LEXERROR);
225+
}
226+
}
227+
210228
static void ScanIdentifier(Accessor& styler, Sci_Position& pos, WordList *keywords) {
211229
Sci_Position start = pos;
212230
while (IsIdentifierContinue(styler.SafeGetCharAt(pos, '\0')))
@@ -704,6 +722,9 @@ void SCI_METHOD LexerRust::Lex(Sci_PositionU startPos, Sci_Position length, int
704722
ScanWhitespace(styler, pos, max);
705723
} else if (c == '/' && (n == '/' || n == '*')) {
706724
ScanComments(styler, pos, max);
725+
} else if (c == 'r' && (n == '#' && IsIdentifierStart(n2))) {
726+
pos += 2;
727+
ScanRawIdentifier(styler, pos);
707728
} else if (c == 'r' && (n == '#' || n == '"')) {
708729
ScanRawString(styler, pos, max, false);
709730
} else if (c == 'b' && n == 'r' && (n2 == '#' || n2 == '"')) {

test/examples/rust/Issue239.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let r#true = false;
3+
println!("{}", r#true);
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2 400 401 + fn main() {
2+
0 401 401 | let r#true = false;
3+
0 401 401 | println!("{}", r#true);
4+
0 401 400 | }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{6}fn{0} {17}main{16}(){0} {16}{{0}
2+
{6}let{0} {17}r#true{0} {16}={0} {6}false{16};{0}
3+
{19}println!{16}({13}"{}"{16},{0} {17}r#true{16});{0}
4+
{16}}

0 commit comments

Comments
 (0)