Skip to content

Commit

Permalink
#239 #240 Rust: Add raw identifier lexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekopalypse authored and nyamatongwe committed Apr 16, 2024
1 parent 3796591 commit 8947524
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/LexillaHistory.html
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ <h3>
Ruby: Allow modifier if, unless, while and until after heredoc delimiter.
<a href="https://github.com/ScintillaOrg/lexilla/issues/236">Issue #236</a>.
</li>
<li>
Rust: Recognize raw identifiers.
<a href="https://github.com/ScintillaOrg/lexilla/issues/239">Issue #239</a>,
<a href="https://github.com/ScintillaOrg/lexilla/pull/240">Pull request #240</a>.
</li>
</ul>
<h3>
<a href="https://www.scintilla.org/lexilla531.zip">Release 5.3.1</a>
Expand Down
21 changes: 21 additions & 0 deletions lexers/LexRust.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ static void GrabString(char* s, Accessor& styler, Sci_Position start, Sci_Positi
s[len] = '\0';
}

static void ScanRawIdentifier(Accessor& styler, Sci_Position& pos) {
Sci_Position start = pos;
while (IsIdentifierContinue(styler.SafeGetCharAt(pos, '\0')))
pos++;

char s[MAX_RUST_IDENT_CHARS + 1];
Sci_Position len = pos - start;
len = len > MAX_RUST_IDENT_CHARS ? MAX_RUST_IDENT_CHARS : len;
GrabString(s, styler, start, len);
// restricted values https://doc.rust-lang.org/reference/identifiers.html#raw-identifiers
if (strcmp(s, "crate") != 0 && strcmp(s, "self") != 0 &&
strcmp(s, "super") != 0 && strcmp(s, "Self") != 0) {
styler.ColourTo(pos - 1, SCE_RUST_IDENTIFIER);
} else {
styler.ColourTo(pos - 1, SCE_RUST_LEXERROR);
}
}

static void ScanIdentifier(Accessor& styler, Sci_Position& pos, WordList *keywords) {
Sci_Position start = pos;
while (IsIdentifierContinue(styler.SafeGetCharAt(pos, '\0')))
Expand Down Expand Up @@ -704,6 +722,9 @@ void SCI_METHOD LexerRust::Lex(Sci_PositionU startPos, Sci_Position length, int
ScanWhitespace(styler, pos, max);
} else if (c == '/' && (n == '/' || n == '*')) {
ScanComments(styler, pos, max);
} else if (c == 'r' && (n == '#' && IsIdentifierStart(n2))) {
pos += 2;
ScanRawIdentifier(styler, pos);
} else if (c == 'r' && (n == '#' || n == '"')) {
ScanRawString(styler, pos, max, false);
} else if (c == 'b' && n == 'r' && (n2 == '#' || n2 == '"')) {
Expand Down
4 changes: 4 additions & 0 deletions test/examples/rust/Issue239.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let r#true = false;
println!("{}", r#true);
}
4 changes: 4 additions & 0 deletions test/examples/rust/Issue239.rs.folded
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2 400 401 + fn main() {
0 401 401 | let r#true = false;
0 401 401 | println!("{}", r#true);
0 401 400 | }
4 changes: 4 additions & 0 deletions test/examples/rust/Issue239.rs.styled
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{6}fn{0} {17}main{16}(){0} {16}{{0}
{6}let{0} {17}r#true{0} {16}={0} {6}false{16};{0}
{19}println!{16}({13}"{}"{16},{0} {17}r#true{16});{0}
{16}}

0 comments on commit 8947524

Please sign in to comment.