{-
    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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
-}

-- Multi-view grammars (undocumented)

LGr.      LGrammar ::= [LDef] ;

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

-- separator LDef ";" ;
[].       [LDef] ::= ;
(:[]).    [LDef] ::= LDef ;
(:).      [LDef] ::= LDef ";" [LDef] ;
-- extra semicolons allowed
_.        [LDef] ::= ";" [LDef] ;

separator nonempty Ident "," ;

-- A Grammar is a sequence of definitions

Grammar . Grammar ::= [Def] ;

-- separator Def ";" ;  -- Note: this still permits a final semicolon.
[].       [Def] ::= ;
(:[]).    [Def] ::= Def ;
(:).      [Def] ::= Def ";" [Def] ;
-- extra semicolons allowed
_.        [Def] ::= ";" [Def] ;

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

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

terminator Item "" ;

-- Categories (non-terminals)
ListCat  . Cat ::= "[" Cat "]" ;
IdCat    . Cat ::= Ident ;

-- Labels with or without profiles
LabNoP   . Label ::= LabelId ;
LabP     . Label ::= LabelId [ProfItem] ;
-- A second label can be given in use of profiles and serves as
-- ``original function name'' (undocumented feature).
LabPF    . Label ::= LabelId LabelId [ProfItem] ;
LabF     . Label ::= LabelId LabelId ;

-- functional labels
Id       . LabelId ::= Ident               ;  -- AST constructor
Wild     . LabelId ::= "_"                 ;  -- No AST constructor (embedding)
ListE    . LabelId ::= "[" "]"             ;  -- Empty list
ListCons . LabelId ::= "(" ":" ")"         ;  -- Cons
ListOne  . LabelId ::= "(" ":" "[" "]" ")" ;  -- Singleton list

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

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

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

-- Pragmas
Comment  .  Def ::= "comment"  String                     ; -- Line comment
Comments .  Def ::= "comment"  String String              ; -- Block comment
Internal .  Def ::= "internal" Label "." Cat "::=" [Item] ; -- No parsing, AST and printing only
Token.      Def ::=            "token" Ident Reg          ; -- Lexer token
PosToken.   Def ::= "position" "token" Ident Reg          ; -- Lexer token with position info
Entryp.     Def ::= "entrypoints" [Ident]                 ; -- Names of parsers
Separator.  Def ::= "separator"   MinimumSize Cat String  ; -- List
Terminator. Def ::= "terminator"  MinimumSize Cat String  ; -- List
Delimiters. Def ::= "delimiters"  Cat String String Separation MinimumSize;
Coercions.  Def ::= "coercions"   Ident Integer           ; -- Embeddings and parenthesized exprs.
Rules.      Def ::= "rules"       Ident "::=" [RHS]       ; -- Automatically generated lables (e.g. enums)
Function.   Def ::= "define"      Ident [Arg] "=" Exp     ;

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

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

-- Layout
Layout.     Def ::= "layout" [String]        ; -- Layout start keywords
LayoutStop. Def ::= "layout" "stop" [String] ; -- Layout stop keywords
LayoutTop.  Def ::= "layout" "toplevel"      ; -- Should the toplevel be a block?

separator nonempty String "," ;

-- 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   Exp ","        ; -- list list
separator nonempty Exp2 "" ; -- argument list

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

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

-- Regular expressions
RSeq.    Reg2 ::= Reg2 Reg3     ;  -- left-associative
RAlt.    Reg1 ::= Reg1 "|" Reg2 ;  -- left-associative
RMinus.  Reg1 ::= Reg2 "-" Reg2 ;  -- non-associative

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"   ;

coercions Reg 3;

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