Skip to content

Commit

Permalink
Further improvements in math precedence.
Browse files Browse the repository at this point in the history
Update tests and add a test case for #55.

Closes #55.
  • Loading branch information
jgm committed Dec 3, 2024
1 parent f7c729d commit 5a50154
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 64 deletions.
8 changes: 5 additions & 3 deletions src/Typst/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,13 @@ mathOperatorTable =
-- but we still don't match typst for "!"(a), which typst DOES consider
-- a function
guard $ case lastMathTok of
Just (pos', MGroup _ (Just t) _)
| pos == pos' -> T.all isLetter t
Just (pos', Text t)
| pos == pos'
-> not (T.all (\c -> isDigit c ||
isSymbol c ||
isPunctuation c) t)
-> case T.unsnoc t of
Nothing -> True
Just (_,c) -> isLetter c
_ -> True
args <- mGrouped '(' ')' True
pure $ \expr -> MGroup Nothing Nothing [expr, args]
Expand Down
24 changes: 8 additions & 16 deletions test/out/math/attach-03.out
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@
, Comment
, Equation
False
[ MGroup
[ MAttach
(Just (Text "1"))
Nothing
Nothing
[ MAttach
(Just (Text "1"))
Nothing
(Code
"test/typ/math/attach-03.typ"
( line 3 , column 2 )
(Ident (Identifier "pi")))
, MGroup (Just "(") (Just ")") [ Text "Y" ]
]
(Code
"test/typ/math/attach-03.typ"
( line 3 , column 2 )
(Ident (Identifier "pi")))
, MGroup (Just "(") (Just ")") [ Text "Y" ]
, Text ","
, MAttach
(Just
Expand Down Expand Up @@ -103,11 +99,7 @@
(MGroup
Nothing
Nothing
[ MGroup
Nothing
Nothing
[ Text "1" , MGroup (Just "(") (Just ")") [ Text "Y" ] ]
]))
[ Text "1" , MGroup (Just "(") (Just ")") [ Text "Y" ] ]))
Nothing
(Code
"test/typ/math/attach-03.typ"
Expand Down
14 changes: 4 additions & 10 deletions test/out/math/frac-06.out
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,8 @@
, Text "+"
, MFrac (MGroup (Just "[") (Just "]") [ Text "x" ]) (Text "2")
, Text ","
, MFrac
(MGroup
Nothing
Nothing
[ Text "1" , MGroup (Just "(") (Just ")") [ Text "x" ] ])
(Text "2")
, Text "1"
, MFrac (MGroup (Just "(") (Just ")") [ Text "x" ]) (Text "2")
, Text ","
, Text "2"
, MFrac (MGroup (Just "[") (Just "]") [ Text "x" ]) (Text "2")
Expand Down Expand Up @@ -202,11 +198,9 @@ document(body: { text(body: [
text(body: [x]),
[]] }))),
text(body: [,]),
text(body: [1]),
math.frac(denom: text(body: [2]),
num: { text(body: [1]),
math.lr(body: ({ [(],
text(body: [x]),
[)] })) }),
num: text(body: [x])),
text(body: [,]),
text(body: [2]),
math.frac(denom: text(body: [2]),
Expand Down
8 changes: 2 additions & 6 deletions test/out/math/multiline-05.out
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,8 @@
(MGroup
(Just "(")
(Just ")")
[ MGroup
Nothing
Nothing
[ Text "5"
, MGroup (Just "(") (Just ")") [ Text "5" , Text "+" , Text "1" ]
]
[ Text "5"
, MGroup (Just "(") (Just ")") [ Text "5" , Text "+" , Text "1" ]
])
(Text "2")
, Text "="
Expand Down
18 changes: 7 additions & 11 deletions test/out/math/op-01.out
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,14 @@
( line 4 , column 5 )
(FuncCall (Ident (Identifier "sin")) [ BlockArg [ Text "x" ] ])
, Text "+"
, MGroup
Nothing
, MAttach
(Just (Text "2"))
Nothing
[ MAttach
(Just (Text "2"))
Nothing
(Code
"test/typ/math/op-01.typ"
( line 4 , column 14 )
(Ident (Identifier "log")))
, MGroup (Just "(") (Just ")") [ Text "x" ]
]
(Code
"test/typ/math/op-01.typ"
( line 4 , column 14 )
(Ident (Identifier "log")))
, MGroup (Just "(") (Just ")") [ Text "x" ]
]
, ParBreak
]
Expand Down
6 changes: 1 addition & 5 deletions test/out/math/unbalanced-00.out
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@
, MGroup
(Just "(")
(Just ")")
[ MGroup
Nothing
Nothing
[ Text "2" , MGroup (Just "(") (Just ")") [ Text "3" ] ]
]
[ Text "2" , MGroup (Just "(") (Just ")") [ Text "3" ] ]
])
]
, ParBreak
Expand Down
8 changes: 2 additions & 6 deletions test/out/regression/issue17.out
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@
, ParBreak
, Equation
False
[ MGroup
Nothing
Nothing
[ MAttach (Just (Text "1")) Nothing (Text "a")
, MGroup (Just "(") (Just ")") [ Text "x" ]
]
[ MAttach (Just (Text "1")) Nothing (Text "a")
, MGroup (Just "(") (Just ")") [ Text "x" ]
]
, ParBreak
, Equation
Expand Down
10 changes: 3 additions & 7 deletions test/out/regression/issue54.out
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@
Nothing
(Text "c")
, HardBreak
, MGroup
Nothing
Nothing
[ MAttach
(Just (MGroup Nothing Nothing [ Text "a" ])) Nothing (Text "c")
, MGroup (Just "(") (Just ")") [ Text "b" ]
]
, MAttach
(Just (MGroup Nothing Nothing [ Text "a" ])) Nothing (Text "c")
, MGroup (Just "(") (Just ")") [ Text "b" ]
, HardBreak
, MAttach
(Just (MGroup Nothing Nothing [ Text "a" , Text "!" ]))
Expand Down
76 changes: 76 additions & 0 deletions test/out/regression/issue55.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--- parse tree ---
[ Code
"test/typ/regression/issue55.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/issue55.typ"
( line 1 , column 47 )
(FuncCall
(Ident (Identifier "repr"))
[ NormalArg (Ident (Identifier "x")) ])
, Space
, Text "/"
, Text "="
, Space
, Code
"test/typ/regression/issue55.typ"
( line 1 , column 59 )
(FuncCall
(Ident (Identifier "repr"))
[ NormalArg (Ident (Identifier "y")) ])
, Text ")"
])
)
]
]))))
, SoftBreak
, Equation
True
[ MGroup (Just "(") (Just ")") [ Text "b" ]
, MFrac
(MGroup (Just "(") (Just ")") [ Text "c" ])
(MGroup Nothing Nothing [ Text "d" ])
, Text "!"
, MFrac
(MGroup (Just "(") (Just ")") [ Text "a" ])
(MGroup Nothing Nothing [ Text "b" ])
, MGroup Nothing Nothing [ HardBreak , Text "!" ]
, MFrac
(MGroup (Just "(") (Just ")") [ Text "a" ])
(MGroup Nothing Nothing [ Text "b" ])
]
, ParBreak
]
--- evaluated ---
document(body: { text(body: [
]),
math.equation(block: true,
body: { math.lr(body: ({ [(],
text(body: [b]),
[)] })),
math.frac(denom: text(body: [d]),
num: text(body: [c])),
text(body: [!]),
math.frac(denom: text(body: [b]),
num: text(body: [a])),
linebreak(),
text(body: [!]),
math.frac(denom: text(body: [b]),
num: text(body: [a])) },
numbering: none),
parbreak() })
5 changes: 5 additions & 0 deletions test/typ/regression/issue55.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$
(b)(c)/(d)
!(a)/(b)
\ !(a)/(b)
$

0 comments on commit 5a50154

Please sign in to comment.