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

Add flake #7

Merged
merged 9 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"haskell.formattingProvider": "fourmolu"
}
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: eo-phi-normalizer
10 changes: 2 additions & 8 deletions eo-phi-normalizer/eo-phi-normalizer.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cabal-version: 2.2
cabal-version: 1.24

-- This file has been generated from package.yaml by hpack version 0.36.0.
--
Expand All @@ -12,7 +12,7 @@ bug-reports: https://github.com/objectionary/eo-phi-normalizer/issues
author: EO/Polystat Development Team
maintainer: [email protected]
copyright: 2023 EO/Polystat Development Team
license: BSD-3-Clause
license: BSD3
license-file: LICENSE
build-type: Custom
extra-source-files:
Expand Down Expand Up @@ -40,8 +40,6 @@ library
Language.EO.Phi.Syntax.Print
other-modules:
Paths_eo_phi_normalizer
autogen-modules:
Paths_eo_phi_normalizer
hs-source-dirs:
src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -Wno-missing-export-lists
Expand All @@ -63,8 +61,6 @@ executable normalize-phi
main-is: Main.hs
other-modules:
Paths_eo_phi_normalizer
autogen-modules:
Paths_eo_phi_normalizer
hs-source-dirs:
app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -Wno-missing-export-lists -threaded -rtsopts -with-rtsopts=-N
Expand All @@ -90,8 +86,6 @@ test-suite eo-phi-normalizer-test
Language.EO.PhiSpec
Test.EO.Phi
Paths_eo_phi_normalizer
autogen-modules:
Paths_eo_phi_normalizer
hs-source-dirs:
test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -Wno-missing-export-lists -threaded -rtsopts -with-rtsopts=-N
Expand Down
5 changes: 4 additions & 1 deletion eo-phi-normalizer/package.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: eo-phi-normalizer
version: 0.1.0
github: "objectionary/eo-phi-normalizer"
license: BSD-3-Clause
license: BSD3
author: "EO/Polystat Development Team"
maintainer: "[email protected]"
copyright: "2023 EO/Polystat Development Team"
Expand All @@ -11,6 +11,9 @@ extra-source-files:
- CHANGELOG.md
- grammar/EO/Phi/Syntax.cf

verbatim:
cabal-version: 1.24

# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
Expand Down
73 changes: 38 additions & 35 deletions eo-phi-normalizer/src/Language/EO/Phi.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE LambdaCase #-}

module Language.EO.Phi (
defaultMain,
normalize,
Expand All @@ -8,22 +9,21 @@ module Language.EO.Phi (
module Language.EO.Phi.Syntax.Abs,
) where


import Data.Char (isSpace)
import System.Exit (exitFailure)

import qualified Language.EO.Phi.Syntax.Par as Phi
import qualified Language.EO.Phi.Syntax.Print as Phi
import Language.EO.Phi.Syntax.Abs
import qualified Language.EO.Phi.Syntax.Abs as Phi
import qualified Language.EO.Phi.Syntax.Par as Phi
import qualified Language.EO.Phi.Syntax.Print as Phi

import Language.EO.Phi.Normalize

-- | Parse a 'Program' or return a parsing error.
parseProgram :: String -> Either String Phi.Program
parseProgram input = Phi.pProgram tokens
where
tokens = Phi.myLexer input
where
tokens = Phi.myLexer input

-- | Parse a 'Program' from a 'String'.
-- May throw an 'error` if input has a syntactical or lexical errors.
Expand All @@ -38,7 +38,7 @@ unsafeParseProgram input =
-- then pretty-prints the result to standard output.
defaultMain :: IO ()
defaultMain = do
input <- getContents -- read entire standard input
input <- getContents -- read entire standard input
let tokens = Phi.myLexer input
case Phi.pProgram tokens of
Left parseError -> do
Expand All @@ -50,7 +50,7 @@ defaultMain = do
-- * Overriding generated pretty-printer

-- | Like 'Phi.printTree', but without spaces around dots and no indentation for curly braces.
printTree :: Phi.Print a => a -> String
printTree :: (Phi.Print a) => a -> String
printTree = shrinkDots . render . Phi.prt 0

-- | Remove spaces around dots.
Expand All @@ -59,31 +59,34 @@ printTree = shrinkDots . render . Phi.prt 0
-- a ↦ ξ.a
shrinkDots :: String -> String
shrinkDots [] = []
shrinkDots (' ':'.':' ':cs) = '.':shrinkDots cs
shrinkDots (c:cs) = c : shrinkDots cs
shrinkDots (' ' : '.' : ' ' : cs) = '.' : shrinkDots cs
shrinkDots (c : cs) = c : shrinkDots cs

-- | Copy of 'Phi.render', except no indentation is made for curly braces.
render :: Phi.Doc -> String
render d = rend 0 False (map ($ "") $ d []) ""
where
rend
:: Int -- ^ Indentation level.
-> Bool -- ^ Pending indentation to be output before next character?
-> [String]
-> ShowS
where
rend ::
Int ->
-- \^ Indentation level.
Bool ->
-- \^ Pending indentation to be output before next character?
[String] ->
ShowS
rend i p = \case
"[" :ts -> char '[' . rend i False ts
"(" :ts -> char '(' . rend i False ts
-- "{" :ts -> onNewLine i p . showChar '{' . new (i+1) ts
-- "}" : ";":ts -> onNewLine (i-1) p . showString "};" . new (i-1) ts
-- "}" :ts -> onNewLine (i-1) p . showChar '}' . new (i-1) ts
[";"] -> char ';'
";" :ts -> char ';' . new i ts
t : ts@(s:_) | closingOrPunctuation s
-> pending . showString t . rend i False ts
t :ts -> pending . space t . rend i False ts
[] -> id
where
"[" : ts -> char '[' . rend i False ts
"(" : ts -> char '(' . rend i False ts
-- "{" :ts -> onNewLine i p . showChar '{' . new (i+1) ts
-- "}" : ";":ts -> onNewLine (i-1) p . showString "};" . new (i-1) ts
-- "}" :ts -> onNewLine (i-1) p . showChar '}' . new (i-1) ts
[";"] -> char ';'
";" : ts -> char ';' . new i ts
t : ts@(s : _)
| closingOrPunctuation s ->
pending . showString t . rend i False ts
t : ts -> pending . space t . rend i False ts
[] -> id
where
-- Output character after pending indentation.
char :: Char -> ShowS
char c = pending . showChar c
Expand All @@ -94,7 +97,7 @@ render d = rend 0 False (map ($ "") $ d []) ""

-- Indentation (spaces) for given indentation level.
indent :: Int -> ShowS
indent i = Phi.replicateS (2*i) (showChar ' ')
indent i = Phi.replicateS (2 * i) (showChar ' ')

-- Continue rendering in new line with new indentation.
new :: Int -> [String] -> ShowS
Expand All @@ -104,16 +107,16 @@ render d = rend 0 False (map ($ "") $ d []) ""
space :: String -> ShowS
space t s =
case (all isSpace t, null spc, null rest) of
(True , _ , True ) -> [] -- remove trailing space
(False, _ , True ) -> t -- remove trailing space
(False, True, False) -> t ++ ' ' : s -- add space if none
_ -> t ++ s
where
(spc, rest) = span isSpace s
(True, _, True) -> [] -- remove trailing space
(False, _, True) -> t -- remove trailing space
(False, True, False) -> t ++ ' ' : s -- add space if none
_ -> t ++ s
where
(spc, rest) = span isSpace s

closingOrPunctuation :: String -> Bool
closingOrPunctuation [c] = c `elem` closerOrPunct
closingOrPunctuation _ = False
closingOrPunctuation _ = False

closerOrPunct :: String
closerOrPunct = ")],;"
24 changes: 13 additions & 11 deletions eo-phi-normalizer/src/Language/EO/Phi/Normalize.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}

module Language.EO.Phi.Normalize where

import Language.EO.Phi.Syntax.Abs
import Data.Maybe (fromMaybe)
import Language.EO.Phi.Syntax.Abs

data Context = Context
{ globalObject :: [Binding]
, thisObject :: [Binding]
, thisObject :: [Binding]
}

lookupBinding :: Attribute -> [Binding] -> Maybe Object
Expand All @@ -20,8 +21,9 @@ lookupBinding _ _ = Nothing
-- | Normalize an input 𝜑-program.
normalize :: Program -> Program
normalize (Program bindings) = Program (map (normalizeBindingWith context) bindings)
where
context = Context
where
context =
Context
{ globalObject = bindings
, thisObject = bindings
}
Expand All @@ -47,8 +49,8 @@ peelObject = \case
GlobalDispatch attr -> PeeledObject HeadGlobal [ActionDispatch attr]
ThisDispatch attr -> PeeledObject HeadThis [ActionDispatch attr]
Termination -> PeeledObject HeadTermination []
where
followedBy (PeeledObject object actions) action = PeeledObject object (actions ++ [action])
where
followedBy (PeeledObject object actions) action = PeeledObject object (actions ++ [action])

unpeelObject :: PeeledObject -> Object
unpeelObject (PeeledObject head_ actions) =
Expand All @@ -63,8 +65,8 @@ unpeelObject (PeeledObject head_ actions) =
ActionDispatch a : as -> go (ThisDispatch a) as
_ -> error "impossible: this object without dispatch!"
HeadTermination -> go Termination actions
where
go = foldl applyAction
applyAction object = \case
ActionDispatch attr -> ObjectDispatch object attr
ActionApplication bindings -> Application object bindings
where
go = foldl applyAction
applyAction object = \case
ActionDispatch attr -> ObjectDispatch object attr
ActionApplication bindings -> Application object bindings
44 changes: 25 additions & 19 deletions eo-phi-normalizer/src/Language/EO/Phi/Syntax/Abs.hs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading