Skip to content

Commit

Permalink
Ensure that set rule doesn't pick up if on next line.
Browse files Browse the repository at this point in the history
Closes #23.
  • Loading branch information
jgm committed Dec 15, 2023
1 parent 9f35c44 commit f675fd9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Typst/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,11 @@ pLetExpr = do
-- set-expr ::= 'set' expr args
pSetExpr :: P Expr
pSetExpr = do
oldAllowNewlines <- stAllowNewlines <$> getState
-- see #23 -- 'set' doesn't go with 'if' unless it's on the same line
updateState $ \st -> st {stAllowNewlines = 0}
set <- pKeyword "set" *> (Set <$> pQualifiedIdentifier <*> pArgs)
updateState $ \st -> st {stAllowNewlines = oldAllowNewlines}
addCondition <- option id $ pKeyword "if" *> ((\c x -> If [(c, x)]) <$> pExpr)
pure $ addCondition set

Expand Down
77 changes: 77 additions & 0 deletions test/out/regression/issue23.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
--- parse tree ---
[ Code
"test/typ/regression/issue23.typ"
( line 1 , column 2 )
(Let
(BasicBind (Just (Identifier "test")))
(FuncExpr
[ NormalParam (Identifier "x") , NormalParam (Identifier "y") ]
(Block
(CodeBlock
[ If
[ ( Equals (Ident (Identifier "x")) (Ident (Identifier "y"))
, Block (Content [ Text "\9989" ])
)
, ( Literal (Boolean True)
, Block
(Content
[ Text "\10060"
, Text "("
, Code
"test/typ/regression/issue23.typ"
( line 1 , column 47 )
(FuncCall
(Ident (Identifier "repr"))
[ NormalArg (Ident (Identifier "x")) ])
, Space
, Text "/"
, Text "="
, Space
, Code
"test/typ/regression/issue23.typ"
( line 1 , column 59 )
(FuncCall
(Ident (Identifier "repr"))
[ NormalArg (Ident (Identifier "y")) ])
, Text ")"
])
)
]
]))))
, SoftBreak
, Code
"test/typ/regression/issue23.typ"
( line 2 , column 2 )
(LetFunc
(Identifier "fn")
[]
(Block
(CodeBlock
[ Set
(Ident (Identifier "text"))
[ KeyValArg (Identifier "fill") (Ident (Identifier "red")) ]
, If
[ ( Literal (Boolean True)
, Block (Content [ SoftBreak , Text "test" , ParBreak ])
)
, ( Literal (Boolean True)
, Block (Content [ SoftBreak , Text "test2" , ParBreak ])
)
]
])))
, ParBreak
, Code
"test/typ/regression/issue23.typ"
( line 11 , column 2 )
(FuncCall (Ident (Identifier "fn")) [])
, ParBreak
]
--- evaluated ---
document(body: { text(body: [
]),
parbreak(),
text(body: [
test],
fill: rgb(100%,25%,21%,100%)),
parbreak(),
parbreak() })
10 changes: 10 additions & 0 deletions test/typ/regression/issue23.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#let fn() = {
set text(fill: red)
if true [
test
] else [
test2
]
}

#fn()

0 comments on commit f675fd9

Please sign in to comment.