summaryrefslogtreecommitdiff
path: root/src/GF/Grammar/API.hs
blob: 182b5e94e5c861442447b555335284e05fa2b637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module GF.Grammar.API (
  Grammar,
  emptyGrammar,
  pTerm,
  prTerm,
  checkTerm,
  computeTerm,
  showTerm,
  TermPrintStyle(..),
  pTermPrintStyle
  ) where

import GF.Source.ParGF
import GF.Source.SourceToGrammar (transExp)
import GF.Grammar.Grammar
import GF.Infra.Ident
import GF.Infra.Modules (greatestResource)
import GF.Compile.GetGrammar
import GF.Grammar.Macros
import GF.Grammar.PrGrammar

import GF.Compile.Rename (renameSourceTerm)
import GF.Compile.CheckGrammar (justCheckLTerm)
import GF.Compile.Compute (computeConcrete)

import GF.Data.Operations
import GF.Infra.Option

import qualified Data.ByteString.Char8 as BS

type Grammar = SourceGrammar

emptyGrammar :: Grammar
emptyGrammar = emptySourceGrammar

pTerm :: String -> Err Term
pTerm s = do
  e <- pExp $ myLexer (BS.pack s)
  transExp e

prTerm :: Term -> String
prTerm = prt

checkTerm :: Grammar -> Term -> Err Term
checkTerm gr t = do
  mo <- maybe (Bad "no source grammar in scope") return $ greatestResource gr
  checkTermAny gr mo t

checkTermAny :: Grammar -> Ident -> Term -> Err Term
checkTermAny gr m t = do
  t1 <- renameSourceTerm gr m t
  justCheckLTerm gr t1

computeTerm :: Grammar -> Term -> Err Term
computeTerm = computeConcrete

showTerm :: TermPrintStyle -> Term -> String
showTerm style t = 
    case style of
      TermPrintTable   -> unlines [p +++ s | (p,s) <- prTermTabular t]
      TermPrintAll     -> unlines [      s | (p,s) <- prTermTabular t]
      TermPrintUnqual  -> prt_ t
      TermPrintDefault -> prt t


data TermPrintStyle = TermPrintTable | TermPrintAll | TermPrintUnqual | TermPrintDefault
  deriving (Show,Eq)

pTermPrintStyle s = case s of
  "table"  -> TermPrintTable
  "all"    -> TermPrintAll
  "unqual" -> TermPrintUnqual
  _        -> TermPrintDefault