summaryrefslogtreecommitdiff
path: root/src/runtime/haskell/PGF
diff options
context:
space:
mode:
authorhallgren <hallgren@chalmers.se>2015-01-14 14:35:39 +0000
committerhallgren <hallgren@chalmers.se>2015-01-14 14:35:39 +0000
commit20b271a2383d81806efdbf8d35f17f989ce770a6 (patch)
tree4924aa32decf9df14b887ec177a4015eb1c7b6c7 /src/runtime/haskell/PGF
parent2e642ace8acd81d71d943e59201770b1470c425b (diff)
Translating linearization functions to Haskell: better treatment of special tokens
Common code has been lifted out from the generated Haskell modules to an auxiliary module PGF.Haskell, which is currently included in the regular PGF library, although it is independent of it and probably belongs in a separate library. The type Str used by linearization functions is now based on a token type Tok, which is defined in PGF.Haskell. PGF.Haskell.Tok is similar to the type GF.Data.Str.Tok, but it has constructors for the special tokens BIND, SOFT_BIND and CAPIT, and there is a function fromStr :: Str -> String that computes the effects of these special tokens.
Diffstat (limited to 'src/runtime/haskell/PGF')
-rw-r--r--src/runtime/haskell/PGF/Haskell.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/runtime/haskell/PGF/Haskell.hs b/src/runtime/haskell/PGF/Haskell.hs
new file mode 100644
index 000000000..8f5021bfe
--- /dev/null
+++ b/src/runtime/haskell/PGF/Haskell.hs
@@ -0,0 +1,44 @@
+-- | Auxiliary types and functions for use with grammars translated to Haskell
+-- with gf -output-format=haskell -haskell=concrete
+module PGF.Haskell where
+import Data.Char(toUpper)
+import Data.List(isPrefixOf)
+import qualified Data.Map as M
+
+-- | For enumerating parameter values used in tables
+class EnumAll a where enumAll :: [a]
+
+-- | Tables
+table vs = let m = M.fromList (zip enumAll vs) in (M.!) m
+
+
+-- | Token sequences, output form linearization functions
+type Str = [Tok] -- token sequence
+
+-- | Tokens
+data Tok = TK String | TP [([Prefix],Str)] Str | BIND | SOFT_BIND | CAPIT
+ deriving (Eq,Ord,Show)
+
+type Prefix = String -- ^ To be matched with the prefix of a following token
+
+-- | Render a token sequence as a 'String'
+fromStr :: Str -> String
+fromStr = from False False
+ where
+ from space cap ts =
+ case ts of
+ [] -> []
+ TK s:ts -> put s++from True cap ts
+ BIND:ts -> from False cap ts
+ SOFT_BIND:ts -> from False cap ts
+ CAPIT:ts -> from space True ts
+ TP alts def:ts -> from space cap (pick alts def r++[TK r]) -- hmm
+ where r = fromStr ts
+ where
+ put s = [' '|space]++up s
+ up = if cap then toUpper1 else id
+
+ toUpper1 (c:s) = toUpper c:s
+ toUpper1 s = s
+
+ pick alts def r = head ([str|(ps,str)<-alts,any (`isPrefixOf` r) ps]++[def])