Skip to content

Commit da3e25a

Browse files
committed
feat: support unsafe expression
1 parent f296690 commit da3e25a

9 files changed

Lines changed: 561 additions & 572 deletions

File tree

syntax/hulo/parser/analyzer.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,42 +3525,55 @@ func (a *Analyzer) VisitExternItem(ctx *generated.ExternItemContext) any {
35253525
func (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 类型
35653578
func (a *Analyzer) convertTokenType(antlrTokenType int) token.Token {
35663579
switch antlrTokenType {

syntax/hulo/parser/generated/huloLexer.interp

Lines changed: 1 addition & 7 deletions
Large diffs are not rendered by default.

syntax/hulo/parser/generated/huloLexer.tokens

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,17 @@ BOOL=109
110110
ANY=110
111111
VOID=111
112112
UnsafeLiteral=112
113-
DOUBLE_LBRACK=113
114-
DOUBLE_RBRACK=114
115-
NumberLiteral=115
116-
StringLiteral=116
117-
BoolLiteral=117
118-
RawStringLiteral=118
119-
FileStringLiteral=119
120-
CommandStringLiteral=120
121-
ESC=121
122-
LineComment=122
123-
BlockComment=123
124-
Identifier=124
125-
WS=125
113+
NumberLiteral=113
114+
StringLiteral=114
115+
BoolLiteral=115
116+
RawStringLiteral=116
117+
FileStringLiteral=117
118+
CommandStringLiteral=118
119+
ESC=119
120+
LineComment=120
121+
BlockComment=121
122+
Identifier=122
123+
WS=123
126124
'mod'=1
127125
'use'=2
128126
'import'=3
@@ -234,5 +232,3 @@ WS=125
234232
'bool'=109
235233
'any'=110
236234
'void'=111
237-
'[['=113
238-
']]'=114

syntax/hulo/parser/generated/huloParser.interp

Lines changed: 1 addition & 5 deletions
Large diffs are not rendered by default.

syntax/hulo/parser/generated/huloParser.tokens

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,17 @@ BOOL=109
110110
ANY=110
111111
VOID=111
112112
UnsafeLiteral=112
113-
DOUBLE_LBRACK=113
114-
DOUBLE_RBRACK=114
115-
NumberLiteral=115
116-
StringLiteral=116
117-
BoolLiteral=117
118-
RawStringLiteral=118
119-
FileStringLiteral=119
120-
CommandStringLiteral=120
121-
ESC=121
122-
LineComment=122
123-
BlockComment=123
124-
Identifier=124
125-
WS=125
113+
NumberLiteral=113
114+
StringLiteral=114
115+
BoolLiteral=115
116+
RawStringLiteral=116
117+
FileStringLiteral=117
118+
CommandStringLiteral=118
119+
ESC=119
120+
LineComment=120
121+
BlockComment=121
122+
Identifier=122
123+
WS=123
126124
'mod'=1
127125
'use'=2
128126
'import'=3
@@ -234,5 +232,3 @@ WS=125
234232
'bool'=109
235233
'any'=110
236234
'void'=111
237-
'[['=113
238-
']]'=114

syntax/hulo/parser/generated/hulo_lexer.go

Lines changed: 386 additions & 384 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

syntax/hulo/parser/generated/hulo_parser.go

Lines changed: 110 additions & 113 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

syntax/hulo/parser/grammar/huloLexer.g4

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,7 @@ BOOL : 'bool';
172172
ANY : 'any';
173173
VOID : 'void';
174174

175-
UnsafeLiteral: DOUBLE_LBRACK (BACKSLASH RBRACK | ~[\]])* DOUBLE_RBRACK;
176-
177-
// UnsafeBlock: LBRACE (BACKSLASH RBRACE | ~[}])* RBRACE;
178-
179-
DOUBLE_LBRACK : '[[';
180-
DOUBLE_RBRACK : ']]';
175+
UnsafeLiteral: '__UNSAFE_BEGIN__' .*? '__UNSAFE_END__';
181176

182177
fragment TRUE : 'true';
183178
fragment FALSE : 'false';
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
unsafe {
1+
__UNSAFE_BEGIN__
22
MsgBox "Hello, World!"
33
Dim count
4-
}
4+
__UNSAFE_END__
55

66
extern count: num
77
echo $count

0 commit comments

Comments
 (0)