Source

xilocaml / Xilocaml.cf

-- This language is subset of Ocaml

-- Unlike in OCaml, nested comments aren't allowed
comment "(*" "*)" ;

position token OInteger digit+ ;
position token OBool 't' 'r' 'u' 'e' | 'f' 'a' 'l' 's' 'e' ;
-- More Options than for Double (mainly 1.)
position token OFloat (((digit+ '.' digit*)|('.' digit+))(('e'|'E') ('+'|'-')? digit+)?)|
             (digit+ ('e'|'E') ('-')? digit+) ;
-- Identifier for function and argument names
position token OIdent lower (letter | digit | '_' | '\'')* ;
-- Identifier for polymorphic types
token OPolyIdent '\'' (letter | digit | '_' | '\'')* ;

Prog. Program ::= ComBeg Exp ComEnd ;
-- Compability with Ocaml
CompatibOpen.  ComBeg ::= "open" "List" ";;" ;
CompatibBeg.   ComBeg ::= ;
CompatibColon. ComEnd ::= ";;" ;
CompatibEnd.   ComEnd ::= ;

-- In Ocaml let has every priority and greedy last Exp (possible "1+let a=2 in a + a")
ELetIn.       Exp  ::= "let" [LocDecl] "in" Exp ;
ELetRecIn.    Exp  ::= "let" "rec" [LocDecl] "in" Exp ;
ELambda.      Exp  ::= "fun" [Argument] "->" Exp ;
EIfThenElse.  Exp  ::= "if" Exp "then" Exp "else" Exp ;
EOr.          Exp1 ::= Exp1 "||"  Exp2 ;
EAnd.         Exp2 ::= Exp2 "&&"  Exp3 ;
ECmp.         Exp3 ::= Exp3 OCmp  Exp4 ;
-- In Ocaml Exp4 part here is Exp
EPrepend.     Exp4 ::= Exp5 "::"  Exp4 ;
EInfixWeak.   Exp5 ::= Exp5 OIfxW Exp6 ;
EInfixStrong. Exp6 ::= Exp6 OIfxS Exp7 ;
EUnary.       Exp7 ::=      OUnr  Exp7 ;
ENot.         Exp8 ::= "not" Exp10 ;
EHead.        Exp8 ::= "hd"  Exp10 ;
ETail.        Exp8 ::= "tl"  Exp10 ;
EAppl.        Exp9 ::= Exp9 Exp10 ;
EInt.         Exp10 ::= OInteger ;
EFloat.       Exp10 ::= OFloat ;
EBool.        Exp10 ::= OBool ;
EList.        Exp10 ::= "[" [ListElem] "]" ;
EVar.         Exp11 ::= OIdent ;
EExplType.    Exp12 ::= "(" Exp ":" Type ")" ;

coercions Exp 12 ;

terminator nonempty Argument "" ;
FunArg.         Argument ::= OIdent ;
FunArgExplType. Argument ::= "(" OIdent ":" Type ")" ;

separator nonempty LocDecl "and" ;
LocalVarDecl.         LocDecl ::= OIdent "=" Exp ;
LocalVarDeclExplType. LocDecl ::= OIdent ":" Type "=" Exp ;
LocalFunDecl.         LocDecl ::= OIdent [Argument] "=" Exp ;
LocalFunDeclExplType. LocDecl ::= OIdent [Argument] ":" Type "=" Exp ;

separator ListElem ";" ;
ListElement. ListElem ::= Exp ;

OEq.  OCmp ::= "=" ;
ONeq. OCmp ::= "<>" ;
OLt.  OCmp ::= "<" ;
OLeq. OCmp ::= "<=" ;
OGt.  OCmp ::= ">" ;
OGeq. OCmp ::= ">=" ;

OAdd.    OIfxW ::= "+" ;
OSub.    OIfxW ::= "-" ;
OAddFlt. OIfxW ::= "+." ;
OSubFlt. OIfxW ::= "-." ;

OMul.    OIfxS ::= "*" ;
ODiv.    OIfxS ::= "/" ;
OMulFlt. OIfxS ::= "*." ;
ODivFlt. OIfxS ::= "/." ;
OMod.    OIfxS ::= "mod" ;

ONeg.    OUnr ::= "-" ;
OPos.    OUnr ::= "+" ;
ONegFlt. OUnr ::= "-." ;
OPosFlt. OUnr ::= "+." ;

TFun.   Type  ::= Type1 "->" Type ;
TList.  Type1 ::= Type1 "list" ;
TInt.   Type2 ::= "int" ;
TFloat. Type2 ::= "float" ;
TBool.  Type2 ::= "bool" ;
TPoly.  Type2 ::= OPolyIdent ;

coercions Type 2 ;