extracted t-dark substitution to separate function

This commit is contained in:
Erin 2022-05-02 23:12:32 +02:00 committed by ondra05
parent b76e890fb7
commit ab76516624

View file

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