Browse Source

Add main function

main
Dylan Baker 3 years ago
parent
commit
c03a5c77d1
5 changed files with 92 additions and 32 deletions
  1. 26
    0
      src/error.rs
  2. 2
    29
      src/lexer.rs
  3. 3
    1
      src/lib.rs
  4. 12
    2
      src/main.rs
  5. 49
    0
      src/token.rs

+ 26
- 0
src/error.rs View File

@@ -0,0 +1,26 @@
1
+use std::{error::Error, fmt, fmt::Display};
2
+
3
+#[derive(Debug, PartialEq)]
4
+pub struct LexerError {
5
+    message: String,
6
+}
7
+
8
+impl Display for LexerError {
9
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10
+        write!(f, "{}", &self.message)
11
+    }
12
+}
13
+
14
+impl Error for LexerError {}
15
+
16
+// impl Into<std::io::Error> for LexerError {
17
+//     fn into(self) -> std::io::Error {
18
+//         std::io::Error::new(std::io::ErrorKind::InvalidInput, self.message)
19
+//     }
20
+// }
21
+
22
+impl From<LexerError> for std::io::Error {
23
+    fn from(e: LexerError) -> Self {
24
+        Self::new(std::io::ErrorKind::InvalidInput, e.message)
25
+    }
26
+}

+ 2
- 29
src/lexer.rs View File

@@ -1,34 +1,8 @@
1 1
 use lazy_static::lazy_static;
2 2
 use regex::Regex;
3 3
 
4
-#[derive(Debug, PartialEq)]
5
-pub struct LexerError {
6
-    message: String,
7
-}
8
-
9
-#[derive(Debug, PartialEq)]
10
-pub struct Token {
11
-    token_type: TokenType,
12
-    value: String,
13
-}
14
-
15
-impl Token {
16
-    pub fn new(token_type: TokenType, value: &str) -> Self {
17
-        Token {
18
-            token_type,
19
-            value: value.to_string(),
20
-        }
21
-    }
22
-}
23
-
24
-#[derive(Clone, Copy, Debug, PartialEq)]
25
-pub enum TokenType {
26
-    Identfiier,
27
-    Keyword,
28
-    Number,
29
-    Star,
30
-    String,
31
-}
4
+use crate::error::LexerError;
5
+use crate::token::{Token, TokenType};
32 6
 
33 7
 #[derive(Debug)]
34 8
 struct Matcher {
@@ -67,7 +41,6 @@ pub fn scan(input: &str) -> Result<Vec<Token>, LexerError> {
67 41
 
68 42
         for matcher in MATCHERS.iter() {
69 43
             if matcher.regex.is_match(&input[position..]) {
70
-                dbg!(&input[position..]);
71 44
                 if let Some(m) = matcher.regex.captures_iter(&input[position..]).next() {
72 45
                     let value = if m.len() > 1 { &m[1] } else { &m[0] };
73 46
                     position += value.len();

+ 3
- 1
src/lib.rs View File

@@ -1 +1,3 @@
1
-mod lexer;
1
+pub mod error;
2
+pub mod lexer;
3
+pub mod token;

+ 12
- 2
src/main.rs View File

@@ -1,3 +1,13 @@
1
-fn main() {
2
-    println!("Hello, world!");
1
+use elastic::lexer;
2
+
3
+use std::env;
4
+
5
+fn main() -> std::io::Result<()> {
6
+    let args: Vec<String> = env::args().into_iter().skip(1).collect();
7
+    let input = args.join(" ");
8
+    let tokens = lexer::scan(&input)?;
9
+    for token in tokens {
10
+        println!("{}", token);
11
+    }
12
+    Ok(())
3 13
 }

+ 49
- 0
src/token.rs View File

@@ -0,0 +1,49 @@
1
+#[derive(Debug, PartialEq)]
2
+pub struct Token {
3
+    token_type: TokenType,
4
+    value: String,
5
+}
6
+
7
+impl Token {
8
+    pub fn new(token_type: TokenType, value: &str) -> Self {
9
+        Token {
10
+            token_type,
11
+            value: value.to_string(),
12
+        }
13
+    }
14
+}
15
+
16
+impl std::fmt::Display for Token {
17
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
18
+        write!(
19
+            f,
20
+            r#"{{ type: {}, value: "{}" }}"#,
21
+            self.token_type, self.value
22
+        )
23
+    }
24
+}
25
+
26
+#[derive(Clone, Copy, Debug, PartialEq)]
27
+pub enum TokenType {
28
+    Identfiier,
29
+    Keyword,
30
+    Number,
31
+    Star,
32
+    String,
33
+}
34
+
35
+impl std::fmt::Display for TokenType {
36
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
37
+        write!(
38
+            f,
39
+            "{}",
40
+            match self {
41
+                TokenType::Identfiier => "Identifier",
42
+                TokenType::Keyword => "Keyword",
43
+                TokenType::Number => "Number",
44
+                TokenType::Star => "Star",
45
+                TokenType::String => "String",
46
+            }
47
+        )
48
+    }
49
+}

Loading…
Cancel
Save