{-
    BNF Converter: Language definition
    Copyright (C) 2004  Author: Markus Forberg, Michael Pellauer, Aarne Ranta

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

-- A Grammar is a sequence of definitions

LGr.      LGrammar ::= [LDef] ;

DefAll.   LDef ::= Def ;
DefSome.  LDef ::= [Ident] ":" Def ;
LDefView. LDef ::= "views" [Ident] ;

separator LDef ";" ;

Grammar . Grammar ::= [Def] ;

separator Def ";" ;

[]  . [Item] ::= ;
(:) . [Item] ::= Item [Item] ;

--The rules of the grammar
Rule . Def ::= Label "." Cat "::=" [Item] ;

-- Items
Terminal  . Item ::= String ;
NTerminal . Item ::= Cat ;

-- Categories
ListCat  . Cat ::= "[" Cat "]" ;
IdCat    . Cat ::= Ident ;

-- labels with or without profiles
LabNoP   . Label ::= LabelId ;
LabP     . Label ::= LabelId [ProfItem] ;
LabPF    . Label ::= LabelId LabelId [ProfItem] ;
LabF     . Label ::= LabelId LabelId ;

-- functional labels
Id       . LabelId ::= Ident ; 
Wild     . LabelId ::= "_" ; 
ListE    . LabelId ::= "[" "]" ;
ListCons . LabelId ::= "(" ":" ")" ; 
ListOne  . LabelId ::= "(" ":" "[" "]" ")" ;

-- profiles (= permutation and binding patterns)
ProfIt   . ProfItem ::= "(" "[" [IntList] "]" "," "[" [Integer] "]" ")" ;

Ints     . IntList ::= "[" [Integer] "]" ;

separator Integer "," ;
separator IntList "," ;
terminator nonempty ProfItem "" ;

-- Pragmas
Comment  .  Def ::= "comment" String ;
Comments .  Def ::= "comment" String String ;
Internal .  Def ::= "internal" Label "." Cat "::=" [Item] ; 
Token.      Def ::= "token" Ident Reg ;
PosToken.   Def ::= "position" "token" Ident Reg ;
Entryp.     Def ::= "entrypoints" [Ident] ;
Separator.  Def ::= "separator" MinimumSize Cat String ;
Terminator. Def ::= "terminator" MinimumSize Cat String ;
Delimiters. Def ::= "delimiters" Cat String String Separation MinimumSize;
Coercions.  Def ::= "coercions" Ident Integer ;
Rules.      Def ::= "rules" Ident "::=" [RHS] ;
Function.   Def ::= "define" Ident [Arg] "=" Exp ;

SepNone.    Separation ::= ;
SepTerm.    Separation ::= "terminator" String;
SepSepar.   Separation ::= "separator" String;

Layout.     Def ::= "layout" [String] ;
LayoutStop. Def ::= "layout" "stop" [String] ;
LayoutTop.  Def ::= "layout" "toplevel" ;

Arg. Arg ::= Ident ;
separator Arg "" ;

-- Expressions
Cons.	    Exp  ::= Exp1 ":" Exp ;
App.	    Exp1 ::= Ident [Exp2] ;
Var.	    Exp2 ::= Ident ;
LitInt.	    Exp2 ::= Integer ;
LitChar.    Exp2 ::= Char ;
LitString.  Exp2 ::= String ;
LitDouble.  Exp2 ::= Double ;
List.	    Exp2 ::= "[" [Exp] "]" ;


coercions Exp 2;
separator nonempty Exp2 "" ;
separator Exp "," ;

separator nonempty String "," ;

separator nonempty RHS "|" ;
RHS. RHS ::= [Item] ;

-- List size condition
MNonempty.  MinimumSize ::= "nonempty" ;
MEmpty.     MinimumSize ::=  ;

-- regular expressions
RSeq.   Reg2 ::= Reg2 Reg3 ;
RAlt.   Reg1 ::= Reg1 "|" Reg2 ;
RMinus. Reg1 ::= Reg2 "-" Reg2 ;

RStar.  Reg3 ::= Reg3 "*" ;
RPlus.  Reg3 ::= Reg3 "+" ;
ROpt.   Reg3 ::= Reg3 "?" ;

REps.   Reg3 ::= "eps" ;

RChar.  Reg3 ::= Char ;           -- single character
RAlts.  Reg3 ::= "[" String "]" ; -- list of alternative characters
RSeqs.  Reg3 ::= "{" String "}" ; -- character sequence

RDigit.  Reg3 ::= "digit" ;
RLetter. Reg3 ::= "letter" ;
RUpper.  Reg3 ::= "upper" ;
RLower.  Reg3 ::= "lower" ;
RAny.    Reg3 ::= "char" ;

_. Reg  ::= Reg1 ;
_. Reg1 ::= Reg2 ;
_. Reg2 ::= Reg3 ;
_. Reg3 ::= "(" Reg ")" ;

-- list of categories in the entrypoint pragma
(:[]).  [Ident] ::= Ident ;
(:).    [Ident] ::= Ident "," [Ident] ;

-- comments in BNF source
comment "--" ;
comment "{-" "-}" ;


