Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /trunk/sml3d/gen/spec-parser/spec.grm
ViewVC logotype

View of /trunk/sml3d/gen/spec-parser/spec.grm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1223 - (download) (annotate)
Wed Aug 3 18:28:46 2011 UTC (7 years ago) by jhr
File size: 7806 byte(s)
  Working on spec parser/database
(* spec.grm
 *
 * COPYRIGHT (c) 2011 The SML3d Project (http://sml3d.cs.uchicago.edu)
 * All rights reserved.
 *
 * An ML-Antrl parser for the OpenGL specification files.
 *)

%name Spec;

%tokens
  : KW_all              ("all")
  | KW_array            ("array")
  | KW_const            ("const")
  | KW_define           ("define")
  | KW_enum             ("enum")
  | KW_float            ("float")
  | KW_in               ("in")
  | KW_inout            ("in/out")
  | KW_mask             ("mask")
  | KW_newcategory      ("newcategory:")
  | KW_out              ("out")
  | KW_param            ("param")
  | KW_reference        ("reference")
  | KW_required_props   ("required-props:")
  | KW_return           ("return")
  | KW_struct           ("struct")
  | KW_use              ("use")
  | KW_value            ("value")
  | KW_void             ("void")                (* NOTE: includes GLvoid *)
  | EQ                  ("=")
  | COLON               (":")
  | COMMA               (",")
  | SEMI                (";")
  | PERIOD              (".")
  | LP                  ("(")
  | RP                  (")")
  | LB                  ("[")
  | RB                  ("]")
  | STAR                ("*")
  | PLUS                ("+")
  | MINUS               ("-")
  | SLASH               ("/")
  | UNKNOWN             ("?")
  | BAR                 ("|")
  | ID of Atom.atom
  | LIT of string       (* numbers, version tags, etc. *)
  | COMPSIZE of string  (* COMPSIZE(...) *)
  | INDENT              (* whitespace at beginning of line *)
  | EOL                 (* end of line *)
  ;

%defs (
  structure S = SpecRep
  structure N = SpecNames
);

%start EnumSpec;
%entry FunctionSpec;
%entry TypemapSpec;

(***** Enumerant specification *****)

EnumSpec
        : TypeDef*
                => (List.concat TypeDef)
        ;

TypeDef
        : TypeIds Kind ":" Note* EOL (INDENT Definition EOL)*
                => (List.map
                      (fn id => S.TyDef{span = FULL_SPAN, name = id, kind = Kind, defs = SR})
                        TypeIds)
        ;

TypeIds
        : ID ("," ID)*  => (ID::SR)
        ;

Kind
        : "enum"        => (S.ENUM)
        | "mask"        => (S.MASK)
        | "float"       => (S.FLOAT) (* is this used? *)
        | "define"      => (S.DEFINE)
        ;

(* the enum.spec file often has debris following the kind *)
Note
        : "(" Word* ")" ":"?
                => ()
        | ID
                => ()
        ;

Word
        : ("=" | ":" | "," | ";" | "." | "*" | "+" | "-" | "/" | "?" | "|")
                => ()
        | Id
                => ()
        | LIT
                => ()
        ;


Definition
        : "use" ID ID
                => (S.EnumDef(FULL_SPAN, ID2, S.UseConst ID1))
        | ID EnumValue
                => (S.EnumDef(FULL_SPAN, ID, EnumValue))
        ;

EnumValue
        : (* empty *)
                => (S.NoConst)
        | "=" LIT
                => (S.NumConst LIT)
        | "=" ID
                => (S.NamedConst ID)
        ;

(***** Function specification ******)

FunctionSpec
        : RequiredProps? ValidProp* Categories
                => (Categories)
        ;

RequiredProps
        : "required-props:" ID* EOL             => ()
        ;

ValidProp
        : Id ":" ValidPropValues EOL       => ()
        ;

Id      : ID            => (ID)
        | "array"       => (N.arrayAtom)
        | "in"          => (N.inAtom)
        | "mask"        => (N.maskAtom)
        | "out"         => (N.outAtom)
        | "param"       => (N.paramAtom)
        | "value"       => (N.valueAtom)
        ;

ValidPropValues
        : "*"                           => ()
        | ValidPropValue+               => ()
        ;

ValidPropValue
        : ID    => (S.NamedValue ID)
        | LIT   => (S.LitValue LIT)
        ;

Categories
        : (* empty *)
                => ([])
        | "newcategory:" ID EOL Categories
                => (Categories)
        | FunctionDecl Categories
                => (FunctionDecl :: Categories)
        ;

(* Specification of an OpenGL function *)
FunctionDecl
        : ID "(" IdList ")" EOL ReturnType ParamsAndProps
                => (let
                    val (params, props) = ParamsAndProps
                    in
                      S.Fun{
                          span = FULL_SPAN,
                          name = ID,
                          returnTy = ReturnType,
                          params = params,
                          props = props
                        }
                    end)
        ;

IdList
        : (* empty *)
                => ([])
        | Id ("," Id)*
                => (Id::SR)
        ;

ReturnType
        : INDENT "return" ID EOL        => (ID)
        ;

ParamsAndProps
        : ParamDecl* FunctionProp*
                => (ParamDecl, FunctionProp)
        ;

ParamDecl
        : INDENT "param" Id ID Direction TransferType Length? ID* EOL
                => (S.Param{
                      span = FULL_SPAN,
                      name = Id,
                      tyName = ID1,
                      dir = Direction,
                      xferTy = TransferType
                    })
        ;

Length
        : "[" "]"
        | "[" IndexExpr ("," IndexExpr)* "]"    => ()
        ;

IndexExpr
        : Term (("+" | "-") Term)*              => ()
        ;

Term
        : Factor (("*" | "/") Factor)*          => ()
        ;

Factor
        : COMPSIZE                              => ()
        | "(" IndexExpr ")"                     => ()
        | LIT                                   => ()
        | ID                                    => ()
        ;

Direction
        : "in"                  => (S.IN)
        | "out"                 => (S.OUT)
        | "in/out"              => (S.INOUT)
        ;

TransferType
        : "array"               => (S.ARRAY)
        | "reference"           => (S.REF)
        | "value"               => (S.VALUE)
        ;

FunctionProp
        : INDENT ID EOL
                => (S.PropName(ID_SPAN, ID))
        | INDENT ID "?" EOL
                => (S.PropUnknown(ID_SPAN, ID))
        | INDENT ID ValidPropValue PropValues EOL
                => (S.PropNameValue(FULL_SPAN, ID, PropValues ValidPropValue))
        ;

PropValues
        : ValidPropValue*
                => (fn pv => pv :: ValidPropValue)
        | ("|" ValidPropValue)+
                => (fn pv => [S.OrValue(pv::SR)])
        ;

(***** Typemap specification ******)

TypemapSpec
        : Typemap*
                => (Typemap)
        ;

Typemap
        : INDENT? UnmappedType "," MappedType@((fn (a, _, _) => a) UnmappedType) ","? EOL
                => (S.TyMap{span = FULL_SPAN, src = UnmappedType, dst = MappedType})
        ;

UnmappedType
        : ID "," TMDirection "," TMTransferTy
                => (ID, TMDirection, TMTransferTy)
        ;

MappedType(srcTy)
        : TMType@(srcTy) "," TMDirection "," TMTransferTy
                => (TMType, TMDirection, TMTransferTy)
        ;

TMDirection
        : Direction     => (SOME Direction)
        | "*"           => (NONE)
        ;

TMTransferTy
        : TransferType  => (SOME TransferType)
        | "*"           => (NONE)
        ;

TMType(srcTy)
        : "*"           => (S.NamedTy srcTy)
        | Type          => (Type)
        ;

Type
        : "const" Type
                => (Type)               (* FIXME: should we remember the const? *)
        | "struct" ID PtrType
                => (PtrType(S.StructTy ID))
        | ID PtrType
                => (PtrType(S.NamedTy ID))
        ;

PtrType
        : (* empty *)
                => (fn ty => ty)
        | "const"
                => (fn ty => ty)        (* FIXME: should we remember the const? *)
        | "*" PtrType
                => (fn ty => S.PtrTy ty)
        ;

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0