Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

423 custom rules #432

Merged
merged 13 commits into from
Jul 19, 2024
1 change: 1 addition & 0 deletions eo-phi-normalizer/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ wrapRawBytesIn = \case
Termination -> wrapTermination
obj@MetaSubstThis{} -> obj
obj@MetaObject{} -> obj
obj@MetaTailContext{} -> obj
obj@MetaFunction{} -> obj

-- * Main
Expand Down
11 changes: 4 additions & 7 deletions eo-phi-normalizer/eo-phi-normalizer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ extra-source-files:
grammar/EO/Phi/Syntax.cf
report/main.js
report/styles.css
data/0.36.0/0.36.0.md
data/0.36.0/all-atoms.md
data/0.36.0/org/eolang/as-phi.phi
data/0.36.0/org/eolang/bool.phi
data/0.36.0/org/eolang/bytes.phi
Expand Down Expand Up @@ -53,8 +51,6 @@ extra-source-files:
data/0.36.0/org/eolang/try.phi
data/0.36.0/org/eolang/tuple.phi
data/0.36.0/org/eolang/while.phi
data/0.37.0/0.37.0.md
data/0.37.0/all-atoms.md
data/0.37.0/org/eolang/as-phi.phi
data/0.37.0/org/eolang/bytes.phi
data/0.37.0/org/eolang/cage.phi
Expand All @@ -79,8 +75,6 @@ extra-source-files:
data/0.37.0/org/eolang/try.phi
data/0.37.0/org/eolang/tuple.phi
data/0.37.0/org/eolang/while.phi
data/0.38.0/0.38.0.md
data/0.38.0/all-atoms.md
data/0.38.0/org/eolang/as-phi.phi
data/0.38.0/org/eolang/bytes.phi
data/0.38.0/org/eolang/cage.phi
Expand All @@ -105,7 +99,6 @@ extra-source-files:
data/0.38.0/org/eolang/try.phi
data/0.38.0/org/eolang/tuple.phi
data/0.38.0/org/eolang/while.phi
data/0.38.4/all-atoms.md
data/0.38.4/org/eolang/as-phi.phi
data/0.38.4/org/eolang/bytes.phi
data/0.38.4/org/eolang/cage.phi
Expand Down Expand Up @@ -189,6 +182,7 @@ library
, blaze-markup
, bytestring
, cereal
, containers
, directory
, file-embed >=0.0.16.0
, filepath
Expand Down Expand Up @@ -228,6 +222,7 @@ executable normalizer
, blaze-markup
, bytestring
, cereal
, containers
, directory
, eo-phi-normalizer
, file-embed >=0.0.16.0
Expand Down Expand Up @@ -296,6 +291,7 @@ test-suite doctests
, blaze-markup
, bytestring
, cereal
, containers
, directory
, doctest-parallel
, eo-phi-normalizer
Expand Down Expand Up @@ -346,6 +342,7 @@ test-suite spec
, blaze-markup
, bytestring
, cereal
, containers
, directory
, eo-phi-normalizer
, file-embed >=0.0.16.0
Expand Down
21 changes: 16 additions & 5 deletions eo-phi-normalizer/grammar/EO/Phi/Syntax.cf
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,46 @@ token Bytes ({"--"} | ["0123456789ABCDEF"] ["0123456789ABCDEF"] {"-"} | ["012345
token Function upper (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ;
token LabelId lower (char - [" \r\n\t,.|':;!?][}{)(⟧⟦"])* ;
token AlphaIndex ({"α0"} | {"α"} (digit - ["0"]) (digit)* ) ;
token MetaId {"!"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ;
token LabelMetaId {"!τ"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ;
token TailMetaId {"!t"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ;
token BindingsMetaId {"!B"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ;
token ObjectMetaId {"!b"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ;
token BytesMetaId {"!y"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ;
token MetaFunctionName {"@"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ;

Program. Program ::= "{" "⟦" [Binding] "⟧" "}" ;

MetaIdLabel. MetaId ::= LabelMetaId ;
MetaIdTail. MetaId ::= TailMetaId ;
MetaIdBindings. MetaId ::= BindingsMetaId ;
MetaIdObject. MetaId ::= ObjectMetaId ;
MetaIdBytes. MetaId ::= BytesMetaId ;

Formation. Object ::= "⟦" [Binding] "⟧" ;
Application. Object ::= Object "(" [Binding] ")" ;
ObjectDispatch. Object ::= Object "." Attribute ;
GlobalObject. Object ::= "Φ";
ThisObject. Object ::= "ξ";
Termination. Object ::= "⊥" ;
MetaSubstThis. Object ::= Object "[" "ξ" "↦" Object "]" ;
MetaObject. Object ::= MetaId ;
MetaObject. Object ::= ObjectMetaId ;
MetaTailContext. Object ::= Object "*" TailMetaId ;
MetaFunction. Object ::= MetaFunctionName "(" Object ")" ;

AlphaBinding. Binding ::= Attribute "↦" Object ;
EmptyBinding. Binding ::= Attribute "↦" "∅" ;
DeltaBinding. Binding ::= "Δ" "⤍" Bytes ;
DeltaEmptyBinding. Binding ::= "Δ" "⤍" "∅" ;
LambdaBinding. Binding ::= "λ" "⤍" Function ;
MetaBindings. Binding ::= MetaId ;
MetaDeltaBinding. Binding ::= "Δ" "⤍" MetaId ;
MetaBindings. Binding ::= BindingsMetaId ;
MetaDeltaBinding. Binding ::= "Δ" "⤍" BytesMetaId ;
separator Binding "," ;

Phi. Attribute ::= "φ" ; -- decoratee object
Rho. Attribute ::= "ρ" ; -- parent object
Label. Attribute ::= LabelId ;
Alpha. Attribute ::= AlphaIndex ;
MetaAttr. Attribute ::= MetaId ;
MetaAttr. Attribute ::= LabelMetaId ;

-- Additional symbols used as attributes in the rules
ObjectAttr. RuleAttribute ::= Attribute ;
Expand Down
1 change: 1 addition & 0 deletions eo-phi-normalizer/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies:
- bytestring
- hashable
- unordered-containers
- containers

default-extensions:
- ImportQualifiedPost
Expand Down
1 change: 1 addition & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Dataize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ defaultContext rules obj =
, outerFormations = NonEmpty.singleton obj
, currentAttr = Phi
, insideFormation = False
, insideAbstractFormation = False
, dataizePackage = True
, minimizeTerms = False
, insideSubObject = False
Expand Down
1 change: 1 addition & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ peelObject = \case
ThisObject -> PeeledObject HeadThis []
Termination -> PeeledObject HeadTermination []
MetaObject _ -> PeeledObject HeadTermination []
MetaTailContext{} -> error "impossible"
MetaFunction _ _ -> error "To be honest, I'm not sure what should be here"
MetaSubstThis{} -> error "impossible"
where
Expand Down
15 changes: 11 additions & 4 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ instance IsString RuleAttribute where fromString = unsafeParseWith pRuleAttribut
instance IsString PeeledObject where fromString = unsafeParseWith pPeeledObject
instance IsString ObjectHead where fromString = unsafeParseWith pObjectHead

instance IsString MetaId where fromString = unsafeParseWith pMetaId

parseWith :: ([Token] -> Either String a) -> String -> Either String a
parseWith parser input = parser tokens
where
Expand All @@ -66,6 +68,7 @@ data Context = Context
, currentAttr :: Attribute
, insideFormation :: Bool
-- ^ Temporary hack for applying Ksi and Phi rules when dataizing
, insideAbstractFormation :: Bool
, dataizePackage :: Bool
-- ^ Temporary flag to only dataize Package attributes for the top-level formation.
, minimizeTerms :: Bool
Expand Down Expand Up @@ -106,9 +109,11 @@ withSubObject :: (Context -> Object -> [(String, Object)]) -> Context -> Object
withSubObject f ctx root =
f ctx root
<|> case root of
Formation bindings
| not (any isEmptyBinding bindings) -> propagateName1 Formation <$> withSubObjectBindings f ((extendContextWith root subctx){insideFormation = True}) bindings
| otherwise -> []
Formation bindings ->
propagateName1 Formation
<$> withSubObjectBindings f ((extendContextWith root subctx){insideFormation = True, insideAbstractFormation = isAbstract}) bindings
where
isAbstract = any isEmptyBinding bindings
Application obj bindings ->
asum
[ propagateName2 Application <$> withSubObject f subctx obj <*> pure bindings
Expand All @@ -120,6 +125,7 @@ withSubObject f ctx root =
Termination -> []
MetaObject _ -> []
MetaFunction _ _ -> []
MetaTailContext{} -> []
MetaSubstThis _ _ -> []
where
subctx = ctx{insideSubObject = True}
Expand Down Expand Up @@ -188,6 +194,7 @@ objectSize = \case
MetaObject{} -> 1 -- should be impossible
MetaFunction{} -> 1 -- should be impossible
MetaSubstThis{} -> 1 -- should be impossible
MetaTailContext{} -> 1 -- should be impossible

bindingSize :: Binding -> Int
bindingSize = \case
Expand Down Expand Up @@ -239,7 +246,7 @@ equalBindings bindings1 bindings2 = and (zipWith equalBinding (sortOn attr bindi
attr DeltaEmptyBinding = Label (LabelId "Δ")
attr (MetaDeltaBinding _) = Label (LabelId "Δ")
attr (LambdaBinding _) = Label (LabelId "λ")
attr (MetaBindings metaId) = MetaAttr metaId
attr (MetaBindings (BindingsMetaId metaId)) = MetaAttr (LabelMetaId metaId)

equalBinding :: Binding -> Binding -> Bool
equalBinding (AlphaBinding attr1 obj1) (AlphaBinding attr2 obj2) = attr1 == attr2 && equalObject obj1 obj2
Expand Down
1 change: 1 addition & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Fast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,5 @@ fastYegorInsideOut ctx = \case
Termination -> Termination
MetaSubstThis{} -> error "impossible MetaSubstThis!"
MetaObject{} -> error "impossible MetaObject!"
MetaTailContext{} -> error "impossible MetaTailContext!"
MetaFunction{} -> error "impossible MetaFunction!"
Loading