/*
* meli
*
* Copyright 2017-2018 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
/*! A parser module for user commands passed through Command mode.
*/
use crate::melib::parser::BytesExt;
use melib::nom::{
self,
branch::alt,
bytes::complete::{is_a, is_not, tag, take_until},
character::complete::{digit1, not_line_ending},
combinator::{map, map_res},
multi::separated_list,
sequence::{pair, preceded, separated_pair},
IResult,
};
pub use melib::thread::{SortField, SortOrder};
use melib::MeliError;
pub mod actions;
use actions::MailboxOperation;
use std::collections::HashSet;
pub mod history;
pub use crate::actions::AccountAction::{self, *};
pub use crate::actions::Action::{self, *};
pub use crate::actions::ComposeAction::{self, *};
pub use crate::actions::ListingAction::{self, *};
pub use crate::actions::MailingListAction::{self, *};
pub use crate::actions::TabAction::{self, *};
pub use crate::actions::TagAction::{self, *};
pub use crate::actions::ViewAction::{self, *};
use std::str::FromStr;
/// Helper macro to convert an array of tokens into a TokenStream
macro_rules! to_stream {
($token: expr) => {
TokenStream {
tokens: &[$token],
}
};
($($tokens:expr),*) => {
TokenStream {
tokens: &[$($tokens),*],
}
};
}
/// Macro to create a const table with every command part that can be auto-completed and its description
macro_rules! define_commands {
( [$({ tags: [$( $tags:literal),*], desc: $desc:literal, tokens: $tokens:expr, parser: ($parser:item)}),*]) => {
pub const COMMAND_COMPLETION: &[(&str, &str, TokenStream)] = &[$($( ($tags, $desc, TokenStream { tokens: $tokens } ) ),*),* ];
$( $parser )*
};
}
pub fn quoted_argument(input: &[u8]) -> IResult<&[u8], &str> {
if input.is_empty() {
return Err(nom::Err::Error((input, nom::error::ErrorKind::Tag)));
}
if input[0] == b'"' {
let mut i = 1;
while i < input.len() {
if input[i] == b'\"' && input[i - 1] != b'\\' {
return Ok((&input[i + 1..], unsafe {
std::str::from_utf8_unchecked(&input[1..i])
}));
}
i += 1;
}
Err(nom::Err::Error((input, nom::error::ErrorKind::Tag)))
} else {
map_res(is_not(" "), std::str::from_utf8)(input)
}
}
#[derive(Debug, Copy, Clone)]
pub struct TokenStream {
tokens: &'static [TokenAdicity],
}
use Token::*;
use TokenAdicity::*;
impl TokenStream {
fn matches<'s>(&self, s: &mut &'s str, sugg: &mut HashSet<String>) -> Vec<(&'s str, Token)> {
let mut tokens = vec![];
for t in self.tokens.iter() {
let mut ptr = 0;
while ptr + 1 < s.len() && s.as_bytes()[ptr].is_ascii_whitespace() {
ptr += 1;
}
*s = &s[ptr..];
//println!("{:?} {:?}", t, s);
if s.is_empty() {
match t.inner() {
Literal(lit) => {
sugg.insert(format!(" {}", lit));
}
Alternatives(v) => {
for t in v.iter() {
//println!("adding empty suggestions for {:?}", t);
let mut _s = *s;
t.matches(&mut _s, sugg);
}
}