Lex and parse string literals

This commit is contained in:
mars 2022-03-02 12:40:46 -07:00
parent 7b0aa4454e
commit 7a1df3f58d
2 changed files with 10 additions and 0 deletions

View File

@ -82,6 +82,9 @@ pub enum Token {
#[regex(r"([0-9]+([.][0-9]*)?|[.][0-9]+)")] #[regex(r"([0-9]+([.][0-9]*)?|[.][0-9]+)")]
DecimalFloat, DecimalFloat,
#[regex(r#""([^"\\]|\\t|\\u|\\n|\\")*""#)]
StringLiteral,
#[regex(r"//[^\n]*", logos::skip)] #[regex(r"//[^\n]*", logos::skip)]
SingleLineComment, SingleLineComment,

View File

@ -369,6 +369,7 @@ impl<'a> Expr<'a> {
Token::HexInteger => Self::Literal(Literal::HexInteger(lexer.slice())), Token::HexInteger => Self::Literal(Literal::HexInteger(lexer.slice())),
Token::DecimalInteger => Self::Literal(Literal::DecimalInteger(lexer.slice())), Token::DecimalInteger => Self::Literal(Literal::DecimalInteger(lexer.slice())),
Token::DecimalFloat => Self::Literal(Literal::DecimalFloat(lexer.slice())), Token::DecimalFloat => Self::Literal(Literal::DecimalFloat(lexer.slice())),
Token::StringLiteral => Self::Literal(Literal::String(lexer.slice())),
Token::True => Self::Literal(Literal::Boolean(true)), Token::True => Self::Literal(Literal::Boolean(true)),
Token::False => Self::Literal(Literal::Boolean(false)), Token::False => Self::Literal(Literal::Boolean(false)),
Token::Semicolon | Token::BraceClose | Token::ParanClose => return (None, tok), Token::Semicolon | Token::BraceClose | Token::ParanClose => return (None, tok),
@ -477,6 +478,7 @@ pub enum Literal<'a> {
HexInteger(&'a str), HexInteger(&'a str),
DecimalInteger(&'a str), DecimalInteger(&'a str),
DecimalFloat(&'a str), DecimalFloat(&'a str),
String(&'a str),
Boolean(bool), Boolean(bool),
} }
@ -579,6 +581,11 @@ mod tests {
parse_expr("0.1 + .1 * 1.;"); parse_expr("0.1 + .1 * 1.;");
} }
#[test]
fn string_literals() {
parse_expr("\"String contents\";");
}
#[test] #[test]
fn locals() { fn locals() {
parse_expr("local1 + local2 - local3;"); parse_expr("local1 + local2 - local3;");