Skip to content

Commit

Permalink
WIP Parse and sketch conversion for user-defined rule sets
Browse files Browse the repository at this point in the history
  • Loading branch information
fizruk committed Jan 15, 2024
1 parent b4ca57e commit 1de94a0
Show file tree
Hide file tree
Showing 13 changed files with 1,203 additions and 1 deletion.
1 change: 1 addition & 0 deletions eo-phi-normalizer/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ main = defaultMainWithHooks $ simpleUserHooks
, postConf = \args flags packageDesc localBuildInfo -> do
#ifndef mingw32_HOST_OS
_ <- system "bnfc -d -p Language.EO.Phi --generic -o src/ grammar/EO/Phi/Syntax.cf"
_ <- system "bnfc -d -p Language.EO.Phi.Rules --generic -o src/ grammar/EO/Phi/Rules/Syntax.cf"
#endif
postConf simpleUserHooks args flags packageDesc localBuildInfo
}
Expand Down
6 changes: 5 additions & 1 deletion eo-phi-normalizer/eo-phi-normalizer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ library
Language.EO.Phi
Language.EO.Phi.Normalize
Language.EO.Phi.Rules.Common
Language.EO.Phi.Rules.PhiPaper
Language.EO.Phi.Rules.Syntax.Abs
Language.EO.Phi.Rules.Syntax.Lex
Language.EO.Phi.Rules.Syntax.Par
Language.EO.Phi.Rules.Syntax.Print
Language.EO.Phi.Rules.Yaml
Language.EO.Phi.Syntax
Language.EO.Phi.Syntax.Abs
Language.EO.Phi.Syntax.Lex
Expand Down
47 changes: 47 additions & 0 deletions eo-phi-normalizer/grammar/EO/Phi/Rules/Syntax.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- ==========================================================
-- BNFC grammar for φ-programs (translated from EO)
-- ==========================================================
--
-- This is a non-ambiguous grammar for φ-programs.

token Bytes ({"--"} | ["0123456789ABCDEF"] ["0123456789ABCDEF"] {"-"} | ["0123456789ABCDEF"] ["0123456789ABCDEF"] ({"-"} ["0123456789ABCDEF"] ["0123456789ABCDEF"])+) ;
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,.|':;!-?][}{)(⟧⟦"])* ;

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

Formation. Object ::= "⟦" [Binding] "⟧" ;
Application. Object ::= Object "(" [Binding] ")" ;
ObjectDispatch. Object ::= Object "." Attribute ;
GlobalDispatch. Object ::= "Φ" "." Attribute ;
ThisDispatch. Object ::= "ξ" "." Attribute ;
Termination. Object ::= "⊥" ;
MetaObject. Object ::= MetaId ;

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

Phi. Attribute ::= "φ" ; -- decoratee object
Rho. Attribute ::= "ρ" ; -- parent object
Sigma. Attribute ::= "σ" ; -- home object
VTX. Attribute ::= "ν" ; -- the vertex identifier (an object that represents the unique identifier of the containing object)
Label. Attribute ::= LabelId ;
Alpha. Attribute ::= AlphaIndex ;
MetaAttr. Attribute ::= MetaId ;

PeeledObject. PeeledObject ::= ObjectHead [ObjectAction] ;

HeadFormation. ObjectHead ::= "⟦" [Binding] "⟧" ;
HeadGlobal. ObjectHead ::= "Φ" ;
HeadThis. ObjectHead ::= "ξ" ;
HeadTermination. ObjectHead ::= "⊥" ;

ActionApplication. ObjectAction ::= "(" [Binding] ")" ;
ActionDispatch. ObjectAction ::= "." Attribute ;
separator ObjectAction "" ;
3 changes: 3 additions & 0 deletions eo-phi-normalizer/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ library:
- Language.EO.Phi.Syntax.Test
- Language.EO.Phi.Syntax.ErrM
- Language.EO.Phi.Syntax.Skel
- Language.EO.Phi.Rules.Syntax.Test
- Language.EO.Phi.Rules.Syntax.ErrM
- Language.EO.Phi.Rules.Syntax.Skel

executables:
normalize-phi:
Expand Down
74 changes: 74 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Syntax/Abs.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- File generated by the BNF Converter (bnfc 2.9.5).

{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

-- | The abstract syntax of language Syntax.

module Language.EO.Phi.Rules.Syntax.Abs where

import Prelude (String)
import qualified Prelude as C (Eq, Ord, Show, Read)
import qualified Data.String

import qualified Data.Data as C (Data, Typeable)
import qualified GHC.Generics as C (Generic)

data Program = Program [Binding]
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic)

data Object
= Formation [Binding]
| Application Object [Binding]
| ObjectDispatch Object Attribute
| GlobalDispatch Attribute
| ThisDispatch Attribute
| Termination
| MetaObject MetaId
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic)

data Binding
= AlphaBinding Attribute Object
| EmptyBinding Attribute
| DeltaBinding Bytes
| LambdaBinding Function
| MetaBindings MetaId
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic)

data Attribute
= Phi
| Rho
| Sigma
| VTX
| Label LabelId
| Alpha AlphaIndex
| MetaAttr MetaId
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic)

data PeeledObject = PeeledObject ObjectHead [ObjectAction]
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic)

data ObjectHead
= HeadFormation [Binding] | HeadGlobal | HeadThis | HeadTermination
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic)

data ObjectAction
= ActionApplication [Binding] | ActionDispatch Attribute
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic)

newtype Bytes = Bytes String
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic, Data.String.IsString)

newtype Function = Function String
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic, Data.String.IsString)

newtype LabelId = LabelId String
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic, Data.String.IsString)

newtype AlphaIndex = AlphaIndex String
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic, Data.String.IsString)

newtype MetaId = MetaId String
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Data, C.Typeable, C.Generic, Data.String.IsString)

95 changes: 95 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Syntax/Doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
The Language Syntax
BNF Converter


%Process by txt2tags to generate html or latex



This document was automatically generated by the //BNF-Converter//. It was generated together with the lexer, the parser, and the abstract syntax module, which guarantees that the document matches with the implementation of the language (provided no hand-hacking has taken place).

==The lexical structure of Syntax==

===Literals===






Bytes literals are recognized by the regular expression
`````{"--"} | ["0123456789ABCDEF"] ["0123456789ABCDEF"] '-' | ["0123456789ABCDEF"] ["0123456789ABCDEF"] ('-' ["0123456789ABCDEF"] ["0123456789ABCDEF"])+`````

Function literals are recognized by the regular expression
`````upper (char - ["
!'(),-.:;?[]{|}⟦⟧"])*`````

LabelId literals are recognized by the regular expression
`````lower (char - ["
!'(),-.:;?[]{|}⟦⟧"])*`````

AlphaIndex literals are recognized by the regular expression
`````{"α0"} | 'α' (digit - '0') digit*`````

MetaId literals are recognized by the regular expression
`````'!' (char - ["
!'(),-.:;?[]{|}⟦⟧"])*`````


===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.

The reserved words used in Syntax are the following:
| ``Δ`` | ``Φ`` | ``λ`` | ``ν``
| ``ξ`` | ``ρ`` | ``σ`` | ``φ``

The symbols used in Syntax are the following:
| { | } | ⟦ | ⟧
| ( | ) | . | ⊥
| ↦ | ∅ | ⤍ | ,

===Comments===
There are no single-line comments in the grammar.There are no multiple-line comments in the grammar.

==The syntactic structure of Syntax==
Non-terminals are enclosed between < and >.
The symbols -> (production), **|** (union)
and **eps** (empty rule) belong to the BNF notation.
All other symbols are terminals.

| //Program// | -> | ``{`` //[Binding]// ``}``
| //Object// | -> | ``⟦`` //[Binding]// ``⟧``
| | **|** | //Object// ``(`` //[Binding]// ``)``
| | **|** | //Object// ``.`` //Attribute//
| | **|** | ``Φ`` ``.`` //Attribute//
| | **|** | ``ξ`` ``.`` //Attribute//
| | **|** | ``⊥``
| | **|** | //MetaId//
| //Binding// | -> | //Attribute// ``↦`` //Object//
| | **|** | //Attribute// ``↦`` ``∅``
| | **|** | ``Δ`` ``⤍`` //Bytes//
| | **|** | ``λ`` ``⤍`` //Function//
| | **|** | //MetaId//
| //[Binding]// | -> | **eps**
| | **|** | //Binding//
| | **|** | //Binding// ``,`` //[Binding]//
| //Attribute// | -> | ``φ``
| | **|** | ``ρ``
| | **|** | ``σ``
| | **|** | ``ν``
| | **|** | //LabelId//
| | **|** | //AlphaIndex//
| | **|** | //MetaId//
| //PeeledObject// | -> | //ObjectHead// //[ObjectAction]//
| //ObjectHead// | -> | ``⟦`` //[Binding]// ``⟧``
| | **|** | ``Φ``
| | **|** | ``ξ``
| | **|** | ``⊥``
| //ObjectAction// | -> | ``(`` //[Binding]// ``)``
| | **|** | ``.`` //Attribute//
| //[ObjectAction]// | -> | **eps**
| | **|** | //ObjectAction// //[ObjectAction]//



%% File generated by the BNF Converter (bnfc 2.9.5).
Expand Down
91 changes: 91 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Rules/Syntax/ErrM.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
-- File generated by the BNF Converter (bnfc 2.9.5).

{-# LANGUAGE CPP #-}

#if __GLASGOW_HASKELL__ >= 708
---------------------------------------------------------------------------
-- Pattern synonyms exist since ghc 7.8.

-- | BNF Converter: Error Monad.
--
-- Module for backwards compatibility.
--
-- The generated parser now uses @'Either' String@ as error monad.
-- This module defines a type synonym 'Err' and pattern synonyms
-- 'Bad' and 'Ok' for 'Left' and 'Right'.

{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE FlexibleInstances #-}

module Language.EO.Phi.Rules.Syntax.ErrM where

import Prelude (id, const, Either(..), String)

import Control.Monad (MonadPlus(..))
import Control.Applicative (Alternative(..))
#if __GLASGOW_HASKELL__ >= 808
import Control.Monad (MonadFail(..))
#endif

-- | Error monad with 'String' error messages.
type Err = Either String

pattern Bad msg = Left msg
pattern Ok a = Right a

#if __GLASGOW_HASKELL__ >= 808
instance MonadFail Err where
fail = Bad
#endif

instance Alternative Err where
empty = Left "Err.empty"
(<|>) Left{} = id
(<|>) x@Right{} = const x

instance MonadPlus Err where
mzero = empty
mplus = (<|>)

#else
---------------------------------------------------------------------------
-- ghc 7.6 and before: use old definition as data type.

-- | BNF Converter: Error Monad

-- Copyright (C) 2004 Author: Aarne Ranta
-- This file comes with NO WARRANTY and may be used FOR ANY PURPOSE.

module Language.EO.Phi.Rules.Syntax.ErrM where

-- the Error monad: like Maybe type with error msgs

import Control.Applicative (Applicative(..), Alternative(..))
import Control.Monad (MonadPlus(..), liftM)

data Err a = Ok a | Bad String
deriving (Read, Show, Eq, Ord)

instance Monad Err where
return = Ok
Ok a >>= f = f a
Bad s >>= _ = Bad s

instance Applicative Err where
pure = Ok
(Bad s) <*> _ = Bad s
(Ok f) <*> o = liftM f o

instance Functor Err where
fmap = liftM

instance MonadPlus Err where
mzero = Bad "Err.mzero"
mplus (Bad _) y = y
mplus x _ = x

instance Alternative Err where
empty = mzero
(<|>) = mplus

#endif
Loading

0 comments on commit 1de94a0

Please sign in to comment.