diff --git a/eo-phi-normalizer/app/Main.hs b/eo-phi-normalizer/app/Main.hs index d450cb208..e7c931edc 100644 --- a/eo-phi-normalizer/app/Main.hs +++ b/eo-phi-normalizer/app/Main.hs @@ -569,6 +569,7 @@ wrapRawBytesIn = \case obj@MetaTailContext{} -> obj obj@MetaFunction{} -> obj obj@ConstString{} -> wrapRawBytesIn (desugar obj) + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj obj@ConstInt{} -> wrapRawBytesIn (desugar obj) obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj obj@ConstFloat{} -> wrapRawBytesIn (desugar obj) diff --git a/eo-phi-normalizer/grammar/EO/Phi/Syntax.cf b/eo-phi-normalizer/grammar/EO/Phi/Syntax.cf index 80e4db791..b30fe1c95 100644 --- a/eo-phi-normalizer/grammar/EO/Phi/Syntax.cf +++ b/eo-phi-normalizer/grammar/EO/Phi/Syntax.cf @@ -41,6 +41,7 @@ token BytesMetaId {"!y"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ; token MetaFunctionName {"@"} (char - [" \r\n\t,.|':;!-?][}{)(⟧⟦"])* ; token IntegerSigned ('-'? digit+) ; token DoubleSigned ('-'? digit+ '.' digit+ ('e' '-'? digit+)?) ; +token StringRaw '"' ((char - ["\"\\"]) | ('\\' ["\"\\tnrfu"]))* '"'; Program. Program ::= "{" "⟦" [Binding] "⟧" "}" ; @@ -57,7 +58,7 @@ GlobalObject. Object ::= "Φ"; GlobalObjectPhiOrg. Object ::= "Φ̇"; ThisObject. Object ::= "ξ"; Termination. Object ::= "⊥" ; -ConstString. Object ::= String ; +ConstStringRaw. Object ::= StringRaw ; ConstIntRaw. Object ::= IntegerSigned ; ConstFloatRaw. Object ::= DoubleSigned ; MetaSubstThis. Object ::= Object "[" "ξ" "↦" Object "]" ; @@ -66,8 +67,9 @@ MetaObject. Object ::= ObjectMetaId ; MetaTailContext. Object ::= Object "*" TailMetaId ; MetaFunction. Object ::= MetaFunctionName "(" Object ")" ; -internal ConstFloat. Object ::= Double; -internal ConstInt. Object ::= Integer; +internal ConstFloat. Object ::= Double; +internal ConstInt. Object ::= Integer; +internal ConstString. Object ::= String; AlphaBinding. Binding ::= Attribute "↦" Object ; AlphaBindingSugar. Binding ::= Object ; diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs b/eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs index 9ecba29b3..7a87863fd 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs @@ -80,6 +80,7 @@ peelObject = \case MetaSubstThis{} -> error "impossible" MetaContextualize{} -> error "impossible" obj@ConstString{} -> peelObject (desugar obj) + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj obj@ConstInt{} -> peelObject (desugar obj) obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj obj@ConstFloat{} -> peelObject (desugar obj) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Pretty.hs b/eo-phi-normalizer/src/Language/EO/Phi/Pretty.hs index a9e98dc61..82101379a 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Pretty.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Pretty.hs @@ -68,6 +68,9 @@ instance Pretty Abs.IntegerSigned where instance Pretty Abs.DoubleSigned where pretty (Abs.DoubleSigned i) = pretty i +instance Pretty Abs.StringRaw where + pretty (Abs.StringRaw i) = pretty i + instance Pretty Abs.Program where pretty = \case Abs.Program bindings -> @@ -100,7 +103,8 @@ instance Pretty Abs.Object where Abs.GlobalObjectPhiOrg -> pretty "Φ̇" Abs.ThisObject -> pretty "ξ" Abs.Termination -> pretty "⊥" - Abs.ConstString str -> pretty (show str) + Abs.ConstStringRaw str -> pretty str + Abs.ConstString str -> pretty str Abs.ConstIntRaw integersigned -> pretty integersigned Abs.ConstFloatRaw doublesigned -> pretty doublesigned Abs.MetaSubstThis object1 object2 -> pretty object1 <+> lbracket <+> pretty "ξ ↦" <+> pretty object2 <+> rbracket @@ -134,6 +138,11 @@ instance Pretty Abs.Attribute where Abs.MetaAttr labelmetaid -> pretty labelmetaid Abs.AttrSugar labelid labelids -> pretty labelid <> lparen <> pretty labelids <> rparen +-- instance {-# OVERLAPPING #-} Pretty AttributeSugar where +-- pretty = \case +-- (Abs.AttrSugar labelid labelids) -> pretty labelid <> lparen <> pretty labelids <> rparen +-- (Abs.AttrNormal labelid) -> pretty labelid + instance {-# OVERLAPPING #-} Pretty [Abs.LabelId] where pretty = hsep . punctuate comma . fmap pretty diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs index a5e621285..7c01340b0 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs @@ -127,6 +127,7 @@ withSubObject f ctx root = MetaSubstThis _ _ -> [] MetaContextualize _ _ -> [] ConstString{} -> [] + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj ConstInt{} -> [] obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj ConstFloat{} -> [] @@ -206,6 +207,7 @@ objectSize = \case obj@MetaContextualize{} -> error ("impossible: expected a desugared object, but got: " <> printTree obj) obj@MetaTailContext{} -> error ("impossible: expected a desugared object, but got: " <> printTree obj) obj@ConstString{} -> objectSize (desugar obj) + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj obj@ConstInt{} -> objectSize (desugar obj) obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj obj@ConstFloat{} -> objectSize (desugar obj) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Fast.hs b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Fast.hs index 58c0a2ff6..25eeda6ad 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Fast.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Fast.hs @@ -213,6 +213,7 @@ fastYegorInsideOut ctx = \case MetaTailContext{} -> error "impossible MetaTailContext!" MetaFunction{} -> error "impossible MetaFunction!" obj@ConstString{} -> obj -- fastYegorInsideOut ctx (desugar obj) + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj -- fastYegorInsideOut ctx (desugar obj) obj@ConstInt{} -> obj -- fastYegorInsideOut ctx (desugar obj) obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj -- fastYegorInsideOut ctx (desugar obj) obj@ConstFloat{} -> obj -- fastYegorInsideOut ctx (desugar obj) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs index a4d5aec1d..75ac44454 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Yaml.hs @@ -240,6 +240,7 @@ objectLabelIds = \case MetaSubstThis obj obj' -> objectLabelIds obj <> objectLabelIds obj' MetaContextualize obj obj' -> objectLabelIds obj <> objectLabelIds obj' obj@ConstString{} -> objectLabelIds (desugar obj) + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj obj@ConstInt{} -> objectLabelIds (desugar obj) obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj obj@ConstFloat{} -> objectLabelIds (desugar obj) @@ -291,6 +292,7 @@ objectMetaIds (MetaTailContext obj x) = objectMetaIds obj <> Set.singleton (Meta objectMetaIds (MetaSubstThis obj obj') = foldMap objectMetaIds [obj, obj'] objectMetaIds (MetaContextualize obj obj') = foldMap objectMetaIds [obj, obj'] objectMetaIds obj@ConstString{} = objectMetaIds (desugar obj) +objectMetaIds obj@ConstStringRaw{} = errorExpectedDesugaredObject obj objectMetaIds obj@ConstInt{} = objectMetaIds (desugar obj) objectMetaIds obj@ConstIntRaw{} = errorExpectedDesugaredObject obj objectMetaIds obj@ConstFloat{} = objectMetaIds (desugar obj) @@ -328,6 +330,7 @@ objectHasMetavars MetaTailContext{} = True objectHasMetavars (MetaSubstThis _ _) = True -- technically not a metavar, but a substitution objectHasMetavars (MetaContextualize _ _) = True objectHasMetavars obj@ConstString{} = objectHasMetavars (desugar obj) +objectHasMetavars obj@ConstStringRaw{} = errorExpectedDesugaredObject obj objectHasMetavars obj@ConstInt{} = objectHasMetavars (desugar obj) objectHasMetavars obj@ConstIntRaw{} = errorExpectedDesugaredObject obj objectHasMetavars obj@ConstFloat{} = objectHasMetavars (desugar obj) @@ -464,6 +467,7 @@ applySubst subst@Subst{..} = \case let holeSubst = mempty{objectMetas = [(holeMetaId, applySubst subst obj)]} in applySubst holeSubst contextObject obj@ConstString{} -> applySubst subst (desugar obj) + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj obj@ConstInt{} -> applySubst subst (desugar obj) obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj obj@ConstFloat{} -> applySubst subst (desugar obj) @@ -538,6 +542,7 @@ matchOneHoleContext ctxId pat obj = matchWhole <> matchPart ThisObject -> [] Termination -> [] ConstString{} -> [] + ConstStringRaw{} -> errorExpectedDesugaredObject obj ConstInt{} -> [] ConstIntRaw{} -> errorExpectedDesugaredObject obj ConstFloat{} -> [] @@ -658,6 +663,7 @@ substThis thisObj = go obj@MetaObject{} -> error ("impossible: trying to substitute ξ in " <> printTree obj) obj@MetaFunction{} -> error ("impossible: trying to substitute ξ in " <> printTree obj) obj@ConstString{} -> obj + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj obj@ConstInt{} -> obj obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj obj@ConstFloat{} -> obj @@ -698,6 +704,7 @@ contextualize thisObj = go obj@MetaObject{} -> error ("impossible: trying to contextualize " <> printTree obj) obj@MetaFunction{} -> error ("impossible: trying to contextualize " <> printTree obj) obj@ConstString{} -> go (desugar obj) + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj obj@ConstInt{} -> go (desugar obj) obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj obj@ConstFloat{} -> go (desugar obj) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Syntax.hs b/eo-phi-normalizer/src/Language/EO/Phi/Syntax.hs index 19bd10320..44d2a21a6 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Syntax.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Syntax.hs @@ -122,6 +122,7 @@ instance DesugarableInitially Object where desugarInitially :: Object -> Object desugarInitially = \case obj@(ConstString{}) -> obj + ConstStringRaw (StringRaw s) -> ConstString (init (tail s)) obj@(ConstInt{}) -> obj ConstIntRaw (IntegerSigned x) -> ConstInt (read x) obj@(ConstFloat{}) -> obj @@ -181,6 +182,7 @@ instance SugarableFinally Object where sugarFinally = \case "Φ.org.eolang" -> GlobalObjectPhiOrg obj@ConstString{} -> obj + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj obj@ConstInt{} -> obj obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj obj@ConstFloat{} -> obj @@ -231,6 +233,7 @@ instance SugarableFinally MetaId desugar :: Object -> Object desugar = \case ConstString string -> wrapBytesInString (stringToBytes string) + obj@ConstStringRaw{} -> errorExpectedDesugaredObject obj ConstInt n -> wrapBytesInInt (intToBytes (fromInteger n)) obj@ConstIntRaw{} -> errorExpectedDesugaredObject obj ConstFloat x -> wrapBytesInFloat (floatToBytes x) @@ -675,3 +678,20 @@ printTree :: (Pretty a, SugarableFinally a) => a -> String printTree = printTreeDontSugar . sugarFinally + +-- >>> stringToBytes "Hello world" +-- Bytes "48-65-6C-6C-6F-20-77-6F-72-6C-64" + +newtype A = A String +instance Show A where show (A s) = s + +t1 = do + t <- readFile "tmp/bar.phi" + pure $ A (printTreeDontSugar (fromString t :: Program)) + +-- >>> t1 +-- { +-- ⟦ +-- org ↦ "\u0001" +-- ⟧ +-- } diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Abs.hs b/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Abs.hs index b92fbeee6..004441766 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Abs.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Abs.hs @@ -67,7 +67,7 @@ data Object | GlobalObjectPhiOrg | ThisObject | Termination - | ConstString String + | ConstStringRaw StringRaw | ConstIntRaw IntegerSigned | ConstFloatRaw DoubleSigned | MetaSubstThis Object Object @@ -77,6 +77,7 @@ data Object | MetaFunction MetaFunctionName Object | ConstFloat Double | ConstInt Integer + | ConstString String deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic) data Binding @@ -149,3 +150,6 @@ newtype IntegerSigned = IntegerSigned String newtype DoubleSigned = DoubleSigned String deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic, Data.String.IsString) +newtype StringRaw = StringRaw String + deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic, Data.String.IsString) + diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Doc.txt b/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Doc.txt index 803b9db59..719cf9d9f 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Doc.txt +++ b/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Doc.txt @@ -38,6 +38,7 @@ except ``"`` unless preceded by ``\``. + Bytes literals are recognized by the regular expression `````{"--"} | ["0123456789ABCDEF"] ["0123456789ABCDEF"] '-' | ["0123456789ABCDEF"] ["0123456789ABCDEF"] ('-' ["0123456789ABCDEF"] ["0123456789ABCDEF"])+````` @@ -82,6 +83,9 @@ IntegerSigned literals are recognized by the regular expression DoubleSigned literals are recognized by the regular expression `````'-'? digit+ '.' digit+ ('e' '-'? digit+)?````` +StringRaw literals are recognized by the regular expression +`````'"' (char - [""\"] | '\' [""\fnrtu"])* '"'````` + ===Reserved words and symbols=== The set of reserved words is the set of terminals appearing in the grammar. Those reserved words that consist of non-letter characters are called symbols, and they are treated in a different way from those that are similar to identifiers. The lexer follows rules familiar from languages like Haskell, C, and Java, including longest match and spacing conventions. @@ -119,7 +123,7 @@ All other symbols are terminals. | | **|** | ``Φ̇`` | | **|** | ``ξ`` | | **|** | ``⊥`` - | | **|** | //String// + | | **|** | //StringRaw// | | **|** | //IntegerSigned// | | **|** | //DoubleSigned// | | **|** | //Object// ``[`` ``ξ`` ``↦`` //Object// ``]`` diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Lex.x b/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Lex.x index 90f1522e1..945d1192c 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Lex.x +++ b/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Lex.x @@ -93,6 +93,10 @@ $s [$u # [\t \n \r \ \! \' \( \) \, \. \: \; \? \[ \] \{ \| \} \⟦ \⟧]] * \- ? $d + \. $d + (e \- ? $d +)? { tok (eitherResIdent T_DoubleSigned) } +-- token StringRaw +\" ([$u # [\" \\]] | \\ [\" \\ f n r t u]) * \" + { tok (eitherResIdent T_StringRaw) } + -- Keywords and Ident $l $i* { tok (eitherResIdent TV) } @@ -134,6 +138,7 @@ data Tok | T_MetaFunctionName !String | T_IntegerSigned !String | T_DoubleSigned !String + | T_StringRaw !String deriving (Eq, Show, Ord) -- | Smart constructor for 'Tok' for the sake of backwards compatibility. @@ -208,6 +213,7 @@ tokenText t = case t of PT _ (T_MetaFunctionName s) -> s PT _ (T_IntegerSigned s) -> s PT _ (T_DoubleSigned s) -> s + PT _ (T_StringRaw s) -> s -- | Convert a token to a string. prToken :: Token -> String diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Par.y b/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Par.y index fb0f57941..1f8726e8d 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Par.y +++ b/eo-phi-normalizer/src/Language/EO/Phi/Syntax/Par.y @@ -85,6 +85,7 @@ import Language.EO.Phi.Syntax.Lex L_MetaFunctionName { PT _ (T_MetaFunctionName $$) } L_IntegerSigned { PT _ (T_IntegerSigned $$) } L_DoubleSigned { PT _ (T_DoubleSigned $$) } + L_StringRaw { PT _ (T_StringRaw $$) } %% @@ -133,6 +134,9 @@ IntegerSigned : L_IntegerSigned { Language.EO.Phi.Syntax.Abs.IntegerSigned $1 } DoubleSigned :: { Language.EO.Phi.Syntax.Abs.DoubleSigned } DoubleSigned : L_DoubleSigned { Language.EO.Phi.Syntax.Abs.DoubleSigned $1 } +StringRaw :: { Language.EO.Phi.Syntax.Abs.StringRaw } +StringRaw : L_StringRaw { Language.EO.Phi.Syntax.Abs.StringRaw $1 } + Program :: { Language.EO.Phi.Syntax.Abs.Program } Program : '{' '⟦' ListBinding '⟧' '}' { Language.EO.Phi.Syntax.Abs.Program $3 } @@ -154,7 +158,7 @@ Object | 'Φ̇' { Language.EO.Phi.Syntax.Abs.GlobalObjectPhiOrg } | 'ξ' { Language.EO.Phi.Syntax.Abs.ThisObject } | '⊥' { Language.EO.Phi.Syntax.Abs.Termination } - | String { Language.EO.Phi.Syntax.Abs.ConstString $1 } + | StringRaw { Language.EO.Phi.Syntax.Abs.ConstStringRaw $1 } | IntegerSigned { Language.EO.Phi.Syntax.Abs.ConstIntRaw $1 } | DoubleSigned { Language.EO.Phi.Syntax.Abs.ConstFloatRaw $1 } | Object '[' 'ξ' '↦' Object ']' { Language.EO.Phi.Syntax.Abs.MetaSubstThis $1 $5 } diff --git a/eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs b/eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs index 3b084236d..a56655863 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/ToLaTeX.hs @@ -91,6 +91,7 @@ instance ToLatex Object where toLatex (MetaSubstThis obj1 obj2) = LaTeX "\\mathbb{S}(" <> toLatex obj1 <> ", " <> toLatex obj2 <> ")" toLatex (MetaContextualize obj1 obj2) = LaTeX "\\lceil" <> toLatex obj1 <> ", " <> toLatex obj2 <> "\\rceil" toLatex (ConstString string) = "|" <> LaTeX (show string) <> "|" + toLatex obj@(ConstStringRaw{}) = errorExpectedDesugaredObject obj toLatex (ConstInt n) = LaTeX (show n) toLatex obj@(ConstIntRaw{}) = errorExpectedDesugaredObject obj toLatex (ConstFloat x) = LaTeX (show x) diff --git a/eo-phi-normalizer/test/Language/EO/Rules/PhiPaperSpec.hs b/eo-phi-normalizer/test/Language/EO/Rules/PhiPaperSpec.hs index de0ae524a..2dc84c754 100644 --- a/eo-phi-normalizer/test/Language/EO/Rules/PhiPaperSpec.hs +++ b/eo-phi-normalizer/test/Language/EO/Rules/PhiPaperSpec.hs @@ -112,6 +112,9 @@ instance Arbitrary Binding where shrink (AlphaBinding attr obj) = AlphaBinding attr <$> shrink obj shrink _ = [] -- do not shrink deltas and lambdas +instance Arbitrary Phi.StringRaw where + arbitrary = Phi.StringRaw <$> arbitraryNonEmptyString + -- | Split an integer into a list of positive integers, -- whose sum is less than or equal the initial one. --