sun::parsing::InterpolatedStringParser

sun::parsing::InterpolatedStringParser

class · Source (opens in a new tab)

class sun::parsing::InterpolatedStringParser

Parser for interpolated template strings.

parseToAst: Hello ${name}! -> InterpolatedStringAST (kept in parse tree) desugar (called by LoweringPass): { var interp_alloc_ = std.HeapAllocator(); var interp_result_ = std.String(interp_alloc_, ""); interp_result_.append_literal("Hello "); interp_result_.append(name); interp_result_.append_literal("!"); interp_result_ }

Public Functions

desugar

public · function · static · Source (opens in a new tab)

std::unique_ptr< sun::ast::BlockExprAST > sun::parsing::InterpolatedStringParser::desugar(sun::ast::InterpolatedStringAST &node)

Desugar the node into the runtime block (consumes segment expressions).

Synthetic nodes are stamped with the template's location.

Related: sun::ast::BlockExprAST, sun::ast::InterpolatedStringAST

parseToAst

public · function · static · Source (opens in a new tab)

std::unique_ptr< InterpolatedStringAST > sun::parsing::InterpolatedStringParser::parseToAst(const std::string &content, const sun::support::Position &start, const sun::support::Position &end, const std::string &filePath, SourceFileId sourceFile)

Parse a template string token into the lossless AST node.

content: inner text without backticks (escapes unprocessed). start/end: the TEMPLATE_STRING token's span (backticks included). Sub-expression positions inside ${...} are rebased to absolute file positions (best-effort; exact for offsets).

Related: sun::ast::InterpolatedStringAST, sun::support::Position, SourceFileId

Private Functions

findMatchingBrace

private · function · static · Source (opens in a new tab)

size_t sun::parsing::InterpolatedStringParser::findMatchingBrace(const std::string &content, size_t start)

Find the matching closing brace for an expression, accounting for nesting.

makeCall

private · function · static · Source (opens in a new tab)

std::unique_ptr< sun::ast::CallExprAST > sun::parsing::InterpolatedStringParser::makeCall(std::unique_ptr< ExprAST > callee, std::vector< std::unique_ptr< ExprAST > > args, const sun::support::Position &loc)

Helper: create call expression AST.

Related: sun::ast::CallExprAST, ExprAST, sun::support::Position

makeMemberAccess

private · function · static · Source (opens in a new tab)

std::unique_ptr< sun::ast::MemberAccessAST > sun::parsing::InterpolatedStringParser::makeMemberAccess(std::unique_ptr< ExprAST > object, const std::string &member, const sun::support::Position &loc)

Helper: create member access AST (obj.member).

Related: sun::ast::MemberAccessAST, ExprAST, sun::support::Position

makeNumberLiteral

private · function · static · Source (opens in a new tab)

std::unique_ptr< sun::ast::NumberExprAST > sun::parsing::InterpolatedStringParser::makeNumberLiteral(int64_t value, const sun::support::Position &loc)

Helper: create string literal AST.

Related: sun::ast::NumberExprAST, sun::support::Position

makeStringLiteral

private · function · static · Source (opens in a new tab)

std::unique_ptr< sun::ast::StringLiteralAST > sun::parsing::InterpolatedStringParser::makeStringLiteral(const std::string &value, const sun::support::Position &loc)

Creates a string literal node retaining its source position.

Related: sun::ast::StringLiteralAST, sun::support::Position

makeVarCreate

private · function · static · Source (opens in a new tab)

std::unique_ptr< sun::ast::VariableCreationAST > sun::parsing::InterpolatedStringParser::makeVarCreate(const std::string &name, std::unique_ptr< ExprAST > value, const sun::support::Position &loc)

Helper: create variable creation AST.

Related: sun::ast::VariableCreationAST, ExprAST, sun::support::Position

makeVarRef

private · function · static · Source (opens in a new tab)

std::unique_ptr< sun::ast::VariableReferenceAST > sun::parsing::InterpolatedStringParser::makeVarRef(const std::string &name, const sun::support::Position &loc)

Helper: create variable reference AST.

Related: sun::ast::VariableReferenceAST, sun::support::Position

parseExpression

private · function · static · Source (opens in a new tab)

std::unique_ptr< ExprAST > sun::parsing::InterpolatedStringParser::parseExpression(const std::string &exprText, SourceFileId sourceFile)

Parse an expression string using a sub-parser.

Related: ExprAST, SourceFileId

processEscapes

private · function · static · Source (opens in a new tab)

std::string sun::parsing::InterpolatedStringParser::processEscapes(const std::string &raw)

Process escape sequences in a literal segment.

rebasePositions

private · function · static · Source (opens in a new tab)

void sun::parsing::InterpolatedStringParser::rebasePositions(ExprAST &expr, int lineBase, int colBase, int offsetBase, const std::string &filePath)

Rebase fragment-relative positions in a sub-expression tree to absolute file positions.

Related: ExprAST

tokenize

private · function · static · Source (opens in a new tab)

std::vector< Segment > sun::parsing::InterpolatedStringParser::tokenize(const std::string &content, const sun::support::Position &start, const std::string &filePath, SourceFileId sourceFile)

Split the template string into segments (alternating literals and expressions).

start = template token start (for position rebasing).

Related: sun::ast::InterpolatedStringAST::Segment, sun::support::Position, SourceFileId