string_literal_expr

func string_literal_expr(value: String, source_id: Int, source_span: Range<Int>, context: &QuoteContext) -> Syntax<Expr>

Construct a category-checked string literal without routing generated text through the lexer. This is intentionally a literal-only constructor: macro authors still cannot manufacture identifiers or their syntax contexts from strings. The generated token points back to the source syntax that caused the literal so diagnostics retain useful provenance.

View Source
pub func string_literal_expr(value: String, source_id: Int, source_span: Range<Int>, context: &QuoteContext) -> Syntax<Expr> {
	let spelling_builder = StringBuilder(capacity: value.utf8_count() + 2)
	spelling_builder.append(string: "\"")
	for character in value {
		spelling_builder.append(string: match character {
			'\\' -> "\\\\",
			'\"' -> "\\\"",
			'\n' -> "\\n",
			'\r' -> "\\r",
			'\t' -> "\\t",
			_ -> character.to_string()
		})
	}
	spelling_builder.append(string: "\"")
	let spelling = spelling_builder.finish()
	let token = SyntaxToken(
		kind: .string_literal,
		leading: "",
		spelling: spelling,
		lexeme_start: 0,
		lexeme_end: spelling.utf8().count(),
		source_id: source_id,
		source_span: source_span,
		context: introduced_context(context: context),
		origin: .definition_site
	)
	Syntax<Expr>(raw: RawSyntax(category: .expr, pieces: [.token(token)], trailing: "", group_when_spliced: true))
}