Skip to content

Commit

Permalink
Minimal support for context.
Browse files Browse the repository at this point in the history
Parse `context` keyword.

New Context constructor in Expr [API change].

Evaluate this by just evaluating the expression, for now.

Note that we don't support the features (like location or
numbering) that context is used to affect anyway, so this change
probably won't be enough for meaningful support. But it might
prevent some documents from issuing errors.

See #53.
  • Loading branch information
jgm committed Oct 29, 2024
1 parent a05431f commit 77c9311
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Typst/Evaluate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,8 @@ evalExpr expr =
_ -> fail $ "For expression requires an Array or Dictionary"
updateState $ \st -> st {evalFlowDirective = FlowNormal}
go items VNone
Context e -> do
evalExpr e -- TODO for now we just ignore "context"
Return mbe -> do
-- these flow directives are examined in CodeBlock
updateState (\st -> st {evalFlowDirective = FlowReturn (isJust mbe)})
Expand Down
4 changes: 4 additions & 0 deletions src/Typst/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ pKeywordExpr =
<|> pBreakExpr
<|> pContinueExpr
<|> pReturnExpr
<|> pContextExpr

-- args ::= ('(' (arg (',' arg)* ','?)? ')' content-block*) | content-block+
pArgs :: P [Arg]
Expand Down Expand Up @@ -1235,5 +1236,8 @@ pReturnExpr = do
then pure $ Return Nothing
else Return <$> (option Nothing (Just <$> pExpr))

pContextExpr :: P Expr
pContextExpr = Context <$> (pKeyword "context" *> pExpr)

pIncludeExpr :: P Expr
pIncludeExpr = Include <$> (pKeyword "include" *> pExpr)
1 change: 1 addition & 0 deletions src/Typst/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ data Expr
| Ident Identifier
| FuncCall Expr [Arg]
| FuncExpr [Param] Expr
| Context Expr
| FieldAccess Expr Expr
| Group Expr
| Array [Spreadable Expr]
Expand Down

0 comments on commit 77c9311

Please sign in to comment.