Browse Source

Reduce to single error type, remove main

main
Dylan Baker 4 years ago
parent
commit
ec30423ebc
6 changed files with 30 additions and 91 deletions
  1. 3
    3
      src/compiler.rs
  2. 7
    61
      src/error.rs
  3. 3
    3
      src/lexer.rs
  4. 9
    0
      src/lib.rs
  5. 0
    16
      src/main.rs
  6. 8
    8
      src/parser.rs

+ 3
- 3
src/compiler.rs View File

2
 
2
 
3
 use std::fmt::{Display, Formatter};
3
 use std::fmt::{Display, Formatter};
4
 
4
 
5
-use crate::error::CompilerError;
5
+use crate::error::ElasticError;
6
 use crate::parser::{Field, Identifier, Select};
6
 use crate::parser::{Field, Identifier, Select};
7
 
7
 
8
 #[derive(Debug, PartialEq)]
8
 #[derive(Debug, PartialEq)]
28
     }
28
     }
29
 }
29
 }
30
 
30
 
31
-pub fn compile(expr: Select) -> Result<Query, CompilerError> {
31
+pub fn compile(expr: Select) -> Result<Query, ElasticError> {
32
     let field_names: Vec<Value> = expr
32
     let field_names: Vec<Value> = expr
33
         .fields
33
         .fields
34
         .iter()
34
         .iter()
52
     use crate::lexer::scan;
52
     use crate::lexer::scan;
53
     use crate::parser::parse;
53
     use crate::parser::parse;
54
 
54
 
55
-    fn _compile(input: &str) -> Result<Query, CompilerError> {
55
+    fn _compile(input: &str) -> Result<Query, ElasticError> {
56
         let tokens = scan(input).unwrap();
56
         let tokens = scan(input).unwrap();
57
         let select = parse(tokens).unwrap();
57
         let select = parse(tokens).unwrap();
58
         compile(select)
58
         compile(select)

+ 7
- 61
src/error.rs View File

1
 use std::{error::Error, fmt, fmt::Display};
1
 use std::{error::Error, fmt, fmt::Display};
2
 
2
 
3
 #[derive(Debug, PartialEq)]
3
 #[derive(Debug, PartialEq)]
4
-pub struct LexerError {
5
-    message: String,
4
+pub struct ElasticError {
5
+    pub message: String,
6
 }
6
 }
7
 
7
 
8
-impl LexerError {
8
+impl ElasticError {
9
     pub fn new(message: &str) -> Self {
9
     pub fn new(message: &str) -> Self {
10
         Self {
10
         Self {
11
             message: message.to_string(),
11
             message: message.to_string(),
13
     }
13
     }
14
 }
14
 }
15
 
15
 
16
-impl Display for LexerError {
16
+impl Display for ElasticError {
17
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18
         write!(f, "{}", &self.message)
18
         write!(f, "{}", &self.message)
19
     }
19
     }
20
 }
20
 }
21
 
21
 
22
-impl Error for LexerError {}
22
+impl Error for ElasticError {}
23
 
23
 
24
-impl From<LexerError> for std::io::Error {
25
-    fn from(e: LexerError) -> Self {
26
-        Self::new(std::io::ErrorKind::InvalidInput, e.message)
27
-    }
28
-}
29
-
30
-#[derive(Debug, PartialEq)]
31
-pub struct ParserError {
32
-    message: String,
33
-}
34
-
35
-impl ParserError {
36
-    pub fn new(message: &str) -> Self {
37
-        Self {
38
-            message: message.to_string(),
39
-        }
40
-    }
41
-}
42
-
43
-impl Display for ParserError {
44
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45
-        write!(f, "{}", &self.message)
46
-    }
47
-}
48
-
49
-impl Error for ParserError {}
50
-
51
-impl From<ParserError> for std::io::Error {
52
-    fn from(e: ParserError) -> Self {
53
-        Self::new(std::io::ErrorKind::InvalidInput, e.message)
54
-    }
55
-}
56
-
57
-#[derive(Debug, PartialEq)]
58
-pub struct CompilerError {
59
-    message: String,
60
-}
61
-
62
-impl CompilerError {
63
-    pub fn new(message: &str) -> Self {
64
-        Self {
65
-            message: message.to_string(),
66
-        }
67
-    }
68
-}
69
-
70
-impl Display for CompilerError {
71
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72
-        write!(f, "{}", &self.message)
73
-    }
74
-}
75
-
76
-impl Error for CompilerError {}
77
-
78
-impl From<CompilerError> for std::io::Error {
79
-    fn from(e: CompilerError) -> Self {
24
+impl From<ElasticError> for std::io::Error {
25
+    fn from(e: ElasticError) -> Self {
80
         Self::new(std::io::ErrorKind::InvalidInput, e.message)
26
         Self::new(std::io::ErrorKind::InvalidInput, e.message)
81
     }
27
     }
82
 }
28
 }

+ 3
- 3
src/lexer.rs View File

1
 use lazy_static::lazy_static;
1
 use lazy_static::lazy_static;
2
 use regex::Regex;
2
 use regex::Regex;
3
 
3
 
4
-use crate::error::LexerError;
4
+use crate::error::ElasticError;
5
 use crate::token::{Token, TokenType};
5
 use crate::token::{Token, TokenType};
6
 
6
 
7
 #[derive(Debug)]
7
 #[derive(Debug)]
32
     ];
32
     ];
33
 }
33
 }
34
 
34
 
35
-pub fn scan(input: &str) -> Result<Vec<Token>, LexerError> {
35
+pub fn scan(input: &str) -> Result<Vec<Token>, ElasticError> {
36
     let mut tokens: Vec<Token> = vec![];
36
     let mut tokens: Vec<Token> = vec![];
37
     let mut position = 0;
37
     let mut position = 0;
38
 
38
 
62
         }
62
         }
63
 
63
 
64
         if !matched {
64
         if !matched {
65
-            return Err(LexerError::new("Unrecognized sequence"));
65
+            return Err(ElasticError::new("Unrecognized sequence"));
66
         }
66
         }
67
     }
67
     }
68
 
68
 

+ 9
- 0
src/lib.rs View File

3
 pub mod lexer;
3
 pub mod lexer;
4
 pub mod parser;
4
 pub mod parser;
5
 pub mod token;
5
 pub mod token;
6
+
7
+use compiler::Query;
8
+use error::ElasticError;
9
+
10
+pub fn compile(input: &str) -> Result<Query, ElasticError> {
11
+    let tokens = lexer::scan(&input)?;
12
+    let select = parser::parse(tokens)?;
13
+    compiler::compile(select)
14
+}

+ 0
- 16
src/main.rs View File

1
-use elastic::parser;
2
-use elastic::{compiler::compile, lexer};
3
-
4
-use std::env;
5
-
6
-fn main() -> std::io::Result<()> {
7
-    let args: Vec<String> = env::args().into_iter().skip(1).collect();
8
-    let input = args.join(" ");
9
-    let tokens = lexer::scan(&input)?;
10
-    let select = parser::parse(tokens)?;
11
-    let result = compile(select)?;
12
-
13
-    println!("{}", result);
14
-
15
-    Ok(())
16
-}

+ 8
- 8
src/parser.rs View File

1
 use std::{iter::Peekable, slice::Iter};
1
 use std::{iter::Peekable, slice::Iter};
2
 
2
 
3
-use crate::error::ParserError;
3
+use crate::error::ElasticError;
4
 use crate::token::{Token, TokenType};
4
 use crate::token::{Token, TokenType};
5
 
5
 
6
 #[derive(Debug, PartialEq)]
6
 #[derive(Debug, PartialEq)]
28
     }
28
     }
29
 }
29
 }
30
 
30
 
31
-pub fn parse(tokens: Vec<Token>) -> Result<Select, ParserError> {
31
+pub fn parse(tokens: Vec<Token>) -> Result<Select, ElasticError> {
32
     parse_select_expression(&mut tokens.iter().peekable())
32
     parse_select_expression(&mut tokens.iter().peekable())
33
 }
33
 }
34
 
34
 
35
-fn parse_select_expression(tokens: &mut Peekable<Iter<Token>>) -> Result<Select, ParserError> {
35
+fn parse_select_expression(tokens: &mut Peekable<Iter<Token>>) -> Result<Select, ElasticError> {
36
     eat(tokens, TokenType::Select)?;
36
     eat(tokens, TokenType::Select)?;
37
     let fields = parse_fields(tokens)?;
37
     let fields = parse_fields(tokens)?;
38
     eat(tokens, TokenType::From)?;
38
     eat(tokens, TokenType::From)?;
41
     Ok(Select { fields, source })
41
     Ok(Select { fields, source })
42
 }
42
 }
43
 
43
 
44
-fn parse_fields(tokens: &mut Peekable<Iter<Token>>) -> Result<Vec<Field>, ParserError> {
44
+fn parse_fields(tokens: &mut Peekable<Iter<Token>>) -> Result<Vec<Field>, ElasticError> {
45
     if current_token_is(tokens, TokenType::Star) {
45
     if current_token_is(tokens, TokenType::Star) {
46
         eat(tokens, TokenType::Star).and_then(|_| Ok(vec![Field::Star]))
46
         eat(tokens, TokenType::Star).and_then(|_| Ok(vec![Field::Star]))
47
     } else {
47
     } else {
72
 fn eat<'a>(
72
 fn eat<'a>(
73
     tokens: &'a mut Peekable<Iter<Token>>,
73
     tokens: &'a mut Peekable<Iter<Token>>,
74
     token_type: TokenType,
74
     token_type: TokenType,
75
-) -> Result<&'a Token, ParserError> {
75
+) -> Result<&'a Token, ElasticError> {
76
     match tokens.next() {
76
     match tokens.next() {
77
         Some(token) => {
77
         Some(token) => {
78
             if token.token_type == token_type {
78
             if token.token_type == token_type {
79
                 Ok(token)
79
                 Ok(token)
80
             } else {
80
             } else {
81
-                Err(ParserError::new(&format!(
81
+                Err(ElasticError::new(&format!(
82
                     "Expected {} but got {}",
82
                     "Expected {} but got {}",
83
                     token_type, token.token_type
83
                     token_type, token.token_type
84
                 )))
84
                 )))
85
             }
85
             }
86
         }
86
         }
87
-        None => Err(ParserError::new("Unexpected end of tokens")),
87
+        None => Err(ElasticError::new("Unexpected end of tokens")),
88
     }
88
     }
89
 }
89
 }
90
 
90
 
93
     use super::*;
93
     use super::*;
94
     use crate::lexer::scan;
94
     use crate::lexer::scan;
95
 
95
 
96
-    fn _parse(input: &str) -> Result<Select, ParserError> {
96
+    fn _parse(input: &str) -> Result<Select, ElasticError> {
97
         let tokens = scan(input).unwrap();
97
         let tokens = scan(input).unwrap();
98
         parse(tokens)
98
         parse(tokens)
99
     }
99
     }

Loading…
Cancel
Save