@@ -3525,42 +3525,55 @@ func (a *Analyzer) VisitExternItem(ctx *generated.ExternItemContext) any {
35253525func (a * Analyzer ) VisitUnsafeExpression (ctx * generated.UnsafeExpressionContext ) any {
35263526 return a .visitWrapper ("UnsafeExpression" , ctx , func () any {
35273527 // Handle unsafe block: unsafe { ... }
3528- if ctx .UnsafeStringLiteral () != nil {
3529- unsafeStringLiteral := ctx .UnsafeStringLiteral ()
3528+ if ctx .UnsafeLiteral () != nil {
3529+ unsafeBlock := ctx .UnsafeLiteral ()
35303530 // Get the text from the UnsafeBlock token
3531- blockText := unsafeStringLiteral .GetText ()
3531+ blockText := unsafeBlock .GetText ()
35323532
3533- // Remove the "unsafe {" prefix and "}" suffix
3534- if len (blockText ) > 2 { // "unsafe {" is 8 characters
3535- blockText = blockText [2 : len (blockText )- 1 ]
3536- }
3537-
3538- return & ast.UnsafeStmt {
3539- Unsafe : token .Pos (unsafeStringLiteral .GetSymbol ().GetStart ()),
3540- Start : token .Pos (unsafeStringLiteral .GetSymbol ().GetStart ()),
3541- Text : blockText , // Get the text inside the block
3542- EndPos : token .Pos (unsafeStringLiteral .GetSymbol ().GetStop ()),
3543- }
3544- }
3533+ // Extract content inside unsafe { ... }
3534+ // The format is: "unsafe { content }"
3535+ content := extractUnsafeContent (blockText )
35453536
3546- // Handle unsafe literal: [[ ... ]]
3547- if ctx .UnsafeStringLiteral () != nil {
3548- text := ctx .UnsafeStringLiteral ().GetText ()
3549- // Remove the [[ and ]] delimiters
3550- if len (text ) >= 4 {
3551- text = text [2 : len (text )- 2 ]
3552- }
35533537 return & ast.UnsafeStmt {
3554- Unsafe : token .Pos (ctx .UnsafeStringLiteral ().GetSymbol ().GetStart ()),
3555- Text : text ,
3556- EndPos : token .Pos (ctx .UnsafeStringLiteral ().GetSymbol ().GetStop ()),
3538+ Unsafe : token .Pos (unsafeBlock .GetSymbol ().GetStart ()),
3539+ Start : token .Pos (unsafeBlock .GetSymbol ().GetStart ()),
3540+ Text : content , // The extracted content inside the block
3541+ EndPos : token .Pos (unsafeBlock .GetSymbol ().GetStop ()),
35573542 }
35583543 }
35593544
35603545 return nil
35613546 })
35623547}
35633548
3549+ // extractUnsafeContent extracts the content between __UNSAFE_BEGIN__ and __UNSAFE_END__
3550+ // Handles the format: "__UNSAFE_BEGIN__content__UNSAFE_END__" and returns "content"
3551+ func extractUnsafeContent (blockText string ) string {
3552+ beginMarker := "__UNSAFE_BEGIN__"
3553+ endMarker := "__UNSAFE_END__"
3554+
3555+ // Find the begin marker
3556+ startIdx := strings .Index (blockText , beginMarker )
3557+ if startIdx == - 1 {
3558+ return ""
3559+ }
3560+
3561+ // Find the end marker
3562+ endIdx := strings .Index (blockText , endMarker )
3563+ if endIdx == - 1 {
3564+ return ""
3565+ }
3566+
3567+ // Extract content between markers
3568+ contentStart := startIdx + len (beginMarker )
3569+ content := blockText [contentStart :endIdx ]
3570+
3571+ // Trim leading/trailing whitespace
3572+ content = strings .TrimSpace (content )
3573+
3574+ return content
3575+ }
3576+
35643577// convertTokenType 将 ANTLR token 类型转换为 Hulo token 类型
35653578func (a * Analyzer ) convertTokenType (antlrTokenType int ) token.Token {
35663579 switch antlrTokenType {
0 commit comments