diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index 3b827124..0d70def0 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -138,14 +138,9 @@ impl<'source> Parser<'source> { /// Get an Identifier fn get_ident(&mut self) -> Result, 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