From 9e5a4b410d026ac4a7d527edc4b6848524d281a3 Mon Sep 17 00:00:00 2001 From: ansh Date: Mon, 24 Jun 2024 04:01:53 -0700 Subject: [PATCH] Add RAW_STRING_LITERAL gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Handle case for RAW_STRING_LITERAL. * ast/rust-ast.cc (AttributeParser::parse_meta_item_inner): Likewise. (AttributeParser::parse_literal): Likewise. * ast/rust-ast.h: Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_literal): Likewise. * lex/rust-lex.cc (Lexer::parse_raw_string): Likewise. * lex/rust-token.cc (Token::as_string): Likewise. * lex/rust-token.h (enum PrimitiveCoreType): Likewise. * parse/rust-parse-impl.h (Parser::parse_attr_input): Likewise. (Parser::parse_literal_expr): Likewise. (Parser::parse_pattern_no_alt): Likewise. Signed-off-by: ansh --- gcc/rust/ast/rust-ast-collector.cc | 6 ++++++ gcc/rust/ast/rust-ast.cc | 5 +++++ gcc/rust/ast/rust-ast.h | 2 ++ gcc/rust/hir/rust-ast-lower-base.cc | 3 +++ gcc/rust/lex/rust-lex.cc | 2 +- gcc/rust/lex/rust-token.cc | 3 +++ gcc/rust/lex/rust-token.h | 8 ++++++++ gcc/rust/parse/rust-parse-impl.h | 17 +++++++++++++++++ 8 files changed, 45 insertions(+), 1 deletion(-) diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index 78a30afe0dd5..3a72bd17935c 100644 --- a/gcc/rust/ast/rust-ast-collector.cc +++ b/gcc/rust/ast/rust-ast-collector.cc @@ -398,6 +398,9 @@ TokenCollector::visit (Token &tok) case BYTE_STRING_LITERAL: push (Rust::Token::make_byte_string (tok.get_locus (), std::move (data))); break; + case RAW_STRING_LITERAL: + push (Rust::Token::make_raw_string (tok.get_locus (), std::move (data))); + break; case INNER_DOC_COMMENT: push (Rust::Token::make_inner_doc_comment (tok.get_locus (), std::move (data))); @@ -777,6 +780,9 @@ TokenCollector::visit (Literal &lit, location_t locus) case Literal::LitType::BYTE_STRING: push (Rust::Token::make_byte_string (locus, std::move (value))); break; + case Literal::LitType::RAW_STRING: + push (Rust::Token::make_raw_string (locus, std::move (value))); + break; case Literal::LitType::INT: push ( Rust::Token::make_int (locus, std::move (value), lit.get_type_hint ())); diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 2a173c4610df..9c0b1bf82452 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -3506,6 +3506,7 @@ AttributeParser::parse_meta_item_inner () case STRING_LITERAL: case BYTE_CHAR_LITERAL: case BYTE_STRING_LITERAL: + case RAW_STRING_LITERAL: case INT_LITERAL: case FLOAT_LITERAL: case TRUE_LITERAL: @@ -3788,6 +3789,10 @@ AttributeParser::parse_literal () skip_token (); return Literal (tok->as_string (), Literal::BYTE_STRING, tok->get_type_hint ()); + case RAW_STRING_LITERAL: + skip_token (); + return Literal (tok->as_string (), Literal::RAW_STRING, + tok->get_type_hint ()); case INT_LITERAL: skip_token (); return Literal (tok->as_string (), Literal::INT, tok->get_type_hint ()); diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 4fb803c258b9..d9edec063879 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -250,6 +250,7 @@ class Token : public TokenTree, public MacroMatch { case STRING_LITERAL: case BYTE_STRING_LITERAL: + case RAW_STRING_LITERAL: return true; default: return false; @@ -311,6 +312,7 @@ struct Literal STRING, BYTE, BYTE_STRING, + RAW_STRING, INT, FLOAT, BOOL, diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index 5113880600ae..207ea6bca82f 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -928,6 +928,9 @@ ASTLoweringBase::lower_literal (const AST::Literal &literal) case AST::Literal::LitType::BYTE_STRING: type = HIR::Literal::LitType::BYTE_STRING; break; + case AST::Literal::LitType::RAW_STRING: // TODO: Lower raw string literals. + rust_unreachable (); + break; case AST::Literal::LitType::INT: type = HIR::Literal::LitType::INT; break; diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index e5c9148976c1..d1845a4f9e71 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -2218,7 +2218,7 @@ Lexer::parse_raw_string (location_t loc, int initial_hash_count) str.shrink_to_fit (); - return Token::make_string (loc, std::move (str)); + return Token::make_raw_string (loc, std::move (str)); } template diff --git a/gcc/rust/lex/rust-token.cc b/gcc/rust/lex/rust-token.cc index 7bb327358a23..e1eb78f66a67 100644 --- a/gcc/rust/lex/rust-token.cc +++ b/gcc/rust/lex/rust-token.cc @@ -247,6 +247,9 @@ Token::as_string () const case BYTE_STRING_LITERAL: return "b\"" + escape_special_chars (get_str (), Context::String) + "\""; + case RAW_STRING_LITERAL: + return "r\"" + escape_special_chars (get_str (), Context::String) + + "\""; case CHAR_LITERAL: return "'" + escape_special_chars (get_str (), Context::Char) + "'"; case BYTE_CHAR_LITERAL: diff --git a/gcc/rust/lex/rust-token.h b/gcc/rust/lex/rust-token.h index 438b29b49572..1a2a28682e15 100644 --- a/gcc/rust/lex/rust-token.h +++ b/gcc/rust/lex/rust-token.h @@ -128,6 +128,7 @@ enum PrimitiveCoreType RS_TOKEN (STRING_LITERAL, "string literal") \ RS_TOKEN (CHAR_LITERAL, "character literal") \ RS_TOKEN (BYTE_STRING_LITERAL, "byte string literal") \ + RS_TOKEN (RAW_STRING_LITERAL, "raw string literal") \ RS_TOKEN (BYTE_CHAR_LITERAL, "byte character literal") \ RS_TOKEN (LIFETIME, "lifetime") /* TODO: improve token type */ \ /* Have "interpolated" tokens (whatever that means)? identifer, path, type, \ @@ -377,6 +378,12 @@ class Token return TokenPtr (new Token (BYTE_STRING_LITERAL, locus, std::move (str))); } + // Makes and returns a new TokenPtr of type RAW_STRING_LITERAL. + static TokenPtr make_raw_string (location_t locus, std::string &&str) + { + return TokenPtr (new Token (RAW_STRING_LITERAL, locus, std::move (str))); + } + // Makes and returns a new TokenPtr of type INNER_DOC_COMMENT. static TokenPtr make_inner_doc_comment (location_t locus, std::string &&str) { @@ -450,6 +457,7 @@ return *str; case STRING_LITERAL: case BYTE_CHAR_LITERAL: case BYTE_STRING_LITERAL: + case RAW_STRING_LITERAL: return true; default: return false; diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 0a20bf632fa7..aff81448deae 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -854,6 +854,9 @@ Parser::parse_attr_input () case BYTE_STRING_LITERAL: lit_type = AST::Literal::BYTE_STRING; break; + case RAW_STRING_LITERAL: + lit_type = AST::Literal::RAW_STRING; + break; case STRING_LITERAL: default: lit_type = AST::Literal::STRING; @@ -7511,6 +7514,11 @@ Parser::parse_literal_expr (AST::AttrVec outer_attrs) literal_value = t->get_str (); lexer.skip_token (); break; + case RAW_STRING_LITERAL: + type = AST::Literal::RAW_STRING; + literal_value = t->get_str (); + lexer.skip_token (); + break; case INT_LITERAL: type = AST::Literal::INT; literal_value = t->get_str (); @@ -10481,6 +10489,11 @@ Parser::parse_pattern_no_alt () return std::unique_ptr ( new AST::LiteralPattern (t->get_str (), AST::Literal::BYTE_STRING, t->get_locus (), t->get_type_hint ())); + case RAW_STRING_LITERAL: + lexer.skip_token (); + return std::unique_ptr ( + new AST::LiteralPattern (t->get_str (), AST::Literal::RAW_STRING, + t->get_locus (), t->get_type_hint ())); // raw string and raw byte string literals too if they are readded to // lexer case MINUS: @@ -12275,6 +12288,10 @@ Parser::null_denotation_not_path ( return std::unique_ptr ( new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE_STRING, tok->get_type_hint (), {}, tok->get_locus ())); + case RAW_STRING_LITERAL: + return std::unique_ptr ( + new AST::LiteralExpr (tok->get_str (), AST::Literal::RAW_STRING, + tok->get_type_hint (), {}, tok->get_locus ())); case CHAR_LITERAL: return std::unique_ptr ( new AST::LiteralExpr (tok->get_str (), AST::Literal::CHAR,