Skip to content

Commit

Permalink
imp: 现在允许fstring的{}中写复杂语句,即{}现在是{% %}的别名
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Aug 4, 2024
1 parent 9d46166 commit fe03d28
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 371 deletions.
9 changes: 5 additions & 4 deletions roll.peg
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,10 @@ strEscape <- "\\n" { return []byte("\n") }
/ "\\}" { return []byte("}") }
/ '\\' { return []byte("\\") }

fstringExpr1 <- exprRoot {c.data.CounterAdd(1)} / &{ p.addErr(errors.New("{} 内必须是一个表达式")); return false; }
fstringExpr <- '{' sp fstringExpr1 sp ('}' / &{ p.addErr(errors.New("无法处理字符 " + string(p.pt.rn))); return false })
// fstringExpr1 <- exprRoot {c.data.CounterAdd(1)} / &{ p.addErr(errors.New("{} 内必须是一个表达式")); return false; }
// fstringExpr <- '{' sp fstringExpr1 sp ('}' / &{ p.addErr(errors.New("无法处理字符 " + string(p.pt.rn))); return false })
fstringStmt <- "{%" sp ({ c.data.AddOp(typeFStringBlockPush) } stmtRoot { c.data.AddOp(typeFStringBlockPop); c.data.CounterAdd(1) } / &{ p.addErr(errors.New("{% %} 内必须是语句块或表达式")); return false; }) sp "%}"
fstringStmt2 <- "{" sp ({ c.data.AddOp(typeFStringBlockPush) } stmtRoot { c.data.AddOp(typeFStringBlockPop); c.data.CounterAdd(1) } / &{ p.addErr(errors.New("{} 内必须是语句块或表达式")); return false; }) sp "}"

fstring <- (
('\'' '\'' { c.data.PushStr("") })
Expand All @@ -374,8 +375,8 @@ fstring <- (
/ ('`' '`' { c.data.PushStr("") })
/ ('\'' { c.data.CounterPush() } strPart1* '\'' { c.data.CounterPop() } )
/ ('"' { c.data.CounterPush() } strPart2* '"' { c.data.CounterPop() })
/ ('`' { c.data.CounterPush() } ( strPart3 / fstringStmt / fstringExpr )* '`' { c.data.AddFormatString(c.data.CounterPop()) })
/ ('\x1e' { c.data.CounterPush() } ( strPart4 / fstringStmt / fstringExpr )* '\x1e' { c.data.AddFormatString(c.data.CounterPop()) }) // 特殊标记 0x1E
/ ('`' { c.data.CounterPush() } ( strPart3 / fstringStmt / fstringStmt2 )* '`' { c.data.AddFormatString(c.data.CounterPop()) })
/ ('\x1e' { c.data.CounterPush() } ( strPart4 / fstringStmt / fstringStmt2 )* '\x1e' { c.data.AddFormatString(c.data.CounterPop()) }) // 特殊标记 0x1E
) sp

keywords <- "while" / "if" / "else" / "continue" / "break" / "return" / "func"
Expand Down
Loading

0 comments on commit fe03d28

Please sign in to comment.