use crate::syntax::ast::keyword::Keyword::*;
use std::error;
use std::fmt::Error;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
#[derive(Clone, PartialEq, Debug)]
pub enum Keyword {
Await,
Break,
Case,
Catch,
Class,
Continue,
Const,
Debugger,
Default,
Delete,
Do,
Else,
Enum,
Export,
Extends,
Finally,
For,
Function,
If,
In,
InstanceOf,
Import,
Let,
New,
Return,
Super,
Switch,
This,
Throw,
Try,
TypeOf,
Var,
Void,
While,
With,
Yield,
}
#[derive(Debug, Clone)]
pub struct KeywordError;
impl Display for KeywordError {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "invalid token")
}
}
impl error::Error for KeywordError {
fn description(&self) -> &str {
"invalid token"
}
fn cause(&self) -> Option<&error::Error> {
None
}
}
impl FromStr for Keyword {
type Err = KeywordError;
fn from_str(s: &str) -> Result<Keyword, Self::Err> {
match s {
"await" => Ok(Await),
"break" => Ok(Break),
"case" => Ok(Case),
"catch" => Ok(Catch),
"class" => Ok(Class),
"continue" => Ok(Continue),
"const" => Ok(Const),
"debugger" => Ok(Debugger),
"default" => Ok(Default),
"delete" => Ok(Delete),
"do" => Ok(Do),
"else" => Ok(Else),
"enum" => Ok(Enum),
"extends" => Ok(Extends),
"export" => Ok(Export),
"finally" => Ok(Finally),
"for" => Ok(For),
"function" => Ok(Function),
"if" => Ok(If),
"in" => Ok(In),
"instanceof" => Ok(InstanceOf),
"import" => Ok(Import),
"let" => Ok(Let),
"new" => Ok(New),
"return" => Ok(Return),
"super" => Ok(Super),
"switch" => Ok(Switch),
"this" => Ok(This),
"throw" => Ok(Throw),
"try" => Ok(Try),
"typeof" => Ok(TypeOf),
"var" => Ok(Var),
"void" => Ok(Void),
"while" => Ok(While),
"with" => Ok(With),
"yield" => Ok(Yield),
_ => Err(KeywordError),
}
}
}
impl Display for Keyword {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(
f,
"{}",
match *self {
Await => "await",
Break => "break",
Case => "case",
Catch => "catch",
Class => "class",
Continue => "continue",
Const => "const",
Debugger => "debugger",
Default => "default",
Delete => "delete",
Do => "do",
Else => "else",
Enum => "enum",
Extends => "extends",
Export => "export",
Finally => "finally",
For => "for",
Function => "function",
If => "if",
In => "in",
InstanceOf => "instanceof",
Import => "import",
Let => "let",
New => "new",
Return => "return",
Super => "super",
Switch => "switch",
This => "this",
Throw => "throw",
Try => "try",
TypeOf => "typeof",
Var => "var",
Void => "void",
While => "while",
With => "with",
Yield => "yield",
}
)
}
}