Skip to content

Commit

Permalink
Add RAW_STRING_LITERAL
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
AnshVM authored and CohenArthur committed Jun 27, 2024
1 parent 40d5292 commit 9e5a4b4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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 ()));
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 ());
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -311,6 +312,7 @@ struct Literal
STRING,
BYTE,
BYTE_STRING,
RAW_STRING,
INT,
FLOAT,
BOOL,
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/hir/rust-ast-lower-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/lex/rust-lex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename IsDigitFunc>
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/lex/rust-token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions gcc/rust/lex/rust-token.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,9 @@ Parser<ManagedTokenSource>::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;
Expand Down Expand Up @@ -7511,6 +7514,11 @@ Parser<ManagedTokenSource>::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 ();
Expand Down Expand Up @@ -10481,6 +10489,11 @@ Parser<ManagedTokenSource>::parse_pattern_no_alt ()
return std::unique_ptr<AST::LiteralPattern> (
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<AST::LiteralPattern> (
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:
Expand Down Expand Up @@ -12275,6 +12288,10 @@ Parser<ManagedTokenSource>::null_denotation_not_path (
return std::unique_ptr<AST::LiteralExpr> (
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<AST::LiteralExpr> (
new AST::LiteralExpr (tok->get_str (), AST::Literal::RAW_STRING,
tok->get_type_hint (), {}, tok->get_locus ()));
case CHAR_LITERAL:
return std::unique_ptr<AST::LiteralExpr> (
new AST::LiteralExpr (tok->get_str (), AST::Literal::CHAR,
Expand Down

0 comments on commit 9e5a4b4

Please sign in to comment.