labltk-compiler.opp.exp 4.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
%{

open Tables

%}
%start entry
%token WIDGET
%token VARIANT
%token UNSAFE
%token TYSTRING
%token TYPE
%token TYINT
%token TYFLOAT
%token TYCHAR
%token TYBOOL
%token SUBTYPE
%token <string> STRING
%token SLASH
%token SEQUENCE
%token SEMICOLON
%token RPAREN
%token RBRACKET
%token RBRACE
%token QUESTION
%token OPTION
%token MODULE
%token LPAREN
%token LIST
%token LBRACKET
%token LBRACE
%token <string> IDENT
%token FUNCTION
%token EXTERNAL
%token EOF
%token COMMA
%token COLON
%token AS

%type <unit> entry
%%

TypeName:
| IDENT
    { String.uncapitalize $1 }
| WIDGET
    { "widget" }

Type0:
| TYINT
    { Int }
| TYFLOAT
    { Float }
| TYBOOL
    { Bool }
| TYCHAR
    { Char }
| TYSTRING
    { String }
| TypeName
    { UserDefined $1 }

Type0_5:
| Type0 SLASH Type0
    { if !Flags.camltk then $1 else $3 }
| Type0
    { $1 }

Type1:
| Type0_5
    { $1 }
| TypeName LPAREN IDENT RPAREN
    { Subtype ($1, $3) }
| WIDGET LPAREN IDENT RPAREN
    { Subtype ("widget", $3) }
| OPTION LPAREN IDENT RPAREN
    { Subtype ("options", $3) }
| Type1 AS STRING
    { As ($1, $3) }
| LBRACE Type_list RBRACE
    { Product $2 }

Type2:
| Type1
    { $1 }
| Type2 LIST
    { List $1 }

Labeled_type2:
| Type2
    { "", $1 }
| IDENT COLON Type2
    { $1, $3 }

Type_list:
| Type2 COMMA Type_list
    { $1 :: $3 }
| Type2
    { [$1] }

Type_record:
| Labeled_type2 COMMA Type_record
    { $1 :: $3 }
| Labeled_type2
    { [$1] }

FType:
| LPAREN RPAREN
    { Unit }
| LPAREN Type2 RPAREN
    { $2 }
| LPAREN Type_record RPAREN
    { Record $2 }

Type:
| Type2
    { $1 }
| FUNCTION FType
    { Function $2 }

SimpleArg:
| STRING
    {StringArg $1}
| Type
    {TypeArg ("", $1) }

Arg:
| STRING
    {StringArg $1}
| Type
    {TypeArg ("", $1) }
| IDENT COLON Type
    {TypeArg ($1, $3)}
| QUESTION IDENT COLON LBRACKET SimpleArgList RBRACKET DefaultList
    {OptionalArgs ( $2, $5, $7 )}
| QUESTION WIDGET COLON LBRACKET SimpleArgList RBRACKET DefaultList
    {OptionalArgs ( "widget", $5, $7 )}
| QUESTION IDENT COLON LBRACKET SimpleArgList RBRACKET
    {OptionalArgs ( $2, $5, [] )}
| QUESTION WIDGET COLON LBRACKET SimpleArgList RBRACKET
    {OptionalArgs ( "widget", $5, [] )}
| WIDGET COLON Type
    {TypeArg ("widget", $3)}
| Template
    { $1 }

SimpleArgList:
| SimpleArg SEMICOLON SimpleArgList
    { $1 :: $3}
| SimpleArg
    { [$1] }

ArgList:
| Arg SEMICOLON ArgList
    { $1 :: $3}
| Arg
    { [$1] }

DefaultList:
| LBRACKET LBRACE ArgList RBRACE RBRACKET
    {$3}

Template:
| LBRACKET ArgList RBRACKET
    { ListArg $2 }

Constructor:
| IDENT Template
    {{ component = Constructor; 
         ml_name = $1;
         var_name = getvarname $1 $2;
         template = $2;
         result = Unit;
         safe = true }}
| IDENT LPAREN IDENT RPAREN Template
    {{ component = Constructor; 
         ml_name = $1;
         var_name = $3;
         template = $5;
         result = Unit;
         safe = true }}

AbbrevConstructor:
| Constructor
    { Full $1 }
| IDENT
    { Abbrev $1 }

Constructors:
| Constructor Constructors
    { $1 :: $2 }
| Constructor
    { [$1] }

AbbrevConstructors:
| AbbrevConstructor AbbrevConstructors
    { $1 :: $2 }
| AbbrevConstructor
    { [$1] }

Safe:
| 
    { true }
| UNSAFE
    { false }

Command:
| Safe FUNCTION FType IDENT Template
    {{component = Command; ml_name = $4; var_name = "";
       template = $5; result = $3; safe = $1 }}

External:
| Safe EXTERNAL IDENT STRING
    {{component = External; ml_name = $3; var_name = "";
       template = StringArg $4; result = Unit; safe = $1}}

Option:
| OPTION IDENT Template
    {{component = Constructor; ml_name = $2; var_name = getvarname $2 $3;
       template = $3; result = Unit; safe = true }}
| OPTION IDENT LPAREN IDENT RPAREN Template
    {{component = Constructor; ml_name = $2; var_name = $4;
       template = $6; result = Unit; safe = true }}
| OPTION IDENT
    { retrieve_option $2 }

WidgetComponents:
| 
    { [] }
| Command WidgetComponents
    { $1 :: $2 }
| Option WidgetComponents
    { $1 :: $2 }
| External WidgetComponents
    { $1 :: $2 }

ModuleComponents:
| 
    { [] }
| Command ModuleComponents
    { $1 :: $2 }
| External ModuleComponents
    { $1 :: $2 }

ParserArity:
| 
    { OneToken }
| SEQUENCE
    { MultipleToken }

entry:
| TYPE ParserArity TypeName LBRACE Constructors RBRACE
    { enter_type $3 $2 $5 }
| VARIANT TYPE ParserArity TypeName LBRACE Constructors RBRACE
    { enter_type $4 $3 $6 ~variant: true }
| TYPE ParserArity TypeName EXTERNAL
    { enter_external_type $3 $2 }
| SUBTYPE ParserArity OPTION LPAREN IDENT RPAREN LBRACE AbbrevConstructors RBRACE
    { enter_subtype "options" $2 $5 $8 }
| SUBTYPE ParserArity TypeName LPAREN IDENT RPAREN LBRACE AbbrevConstructors RBRACE
    { enter_subtype $3 $2 $5 $8 }
| Command
    { enter_function $1 }
| WIDGET IDENT LBRACE WidgetComponents RBRACE
    { enter_widget $2 $4 }
| MODULE IDENT LBRACE ModuleComponents RBRACE
    { enter_module (String.uncapitalize $2) $4 }
| EOF
    { raise End_of_file }

%%