Skip to content

Commit 914eba8

Browse files
committed
calc() support
1 parent e016337 commit 914eba8

6 files changed

Lines changed: 348 additions & 255 deletions

File tree

src/CssParser/FixRule.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import CssParser.Ident
77
PropertyName,
88
TagName(TagName, NoTag) )
99
import CssParser.Prelude
10+
import CssParser.Parser.Monad
1011
import CssParser.Rule
1112
( Attr,
1213
Class(AtomicClass),
@@ -17,8 +18,9 @@ import CssParser.Rule
1718
Selector(..),
1819
TagRelation(Descendant),
1920
TagSelector(..) )
21+
import CssParser.Show ( CssShow(toCssText) )
2022
import CssParser.Rule.Value
21-
23+
import Data.Text qualified as T
2224

2325
tagSelectorOnly :: Ident -> TagSelector
2426
tagSelectorOnly tn = TagSelector NoBar (TagName tn) [] Nothing []
@@ -173,3 +175,27 @@ mkLeaf :: PropertyName -> NonEmpty PropVals -> CssRuleBodyItem
173175
mkLeaf pn = \case
174176
(x :| []) -> CssLeafRule pn x
175177
o -> CssEnumLeaf pn (PropValsList o)
178+
179+
fmap2 :: (Functor f1, Functor f2) => (a -> b) -> f1 (f2 a) -> f1 (f2 b)
180+
fmap2 f = fmap (fmap f)
181+
182+
chopOffLeftmostSign :: CalcExpr -> Maybe (CalcOp, CalcExpr)
183+
chopOffLeftmostSign = \case
184+
BinOpCe a op b -> do
185+
case chopOffLeftmostSign a of
186+
Nothing -> Nothing
187+
Just (lop, a') -> Just (lop, BinOpCe a' op b)
188+
ValCe (RawNum rn) pt ->
189+
case T.uncons rn of
190+
Just ('-', absRn) -> pure (MinusCe, ValCe (RawNum absRn) pt)
191+
Just ('+', absRn) -> pure (PlusCe, ValCe (RawNum absRn) pt)
192+
_ -> Nothing
193+
_ -> Nothing
194+
195+
recoverCalcBinOp :: CalcExpr -> CalcExpr -> P CalcExpr
196+
recoverCalcBinOp fo so =
197+
case chopOffLeftmostSign so of
198+
Nothing ->
199+
fail $ "Expected operator between " <> unpack (toCssText fo) <> " and " <> unpack (toCssText so)
200+
Just (lop, so') ->
201+
pure $ BinOpCe fo lop so'

0 commit comments

Comments
 (0)