Improve ternary expression parsing definition
This commit is contained in:
parent
bb9b6c8128
commit
167f448ae1
@ -1,7 +1,9 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>#(page.Title)</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
#for(i, user in users):
|
||||
<div>
|
||||
@ -13,4 +15,5 @@
|
||||
</div>
|
||||
#!for
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
888
parser/parser.go
888
parser/parser.go
File diff suppressed because it is too large
Load Diff
@ -83,8 +83,21 @@ ExprTag = '#' ignoreErr:'?'? '(' item:Expr ')' {
|
||||
}, nil
|
||||
}
|
||||
|
||||
Expr = Ternary / Assignment / LogicalExpr
|
||||
Assignable = Ternary / LogicalExpr
|
||||
Expr = Assignment / TernaryExpr
|
||||
Assignable = TernaryExpr
|
||||
|
||||
TernaryExpr = _ cond:LogicalExpr vals:(_ '?' _ Value _ ':' _ Value)? {
|
||||
if vals == nil {
|
||||
return cond, nil
|
||||
} else {
|
||||
s := toAnySlice(vals)
|
||||
return ast.Ternary{
|
||||
Condition: cond.(ast.Node),
|
||||
IfTrue: s[3].(ast.Node),
|
||||
Else: s[7].(ast.Node),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
LogicalExpr = _ first:ComparisonExpr rest:(_ LogicalOp _ ComparisonExpr)* _ {
|
||||
return toExpr(c, first, rest), nil
|
||||
@ -174,14 +187,6 @@ Assignment = name:Ident _ '=' _ value:Assignable {
|
||||
}, nil
|
||||
}
|
||||
|
||||
Ternary = cond:Assignable _ '?' _ ifTrue:Value _ ':' _ elseVal:Value {
|
||||
return ast.Ternary{
|
||||
Condition: cond.(ast.Node),
|
||||
IfTrue: ifTrue.(ast.Node),
|
||||
Else: elseVal.(ast.Node),
|
||||
}, nil
|
||||
}
|
||||
|
||||
MethodCall = value:Value '.' name:Ident params:ParamList {
|
||||
return ast.MethodCall{
|
||||
Value: value.(ast.Node),
|
||||
|
Loading…
Reference in New Issue
Block a user