summaryrefslogtreecommitdiff
path: root/src/parse/scanner.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-11-02 12:13:45 +0100
committerMartin Haug <mhaug@live.de>2021-11-05 13:46:41 +0100
commit65fac0e57c9852eb2131aa06c0bac43b70bfbfbc (patch)
tree8ed11d7cefd4e64f523b975f077e4b10f67a7cb9 /src/parse/scanner.rs
parent42afb27cef5540535420fb6d8d9d2fcda7300a47 (diff)
Refactoring
Co-Authored-By: Martin <mhaug@live.de>
Diffstat (limited to 'src/parse/scanner.rs')
-rw-r--r--src/parse/scanner.rs49
1 files changed, 39 insertions, 10 deletions
diff --git a/src/parse/scanner.rs b/src/parse/scanner.rs
index edf28e17..92a2333d 100644
--- a/src/parse/scanner.rs
+++ b/src/parse/scanner.rs
@@ -1,5 +1,7 @@
use std::slice::SliceIndex;
+use unicode_xid::UnicodeXID;
+
/// A featureful char-based scanner.
#[derive(Copy, Clone)]
pub struct Scanner<'s> {
@@ -106,16 +108,6 @@ impl<'s> Scanner<'s> {
self.index
}
- /// The column index of a given index in the source string.
- #[inline]
- pub fn column(&self, index: usize) -> usize {
- self.src[.. index]
- .chars()
- .rev()
- .take_while(|&c| !is_newline(c))
- .count()
- }
-
/// Jump to an index in the source string.
#[inline]
pub fn jump(&mut self, index: usize) {
@@ -124,6 +116,12 @@ impl<'s> Scanner<'s> {
self.index = index;
}
+ /// The full source string.
+ #[inline]
+ pub fn src(&self) -> &'s str {
+ &self.src
+ }
+
/// Slice out part of the source string.
#[inline]
pub fn get<I>(&self, index: I) -> &'s str
@@ -160,6 +158,16 @@ impl<'s> Scanner<'s> {
// optimized away in some cases.
self.src.get(start .. self.index).unwrap_or_default()
}
+
+ /// The column index of a given index in the source string.
+ #[inline]
+ pub fn column(&self, index: usize) -> usize {
+ self.src[.. index]
+ .chars()
+ .rev()
+ .take_while(|&c| !is_newline(c))
+ .count()
+ }
}
/// Whether this character denotes a newline.
@@ -173,3 +181,24 @@ pub fn is_newline(character: char) -> bool {
'\u{0085}' | '\u{2028}' | '\u{2029}'
)
}
+
+/// Whether a string is a valid identifier.
+#[inline]
+pub fn is_ident(string: &str) -> bool {
+ let mut chars = string.chars();
+ chars
+ .next()
+ .map_or(false, |c| is_id_start(c) && chars.all(is_id_continue))
+}
+
+/// Whether a character can start an identifier.
+#[inline]
+pub fn is_id_start(c: char) -> bool {
+ c.is_xid_start() || c == '_'
+}
+
+/// Whether a character can continue an identifier.
+#[inline]
+pub fn is_id_continue(c: char) -> bool {
+ c.is_xid_continue() || c == '_' || c == '-'
+}