Browse Source

Ignore whitespace

main
Dylan Baker 3 years ago
parent
commit
adca91b52e
1 changed files with 17 additions and 1 deletions
  1. 17
    1
      src/lexer.rs

+ 17
- 1
src/lexer.rs View File

@@ -45,9 +45,10 @@ impl Matcher {
45 45
 }
46 46
 
47 47
 lazy_static! {
48
+    static ref WHITESPACE_REGEX: Regex = Regex::new(r"\s").unwrap();
48 49
     static ref MATCHERS: Vec<Matcher> = vec![
49 50
         Matcher::new(r#""(.*)""#, TokenType::String),
50
-        Matcher::new(r#"SELECT"#, TokenType::Keyword),
51
+        Matcher::new(r#"(SELECT|FROM)"#, TokenType::Keyword),
51 52
         Matcher::new(r#"[a-z][a-zA-Z_]*"#, TokenType::Identfiier),
52 53
         Matcher::new(r#"[0-9]+"#, TokenType::Number)
53 54
     ];
@@ -58,6 +59,10 @@ pub fn scan(input: &str) -> Result<Vec<Token>, LexerError> {
58 59
     let mut position = 0;
59 60
 
60 61
     while position < input.len() {
62
+        while WHITESPACE_REGEX.is_match(&input[position..position + 1]) {
63
+            position += 1;
64
+        }
65
+
61 66
         for matcher in MATCHERS.iter() {
62 67
             if matcher.regex.is_match(&input[position..]) {
63 68
                 dbg!(&input[position..]);
@@ -106,6 +111,17 @@ mod tests {
106 111
         )
107 112
     }
108 113
 
114
+    #[test]
115
+    fn it_scans_two_keywords() {
116
+        assert_eq!(
117
+            scan("SELECT FROM").unwrap(),
118
+            vec![
119
+                Token::new(TokenType::Keyword, "SELECT"),
120
+                Token::new(TokenType::Keyword, "FROM")
121
+            ]
122
+        )
123
+    }
124
+
109 125
     #[test]
110 126
     fn it_scans_an_identifier() {
111 127
         assert_eq!(

Loading…
Cancel
Save