stage1 重构代码 语法上重新实现解包语法
This commit is contained in:
1 parent
2a84b61e3d
commit
54425a50c7
10 files changed
+2681
-2478
No files matched your search
+86
-44
@@ -1,4 +1,3 @@
|
||||
// 顶层容器
|
||||
Root = { SOI ~ ContainerMembers ~ EOI }
|
||||
ContainerMembers = { ExternalHeaderList ~ (ContainerDeclaration ~ (COMMA | SEMICOLON)*)* }
|
||||
|
||||
@@ -22,7 +21,6 @@ ContainerDeclaration = {
|
||||
|
||||
ComptimeDecl = { KEYWORD_comptime ~ (Block | Expr) }
|
||||
|
||||
// 函数声明(可带 comptime 前缀,支持修饰符)
|
||||
FnDecl = {
|
||||
DeclHeader ~ KEYWORD_comptime? ~ KEYWORD_fn ~ IDENTIFIER ~ LPAREN ~ ParamDeclList ~ RPAREN ~ (COLON ~ TypeExpr)? ~ (SEMICOLON | Block)
|
||||
}
|
||||
@@ -30,35 +28,38 @@ FnDecl = {
|
||||
ParamDeclList = { (ParamDecl ~ COMMA)* ~ (ParamDecl | DOT3)? }
|
||||
ParamDecl = { HeaderList? ~ BindingIdent ~ COLON ~ TypeExpr }
|
||||
|
||||
// 变量声明(支持修饰符、元组解构与类型推导)
|
||||
VarDecl = { DeclHeader ~ KEYWORD_var ~ VarBody }
|
||||
ConstDecl = { DeclHeader ~ KEYWORD_const ~ VarBody }
|
||||
VarBody = { VarPattern ~ (COLON ~ TypeExpr)? ~ (EQUAL ~ Expr)? }
|
||||
VarBody = { Pattern ~ (COLON ~ TypeExpr)? ~ (EQUAL ~ Expr)? }
|
||||
|
||||
// 类型声明(支持修饰符)
|
||||
TypeDecl = { DeclHeader ~ KEYWORD_type ~ IDENTIFIER ~ EQUAL ~ TypeExpr }
|
||||
|
||||
// 容器成员(支持修饰符)
|
||||
MemberDecl = { DeclHeader ~ IDENTIFIER ~ (COLON ~ TypeExpr)? ~ (EQUAL ~ Expr)? }
|
||||
|
||||
// 变量声明左值模式(允许单独标识符)
|
||||
VarPattern = { VarPatternAtom ~ (COMMA ~ VarPatternAtom)* }
|
||||
VarPatternAtom = {
|
||||
BindingIdent
|
||||
| DOT ~ IDENTIFIER ~ (LBRACKET ~ VarPattern ~ RBRACKET)?
|
||||
| LPAREN ~ VarPattern ~ RPAREN
|
||||
}
|
||||
|
||||
// 匹配模式(不允许单独标识符)
|
||||
MatchPattern = { MatchPatternAtom ~ (COMMA ~ MatchPatternAtom)* }
|
||||
MatchPatternAtom = {
|
||||
Pattern = { PatternAtom ~ (COMMA ~ PatternAtom)* }
|
||||
PatternAtom = {
|
||||
UNDERSCORE
|
||||
| Expr
|
||||
| DOT ~ IDENTIFIER ~ (LBRACKET ~ MatchPattern ~ RBRACKET)?
|
||||
| LPAREN ~ MatchPattern ~ RPAREN
|
||||
| AnonymousAggregatePattern
|
||||
| DOT ~ IDENTIFIER ~ (LPAREN ~ Pattern? ~ RPAREN)?
|
||||
| LPAREN ~ Pattern? ~ RPAREN
|
||||
| PatternLiteral
|
||||
| IDENTIFIER
|
||||
}
|
||||
|
||||
AnonymousAggregatePattern = { DOT ~ LBRACE ~ PatternFieldList? ~ RBRACE }
|
||||
PatternFieldList = { PatternField ~ ((COMMA | SEMICOLON) ~ PatternField)* ~ (COMMA | SEMICOLON)? }
|
||||
PatternField = { DOT ~ IDENTIFIER ~ (EQUAL ~ PatternAtom)? }
|
||||
|
||||
PatternLiteral = {
|
||||
FLOAT_LITERAL
|
||||
| INTEGER_LITERAL
|
||||
| CHAR_LITERAL
|
||||
| STRING_LITERAL
|
||||
| KEYWORD_true
|
||||
| KEYWORD_false
|
||||
| KEYWORD_null
|
||||
}
|
||||
|
||||
// 块内语句
|
||||
Block = { LBRACE ~ Statement* ~ Expr? ~ RBRACE }
|
||||
Statement = {
|
||||
RetStatement
|
||||
@@ -70,7 +71,6 @@ Statement = {
|
||||
| ComptimeStatement
|
||||
}
|
||||
|
||||
// 声明语句(内部声明已包含 HeaderList)
|
||||
DeclStatement = { (VarDecl | ConstDecl | TypeDecl) ~ (COMMA | SEMICOLON)? }
|
||||
ExprStatement = { Expr ~ (COMMA | SEMICOLON)? }
|
||||
ComptimeStatement = { ComptimeDecl ~ (COMMA | SEMICOLON)? }
|
||||
@@ -80,11 +80,8 @@ BreakStatement = { KEYWORD_break ~ Label? ~ Expr? ~ (COMMA | SEMICOLON)? }
|
||||
ContinueStatement = { KEYWORD_continue ~ Label? ~ (COMMA | SEMICOLON)? }
|
||||
DeferStatement = { KEYWORD_defer ~ (Block | Statement) ~ (COMMA | SEMICOLON)? }
|
||||
|
||||
// 标签定义($ 前缀)
|
||||
Label = { DOLLAR ~ IDENTIFIER }
|
||||
|
||||
// ============ 表达式 ============
|
||||
|
||||
Expr = { AssignExpr }
|
||||
|
||||
AssignExpr = { BoolOrExpr ~ (AssignOp ~ AssignExpr)? }
|
||||
@@ -125,8 +122,8 @@ PrimaryExpr = {
|
||||
| AnonymousAggregateLiteral
|
||||
| ArrayLiteral
|
||||
| AnonymousArrayLiteral
|
||||
| ComptimeCallExpr // @name(args)
|
||||
|
||||
| VariantConstruct
|
||||
| ComptimeCallExpr
|
||||
| IDENTIFIER
|
||||
| Block
|
||||
| IfExpr
|
||||
@@ -140,26 +137,74 @@ PrimaryExpr = {
|
||||
|
||||
TupleLiteral = { ParenExpr }
|
||||
|
||||
// ============ 聚合字面量 ============
|
||||
|
||||
AggregateLiteral = { IDENTIFIER ~ LBRACE ~ StructInitList? ~ RBRACE }
|
||||
AnonymousAggregateLiteral = { DOT ~ LBRACE ~ StructInitList? ~ RBRACE }
|
||||
|
||||
StructInitList = { StructInit ~ ((COMMA | SEMICOLON) ~ StructInit)* ~ (COMMA | SEMICOLON)? }
|
||||
StructInit = { DOT ~ IDENTIFIER ~ (EQUAL ~ Expr)? }
|
||||
|
||||
// ============ 数组/切片字面量 ============
|
||||
VariantConstruct = { DOT ~ IDENTIFIER ~ (LPAREN ~ ExprList? ~ RPAREN)? }
|
||||
|
||||
ArrayLiteral = {
|
||||
// 切片字面量: []T[1,2,3]
|
||||
// 数组字面量: [N]T[1,2,3] 或 [_]T[1,2,3]
|
||||
LBRACKET ~ (INTEGER_LITERAL | UNDERSCORE)? ~ RBRACKET ~ TypeExpr ~ LBRACKET ~ ExprList? ~ RBRACKET
|
||||
LBRACKET ~ RBRACKET ~ TypeExpr ~ LBRACKET ~ ExprList? ~ RBRACKET
|
||||
| LBRACKET ~ (INTEGER_LITERAL | UNDERSCORE) ~ RBRACKET ~ TypeExpr ~ LBRACKET ~ ExprList? ~ RBRACKET
|
||||
}
|
||||
|
||||
AnonymousArrayLiteral = {
|
||||
DOT ~ LBRACKET ~ ExprList? ~ RBRACKET
|
||||
}
|
||||
|
||||
// ============ 类型表达式(独立,限制语法) ============
|
||||
ExprNoStruct = { AssignExprNoStruct }
|
||||
|
||||
AssignExprNoStruct = { BoolOrExprNoStruct ~ (AssignOp ~ AssignExprNoStruct)? }
|
||||
BoolOrExprNoStruct = { BoolAndExprNoStruct ~ (BOOL_OR ~ BoolAndExprNoStruct)* }
|
||||
BoolAndExprNoStruct = { BitOrExprNoStruct ~ (BOOL_AND ~ BitOrExprNoStruct)* }
|
||||
BitOrExprNoStruct = { BitXorExprNoStruct ~ (PIPE ~ BitXorExprNoStruct)* }
|
||||
BitXorExprNoStruct = { BitAndExprNoStruct ~ (CARET ~ BitAndExprNoStruct)* }
|
||||
BitAndExprNoStruct = { CmpEqExprNoStruct ~ (AMPERSAND ~ CmpEqExprNoStruct)* }
|
||||
CmpEqExprNoStruct = { CmpExprNoStruct ~ ((EQ_EQ | BANG_EQUAL) ~ CmpExprNoStruct)* }
|
||||
CmpExprNoStruct = { ArrowExprNoStruct ~ ((L_ARROW_EQ | L_ARROW | R_ARROW_EQ | R_ARROW) ~ ArrowExprNoStruct)* }
|
||||
|
||||
ArrowExprNoStruct = { RangeExprNoStruct ~ ((THIN_R_ARROW | THIN_L_ARROW | FAT_D_ARROW | THIN_D_ARROW) ~ RangeExprNoStruct)* }
|
||||
|
||||
RangeExprNoStruct = { ShiftExprNoStruct ~ (DOT2 ~ ShiftExprNoStruct?)? }
|
||||
ShiftExprNoStruct = { AddExprNoStruct ~ ((L_ARROW2_EQ | L_ARROW2 | R_ARROW2_EQ | R_ARROW2) ~ AddExprNoStruct)* }
|
||||
AddExprNoStruct = { MulExprNoStruct ~ ((PLUS | MINUS) ~ MulExprNoStruct)* }
|
||||
MulExprNoStruct = { CastExprNoStruct ~ ((ASTERISK | SLASH | PERCENT) ~ CastExprNoStruct)* }
|
||||
CastExprNoStruct = { PrefixExprNoStruct ~ (KEYWORD_as ~ TypeExpr)* }
|
||||
PrefixExprNoStruct = { PrefixOp* ~ PostfixExprNoStruct }
|
||||
|
||||
PostfixExprNoStruct = {
|
||||
PrimaryExprNoStruct ~ (LPAREN ~ ExprList ~ RPAREN | DOT ~ IDENTIFIER | DOT ~ ASTERISK | LBRACKET ~ Expr ~ RBRACKET | LBRACKET ~ Expr ~ DOT2 ~ Expr? ~ RBRACKET)*
|
||||
}
|
||||
|
||||
PrimaryExprNoStruct = {
|
||||
FLOAT_LITERAL
|
||||
| INTEGER_LITERAL
|
||||
| CHAR_LITERAL
|
||||
| STRING_LITERAL
|
||||
| KEYWORD_true
|
||||
| KEYWORD_false
|
||||
| KEYWORD_null
|
||||
| TupleLiteral
|
||||
| AnonymousAggregateLiteral
|
||||
| ArrayLiteral
|
||||
| AnonymousArrayLiteral
|
||||
| VariantConstruct
|
||||
| ComptimeCallExpr
|
||||
| IDENTIFIER
|
||||
| Block
|
||||
| IfExpr
|
||||
| IfVarExpr
|
||||
| WhileExpr
|
||||
| LoopExpr
|
||||
| ForExpr
|
||||
| MatchExpr
|
||||
| FnTypeExpr
|
||||
}
|
||||
|
||||
VarBodyNoStruct = { Pattern ~ (COLON ~ TypeExpr)? ~ (EQUAL ~ ExprNoStruct)? }
|
||||
|
||||
TypeExpr = { HeaderList? ~ TypePrefixExpr }
|
||||
|
||||
TypePrefixExpr = { ASTERISK* ~ TypePostfixExpr }
|
||||
@@ -208,26 +253,23 @@ FnTypeExpr = { KEYWORD_comptime? ~ KEYWORD_fn ~ LPAREN ~ TypeExprList? ~ RPAREN
|
||||
|
||||
AggregateTypeExpr = { (KEYWORD_struct | KEYWORD_union | KEYWORD_enum | KEYWORD_shape) ~ LBRACE ~ ContainerMembers ~ RBRACE }
|
||||
|
||||
// ============ 控制流 ============
|
||||
IfExpr = { KEYWORD_if ~ Expr ~ Block ~ (KEYWORD_else ~ (Block | IfExpr | IfVarExpr))? }
|
||||
IfVarExpr = { KEYWORD_if ~ VarBody ~ Block ~ (KEYWORD_else ~ (Block | IfExpr | IfVarExpr))? }
|
||||
IfExpr = { KEYWORD_if ~ ExprNoStruct ~ Block ~ (KEYWORD_else ~ (Block | IfExpr | IfVarExpr))? }
|
||||
IfVarExpr = { KEYWORD_if ~ KEYWORD_var ~ VarBodyNoStruct ~ Block ~ (KEYWORD_else ~ (Block | IfExpr | IfVarExpr))? }
|
||||
|
||||
WhileExpr = { Label? ~ KEYWORD_while ~ Expr ~ Block ~ (KEYWORD_else ~ (Block | Expr))? }
|
||||
WhileExpr = { Label? ~ KEYWORD_while ~ ExprNoStruct ~ Block ~ (KEYWORD_else ~ (Block | Expr))? }
|
||||
LoopExpr = { Label? ~ KEYWORD_loop ~ Block }
|
||||
ForExpr = { Label? ~ KEYWORD_for ~ Expr ~ (COMMA ~ Expr)* ~ KEYWORD_as ~ BindingIdent ~ (COMMA ~ BindingIdent)* ~ Block ~ (KEYWORD_else ~ (Block | Expr))? }
|
||||
ForExpr = { Label? ~ KEYWORD_for ~ Pattern ~ COLON ~ ExprNoStruct ~ (COMMA ~ ExprNoStruct)* ~ Block ~ (KEYWORD_else ~ (Block | Expr))? }
|
||||
|
||||
MatchExpr = { KEYWORD_match ~ Expr ~ LBRACE ~ MatchArm* ~ RBRACE }
|
||||
MatchArm = { MatchPattern ~ (COMMA ~ MatchPattern)* ~ FAT_R_ARROW ~ Block }
|
||||
MatchExpr = { KEYWORD_match ~ ExprNoStruct ~ LBRACE ~ MatchArm* ~ RBRACE }
|
||||
MatchArm = { Pattern ~ FAT_R_ARROW ~ Block ~ COMMA? }
|
||||
|
||||
// ============ 其他表达式 ============
|
||||
ComptimeCallExpr = { AT ~ IDENTIFIER ~ LPAREN ~ ExprList? ~ RPAREN }
|
||||
ParenExpr = {
|
||||
|
||||
LPAREN ~ RPAREN
|
||||
| LPAREN ~ Expr ~ COMMA ~ (Expr ~ COMMA)* ~ Expr? ~ RPAREN
|
||||
| LPAREN ~ Expr ~ RPAREN
|
||||
}
|
||||
|
||||
// ============ Token 定义 ============
|
||||
AssignOp = { EQUAL | PLUS_EQ | MINUS_EQ | ASTERISK_EQ | SLASH_EQ | PERCENT_EQ | AMPERSAND_EQ | PIPE_EQ | CARET_EQ | L_ARROW2_EQ | R_ARROW2_EQ }
|
||||
BOOL_OR = { PIPE ~ PIPE }
|
||||
BOOL_AND = { AMPERSAND ~ AMPERSAND }
|
||||
|
||||
Reference in new issue
Block a user