extracted t-dark substitution to separate function

pull/10/head
ondra05 2022-05-02 23:12:32 +02:00
parent b6f37b6ced
commit 77dc87901e
1 changed files with 14 additions and 18 deletions

View File

@ -138,14 +138,9 @@ impl<'source> Parser<'source> {
/// Get an Identifier
fn get_ident(&mut self) -> Result<Spanned<String>, Error> {
match self.checked_next()? {
Token::Identifier(ident) => Ok(Spanned::new(
if self.tdark {
ident.replace("lang", "script")
} else {
ident
},
self.lexer.span(),
)),
Token::Identifier(ident) => {
Ok(Spanned::new(self.tdark_subst(ident), self.lexer.span()))
}
t => Err(Error::new(ErrorKind::UnexpectedToken(t), self.lexer.span())),
}
}
@ -168,11 +163,7 @@ impl<'source> Parser<'source> {
match token {
// Values
Token::Identifier(i) => Ok(Spanned::new(
Expr::Variable(if self.tdark {
i.replace("lang", "script")
} else {
i
}),
Expr::Variable(self.tdark_subst(i)),
start..self.lexer.span().end,
)),
Token::Integer(i) => Ok(Spanned::new(
@ -180,11 +171,7 @@ impl<'source> Parser<'source> {
start..self.lexer.span().end,
)),
Token::String(s) => Ok(Spanned::new(
Expr::Literal(Literal::Str(if self.tdark {
s.replace("lang", "script")
} else {
s
})),
Expr::Literal(Literal::Str(self.tdark_subst(s))),
start..self.lexer.span().end,
)),
Token::Char(c) => Ok(Spanned::new(
@ -607,6 +594,15 @@ impl<'source> Parser<'source> {
body: self.get_block()?,
})
}
/// Perform lang -> script substitution if in T-Dark block
fn tdark_subst(&self, string: String) -> String {
if self.tdark {
string.replace("lang", "script")
} else {
string
}
}
}
/// Parse AbleScript code into AST