Add a bunch of character stuff to the lexer module
This commit is contained in:
parent
9e51659a30
commit
ac4cff9a51
2 changed files with 62 additions and 1 deletions
56
src/lexer.rs
Normal file
56
src/lexer.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
//! # Lexer
|
||||
|
||||
mod characters {
|
||||
use std::collections::HashSet;
|
||||
use std::iter::FromIterator;
|
||||
|
||||
pub type CharSet = HashSet<char>;
|
||||
|
||||
pub fn ascii_letters() -> CharSet {
|
||||
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars();
|
||||
return CharSet::from_iter(letters);
|
||||
}
|
||||
|
||||
pub fn ascii_digits() -> CharSet {
|
||||
let digits = "1234567890".chars();
|
||||
return CharSet::from_iter(digits);
|
||||
}
|
||||
|
||||
pub fn identifier_initials() -> CharSet {
|
||||
let letters = ascii_letters();
|
||||
let extras = CharSet::from_iter("!$%&*/:<=>?~_^".chars());
|
||||
let mut initials = CharSet::new();
|
||||
initials.extend(letters.iter());
|
||||
initials.extend(extras.iter());
|
||||
return initials;
|
||||
}
|
||||
|
||||
pub fn identifier_subsequents() -> CharSet {
|
||||
let initials = identifier_initials();
|
||||
let digits = ascii_digits();
|
||||
let extras = CharSet::from_iter(".+-".chars());
|
||||
let mut subsequents = CharSet::new();
|
||||
subsequents.extend(initials.iter());
|
||||
subsequents.extend(digits.iter());
|
||||
subsequents.extend(extras.iter());
|
||||
return subsequents;
|
||||
}
|
||||
}
|
||||
|
||||
enum Kind {
|
||||
LeftParen,
|
||||
RightParen,
|
||||
Identifier,
|
||||
}
|
||||
|
||||
struct Token {
|
||||
kind: Kind,
|
||||
value: String,
|
||||
}
|
||||
|
||||
pub fn hello(person: &str) {
|
||||
println!("Hello, {}!", person);
|
||||
for (idx, c) in person.char_indices() {
|
||||
println!(" {}, {} -> {}", c, idx, characters::ascii_letters().contains(&c));
|
||||
}
|
||||
}
|
|
@ -1,3 +1,8 @@
|
|||
mod lexer;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
lexer::hello("Eryn");
|
||||
lexer::hello("Emily");
|
||||
let s = "Jonas".to_string();
|
||||
lexer::hello(&s);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue