2018-12-27 23:46:15 +09:00
|
|
|
use std::io::Write;
|
2019-03-14 22:30:42 +09:00
|
|
|
use std::result::Result;
|
2018-12-26 14:45:31 +09:00
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
use super::path_reader::{PathReader, ReaderError};
|
2018-12-26 14:45:31 +09:00
|
|
|
|
2019-06-11 18:40:18 +09:00
|
|
|
pub const ABSOLUTE: &'static str = "$";
|
|
|
|
pub const DOT: &'static str = ".";
|
|
|
|
pub const AT: &'static str = "@";
|
|
|
|
pub const OPEN_ARRAY: &'static str = "[";
|
|
|
|
pub const CLOSE_ARRAY: &'static str = "]";
|
|
|
|
pub const ASTERISK: &'static str = "*";
|
|
|
|
pub const QUESTION: &'static str = "?";
|
|
|
|
pub const COMMA: &'static str = ",";
|
|
|
|
pub const SPLIT: &'static str = ":";
|
|
|
|
pub const OPEN_PARENTHESIS: &'static str = "(";
|
|
|
|
pub const CLOSE_PARENTHESIS: &'static str = ")";
|
|
|
|
pub const KEY: &'static str = "Key";
|
|
|
|
pub const DOUBLE_QUOTA: &'static str = "\"";
|
|
|
|
pub const SINGLE_QUOTA: &'static str = "'";
|
|
|
|
pub const EQUAL: &'static str = "==";
|
|
|
|
pub const GREATER_OR_EQUAL: &'static str = ">=";
|
|
|
|
pub const GREATER: &'static str = ">";
|
|
|
|
pub const LITTLE: &'static str = "<";
|
|
|
|
pub const LITTLE_OR_EQUAL: &'static str = "<=";
|
|
|
|
pub const NOT_EQUAL: &'static str = "!=";
|
|
|
|
pub const AND: &'static str = "&&";
|
|
|
|
pub const OR: &'static str = "||";
|
|
|
|
pub const WHITESPACE: &'static str = " ";
|
2018-12-26 14:45:31 +09:00
|
|
|
|
|
|
|
const CH_DOLLA: char = '$';
|
|
|
|
const CH_DOT: char = '.';
|
|
|
|
const CH_ASTERISK: char = '*';
|
|
|
|
const CH_LARRAY: char = '[';
|
|
|
|
const CH_RARRAY: char = ']';
|
|
|
|
const CH_LPAREN: char = '(';
|
|
|
|
const CH_RPAREN: char = ')';
|
|
|
|
const CH_AT: char = '@';
|
|
|
|
const CH_QUESTION: char = '?';
|
|
|
|
const CH_COMMA: char = ',';
|
|
|
|
const CH_SEMICOLON: char = ':';
|
|
|
|
const CH_EQUAL: char = '=';
|
|
|
|
const CH_AMPERSAND: char = '&';
|
|
|
|
const CH_PIPE: char = '|';
|
|
|
|
const CH_LITTLE: char = '<';
|
|
|
|
const CH_GREATER: char = '>';
|
|
|
|
const CH_EXCLAMATION: char = '!';
|
|
|
|
const CH_SINGLE_QUOTA: char = '\'';
|
|
|
|
const CH_DOUBLE_QUOTA: char = '"';
|
|
|
|
|
2018-12-27 23:46:15 +09:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum TokenError {
|
|
|
|
Eof,
|
|
|
|
Position(usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_token_error(read_err: ReaderError) -> TokenError {
|
|
|
|
match read_err {
|
|
|
|
ReaderError::Eof => TokenError::Eof
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
2018-12-26 14:45:31 +09:00
|
|
|
pub enum Token {
|
|
|
|
Absolute(usize),
|
2018-12-27 23:46:15 +09:00
|
|
|
Dot(usize),
|
2018-12-26 14:45:31 +09:00
|
|
|
At(usize),
|
|
|
|
OpenArray(usize),
|
|
|
|
CloseArray(usize),
|
|
|
|
Asterisk(usize),
|
|
|
|
Question(usize),
|
|
|
|
Comma(usize),
|
|
|
|
Split(usize),
|
|
|
|
OpenParenthesis(usize),
|
|
|
|
CloseParenthesis(usize),
|
2019-03-11 17:35:15 +09:00
|
|
|
Key(usize, String),
|
|
|
|
DoubleQuoted(usize, String),
|
|
|
|
SingleQuoted(usize, String),
|
2018-12-26 14:45:31 +09:00
|
|
|
Equal(usize),
|
|
|
|
GreaterOrEqual(usize),
|
|
|
|
Greater(usize),
|
|
|
|
Little(usize),
|
|
|
|
LittleOrEqual(usize),
|
|
|
|
NotEqual(usize),
|
|
|
|
And(usize),
|
|
|
|
Or(usize),
|
2018-12-27 23:46:15 +09:00
|
|
|
Whitespace(usize, usize),
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Token {
|
2018-12-27 23:46:15 +09:00
|
|
|
pub fn partial_eq(&self, other: Token) -> bool {
|
|
|
|
self.to_simple() == other.to_simple()
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:40:18 +09:00
|
|
|
pub fn simple_eq(&self, str_token: &str) -> bool {
|
|
|
|
self.to_simple() == str_token
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:46:15 +09:00
|
|
|
fn to_simple(&self) -> &'static str {
|
2018-12-26 14:45:31 +09:00
|
|
|
match self {
|
|
|
|
Token::Absolute(_) => ABSOLUTE,
|
2018-12-27 23:46:15 +09:00
|
|
|
Token::Dot(_) => DOT,
|
2018-12-26 14:45:31 +09:00
|
|
|
Token::At(_) => AT,
|
|
|
|
Token::OpenArray(_) => OPEN_ARRAY,
|
|
|
|
Token::CloseArray(_) => CLOSE_ARRAY,
|
|
|
|
Token::Asterisk(_) => ASTERISK,
|
|
|
|
Token::Question(_) => QUESTION,
|
|
|
|
Token::Comma(_) => COMMA,
|
|
|
|
Token::Split(_) => SPLIT,
|
2018-12-27 23:46:15 +09:00
|
|
|
Token::OpenParenthesis(_) => OPEN_PARENTHESIS,
|
|
|
|
Token::CloseParenthesis(_) => CLOSE_PARENTHESIS,
|
2018-12-26 14:45:31 +09:00
|
|
|
Token::Key(_, _) => KEY,
|
2018-12-27 23:46:15 +09:00
|
|
|
Token::DoubleQuoted(_, _) => DOUBLE_QUOTA,
|
|
|
|
Token::SingleQuoted(_, _) => SINGLE_QUOTA,
|
2018-12-26 14:45:31 +09:00
|
|
|
Token::Equal(_) => EQUAL,
|
|
|
|
Token::GreaterOrEqual(_) => GREATER_OR_EQUAL,
|
|
|
|
Token::Greater(_) => GREATER,
|
|
|
|
Token::Little(_) => LITTLE,
|
|
|
|
Token::LittleOrEqual(_) => LITTLE_OR_EQUAL,
|
|
|
|
Token::NotEqual(_) => NOT_EQUAL,
|
|
|
|
Token::And(_) => AND,
|
|
|
|
Token::Or(_) => OR,
|
2018-12-27 23:46:15 +09:00
|
|
|
Token::Whitespace(_, _) => WHITESPACE
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn simple_matched_token(ch: char, pos: usize) -> Option<Token> {
|
|
|
|
match ch {
|
|
|
|
CH_DOLLA => Some(Token::Absolute(pos)),
|
2018-12-27 23:46:15 +09:00
|
|
|
CH_DOT => Some(Token::Dot(pos)),
|
2018-12-26 14:45:31 +09:00
|
|
|
CH_ASTERISK => Some(Token::Asterisk(pos)),
|
|
|
|
CH_LARRAY => Some(Token::OpenArray(pos)),
|
|
|
|
CH_RARRAY => Some(Token::CloseArray(pos)),
|
|
|
|
CH_LPAREN => Some(Token::OpenParenthesis(pos)),
|
|
|
|
CH_RPAREN => Some(Token::CloseParenthesis(pos)),
|
|
|
|
CH_AT => Some(Token::At(pos)),
|
|
|
|
CH_QUESTION => Some(Token::Question(pos)),
|
|
|
|
CH_COMMA => Some(Token::Comma(pos)),
|
|
|
|
CH_SEMICOLON => Some(Token::Split(pos)),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Tokenizer<'a> {
|
|
|
|
input: PathReader<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Tokenizer<'a> {
|
|
|
|
pub fn new(input: &'a str) -> Self {
|
|
|
|
Tokenizer {
|
|
|
|
input: PathReader::new(input),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn single_quota(&mut self, pos: usize, ch: char) -> Result<Token, TokenError> {
|
2019-03-11 17:35:15 +09:00
|
|
|
let (_, val) = self.input.take_while(|c| *c != ch).map_err(to_token_error)?;
|
2018-12-27 23:46:15 +09:00
|
|
|
self.input.next_char().map_err(to_token_error)?;
|
2019-03-11 17:35:15 +09:00
|
|
|
Ok(Token::SingleQuoted(pos, val))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn double_quota(&mut self, pos: usize, ch: char) -> Result<Token, TokenError> {
|
2019-03-11 17:35:15 +09:00
|
|
|
let (_, val) = self.input.take_while(|c| *c != ch).map_err(to_token_error)?;
|
2018-12-27 23:46:15 +09:00
|
|
|
self.input.next_char().map_err(to_token_error)?;
|
2019-03-11 17:35:15 +09:00
|
|
|
Ok(Token::DoubleQuoted(pos, val))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn equal(&mut self, pos: usize, _: char) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
let (_, ch) = self.input.peek_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
match ch {
|
|
|
|
CH_EQUAL => {
|
2018-12-27 23:46:15 +09:00
|
|
|
self.input.next_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
Ok(Token::Equal(pos))
|
|
|
|
}
|
2018-12-27 23:46:15 +09:00
|
|
|
_ => Err(TokenError::Position(pos))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn not_equal(&mut self, pos: usize, _: char) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
let (_, ch) = self.input.peek_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
match ch {
|
|
|
|
CH_EQUAL => {
|
2018-12-27 23:46:15 +09:00
|
|
|
self.input.next_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
Ok(Token::NotEqual(pos))
|
|
|
|
}
|
2018-12-27 23:46:15 +09:00
|
|
|
_ => Err(TokenError::Position(pos))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn little(&mut self, pos: usize, _: char) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
let (_, ch) = self.input.peek_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
match ch {
|
|
|
|
CH_EQUAL => {
|
2018-12-27 23:46:15 +09:00
|
|
|
self.input.next_char().map_err(to_token_error)?;
|
|
|
|
Ok(Token::LittleOrEqual(pos))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
2018-12-27 23:46:15 +09:00
|
|
|
_ => Ok(Token::Little(pos)),
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn greater(&mut self, pos: usize, _: char) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
let (_, ch) = self.input.peek_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
match ch {
|
|
|
|
CH_EQUAL => {
|
2018-12-27 23:46:15 +09:00
|
|
|
self.input.next_char().map_err(to_token_error)?;
|
|
|
|
Ok(Token::GreaterOrEqual(pos))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
2018-12-27 23:46:15 +09:00
|
|
|
_ => Ok(Token::Greater(pos)),
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn and(&mut self, pos: usize, _: char) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
let (_, ch) = self.input.peek_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
match ch {
|
|
|
|
CH_AMPERSAND => {
|
2018-12-27 23:46:15 +09:00
|
|
|
let _ = self.input.next_char().map_err(to_token_error);
|
2018-12-26 14:45:31 +09:00
|
|
|
Ok(Token::And(pos))
|
|
|
|
}
|
2018-12-27 23:46:15 +09:00
|
|
|
_ => Err(TokenError::Position(pos))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn or(&mut self, pos: usize, _: char) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
let (_, ch) = self.input.peek_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
match ch {
|
|
|
|
CH_PIPE => {
|
2018-12-27 23:46:15 +09:00
|
|
|
self.input.next_char().map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
Ok(Token::Or(pos))
|
|
|
|
}
|
2018-12-27 23:46:15 +09:00
|
|
|
_ => Err(TokenError::Position(pos))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn whitespace(&mut self, pos: usize, _: char) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
let (_, vec) = self.input.take_while(|c| c.is_whitespace()).map_err(to_token_error)?;
|
|
|
|
Ok(Token::Whitespace(pos, vec.len()))
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
fn other(&mut self, pos: usize, ch: char) -> Result<Token, TokenError> {
|
2018-12-26 14:45:31 +09:00
|
|
|
let fun = |c: &char| {
|
|
|
|
match simple_matched_token(*c, pos) {
|
|
|
|
Some(_) => false,
|
|
|
|
_ if c == &CH_LITTLE
|
|
|
|
|| c == &CH_GREATER
|
|
|
|
|| c == &CH_EQUAL
|
|
|
|
|| c == &CH_AMPERSAND
|
|
|
|
|| c == &CH_PIPE
|
|
|
|
|| c == &CH_EXCLAMATION => false,
|
|
|
|
_ => !c.is_whitespace()
|
|
|
|
}
|
|
|
|
};
|
2018-12-27 23:46:15 +09:00
|
|
|
let (_, mut vec) = self.input.take_while(fun).map_err(to_token_error)?;
|
2018-12-26 14:45:31 +09:00
|
|
|
vec.insert(0, ch);
|
|
|
|
Ok(Token::Key(pos, vec))
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
pub fn next_token(&mut self) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
let (pos, ch) = self.input.next_char().map_err(to_token_error)?;
|
|
|
|
match simple_matched_token(ch, pos) {
|
2018-12-26 14:45:31 +09:00
|
|
|
Some(t) => Ok(t),
|
|
|
|
None => {
|
|
|
|
match ch {
|
|
|
|
CH_SINGLE_QUOTA => self.single_quota(pos, ch),
|
|
|
|
CH_DOUBLE_QUOTA => self.double_quota(pos, ch),
|
|
|
|
CH_EQUAL => self.equal(pos, ch),
|
2018-12-27 23:46:15 +09:00
|
|
|
CH_GREATER => self.greater(pos, ch),
|
|
|
|
CH_LITTLE => self.little(pos, ch),
|
2018-12-26 14:45:31 +09:00
|
|
|
CH_AMPERSAND => self.and(pos, ch),
|
|
|
|
CH_PIPE => self.or(pos, ch),
|
|
|
|
CH_EXCLAMATION => self.not_equal(pos, ch),
|
|
|
|
_ if ch.is_whitespace() => self.whitespace(pos, ch),
|
|
|
|
_ => self.other(pos, ch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 23:46:15 +09:00
|
|
|
}
|
2018-12-26 14:45:31 +09:00
|
|
|
|
2018-12-27 23:46:15 +09:00
|
|
|
fn current_pos(&self) -> usize {
|
|
|
|
self.input.current_pos()
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:45:26 +09:00
|
|
|
pub struct TokenReader<'a> {
|
2018-12-27 23:46:15 +09:00
|
|
|
origin_input: &'a str,
|
|
|
|
err: TokenError,
|
|
|
|
err_pos: usize,
|
|
|
|
tokens: Vec<(usize, Token)>,
|
|
|
|
curr_pos: Option<usize>,
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:45:26 +09:00
|
|
|
impl<'a> TokenReader<'a> {
|
2018-12-27 23:46:15 +09:00
|
|
|
pub fn new(input: &'a str) -> Self {
|
|
|
|
let mut tokenizer = Tokenizer::new(input);
|
|
|
|
let mut tokens = vec![];
|
|
|
|
loop {
|
|
|
|
match tokenizer.next_token() {
|
|
|
|
Ok(t) => {
|
|
|
|
tokens.insert(0, (tokenizer.current_pos(), t));
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2019-06-03 18:45:26 +09:00
|
|
|
return TokenReader {
|
2019-02-22 00:22:30 +09:00
|
|
|
origin_input: input.clone(),
|
|
|
|
err: e,
|
|
|
|
err_pos: tokenizer.current_pos(),
|
|
|
|
tokens,
|
|
|
|
curr_pos: None,
|
|
|
|
};
|
2018-12-27 23:46:15 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 14:45:31 +09:00
|
|
|
|
2019-06-11 18:40:18 +09:00
|
|
|
pub fn peek_is(&self, simple_token: &str) -> bool {
|
|
|
|
match self.peek_token() {
|
|
|
|
Ok(t) => t.simple_eq(simple_token),
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
pub fn peek_token(&self) -> Result<&Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
match self.tokens.last() {
|
|
|
|
Some((_, t)) => {
|
|
|
|
trace!("%{:?}", t);
|
|
|
|
Ok(t)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
trace!("%{:?}", self.err);
|
|
|
|
Err(self.err.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:30:42 +09:00
|
|
|
pub fn next_token(&mut self) -> Result<Token, TokenError> {
|
2018-12-27 23:46:15 +09:00
|
|
|
match self.tokens.pop() {
|
|
|
|
Some((pos, t)) => {
|
|
|
|
self.curr_pos = Some(pos);
|
|
|
|
trace!("@{:?}", t);
|
|
|
|
Ok(t)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
trace!("@{:?}", self.err);
|
|
|
|
Err(self.err.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn err_msg_with_pos(&self, pos: usize) -> String {
|
|
|
|
let mut w = Vec::new();
|
|
|
|
writeln!(&mut w, "{}", self.origin_input).unwrap();
|
|
|
|
writeln!(&mut w, "{}", "^".repeat(pos)).unwrap();
|
|
|
|
match std::str::from_utf8(&w[..]) {
|
|
|
|
Ok(s) => s.to_owned(),
|
|
|
|
Err(_) => panic!("Invalid UTF-8")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn err_msg(&self) -> String {
|
|
|
|
match self.curr_pos {
|
|
|
|
Some(pos) => {
|
|
|
|
self.err_msg_with_pos(pos)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.err_msg_with_pos(self.err_pos)
|
|
|
|
}
|
2018-12-26 14:45:31 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|