-- top-level grammar

-- Canonical GF. AR 27/4/2003

entrypoints Canon, Line ;

-- old approach: read in a whole grammar

MGr. Canon ::= "grammar" [Ident] "of" Ident ";" [Module] ;
Gr.  Canon ::= [Module] ;

-- new approach: read line by line

LMulti.  Line ::= "grammar" [Ident] "of" Ident ";" ;
LHeader. Line ::= ModType "=" Extend Open "{" ;
LFlag.   Line ::= Flag ";" ;
LDef.    Line ::= Def ";" ;
LEnd.    Line ::= "}" ;    

Mod. Module ::= ModType "=" Extend Open "{" [Flag] [Def] "}" ;

MTAbs.   ModType ::= "abstract" Ident ; 
MTCnc.   ModType ::= "concrete" Ident "of" Ident ;
MTRes.   ModType ::= "resource" Ident ;
MTTrans. ModType ::= "transfer" Ident ":" Ident "->" Ident ;

separator Module "" ;

Ext.   Extend ::= [Ident] "**" ;
NoExt. Extend ::= ;

Opens.   Open ::= "open" [Ident] "in" ;
NoOpens. Open ::= ;


-- judgements

Flg.  Flag ::= "flags" Ident "=" Ident ;  --- to have the same res word as in GF

AbsDCat.   Def ::= "cat" Ident "[" [Decl] "]" "=" [CIdent] ;
AbsDFun.   Def ::= "fun" Ident ":" Exp "=" Exp ;
AbsDTrans. Def ::= "transfer" Ident "=" Exp ;

ResDPar.   Def ::= "param"  Ident "=" [ParDef] ;
ResDOper.  Def ::= "oper"   Ident ":" CType "=" Term ;

CncDCat.   Def ::= "lincat" Ident "=" CType "=" Term ";" Term  ; 
CncDFun.   Def ::= "lin"    Ident ":" CIdent "=" "\\" [ArgVar] "->" Term ";" Term ;

AnyDInd.   Def ::= Ident Status "in" Ident ;

ParD. ParDef ::= Ident [CType] ;

-- the canonicity of an indirected constant

Canon.  Status ::= "data" ;
NonCan. Status ::= ;

-- names originating from resource modules: prefixed by the module name

CIQ. CIdent ::= Ident "." Ident ;

-- types and terms in abstract syntax; no longer type-annotated

EApp.   Exp1 ::= Exp1 Exp2 ;
EProd.  Exp  ::= "(" Ident ":" Exp ")" "->" Exp ;
EAbs.   Exp  ::= "\\" Ident "->" Exp ;
EAtom.  Exp2 ::= Atom ;
EData.  Exp2 ::= "data" ;

EEq.    Exp  ::= "{" [Equation] "}" ;  -- list of pattern eqs; primitive notion: []

coercions Exp 2 ;

SType.  Sort ::= "Type" ;

Equ. Equation ::= [APatt] "->" Exp ;

APC. APatt ::= "(" CIdent [APatt] ")" ;
APV. APatt ::= Ident ;
APS. APatt ::= String ;
API. APatt ::= Integer ;
APF. APatt ::= Double ;
APW. APatt ::= "_" ;

separator Decl ";" ;
terminator APatt "" ;
terminator Equation ";" ;

AC. Atom ::= CIdent ;
AD. Atom ::= "<" CIdent ">" ;
AV. Atom ::= "$" Ident ;
AM. Atom ::= "?" Integer ;
AS. Atom ::= String ;
AI. Atom ::= Integer ;
AT. Atom ::= Sort ;

Decl. Decl ::= Ident ":" Exp ;


-- types, terms, and patterns in concrete syntax

RecType. CType ::= "{" [Labelling] "}" ;
Table.   CType ::= "(" CType "=>" CType ")" ;
Cn.      CType ::= CIdent ;
TStr.    CType ::= "Str" ;
TInts.   CType ::= "Ints" Integer ;

Lbg.  Labelling ::= Label ":" CType ;

Arg.  Term2 ::= ArgVar ;
I.    Term2 ::= CIdent ; -- from resources
Par.  Term2 ::= "<" CIdent [Term2] ">" ;
LI.   Term2 ::= "$" Ident ;  -- from pattern variables

R.    Term2 ::= "{" [Assign] "}" ;
P.    Term1 ::= Term2 "." Label ;
T.    Term1 ::= "table" CType "{" [Case] "}" ;
V.    Term1 ::= "table" CType "[" [Term2] "]" ;
S.    Term1 ::= Term1 "!" Term2 ;
C.    Term  ::= Term "++" Term1 ;
FV.   Term1 ::= "variants" "{" [Term2] "}" ;  --- no separator!

EInt.   Term2 ::= Integer ;
EFloat. Term2 ::= Double ;
K.    Term2 ::= Tokn ;
E.    Term2 ::= "[" "]" ;

KS.   Tokn  ::= String ;
KP.   Tokn  ::= "[" "pre" [String] "{" [Variant] "}" "]" ;
internal KM.   Tokn  ::= String ; -- mark-up

Ass.  Assign  ::= Label "=" Term ;
Cas.  Case    ::= [Patt] "=>" Term ;
Var.  Variant ::= [String] "/" [String] ;

coercions Term 2 ;

L.    Label  ::= Ident ;
LV.   Label  ::= "$" Integer ;
A.    ArgVar ::= Ident "@" Integer ;             -- no bindings
AB.   ArgVar ::= Ident "+" Integer "@" Integer ; -- with a number of bindings

PC.   Patt ::= "(" CIdent [Patt] ")" ;
PV.   Patt ::= Ident ;
PW.   Patt ::= "_" ;
PR.   Patt ::= "{" [PattAssign] "}" ;
PI.   Patt ::= Integer ;
PF.   Patt ::= Double ;

PAss. PattAssign ::= Label "=" Patt ;

--- here we use the new pragmas to generate list rules

terminator Flag ";" ;
terminator Def ";" ;
separator ParDef "|" ;
separator CType "" ;
separator CIdent "" ;
separator Assign ";" ;
separator ArgVar "," ;
separator Labelling ";" ;
separator Case ";" ;
separator Term2 "" ;
separator String "" ;
separator Variant ";" ;
separator PattAssign ";" ;
separator Patt "" ;
separator Ident "," ;

