1 (* camlp4r pa_extend.cmo q_MLast.cmo *)
2 (***********************************************************************)
6 (* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
8 (* Copyright 2002 Institut National de Recherche en Informatique et *)
9 (* Automatique. Distributed only by permission. *)
11 (***********************************************************************)
13 (* $Id: pa_o.ml,v 1.58.2.1 2004/08/18 11:17:37 mauny Exp $ *)
18 Pcaml.syntax_name.val := "OCaml";
19 Pcaml.no_constructors_arity.val := True;
21 (* ------------------------------------------------------------------------- *)
22 (* Hacked version of the lexer. *)
23 (* ------------------------------------------------------------------------- *)
27 value jrh_lexer = ref False;
29 value no_quotations = ref False;
31 (* The string buffering machinery *)
33 value buff = ref (String.create 80);
36 if len >= String.length buff.val then
37 buff.val := buff.val ^ String.create (String.length buff.val)
44 add_rec len 0 where rec add_rec len i =
45 if i == String.length s then len else add_rec (store len s.[i]) (succ i)
47 value get_buff len = String.sub buff.val 0 len;
51 value stream_peek_nth n strm =
52 loop n (Stream.npeek n strm) where rec loop n =
55 | [x] -> if n == 1 then Some x else None
56 | [_ :: l] -> loop (n - 1) l ]
61 [ [: `('A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
62 '\248'..'\255' | '0'..'9' | '_' | ''' as
70 [ [: `('!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
71 '%' | '.' | ':' | '<' | '>' | '|' | '$' as
75 ident2 (store len c) s
79 [ [: `('0'..'9' | 'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
80 '\248'..'\255' | '_' | '!' | '%' | '&' | '*' | '+' | '-' | '.' |
81 '/' | ':' | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' | ''' |
86 ident3 (store len c) s
90 [ [: `'o' | 'O'; s :] -> digits octal (store len 'o') s
91 | [: `'x' | 'X'; s :] -> digits hexa (store len 'x') s
92 | [: `'b' | 'B'; s :] -> digits binary (store len 'b') s
93 | [: a = number len :] -> a ]
96 [ [: d = kind; s :] -> digits_under kind (store len d) s
97 | [: :] -> raise (Stream.Error "ill-formed integer constant") ]
98 and digits_under kind len =
100 [ [: d = kind; s :] -> digits_under kind (store len d) s
101 | [: `'_'; s :] -> digits_under kind len s
102 | [: `'l' :] -> ("INT32", get_buff len)
103 | [: `'L' :] -> ("INT64", get_buff len)
104 | [: `'n' :] -> ("NATIVEINT", get_buff len)
105 | [: :] -> ("INT", get_buff len) ]
106 and octal = parser [ [: `('0'..'7' as d) :] -> d ]
107 and hexa = parser [ [: `('0'..'9' | 'a'..'f' | 'A'..'F' as d) :] -> d ]
108 and binary = parser [ [: `('0'..'1' as d) :] -> d ]
111 [ [: `('0'..'9' as c); s :] -> number (store len c) s
112 | [: `'_'; s :] -> number len s
113 | [: `'.'; s :] -> decimal_part (store len '.') s
114 | [: `'e' | 'E'; s :] -> exponent_part (store len 'E') s
115 | [: `'l' :] -> ("INT32", get_buff len)
116 | [: `'L' :] -> ("INT64", get_buff len)
117 | [: `'n' :] -> ("NATIVEINT", get_buff len)
118 | [: :] -> ("INT", get_buff len) ]
119 and decimal_part len =
121 [ [: `('0'..'9' as c); s :] -> decimal_part (store len c) s
122 | [: `'_'; s :] -> decimal_part len s
123 | [: `'e' | 'E'; s :] -> exponent_part (store len 'E') s
124 | [: :] -> ("FLOAT", get_buff len) ]
125 and exponent_part len =
127 [ [: `('+' | '-' as c); s :] -> end_exponent_part (store len c) s
128 | [: a = end_exponent_part len :] -> a ]
129 and end_exponent_part len =
131 [ [: `('0'..'9' as c); s :] -> end_exponent_part_under (store len c) s
132 | [: :] -> raise (Stream.Error "ill-formed floating-point constant") ]
133 and end_exponent_part_under len =
135 [ [: `('0'..'9' as c); s :] -> end_exponent_part_under (store len c) s
136 | [: `'_'; s :] -> end_exponent_part_under len s
137 | [: :] -> ("FLOAT", get_buff len) ]
140 value error_on_unknown_keywords = ref False;
141 value err loc msg = raise_with_loc loc (Token.Error msg);
143 (* ------------------------------------------------------------------------- *)
144 (* JRH's hack to make the case distinction "unmixed" versus "mixed" *)
145 (* ------------------------------------------------------------------------- *)
147 value is_uppercase s = String.uppercase s = s;
148 value is_only_lowercase s = String.lowercase s = s && not(is_uppercase s);
150 value jrh_identifier find_kwd id =
151 let jflag = jrh_lexer.val in
152 if id = "set_jrh_lexer" then
153 (let _ = jrh_lexer.val := True in ("",find_kwd "true"))
154 else if id = "unset_jrh_lexer" then
155 (let _ = jrh_lexer.val := False in ("",find_kwd "false"))
157 try ("", find_kwd id) with
160 if is_uppercase (String.sub id 0 1) then ("UIDENT", id)
162 else if is_uppercase (String.sub id 0 1) &&
163 is_only_lowercase (String.sub id 1 (String.length id - 1))
164 (***** Carl's alternative version
165 then ("UIDENT", id) else if is_uppercase (String.sub id 0 1) then ("LIDENT", "__uc_"^id) else ("LIDENT", id)];
167 then ("UIDENT", id) else ("LIDENT", id)];
169 (* ------------------------------------------------------------------------- *)
170 (* Back to original file with the mod of using the above. *)
171 (* ------------------------------------------------------------------------- *)
173 (* Debugging positions and locations *)
174 value eprint_pos msg p =
175 Printf.eprintf "%s: fname=%s; lnum=%d; bol=%d; cnum=%d\n%!"
176 msg p.Lexing.pos_fname p.Lexing.pos_lnum p.Lexing.pos_bol p.Lexing.pos_cnum
179 value eprint_loc (bp, ep) =
180 do { eprint_pos "P1" bp; eprint_pos "P2" ep }
183 value check_location msg ((bp, ep) as loc) =
185 if (bp.Lexing.pos_lnum > ep.Lexing.pos_lnum ||
186 bp.Lexing.pos_bol > ep.Lexing.pos_bol ||
187 bp.Lexing.pos_cnum > ep.Lexing.pos_cnum ||
188 bp.Lexing.pos_lnum < 0 || ep.Lexing.pos_lnum < 0 ||
189 bp.Lexing.pos_bol < 0 || ep.Lexing.pos_bol < 0 ||
190 bp.Lexing.pos_cnum < 0 || ep.Lexing.pos_cnum < 0)
191 (* Here, we don't check
192 bp.Lexing.pos_cnum < bp.Lexing.pos_bol || ep.Lexing.pos_cnum < bp.Lexing.pos_bol
193 since the lexer is called on antiquotations, with cnum=0, but lnum and bolpos
194 have "correct" values *)
197 Printf.eprintf "*** Warning: (%s) strange positions ***\n" msg;
206 value next_token_fun dfa ssd find_kwd fname lnum bolpos glexr =
208 {Lexing.pos_fname = fname.val; Lexing.pos_lnum = lnum.val;
209 Lexing.pos_bol = bolpos.val; Lexing.pos_cnum = p} in
210 let mkloc (bp, ep) = (make_pos bp, make_pos ep) in
211 let keyword_or_error (bp,ep) s =
212 let loc = mkloc (bp, ep) in
213 try (("", find_kwd s), loc) with
215 if error_on_unknown_keywords.val then err loc ("illegal token: " ^ s)
216 else (("", s), loc) ] in
217 let error_if_keyword ( ((_,id) as a), bep) =
218 let loc = mkloc bep in
221 err loc ("illegal use of a keyword as a label: " ^ id) }
222 with [ Not_found -> (a, loc) ]
224 let rec next_token after_space =
226 [ [: `'\010'; s :] ep ->
227 do { bolpos.val := ep; incr lnum; next_token True s }
228 | [: `'\013'; s :] ep ->
230 match Stream.peek s with
231 [ Some '\010' -> do { Stream.junk s; ep+1 }
233 do { bolpos.val := ep; incr lnum; next_token True s }
234 | [: `' ' | '\t' | '\026' | '\012'; s :] -> next_token True s
235 | [: `'#' when bp = bolpos.val; s :] ->
236 if linedir 1 s then do { line_directive s; next_token True s }
237 else keyword_or_error (bp, bp + 1) "#"
238 | [: `'('; s :] -> left_paren bp s
239 | [: `('A'..'Z' | '\192'..'\214' | '\216'..'\222' as c); s :] ->
240 let id = get_buff (ident (store 0 c) s) in
241 let loc = mkloc (bp, (Stream.count s)) in
242 (jrh_identifier find_kwd id, loc)
245 (try ("", find_kwd id) with [ Not_found -> ("UIDENT", id) ], loc)
249 | [: `('a'..'z' | '\223'..'\246' | '\248'..'\255' | '_' as c); s :] ->
250 let id = get_buff (ident (store 0 c) s) in
251 let loc = mkloc (bp, (Stream.count s)) in
252 (jrh_identifier find_kwd id, loc)
255 (try ("", find_kwd id) with [ Not_found -> ("LIDENT", id) ], loc)
258 | [: `('1'..'9' as c); s :] ->
259 let tok = number (store 0 c) s in
260 let loc = mkloc (bp, (Stream.count s)) in
263 let tok = base_number (store 0 '0') s in
264 let loc = mkloc (bp, (Stream.count s)) in
267 match Stream.npeek 2 s with
268 [ [_; '''] | ['\\'; _] ->
269 let tok = ("CHAR", get_buff (char bp 0 s)) in
270 let loc = mkloc (bp, (Stream.count s)) in
272 | _ -> keyword_or_error (bp, Stream.count s) "'" ]
274 let tok = ("STRING", get_buff (string bp 0 s)) in
275 let loc = mkloc (bp, Stream.count s) in
278 let tok = ("QUOTATION", "tot:"^(qstring bp 0 s)) in
279 let loc = mkloc (bp, Stream.count s) in
282 let tok = dollar bp 0 s in
283 let loc = mkloc (bp, Stream.count s) in
285 | [: `('!' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' | '%' as c);
287 let id = get_buff (ident2 (store 0 c) s) in
288 keyword_or_error (bp, Stream.count s) id
292 [ [: `('a'..'z' as c); len = ident (store 0 c); s :] ep ->
293 let id = get_buff len in
295 [ [: `':' :] eb -> error_if_keyword (("LABEL", id), (bp, ep))
296 | [: :] -> error_if_keyword (("TILDEIDENT", id), (bp, ep)) ]
298 let id = get_buff (ident2 (store 0 c) s) in
299 keyword_or_error (bp, Stream.count s) id ] :] ->
305 [ [: `('a'..'z' as c); len = ident (store 0 c); s :] ep ->
306 let id = get_buff len in
308 [ [: `':' :] eb -> error_if_keyword (("OPTLABEL", id), (bp,ep))
309 | [: :] -> error_if_keyword (("QUESTIONIDENT", id), (bp, ep)) ]
311 let id = get_buff (ident2 (store 0 c) s) in
312 keyword_or_error (bp, Stream.count s) id ] :] ->
314 | [: `'<'; s :] -> less bp s
318 [ [: `(']' | ':' | '=' | '>' as c2) :] -> store (store 0 c1) c2
319 | [: :] -> store 0 c1 ] :] ep ->
320 let id = get_buff len in
321 keyword_or_error (bp, ep) id
322 | [: `('>' | '|' as c1);
325 [ [: `(']' | '}' as c2) :] -> store (store 0 c1) c2
326 | [: a = ident2 (store 0 c1) :] -> a ] :] ep ->
327 let id = get_buff len in
328 keyword_or_error (bp, ep) id
329 | [: `('[' | '{' as c1); s :] ->
331 match Stream.npeek 2 s with
332 [ ['<'; '<' | ':'] -> store 0 c1
335 [ [: `('|' | '<' | ':' as c2) :] -> store (store 0 c1) c2
336 | [: :] -> store 0 c1 ] ]
338 let ep = Stream.count s in
339 let id = get_buff len in
340 keyword_or_error (bp, ep) id
345 | [: :] -> if ssd && after_space then " ." else "." ] :] ep ->
346 keyword_or_error (bp, ep) id
351 | [: :] -> ";" ] :] ep ->
352 keyword_or_error (bp, ep) id
353 | [: `'\\'; s :] ep -> (("LIDENT", get_buff (ident3 0 s)), mkloc (bp, ep))
354 | [: `c :] ep -> keyword_or_error (bp, ep) (String.make 1 c)
355 | [: _ = Stream.empty :] -> (("EOI", ""), mkloc (bp, succ bp)) ]
357 if no_quotations.val then
358 match strm with parser
359 [ [: len = ident2 (store 0 '<') :] ep ->
360 let id = get_buff len in
361 keyword_or_error (bp, ep) id ]
363 match strm with parser
364 [ [: `'<'; len = quotation bp 0 :] ep ->
365 (("QUOTATION", ":" ^ get_buff len), mkloc (bp, ep))
366 | [: `':'; i = parser [: len = ident 0 :] -> get_buff len;
367 `'<' ? "character '<' expected"; len = quotation bp 0 :] ep ->
368 (("QUOTATION", i ^ ":" ^ get_buff len), mkloc (bp, ep))
369 | [: len = ident2 (store 0 '<') :] ep ->
370 let id = get_buff len in
371 keyword_or_error (bp, ep) id ]
375 | [: `'\\'; `c; s :] ep -> string bp (store (store len '\\') c) s
376 | [: `'\010'; s :] ep -> do { bolpos.val := ep; incr lnum; string bp (store len '\010') s }
377 | [: `'\013'; s :] ep ->
379 match Stream.peek s with
380 [ Some '\010' -> do { Stream.junk s; (store (store len '\013') '\010', ep+1) }
381 | _ -> (store len '\013', ep) ] in
382 do { bolpos.val := ep; incr lnum; string bp len s }
383 | [: `c; s :] -> string bp (store len c) s
384 | [: :] ep -> err (mkloc (bp, ep)) "string not terminated" ]
387 [ [: `'`' :] -> get_buff len
388 | [: `c; s :] -> qstring bp (store len c) s
389 | [: :] ep -> err (mkloc (bp, ep)) "quotation not terminated" ]
392 [ [: `'''; s :] -> if len = 0 then char bp (store len ''') s else len
393 | [: `'\\'; `c; s :] -> char bp (store (store len '\\') c) s
394 | [: `'\010'; s :] -> do {bolpos.val := bp+1; incr lnum; char bp (store len '\010') s}
395 | [: `'\013'; s :] ->
397 match Stream.peek s with
398 [ Some '\010' -> do { Stream.junk s; bp+2 }
400 do { bolpos.val := bol; incr lnum; char bp (store len '\013') s}
401 | [: `c; s :] -> char bp (store len c) s
402 | [: :] ep -> err (mkloc (bp, ep)) "char not terminated" ]
405 [ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
406 | [: `('a'..'z' | 'A'..'Z' as c); s :] -> antiquot bp (store len c) s
407 | [: `('0'..'9' as c); s :] -> maybe_locate bp (store len c) s
409 let k = get_buff len in
410 ("ANTIQUOT", k ^ ":" ^ locate_or_antiquot_rest bp 0 s)
411 | [: `'\\'; `c; s :] ->
412 ("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
417 ("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
418 | [: :] ep -> err (mkloc (bp, ep)) "antiquotation not terminated" ]
419 else ("", get_buff (ident2 (store 0 '$') s)) ]
420 and maybe_locate bp len =
422 [ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
423 | [: `('0'..'9' as c); s :] -> maybe_locate bp (store len c) s
425 ("LOCATE", get_buff len ^ ":" ^ locate_or_antiquot_rest bp 0 s)
426 | [: `'\\'; `c; s :] ->
427 ("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
429 ("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
430 | [: :] ep -> err (mkloc (bp, ep)) "antiquotation not terminated" ]
431 and antiquot bp len =
433 [ [: `'$' :] -> ("ANTIQUOT", ":" ^ get_buff len)
434 | [: `('a'..'z' | 'A'..'Z' | '0'..'9' as c); s :] ->
435 antiquot bp (store len c) s
437 let k = get_buff len in
438 ("ANTIQUOT", k ^ ":" ^ locate_or_antiquot_rest bp 0 s)
439 | [: `'\\'; `c; s :] ->
440 ("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
442 ("ANTIQUOT", ":" ^ locate_or_antiquot_rest bp (store len c) s)
443 | [: :] ep -> err (mkloc (bp, ep)) "antiquotation not terminated" ]
444 and locate_or_antiquot_rest bp len =
446 [ [: `'$' :] -> get_buff len
447 | [: `'\\'; `c; s :] -> locate_or_antiquot_rest bp (store len c) s
448 | [: `c; s :] -> locate_or_antiquot_rest bp (store len c) s
449 | [: :] ep -> err (mkloc (bp, ep)) "antiquotation not terminated" ]
450 and quotation bp len =
452 [ [: `'>'; s :] -> maybe_end_quotation bp len s
454 quotation bp (maybe_nested_quotation bp (store len '<') s) s
458 [ [: `('>' | '<' | '\\' as c) :] -> store len c
459 | [: :] -> store len '\\' ];
462 | [: `'\010'; s :] -> do {bolpos.val := bp+1; incr lnum; quotation bp (store len '\010') s}
463 | [: `'\013'; s :] ->
465 match Stream.peek s with
466 [ Some '\010' -> do { Stream.junk s; bp+2 }
468 do { bolpos.val := bol; incr lnum; quotation bp (store len '\013') s}
469 | [: `c; s :] -> quotation bp (store len c) s
470 | [: :] ep -> err (mkloc (bp, ep)) "quotation not terminated" ]
471 and maybe_nested_quotation bp len =
473 [ [: `'<'; s :] -> mstore (quotation bp (store len '<') s) ">>"
474 | [: `':'; len = ident (store len ':');
477 [ [: `'<'; s :] -> mstore (quotation bp (store len '<') s) ">>"
478 | [: :] -> len ] :] ->
481 and maybe_end_quotation bp len =
484 | [: a = quotation bp (store len '>') :] -> a ]
487 [ [: `'*'; _ = comment bp; a = next_token True :] -> a
488 | [: :] ep -> keyword_or_error (bp, ep) "(" ]
491 [ [: `'('; s :] -> left_paren_in_comment bp s
492 | [: `'*'; s :] -> star_in_comment bp s
493 | [: `'"'; _ = string bp 0; s :] -> comment bp s
494 | [: `'''; s :] -> quote_in_comment bp s
495 | [: `'\010'; s :] ep -> do { bolpos.val := ep; incr lnum; comment bp s }
496 | [: `'\013'; s :] ep ->
498 match Stream.peek s with
499 [ Some '\010' -> do { Stream.junk s; ep+1 }
501 do { bolpos.val := ep; incr lnum; comment bp s }
502 | [: `c; s :] -> comment bp s
503 | [: :] ep -> err (mkloc (bp, ep)) "comment not terminated" ]
504 and quote_in_comment bp =
506 [ [: `'''; s :] -> comment bp s
507 | [: `'\\'; s :] -> quote_antislash_in_comment bp 0 s
510 match Stream.npeek 2 s with
511 [ [ ( '\013' | '\010' ); '''] ->
512 do { bolpos.val := bp + 1; incr lnum;
513 Stream.junk s; Stream.junk s }
514 | [ '\013'; '\010' ] ->
515 match Stream.npeek 3 s with
516 [ [_; _; '''] -> do { bolpos.val := bp + 2; incr lnum;
517 Stream.junk s; Stream.junk s; Stream.junk s }
519 | [_; '''] -> do { Stream.junk s; Stream.junk s }
523 and quote_any_in_comment bp =
525 [ [: `'''; s :] -> comment bp s
526 | [: a = comment bp :] -> a ]
527 and quote_antislash_in_comment bp len =
529 [ [: `'''; s :] -> comment bp s
530 | [: `'\\' | '"' | 'n' | 't' | 'b' | 'r'; s :] ->
531 quote_any_in_comment bp s
532 | [: `'0'..'9'; s :] -> quote_antislash_digit_in_comment bp s
533 | [: a = comment bp :] -> a ]
534 and quote_antislash_digit_in_comment bp =
536 [ [: `'0'..'9'; s :] -> quote_antislash_digit2_in_comment bp s
537 | [: a = comment bp :] -> a ]
538 and quote_antislash_digit2_in_comment bp =
540 [ [: `'0'..'9'; s :] -> quote_any_in_comment bp s
541 | [: a = comment bp :] -> a ]
542 and left_paren_in_comment bp =
544 [ [: `'*'; s :] -> do { comment bp s; comment bp s }
545 | [: a = comment bp :] -> a ]
546 and star_in_comment bp =
549 | [: a = comment bp :] -> a ]
551 match stream_peek_nth n s with
552 [ Some (' ' | '\t') -> linedir (n + 1) s
553 | Some ('0'..'9') -> True
557 [ [: `'\010'; s :] ep ->
558 do { bolpos.val := ep; incr lnum }
559 | [: `'\013'; s :] ep ->
561 match Stream.peek s with
562 [ Some '\010' -> do { Stream.junk s; ep+1 }
564 do { bolpos.val := ep; incr lnum }
565 | [: `_; s :] -> any_to_nl s
567 and line_directive = parser (* we are sure that there is a line directive here *)
568 [ [: _ = skip_spaces; n = line_directive_number 0;
569 _ = skip_spaces; _ = line_directive_string;
571 -> do { bolpos.val := ep; lnum.val := n }
573 and skip_spaces = parser
574 [ [: `' ' | '\t'; s :] -> skip_spaces s
576 and line_directive_number n = parser
577 [ [: `('0'..'9' as c) ; s :]
578 -> line_directive_number (10*n + (Char.code c - Char.code '0')) s
580 and line_directive_string = parser
581 [ [: ` '"' ; _ = line_directive_string_contents 0 :] -> ()
584 and line_directive_string_contents len = parser
585 [ [: ` '\010' | '\013' :] -> ()
586 | [: ` '"' :] -> fname.val := get_buff len
587 | [: `c; s :] -> line_directive_string_contents (store len c) s
592 let glex = glexr.val in
593 let comm_bp = Stream.count cstrm in
594 let r = next_token False cstrm in
596 match glex.tok_comm with
598 let next_bp = (fst (snd r)).Lexing.pos_cnum in
599 if next_bp > comm_bp then
600 let comm_loc = mkloc (comm_bp, next_bp) in
601 glex.tok_comm := Some [comm_loc :: list]
607 [ Stream.Error str ->
608 err (mkloc (Stream.count cstrm, Stream.count cstrm + 1)) str ]
612 value dollar_for_antiquotation = ref True;
613 value specific_space_dot = ref False;
615 value func kwd_table glexr =
616 let bolpos = ref 0 in
618 let fname = ref "" in
619 let find = Hashtbl.find kwd_table in
620 let dfa = dollar_for_antiquotation.val in
621 let ssd = specific_space_dot.val in
622 Token.lexer_func_of_parser (next_token_fun dfa ssd find fname lnum bolpos glexr)
625 value rec check_keyword_stream =
626 parser [: _ = check; _ = Stream.empty :] -> True
629 [ [: `'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' | '\248'..'\255'
633 | [: `'!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
639 match Stream.npeek 1 s with
641 | _ -> check_ident2 s ]
645 [ [: `']' | ':' | '=' | '>' :] -> ()
646 | [: :] -> () ] :] ep ->
651 [ [: `']' | '}' :] -> ()
652 | [: a = check_ident2 :] -> a ] :] ->
654 | [: `'[' | '{'; s :] ->
655 match Stream.npeek 2 s with
656 [ ['<'; '<' | ':'] -> ()
659 [ [: `'|' | '<' | ':' :] -> ()
665 | [: :] -> () ] :] ->
670 [ [: `'A'..'Z' | 'a'..'z' | '\192'..'\214' | '\216'..'\246' |
671 '\248'..'\255' | '0'..'9' | '_' | '''
678 [ [: `'!' | '?' | '~' | '=' | '@' | '^' | '&' | '+' | '-' | '*' | '/' |
679 '%' | '.' | ':' | '<' | '>' | '|'
686 value check_keyword s =
687 try check_keyword_stream (Stream.of_string s) with _ -> False
690 value error_no_respect_rules p_con p_prm =
694 (if p_con = "" then "\"" ^ p_prm ^ "\""
695 else if p_prm = "" then p_con
696 else p_con ^ " \"" ^ p_prm ^ "\"") ^
697 " does not respect Plexer rules"))
700 value error_ident_and_keyword p_con p_prm =
703 ("the token \"" ^ p_prm ^ "\" is used as " ^ p_con ^
707 value using_token kwd_table ident_table (p_con, p_prm) =
710 if not (Hashtbl.mem kwd_table p_prm) then
711 if check_keyword p_prm then
712 if Hashtbl.mem ident_table p_prm then
713 error_ident_and_keyword (Hashtbl.find ident_table p_prm) p_prm
714 else Hashtbl.add kwd_table p_prm p_prm
715 else error_no_respect_rules p_con p_prm
718 if p_prm = "" then ()
721 [ 'A'..'Z' -> error_no_respect_rules p_con p_prm
723 if Hashtbl.mem kwd_table p_prm then
724 error_ident_and_keyword p_con p_prm
725 else Hashtbl.add ident_table p_prm p_con ]
727 if p_prm = "" then ()
730 [ 'a'..'z' -> error_no_respect_rules p_con p_prm
732 if Hashtbl.mem kwd_table p_prm then
733 error_ident_and_keyword p_con p_prm
734 else Hashtbl.add ident_table p_prm p_con ]
735 | "INT" | "INT32" | "INT64" | "NATIVEINT"
736 | "FLOAT" | "CHAR" | "STRING"
737 | "TILDEIDENT" | "QUESTIONIDENT" | "LABEL" | "OPTLABEL"
738 | "QUOTATION" | "ANTIQUOT" | "LOCATE" | "EOI" ->
743 ("the constructor \"" ^ p_con ^
744 "\" is not recognized by Plexer")) ]
747 value removing_token kwd_table ident_table (p_con, p_prm) =
749 [ "" -> Hashtbl.remove kwd_table p_prm
750 | "LIDENT" | "UIDENT" ->
751 if p_prm <> "" then Hashtbl.remove ident_table p_prm else ()
757 [ ("", t) -> "'" ^ t ^ "'"
758 | ("LIDENT", "") -> "lowercase identifier"
759 | ("LIDENT", t) -> "'" ^ t ^ "'"
760 | ("UIDENT", "") -> "uppercase identifier"
761 | ("UIDENT", t) -> "'" ^ t ^ "'"
762 | ("INT", "") -> "integer"
763 | ("INT32", "") -> "32 bits integer"
764 | ("INT64", "") -> "64 bits integer"
765 | ("NATIVEINT", "") -> "native integer"
766 | (("INT" | "INT32" | "NATIVEINT"), s) -> "'" ^ s ^ "'"
767 | ("FLOAT", "") -> "float"
768 | ("STRING", "") -> "string"
769 | ("CHAR", "") -> "char"
770 | ("QUOTATION", "") -> "quotation"
771 | ("ANTIQUOT", k) -> "antiquot \"" ^ k ^ "\""
772 | ("LOCATE", "") -> "locate"
773 | ("EOI", "") -> "end of input"
775 | (con, prm) -> con ^ " \"" ^ prm ^ "\"" ]
778 value eq_before_colon p e =
779 loop 0 where rec loop i =
780 if i == String.length e then
781 failwith "Internal error in Plexer: incorrect ANTIQUOT"
782 else if i == String.length p then e.[i] == ':'
783 else if p.[i] == e.[i] then loop (i + 1)
787 value after_colon e =
789 let i = String.index e ':' in
790 String.sub e (i + 1) (String.length e - i - 1)
797 [ ("ANTIQUOT", p_prm) ->
799 [ ("ANTIQUOT", prm) when eq_before_colon p_prm prm -> after_colon prm
800 | _ -> raise Stream.Failure ]
801 | tok -> Token.default_match tok ]
805 let kwd_table = Hashtbl.create 301 in
806 let id_table = Hashtbl.create 301 in
809 {tok_func = fun []; tok_using = fun []; tok_removing = fun [];
810 tok_match = fun []; tok_text = fun []; tok_comm = None}
813 {tok_func = func kwd_table glexr;
814 tok_using = using_token kwd_table id_table;
815 tok_removing = removing_token kwd_table id_table; tok_match = tok_match;
816 tok_text = text; tok_comm = None}
818 do { glexr.val := glex; glex }
823 [ ("ANTIQUOT", p_prm) ->
826 [: `("ANTIQUOT", prm) when eq_before_colon p_prm prm :] ->
834 let kwd_table = Hashtbl.create 301 in
835 let id_table = Hashtbl.create 301 in
838 {tok_func = fun []; tok_using = fun []; tok_removing = fun [];
839 tok_match = fun []; tok_text = fun []; tok_comm = None}
841 {func = func kwd_table glexr; using = using_token kwd_table id_table;
842 removing = removing_token kwd_table id_table; tparse = tparse; text = text}
845 (* ------------------------------------------------------------------------- *)
846 (* Resume the main file. *)
847 (* ------------------------------------------------------------------------- *)
850 let odfa = dollar_for_antiquotation.val in
851 dollar_for_antiquotation.val := False;
852 Grammar.Unsafe.gram_reinit gram (gmake ());
853 dollar_for_antiquotation.val := odfa;
854 Grammar.Unsafe.clear_entry interf;
855 Grammar.Unsafe.clear_entry implem;
856 Grammar.Unsafe.clear_entry top_phrase;
857 Grammar.Unsafe.clear_entry use_file;
858 Grammar.Unsafe.clear_entry module_type;
859 Grammar.Unsafe.clear_entry module_expr;
860 Grammar.Unsafe.clear_entry sig_item;
861 Grammar.Unsafe.clear_entry str_item;
862 Grammar.Unsafe.clear_entry expr;
863 Grammar.Unsafe.clear_entry patt;
864 Grammar.Unsafe.clear_entry ctyp;
865 Grammar.Unsafe.clear_entry let_binding;
866 Grammar.Unsafe.clear_entry type_declaration;
867 Grammar.Unsafe.clear_entry class_type;
868 Grammar.Unsafe.clear_entry class_expr;
869 Grammar.Unsafe.clear_entry class_sig_item;
870 Grammar.Unsafe.clear_entry class_str_item
873 Pcaml.parse_interf.val := Grammar.Entry.parse interf;
874 Pcaml.parse_implem.val := Grammar.Entry.parse implem;
882 value mkumin loc f arg =
884 [ ("-", <:expr< $int:n$ >>) when int_of_string n > 0 ->
887 | ("-", MLast.ExInt32 loc n) when (Int32.of_string n) > 0l ->
888 MLast.ExInt32 loc ("-" ^ n)
889 | ("-", MLast.ExInt64 loc n) when (Int64.of_string n) > 0L ->
890 MLast.ExInt64 loc ("-" ^ n)
891 | ("-", MLast.ExNativeInt loc n) when (Nativeint.of_string n) > 0n ->
892 MLast.ExNativeInt loc ("-" ^ n)
893 | (_, <:expr< $flo:n$ >>) when float_of_string n > 0.0 ->
898 <:expr< $lid:f$ $arg$ >> ]
902 value mklistexp loc last =
903 loop True where rec loop top =
908 | None -> <:expr< [] >> ]
910 let loc = if top then loc else (fst (MLast.loc_of_expr e1), snd loc) in
911 <:expr< [$e1$ :: $loop False el$] >> ]
914 value mklistpat loc last =
915 loop True where rec loop top =
920 | None -> <:patt< [] >> ]
922 let loc = if top then loc else (fst (MLast.loc_of_patt p1), snd loc) in
923 <:patt< [$p1$ :: $loop False pl$] >> ]
926 (*** JRH pulled this outside so user can add new infixes here too ***)
928 value ht = Hashtbl.create 73;
930 (*** And JRH added all the new HOL Light infixes here already ***)
933 let ct = Hashtbl.create 73 in
935 List.iter (fun x -> Hashtbl.add ht x True)
936 ["asr"; "land"; "lor"; "lsl"; "lsr"; "lxor"; "mod"; "or"; "o"; "upto";
937 "F_F"; "THENC"; "THEN"; "THENL"; "ORELSE"; "ORELSEC";
938 "THEN_TCL"; "ORELSE_TCL"];
939 List.iter (fun x -> Hashtbl.add ct x True)
940 ['!'; '&'; '*'; '+'; '-'; '/'; ':'; '<'; '='; '>'; '@'; '^'; '|'; '~';
943 try Hashtbl.find ht x with
944 [ Not_found -> try Hashtbl.find ct x.[0] with _ -> False ]
948 (*** JRH added this so parenthesised operators undergo same mapping ***)
950 value translate_operator =
954 | "THENC" -> "thenc_"
955 | "THENL" -> "thenl_"
956 | "ORELSE" -> "orelse_"
957 | "ORELSEC" -> "orelsec_"
958 | "THEN_TCL" -> "then_tcl_"
959 | "ORELSE_TCL" -> "orelse_tcl_"
963 (*** And JRH inserted it in here ***)
965 value operator_rparen =
966 Grammar.Entry.of_parser gram "operator_rparen"
968 match Stream.npeek 2 strm with
969 [ [("", s); ("", ")")] when is_operator s ->
970 do { Stream.junk strm; Stream.junk strm; translate_operator s }
971 | _ -> raise Stream.Failure ])
975 Grammar.Entry.of_parser gram "lident_colon"
977 match Stream.npeek 2 strm with
978 [ [("LIDENT", i); ("", ":")] ->
979 do { Stream.junk strm; Stream.junk strm; i }
980 | _ -> raise Stream.Failure ])
985 ['!'; '$'; '%'; '&'; '*'; '+'; '-'; '.'; '/'; ':'; '<'; '='; '>'; '?';
989 if i == String.length s then True
990 else if List.mem s.[i] list then loop s (i + 1)
997 let list = ['!'; '?'; '~'] in
998 let excl = ["!="; "??"] in
999 Grammar.Entry.of_parser gram "prefixop"
1003 not (List.mem x excl) && String.length x >= 2 &&
1004 List.mem x.[0] list && symbolchar x 1 :] ->
1009 let list = ['='; '<'; '>'; '|'; '&'; '$'] in
1010 let excl = ["<-"; "||"; "&&"] in
1011 Grammar.Entry.of_parser gram "infixop0"
1015 not (List.mem x excl) && String.length x >= 2 &&
1016 List.mem x.[0] list && symbolchar x 1 :] ->
1021 let list = ['@'; '^'] in
1022 Grammar.Entry.of_parser gram "infixop1"
1026 String.length x >= 2 && List.mem x.[0] list &&
1027 symbolchar x 1 :] ->
1032 let list = ['+'; '-'] in
1033 Grammar.Entry.of_parser gram "infixop2"
1037 x <> "->" && String.length x >= 2 && List.mem x.[0] list &&
1038 symbolchar x 1 :] ->
1043 let list = ['*'; '/'; '%'] in
1044 Grammar.Entry.of_parser gram "infixop3"
1048 String.length x >= 2 && List.mem x.[0] list &&
1049 symbolchar x 1 :] ->
1054 Grammar.Entry.of_parser gram "infixop4"
1058 String.length x >= 3 && x.[0] == '*' && x.[1] == '*' &&
1059 symbolchar x 2 :] ->
1063 value test_constr_decl =
1064 Grammar.Entry.of_parser gram "test_constr_decl"
1066 match Stream.npeek 1 strm with
1067 [ [("UIDENT", _)] ->
1068 match Stream.npeek 2 strm with
1069 [ [_; ("", ".")] -> raise Stream.Failure
1070 | [_; ("", "(")] -> raise Stream.Failure
1072 | _ -> raise Stream.Failure ]
1074 | _ -> raise Stream.Failure ])
1077 value stream_peek_nth n strm =
1078 loop n (Stream.npeek n strm) where rec loop n =
1081 | [x] -> if n == 1 then Some x else None
1082 | [_ :: l] -> loop (n - 1) l ]
1085 (* horrible hack to be able to parse class_types *)
1087 value test_ctyp_minusgreater =
1088 Grammar.Entry.of_parser gram "test_ctyp_minusgreater"
1090 let rec skip_simple_ctyp n =
1091 match stream_peek_nth n strm with
1092 [ Some ("", "->") -> n
1093 | Some ("", "[" | "[<") ->
1094 skip_simple_ctyp (ignore_upto "]" (n + 1) + 1)
1095 | Some ("", "(") -> skip_simple_ctyp (ignore_upto ")" (n + 1) + 1)
1098 "as" | "'" | ":" | "*" | "." | "#" | "<" | ">" | ".." | ";" |
1100 skip_simple_ctyp (n + 1)
1101 | Some ("QUESTIONIDENT" | "LIDENT" | "UIDENT", _) ->
1102 skip_simple_ctyp (n + 1)
1103 | Some _ | None -> raise Stream.Failure ]
1104 and ignore_upto end_kwd n =
1105 match stream_peek_nth n strm with
1106 [ Some ("", prm) when prm = end_kwd -> n
1107 | Some ("", "[" | "[<") ->
1108 ignore_upto end_kwd (ignore_upto "]" (n + 1) + 1)
1109 | Some ("", "(") -> ignore_upto end_kwd (ignore_upto ")" (n + 1) + 1)
1110 | Some _ -> ignore_upto end_kwd (n + 1)
1111 | None -> raise Stream.Failure ]
1113 match Stream.peek strm with
1114 [ Some (("", "[") | ("LIDENT" | "UIDENT", _)) -> skip_simple_ctyp 1
1115 | Some ("", "object") -> raise Stream.Failure
1119 value test_label_eq =
1120 Grammar.Entry.of_parser gram "test_label_eq"
1121 (test 1 where rec test lev strm =
1122 match stream_peek_nth lev strm with
1123 [ Some (("UIDENT", _) | ("LIDENT", _) | ("", ".")) ->
1125 | Some ("", "=") -> ()
1126 | _ -> raise Stream.Failure ])
1129 value test_typevar_list_dot =
1130 Grammar.Entry.of_parser gram "test_typevar_list_dot"
1131 (let rec test lev strm =
1132 match stream_peek_nth lev strm with
1133 [ Some ("", "'") -> test2 (lev + 1) strm
1134 | Some ("", ".") -> ()
1135 | _ -> raise Stream.Failure ]
1136 and test2 lev strm =
1137 match stream_peek_nth lev strm with
1138 [ Some ("UIDENT" | "LIDENT", _) -> test (lev + 1) strm
1139 | _ -> raise Stream.Failure ]
1144 value constr_arity = ref [("Some", 1); ("Match_Failure", 1)];
1146 value rec is_expr_constr_call =
1148 [ <:expr< $uid:_$ >> -> True
1149 | <:expr< $uid:_$.$e$ >> -> is_expr_constr_call e
1150 | <:expr< $e$ $_$ >> -> is_expr_constr_call e
1154 value rec constr_expr_arity loc =
1156 [ <:expr< $uid:c$ >> ->
1157 try List.assoc c constr_arity.val with [ Not_found -> 0 ]
1158 | <:expr< $uid:_$.$e$ >> -> constr_expr_arity loc e
1159 | <:expr< $e$ $_$ >> ->
1160 if is_expr_constr_call e then
1161 Stdpp.raise_with_loc loc (Stream.Error "currified constructor")
1166 value rec is_patt_constr_call =
1168 [ <:patt< $uid:_$ >> -> True
1169 | <:patt< $uid:_$.$p$ >> -> is_patt_constr_call p
1170 | <:patt< $p$ $_$ >> -> is_patt_constr_call p
1174 value rec constr_patt_arity loc =
1176 [ <:patt< $uid:c$ >> ->
1177 try List.assoc c constr_arity.val with [ Not_found -> 0 ]
1178 | <:patt< $uid:_$.$p$ >> -> constr_patt_arity loc p
1179 | <:patt< $p$ $_$ >> ->
1180 if is_patt_constr_call p then
1181 Stdpp.raise_with_loc loc (Stream.Error "currified constructor")
1188 [ <:expr< do { $list:el$ } >> -> el
1192 value choose_tvar tpl =
1193 let rec find_alpha v =
1194 let s = String.make 1 v in
1195 if List.mem_assoc s tpl then
1196 if v = 'z' then None else find_alpha (Char.chr (Char.code v + 1))
1197 else Some (String.make 1 v)
1200 let v = "a" ^ string_of_int n in
1201 if List.mem_assoc v tpl then make_n (succ n) else v
1203 match find_alpha 'a' with
1205 | None -> make_n 1 ]
1208 value rec patt_lid =
1210 [ <:patt< $p1$ $p2$ >> ->
1212 [ <:patt< $lid:i$ >> -> Some (MLast.loc_of_patt p1, i, [p2])
1214 match patt_lid p1 with
1215 [ Some (loc, i, pl) -> Some (loc, i, [p2 :: pl])
1220 value bigarray_get loc arr arg =
1223 [ <:expr< ($list:el$) >> -> el
1227 [ [c1] -> <:expr< Bigarray.Array1.get $arr$ $c1$ >>
1228 | [c1; c2] -> <:expr< Bigarray.Array2.get $arr$ $c1$ $c2$ >>
1229 | [c1; c2; c3] -> <:expr< Bigarray.Array3.get $arr$ $c1$ $c2$ $c3$ >>
1230 | coords -> <:expr< Bigarray.Genarray.get $arr$ [| $list:coords$ |] >> ]
1233 value bigarray_set loc var newval =
1235 [ <:expr< Bigarray.Array1.get $arr$ $c1$ >> ->
1236 Some <:expr< Bigarray.Array1.set $arr$ $c1$ $newval$ >>
1237 | <:expr< Bigarray.Array2.get $arr$ $c1$ $c2$ >> ->
1238 Some <:expr< Bigarray.Array2.set $arr$ $c1$ $c2$ $newval$ >>
1239 | <:expr< Bigarray.Array3.get $arr$ $c1$ $c2$ $c3$ >> ->
1240 Some <:expr< Bigarray.Array3.set $arr$ $c1$ $c2$ $c3$ $newval$ >>
1241 | <:expr< Bigarray.Genarray.get $arr$ [| $list:coords$ |] >> ->
1242 Some <:expr< Bigarray.Genarray.set $arr$ [| $list:coords$ |] $newval$ >>
1248 match cs with parser
1249 [ [: `';' :] -> sync_semi cs
1250 | [: `_ :] -> sync cs ]
1252 match cs with parser
1253 [ [: `';' :] -> sync_semisemi cs
1254 | [: :] -> sync cs ]
1255 and sync_semisemi cs =
1256 match Stream.peek cs with
1257 [ Some ('\010' | '\013') -> ()
1258 | _ -> sync_semi cs ]
1260 Pcaml.sync.val := sync;
1265 GLOBAL: sig_item str_item ctyp patt expr module_type module_expr class_type
1266 class_expr class_sig_item class_str_item let_binding type_declaration;
1268 [ [ "functor"; "("; i = UIDENT; ":"; t = module_type; ")"; "->";
1270 <:module_expr< functor ( $i$ : $t$ ) -> $me$ >>
1271 | "struct"; st = LIST0 [ s = str_item; OPT ";;" -> s ]; "end" ->
1272 <:module_expr< struct $list:st$ end >> ]
1273 | [ me1 = SELF; me2 = SELF -> <:module_expr< $me1$ $me2$ >> ]
1274 | [ i = mod_expr_ident -> i
1275 | "("; me = SELF; ":"; mt = module_type; ")" ->
1276 <:module_expr< ( $me$ : $mt$ ) >>
1277 | "("; me = SELF; ")" -> <:module_expr< $me$ >> ] ]
1282 [ i = SELF; "."; j = SELF -> <:module_expr< $i$ . $j$ >> ]
1283 | [ i = UIDENT -> <:module_expr< $uid:i$ >> ] ]
1288 [ "exception"; (_, c, tl) = constructor_declaration; b = rebind_exn ->
1289 <:str_item< exception $c$ of $list:tl$ = $b$ >>
1290 | "external"; i = LIDENT; ":"; t = ctyp; "="; pd = LIST1 STRING ->
1291 <:str_item< external $i$ : $t$ = $list:pd$ >>
1292 | "external"; "("; i = operator_rparen; ":"; t = ctyp; "=";
1293 pd = LIST1 STRING ->
1294 <:str_item< external $i$ : $t$ = $list:pd$ >>
1295 | "include"; me = module_expr -> <:str_item< include $me$ >>
1296 | "module"; i = UIDENT; mb = module_binding ->
1297 <:str_item< module $i$ = $mb$ >>
1298 | "module"; "rec"; nmtmes = LIST1 module_rec_binding SEP "and" ->
1299 MLast.StRecMod loc nmtmes
1300 | "module"; "type"; i = UIDENT; "="; mt = module_type ->
1301 <:str_item< module type $i$ = $mt$ >>
1302 | "open"; i = mod_ident -> <:str_item< open $i$ >>
1303 | "type"; tdl = LIST1 type_declaration SEP "and" ->
1304 <:str_item< type $list:tdl$ >>
1305 | "let"; r = OPT "rec"; l = LIST1 let_binding SEP "and"; "in";
1307 let e = <:expr< let $opt:o2b r$ $list:l$ in $x$ >> in
1308 <:str_item< $exp:e$ >>
1309 | "let"; r = OPT "rec"; l = LIST1 let_binding SEP "and" ->
1311 [ [(<:patt< _ >>, e)] -> <:str_item< $exp:e$ >>
1312 | _ -> <:str_item< value $opt:o2b r$ $list:l$ >> ]
1313 | "let"; "module"; m = UIDENT; mb = module_binding; "in"; e = expr ->
1314 <:str_item< let module $m$ = $mb$ in $e$ >>
1315 | e = expr -> <:str_item< $exp:e$ >> ] ]
1319 [ [ "="; sl = mod_ident -> sl
1324 [ "("; m = UIDENT; ":"; mt = module_type; ")"; mb = SELF ->
1325 <:module_expr< functor ( $m$ : $mt$ ) -> $mb$ >>
1326 | ":"; mt = module_type; "="; me = module_expr ->
1327 <:module_expr< ( $me$ : $mt$ ) >>
1328 | "="; me = module_expr -> <:module_expr< $me$ >> ] ]
1331 [ [ m = UIDENT; ":"; mt = module_type; "="; me = module_expr ->
1336 [ [ "functor"; "("; i = UIDENT; ":"; t = SELF; ")"; "->"; mt = SELF ->
1337 <:module_type< functor ( $i$ : $t$ ) -> $mt$ >> ]
1338 | [ mt = SELF; "with"; wcl = LIST1 with_constr SEP "and" ->
1339 <:module_type< $mt$ with $list:wcl$ >> ]
1340 | [ "sig"; sg = LIST0 [ s = sig_item; OPT ";;" -> s ]; "end" ->
1341 <:module_type< sig $list:sg$ end >>
1342 | i = mod_type_ident -> i
1343 | "("; mt = SELF; ")" -> <:module_type< $mt$ >> ] ]
1347 [ m1 = SELF; "."; m2 = SELF -> <:module_type< $m1$ . $m2$ >>
1348 | m1 = SELF; "("; m2 = SELF; ")" -> <:module_type< $m1$ $m2$ >> ]
1349 | [ m = UIDENT -> <:module_type< $uid:m$ >>
1350 | m = LIDENT -> <:module_type< $lid:m$ >> ] ]
1354 [ "exception"; (_, c, tl) = constructor_declaration ->
1355 <:sig_item< exception $c$ of $list:tl$ >>
1356 | "external"; i = LIDENT; ":"; t = ctyp; "="; pd = LIST1 STRING ->
1357 <:sig_item< external $i$ : $t$ = $list:pd$ >>
1358 | "external"; "("; i = operator_rparen; ":"; t = ctyp; "=";
1359 pd = LIST1 STRING ->
1360 <:sig_item< external $i$ : $t$ = $list:pd$ >>
1361 | "include"; mt = module_type -> <:sig_item< include $mt$ >>
1362 | "module"; i = UIDENT; mt = module_declaration ->
1363 <:sig_item< module $i$ : $mt$ >>
1364 | "module"; "rec"; mds = LIST1 module_rec_declaration SEP "and" ->
1365 MLast.SgRecMod loc mds
1366 | "module"; "type"; i = UIDENT; "="; mt = module_type ->
1367 <:sig_item< module type $i$ = $mt$ >>
1368 | "module"; "type"; i = UIDENT ->
1369 <:sig_item< module type $i$ = 'abstract >>
1370 | "open"; i = mod_ident -> <:sig_item< open $i$ >>
1371 | "type"; tdl = LIST1 type_declaration SEP "and" ->
1372 <:sig_item< type $list:tdl$ >>
1373 | "val"; i = LIDENT; ":"; t = ctyp -> <:sig_item< value $i$ : $t$ >>
1374 | "val"; "("; i = operator_rparen; ":"; t = ctyp ->
1375 <:sig_item< value $i$ : $t$ >> ] ]
1379 [ ":"; mt = module_type -> <:module_type< $mt$ >>
1380 | "("; i = UIDENT; ":"; t = module_type; ")"; mt = SELF ->
1381 <:module_type< functor ( $i$ : $t$ ) -> $mt$ >> ] ]
1383 module_rec_declaration:
1384 [ [ m = UIDENT; ":"; mt = module_type -> (m, mt)] ]
1386 (* "with" constraints (additional type equations over signature
1389 [ [ "type"; tpl = type_parameters; i = mod_ident; "="; t = ctyp ->
1390 MLast.WcTyp loc i tpl t
1391 | "module"; i = mod_ident; "="; me = module_expr ->
1392 MLast.WcMod loc i me ] ]
1394 (* Core expressions *)
1397 [ e1 = SELF; ";"; e2 = SELF ->
1398 <:expr< do { $list:[e1 :: get_seq e2]$ } >>
1399 | e1 = SELF; ";" -> e1 ]
1401 [ "let"; o = OPT "rec"; l = LIST1 let_binding SEP "and"; "in";
1402 x = expr LEVEL "top" ->
1403 <:expr< let $opt:o2b o$ $list:l$ in $x$ >>
1404 | "let"; "module"; m = UIDENT; mb = module_binding; "in";
1405 e = expr LEVEL "top" ->
1406 <:expr< let module $m$ = $mb$ in $e$ >>
1407 | "function"; OPT "|"; l = LIST1 match_case SEP "|" ->
1408 <:expr< fun [ $list:l$ ] >>
1409 | "fun"; p = simple_patt; e = fun_def ->
1410 <:expr< fun [$p$ -> $e$] >>
1411 | "match"; e = SELF; "with"; OPT "|"; l = LIST1 match_case SEP "|" ->
1412 <:expr< match $e$ with [ $list:l$ ] >>
1413 | "try"; e = SELF; "with"; OPT "|"; l = LIST1 match_case SEP "|" ->
1414 <:expr< try $e$ with [ $list:l$ ] >>
1415 | "if"; e1 = SELF; "then"; e2 = expr LEVEL "expr1";
1416 "else"; e3 = expr LEVEL "expr1" ->
1417 <:expr< if $e1$ then $e2$ else $e3$ >>
1418 | "if"; e1 = SELF; "then"; e2 = expr LEVEL "expr1" ->
1419 <:expr< if $e1$ then $e2$ else () >>
1420 | "for"; i = LIDENT; "="; e1 = SELF; df = direction_flag; e2 = SELF;
1421 "do"; e = SELF; "done" ->
1422 <:expr< for $i$ = $e1$ $to:df$ $e2$ do { $list:get_seq e$ } >>
1423 | "while"; e1 = SELF; "do"; e2 = SELF; "done" ->
1424 <:expr< while $e1$ do { $list:get_seq e2$ } >>
1425 | "object"; cspo = OPT class_self_patt; cf = class_structure; "end" ->
1426 (* <:expr< object $opt:cspo$ $list:cf$ end >> *)
1427 MLast.ExObj loc cspo cf ]
1428 | [ e = SELF; ","; el = LIST1 NEXT SEP "," ->
1429 <:expr< ( $list:[e :: el]$ ) >> ]
1431 [ e1 = SELF; ":="; e2 = expr LEVEL "expr1" ->
1432 <:expr< $e1$.val := $e2$ >>
1433 | e1 = SELF; "<-"; e2 = expr LEVEL "expr1" ->
1434 match bigarray_set loc e1 e2 with
1436 | None -> <:expr< $e1$ := $e2$ >> ] ]
1438 [ e1 = SELF; "or"; e2 = SELF -> <:expr< $lid:"or"$ $e1$ $e2$ >>
1439 | e1 = SELF; "||"; e2 = SELF -> <:expr< $e1$ || $e2$ >> ]
1441 [ e1 = SELF; "&"; e2 = SELF -> <:expr< $lid:"&"$ $e1$ $e2$ >>
1442 | e1 = SELF; "&&"; e2 = SELF -> <:expr< $e1$ && $e2$ >> ]
1444 [ e1 = SELF; "<"; e2 = SELF -> <:expr< $e1$ < $e2$ >>
1445 | e1 = SELF; ">"; e2 = SELF -> <:expr< $e1$ > $e2$ >>
1446 | e1 = SELF; "<="; e2 = SELF -> <:expr< $e1$ <= $e2$ >>
1447 | e1 = SELF; ">="; e2 = SELF -> <:expr< $e1$ >= $e2$ >>
1448 | e1 = SELF; "="; e2 = SELF -> <:expr< $e1$ = $e2$ >>
1449 | e1 = SELF; "<>"; e2 = SELF -> <:expr< $e1$ <> $e2$ >>
1450 | e1 = SELF; "=="; e2 = SELF -> <:expr< $e1$ == $e2$ >>
1451 | e1 = SELF; "!="; e2 = SELF -> <:expr< $e1$ != $e2$ >>
1452 | e1 = SELF; "$"; e2 = SELF -> <:expr< $lid:"\$"$ $e1$ $e2$ >>
1453 | e1 = SELF; op = infixop0; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
1455 [ e1 = SELF; "^"; e2 = SELF -> <:expr< $e1$ ^ $e2$ >>
1456 | e1 = SELF; "@"; e2 = SELF -> <:expr< $e1$ @ $e2$ >>
1457 | e1 = SELF; op = infixop1; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
1459 [ e1 = SELF; "::"; e2 = SELF -> <:expr< [$e1$ :: $e2$] >> ]
1461 [ e1 = SELF; "+"; e2 = SELF -> <:expr< $e1$ + $e2$ >>
1462 | e1 = SELF; "-"; e2 = SELF -> <:expr< $e1$ - $e2$ >>
1463 | e1 = SELF; op = infixop2; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
1465 [ e1 = SELF; "*"; e2 = SELF -> <:expr< $e1$ * $e2$ >>
1466 | e1 = SELF; "/"; e2 = SELF -> <:expr< $e1$ / $e2$ >>
1467 | e1 = SELF; "%"; e2 = SELF -> <:expr< $lid:"%"$ $e1$ $e2$ >>
1468 | e1 = SELF; "land"; e2 = SELF -> <:expr< $e1$ land $e2$ >>
1469 | e1 = SELF; "lor"; e2 = SELF -> <:expr< $e1$ lor $e2$ >>
1470 | e1 = SELF; "lxor"; e2 = SELF -> <:expr< $e1$ lxor $e2$ >>
1471 | e1 = SELF; "mod"; e2 = SELF -> <:expr< $e1$ mod $e2$ >>
1472 | e1 = SELF; op = infixop3; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
1474 [ e1 = SELF; "**"; e2 = SELF -> <:expr< $e1$ ** $e2$ >>
1475 | e1 = SELF; "asr"; e2 = SELF -> <:expr< $e1$ asr $e2$ >>
1476 | e1 = SELF; "lsl"; e2 = SELF -> <:expr< $e1$ lsl $e2$ >>
1477 | e1 = SELF; "lsr"; e2 = SELF -> <:expr< $e1$ lsr $e2$ >>
1478 | e1 = SELF; op = infixop4; e2 = SELF -> <:expr< $lid:op$ $e1$ $e2$ >> ]
1479 | "unary minus" NONA
1480 [ "-"; e = SELF -> <:expr< $mkumin loc "-" e$ >>
1481 | "-."; e = SELF -> <:expr< $mkumin loc "-." e$ >> ]
1483 [ e1 = SELF; e2 = SELF ->
1484 match constr_expr_arity loc e1 with
1485 [ 1 -> <:expr< $e1$ $e2$ >>
1488 [ <:expr< ( $list:el$ ) >> ->
1489 List.fold_left (fun e1 e2 -> <:expr< $e1$ $e2$ >>) e1 el
1490 | _ -> <:expr< $e1$ $e2$ >> ] ]
1491 | "assert"; e = SELF ->
1493 [ <:expr< False >> -> <:expr< assert False >>
1494 | _ -> <:expr< assert ($e$) >> ]
1495 | "lazy"; e = SELF ->
1496 <:expr< lazy ($e$) >> ]
1498 [ e1 = SELF; "."; "("; e2 = SELF; ")" -> <:expr< $e1$ .( $e2$ ) >>
1499 | e1 = SELF; "."; "["; e2 = SELF; "]" -> <:expr< $e1$ .[ $e2$ ] >>
1500 | e1 = SELF; "."; "{"; e2 = SELF; "}" -> bigarray_get loc e1 e2
1501 | e1 = SELF; "."; e2 = SELF -> <:expr< $e1$ . $e2$ >> ]
1503 [ "!"; e = SELF -> <:expr< $e$ . val>>
1504 | "~-"; e = SELF -> <:expr< ~- $e$ >>
1505 | "~-."; e = SELF -> <:expr< ~-. $e$ >>
1506 | f = prefixop; e = SELF -> <:expr< $lid:f$ $e$ >> ]
1508 [ s = INT -> <:expr< $int:s$ >>
1509 | s = INT32 -> MLast.ExInt32 loc s
1510 | s = INT64 -> MLast.ExInt64 loc s
1511 | s = NATIVEINT -> MLast.ExNativeInt loc s
1512 | s = FLOAT -> <:expr< $flo:s$ >>
1513 | s = STRING -> <:expr< $str:s$ >>
1514 | c = CHAR -> <:expr< $chr:c$ >>
1515 | UIDENT "True" -> <:expr< $uid:" True"$ >>
1516 | UIDENT "False" -> <:expr< $uid:" False"$ >>
1517 | i = expr_ident -> i
1518 | s = "false" -> <:expr< False >>
1519 | s = "true" -> <:expr< True >>
1520 | "["; "]" -> <:expr< [] >>
1521 | "["; el = expr1_semi_list; "]" -> <:expr< $mklistexp loc None el$ >>
1522 | "[|"; "|]" -> <:expr< [| |] >>
1523 | "[|"; el = expr1_semi_list; "|]" -> <:expr< [| $list:el$ |] >>
1524 | "{"; test_label_eq; lel = lbl_expr_list; "}" ->
1525 <:expr< { $list:lel$ } >>
1526 | "{"; e = expr LEVEL "."; "with"; lel = lbl_expr_list; "}" ->
1527 <:expr< { ($e$) with $list:lel$ } >>
1528 | "("; ")" -> <:expr< () >>
1529 | "("; op = operator_rparen -> <:expr< $lid:op$ >>
1530 | "("; e = SELF; ":"; t = ctyp; ")" -> <:expr< ($e$ : $t$) >>
1531 | "("; e = SELF; ")" -> <:expr< $e$ >>
1532 | "begin"; e = SELF; "end" -> <:expr< $e$ >>
1533 | "begin"; "end" -> <:expr< () >>
1537 let i = String.index x ':' in
1538 ({Lexing.pos_fname = "";
1539 Lexing.pos_lnum = 0;
1541 Lexing.pos_cnum = int_of_string (String.sub x 0 i)},
1542 String.sub x (i + 1) (String.length x - i - 1))
1544 [ Not_found | Failure _ -> (Token.nowhere, x) ]
1546 Pcaml.handle_expr_locate loc x
1550 let i = String.index x ':' in
1552 String.sub x (i + 1) (String.length x - i - 1))
1554 [ Not_found -> ("", x) ]
1556 Pcaml.handle_expr_quotation loc x ] ]
1559 [ [ p = patt; e = fun_binding ->
1560 match patt_lid p with
1561 [ Some (loc, i, pl) ->
1563 List.fold_left (fun e p -> <:expr< fun $p$ -> $e$ >>) e pl
1565 (<:patt< $lid:i$ >>, e)
1566 | None -> (p, e) ] ] ]
1570 [ p = simple_patt; e = SELF -> <:expr< fun $p$ -> $e$ >>
1571 | "="; e = expr -> <:expr< $e$ >>
1572 | ":"; t = ctyp; "="; e = expr -> <:expr< ($e$ : $t$) >> ] ]
1575 [ [ x1 = patt; w = OPT [ "when"; e = expr -> e ]; "->"; x2 = expr ->
1579 [ [ le = lbl_expr; ";"; lel = SELF -> [le :: lel]
1580 | le = lbl_expr; ";" -> [le]
1581 | le = lbl_expr -> [le] ] ]
1584 [ [ i = patt_label_ident; "="; e = expr LEVEL "expr1" -> (i, e) ] ]
1587 [ [ e = expr LEVEL "expr1"; ";"; el = SELF -> [e :: el]
1588 | e = expr LEVEL "expr1"; ";" -> [e]
1589 | e = expr LEVEL "expr1" -> [e] ] ]
1593 [ p = simple_patt; e = SELF -> <:expr< fun $p$ -> $e$ >>
1594 | "->"; e = expr -> <:expr< $e$ >> ] ]
1598 [ i = LIDENT -> <:expr< $lid:i$ >>
1599 | i = UIDENT -> <:expr< $uid:i$ >>
1600 | i = UIDENT; "."; j = SELF ->
1603 [ <:expr< $x$ . $y$ >> -> loop <:expr< $m$ . $x$ >> y
1604 | e -> <:expr< $m$ . $e$ >> ]
1606 loop <:expr< $uid:i$ >> j
1607 | i = UIDENT; "."; "("; j = operator_rparen ->
1608 <:expr< $uid:i$ . $lid:j$ >> ] ]
1613 [ p1 = SELF; "as"; i = LIDENT -> <:patt< ($p1$ as $lid:i$) >> ]
1615 [ p1 = SELF; "|"; p2 = SELF -> <:patt< $p1$ | $p2$ >> ]
1616 | [ p = SELF; ","; pl = LIST1 NEXT SEP "," ->
1617 <:patt< ( $list:[p :: pl]$) >> ]
1619 [ p1 = SELF; ".."; p2 = SELF -> <:patt< $p1$ .. $p2$ >> ]
1621 [ p1 = SELF; "::"; p2 = SELF -> <:patt< [$p1$ :: $p2$] >> ]
1623 [ p1 = SELF; p2 = SELF ->
1624 match constr_patt_arity loc p1 with
1625 [ 1 -> <:patt< $p1$ $p2$ >>
1629 [ <:patt< _ >> when n > 1 ->
1631 loop n where rec loop n =
1632 if n = 0 then [] else [<:patt< _ >> :: loop (n - 1)]
1634 <:patt< ( $list:pl$ ) >>
1638 [ <:patt< ( $list:pl$ ) >> ->
1639 List.fold_left (fun p1 p2 -> <:patt< $p1$ $p2$ >>) p1 pl
1640 | _ -> <:patt< $p1$ $p2$ >> ] ] ]
1642 [ p1 = SELF; "."; p2 = SELF -> <:patt< $p1$ . $p2$ >> ]
1644 [ p = simple_patt -> p ] ]
1648 [ [ s = LIDENT -> <:patt< $lid:s$ >>
1649 | s = UIDENT -> <:patt< $uid:s$ >>
1650 | s = INT -> <:patt< $int:s$ >>
1651 | s = INT32 -> MLast.PaInt32 loc s
1652 | s = INT64 -> MLast.PaInt64 loc s
1653 | s = NATIVEINT -> MLast.PaNativeInt loc s
1654 | "-"; s = INT -> <:patt< $int:"-" ^ s$ >>
1655 | "-"; s = INT32 -> MLast.PaInt32 loc ("-" ^ s)
1656 | "-"; s = INT64 -> MLast.PaInt64 loc ("-" ^ s)
1657 | "-"; s = NATIVEINT -> MLast.PaNativeInt loc ("-" ^ s)
1658 | "-"; s = FLOAT -> <:patt< $flo:"-" ^ s$ >>
1659 | s = FLOAT -> <:patt< $flo:s$ >>
1660 | s = STRING -> <:patt< $str:s$ >>
1661 | s = CHAR -> <:patt< $chr:s$ >>
1662 | UIDENT "True" -> <:patt< $uid:" True"$ >>
1663 | UIDENT "False" -> <:patt< $uid:" False"$ >>
1664 | s = "false" -> <:patt< False >>
1665 | s = "true" -> <:patt< True >>
1666 | "["; "]" -> <:patt< [] >>
1667 | "["; pl = patt_semi_list; "]" -> <:patt< $mklistpat loc None pl$ >>
1668 | "[|"; "|]" -> <:patt< [| |] >>
1669 | "[|"; pl = patt_semi_list; "|]" -> <:patt< [| $list:pl$ |] >>
1670 | "{"; lpl = lbl_patt_list; "}" -> <:patt< { $list:lpl$ } >>
1671 | "("; ")" -> <:patt< () >>
1672 | "("; op = operator_rparen -> <:patt< $lid:op$ >>
1673 | "("; p = patt; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >>
1674 | "("; p = patt; ")" -> <:patt< $p$ >>
1675 | "_" -> <:patt< _ >>
1676 | "`"; s = ident -> <:patt< ` $s$ >>
1677 | "#"; t = mod_ident -> <:patt< # $list:t$ >>
1681 let i = String.index x ':' in
1682 ({Lexing.pos_fname = "";
1683 Lexing.pos_lnum = 0;
1685 Lexing.pos_cnum = int_of_string (String.sub x 0 i)},
1686 String.sub x (i + 1) (String.length x - i - 1))
1688 [ Not_found | Failure _ -> (Token.nowhere, x) ]
1690 Pcaml.handle_patt_locate loc x
1694 let i = String.index x ':' in
1696 String.sub x (i + 1) (String.length x - i - 1))
1698 [ Not_found -> ("", x) ]
1700 Pcaml.handle_patt_quotation loc x ] ]
1704 [ [ p = patt; ";"; pl = SELF -> [p :: pl]
1705 | p = patt; ";" -> [p]
1706 | p = patt -> [p] ] ]
1709 [ [ le = lbl_patt; ";"; lel = SELF -> [le :: lel]
1710 | le = lbl_patt; ";" -> [le]
1711 | le = lbl_patt -> [le] ] ]
1714 [ [ i = patt_label_ident; "="; p = patt -> (i, p) ] ]
1718 [ p1 = SELF; "."; p2 = SELF -> <:patt< $p1$ . $p2$ >> ]
1720 [ i = UIDENT -> <:patt< $uid:i$ >>
1721 | i = LIDENT -> <:patt< $lid:i$ >> ] ]
1723 (* Type declaration *)
1725 [ [ tpl = type_parameters; n = type_patt; "="; tk = type_kind;
1726 cl = LIST0 constrain ->
1728 | tpl = type_parameters; n = type_patt; cl = LIST0 constrain ->
1729 (n, tpl, <:ctyp< '$choose_tvar tpl$ >>, cl) ] ]
1732 [ [ n = LIDENT -> (loc, n) ] ]
1735 [ [ "constraint"; t1 = ctyp; "="; t2 = ctyp -> (t1, t2) ] ]
1738 [ [ "private"; "{"; ldl = label_declarations; "}" ->
1739 <:ctyp< private { $list:ldl$ } >>
1740 | "private"; OPT "|";
1741 cdl = LIST1 constructor_declaration SEP "|" -> <:ctyp< private [ $list:cdl$ ] >>
1742 | test_constr_decl; OPT "|";
1743 cdl = LIST1 constructor_declaration SEP "|" -> <:ctyp< [ $list:cdl$ ] >>
1744 | t = ctyp -> <:ctyp< $t$ >>
1745 | t = ctyp; "="; "private"; "{"; ldl = label_declarations; "}" ->
1746 <:ctyp< $t$ == private { $list:ldl$ } >>
1747 | t = ctyp; "="; "{"; ldl = label_declarations; "}" ->
1748 <:ctyp< $t$ == { $list:ldl$ } >>
1749 | t = ctyp; "="; "private"; OPT "|"; cdl = LIST1 constructor_declaration SEP "|" ->
1750 <:ctyp< $t$ == private [ $list:cdl$ ] >>
1751 | t = ctyp; "="; OPT "|"; cdl = LIST1 constructor_declaration SEP "|" ->
1752 <:ctyp< $t$ == [ $list:cdl$ ] >>
1753 | "{"; ldl = label_declarations; "}" ->
1754 <:ctyp< { $list:ldl$ } >> ] ]
1757 [ [ -> (* empty *) []
1758 | tp = type_parameter -> [tp]
1759 | "("; tpl = LIST1 type_parameter SEP ","; ")" -> tpl ] ]
1762 [ [ "'"; i = ident -> (i, (False, False))
1763 | "+"; "'"; i = ident -> (i, (True, False))
1764 | "-"; "'"; i = ident -> (i, (False, True)) ] ]
1766 constructor_declaration:
1767 [ [ ci = UIDENT; "of"; cal = LIST1 ctyp LEVEL "ctyp1" SEP "*" ->
1769 | ci = UIDENT -> (loc, ci, []) ] ]
1772 [ [ ld = label_declaration; ";"; ldl = SELF -> [ld :: ldl]
1773 | ld = label_declaration; ";" -> [ld]
1774 | ld = label_declaration -> [ld] ] ]
1777 [ [ i = LIDENT; ":"; t = poly_type -> (loc, i, False, t)
1778 | "mutable"; i = LIDENT; ":"; t = poly_type -> (loc, i, True, t) ] ]
1782 [ [ t1 = SELF; "as"; "'"; i = ident -> <:ctyp< $t1$ as '$i$ >> ]
1784 [ t1 = SELF; "->"; t2 = SELF -> <:ctyp< $t1$ -> $t2$ >> ]
1786 [ t = SELF; "*"; tl = LIST1 (ctyp LEVEL "ctyp1") SEP "*" ->
1787 <:ctyp< ( $list:[t :: tl]$ ) >> ]
1789 [ t1 = SELF; t2 = SELF -> <:ctyp< $t2$ $t1$ >> ]
1791 [ t1 = SELF; "."; t2 = SELF -> <:ctyp< $t1$ . $t2$ >>
1792 | t1 = SELF; "("; t2 = SELF; ")" -> <:ctyp< $t1$ $t2$ >> ]
1794 [ "'"; i = ident -> <:ctyp< '$i$ >>
1795 | "_" -> <:ctyp< _ >>
1796 | i = LIDENT -> <:ctyp< $lid:i$ >>
1797 | i = UIDENT -> <:ctyp< $uid:i$ >>
1798 | "("; t = SELF; ","; tl = LIST1 ctyp SEP ","; ")";
1799 i = ctyp LEVEL "ctyp2" ->
1800 List.fold_left (fun c a -> <:ctyp< $c$ $a$ >>) i [t :: tl]
1801 | "("; t = SELF; ")" -> <:ctyp< $t$ >> ] ]
1806 | i = UIDENT -> i ] ]
1812 | i = UIDENT; "."; j = SELF -> [i :: j] ] ]
1817 | "downto" -> False ] ]
1819 (* Objects and Classes *)
1821 [ [ "class"; cd = LIST1 class_declaration SEP "and" ->
1822 <:str_item< class $list:cd$ >>
1823 | "class"; "type"; ctd = LIST1 class_type_declaration SEP "and" ->
1824 <:str_item< class type $list:ctd$ >> ] ]
1827 [ [ "class"; cd = LIST1 class_description SEP "and" ->
1828 <:sig_item< class $list:cd$ >>
1829 | "class"; "type"; ctd = LIST1 class_type_declaration SEP "and" ->
1830 <:sig_item< class type $list:ctd$ >> ] ]
1832 (* Class expressions *)
1834 [ [ vf = OPT "virtual"; ctp = class_type_parameters; i = LIDENT;
1835 cfb = class_fun_binding ->
1836 {MLast.ciLoc = loc; MLast.ciVir = o2b vf; MLast.ciPrm = ctp;
1837 MLast.ciNam = i; MLast.ciExp = cfb} ] ]
1840 [ [ "="; ce = class_expr -> ce
1841 | ":"; ct = class_type; "="; ce = class_expr ->
1842 <:class_expr< ($ce$ : $ct$) >>
1843 | p = simple_patt; cfb = SELF ->
1844 <:class_expr< fun $p$ -> $cfb$ >> ] ]
1846 class_type_parameters:
1848 | "["; tpl = LIST1 type_parameter SEP ","; "]" -> (loc, tpl) ] ]
1851 [ [ p = simple_patt; "->"; ce = class_expr ->
1852 <:class_expr< fun $p$ -> $ce$ >>
1853 | p = labeled_patt; "->"; ce = class_expr ->
1854 <:class_expr< fun $p$ -> $ce$ >>
1855 | p = simple_patt; cfd = SELF ->
1856 <:class_expr< fun $p$ -> $cfd$ >>
1857 | p = labeled_patt; cfd = SELF ->
1858 <:class_expr< fun $p$ -> $cfd$ >> ] ]
1862 [ "fun"; cfd = class_fun_def -> cfd
1863 | "let"; rf = OPT "rec"; lb = LIST1 let_binding SEP "and"; "in";
1865 <:class_expr< let $opt:o2b rf$ $list:lb$ in $ce$ >> ]
1867 [ ce = SELF; e = expr LEVEL "label" ->
1868 <:class_expr< $ce$ $e$ >> ]
1870 [ "["; ct = ctyp; ","; ctcl = LIST1 ctyp SEP ","; "]";
1871 ci = class_longident ->
1872 <:class_expr< $list:ci$ [ $list:[ct :: ctcl]$ ] >>
1873 | "["; ct = ctyp; "]"; ci = class_longident ->
1874 <:class_expr< $list:ci$ [ $ct$ ] >>
1875 | ci = class_longident -> <:class_expr< $list:ci$ >>
1876 | "object"; cspo = OPT class_self_patt; cf = class_structure; "end" ->
1877 <:class_expr< object $opt:cspo$ $list:cf$ end >>
1878 | "("; ce = SELF; ":"; ct = class_type; ")" ->
1879 <:class_expr< ($ce$ : $ct$) >>
1880 | "("; ce = SELF; ")" -> ce ] ]
1883 [ [ cf = LIST0 class_str_item -> cf ] ]
1886 [ [ "("; p = patt; ")" -> p
1887 | "("; p = patt; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >> ] ]
1890 [ [ "inherit"; ce = class_expr; pb = OPT [ "as"; i = LIDENT -> i ] ->
1891 <:class_str_item< inherit $ce$ $opt:pb$ >>
1892 | "val"; mf = OPT "mutable"; lab = label; e = cvalue_binding ->
1893 <:class_str_item< value $opt:o2b mf$ $lab$ = $e$ >>
1894 | "method"; "private"; "virtual"; l = label; ":"; t = poly_type ->
1895 <:class_str_item< method virtual private $l$ : $t$ >>
1896 | "method"; "virtual"; "private"; l = label; ":"; t = poly_type ->
1897 <:class_str_item< method virtual private $l$ : $t$ >>
1898 | "method"; "virtual"; l = label; ":"; t = poly_type ->
1899 <:class_str_item< method virtual $l$ : $t$ >>
1900 | "method"; "private"; l = label; ":"; t = poly_type; "="; e = expr ->
1901 MLast.CrMth loc l True e (Some t)
1902 | "method"; "private"; l = label; sb = fun_binding ->
1903 MLast.CrMth loc l True sb None
1904 | "method"; l = label; ":"; t = poly_type; "="; e = expr ->
1905 MLast.CrMth loc l False e (Some t)
1906 | "method"; l = label; sb = fun_binding ->
1907 MLast.CrMth loc l False sb None
1908 | "constraint"; t1 = ctyp; "="; t2 = ctyp ->
1909 <:class_str_item< type $t1$ = $t2$ >>
1910 | "initializer"; se = expr -> <:class_str_item< initializer $se$ >> ] ]
1913 [ [ "="; e = expr -> e
1914 | ":"; t = ctyp; "="; e = expr -> <:expr< ($e$ : $t$) >>
1915 | ":"; t = ctyp; ":>"; t2 = ctyp; "="; e = expr ->
1916 <:expr< ($e$ : $t$ :> $t2$) >>
1917 | ":>"; t = ctyp; "="; e = expr ->
1918 <:expr< ($e$ :> $t$) >> ] ]
1921 [ [ i = LIDENT -> i ] ]
1925 [ [ test_ctyp_minusgreater; t = ctyp LEVEL "star"; "->"; ct = SELF ->
1926 <:class_type< [ $t$ ] -> $ct$ >>
1927 | cs = class_signature -> cs ] ]
1930 [ [ "["; tl = LIST1 ctyp SEP ","; "]"; id = clty_longident ->
1931 <:class_type< $list:id$ [ $list:tl$ ] >>
1932 | id = clty_longident -> <:class_type< $list:id$ >>
1933 | "object"; cst = OPT class_self_type; csf = LIST0 class_sig_item;
1935 <:class_type< object $opt:cst$ $list:csf$ end >> ] ]
1938 [ [ "("; t = ctyp; ")" -> t ] ]
1941 [ [ "inherit"; cs = class_signature -> <:class_sig_item< inherit $cs$ >>
1942 | "val"; mf = OPT "mutable"; l = label; ":"; t = ctyp ->
1943 <:class_sig_item< value $opt:o2b mf$ $l$ : $t$ >>
1944 | "method"; "private"; "virtual"; l = label; ":"; t = poly_type ->
1945 <:class_sig_item< method virtual private $l$ : $t$ >>
1946 | "method"; "virtual"; "private"; l = label; ":"; t = poly_type ->
1947 <:class_sig_item< method virtual private $l$ : $t$ >>
1948 | "method"; "virtual"; l = label; ":"; t = poly_type ->
1949 <:class_sig_item< method virtual $l$ : $t$ >>
1950 | "method"; "private"; l = label; ":"; t = poly_type ->
1951 <:class_sig_item< method private $l$ : $t$ >>
1952 | "method"; l = label; ":"; t = poly_type ->
1953 <:class_sig_item< method $l$ : $t$ >>
1954 | "constraint"; t1 = ctyp; "="; t2 = ctyp ->
1955 <:class_sig_item< type $t1$ = $t2$ >> ] ]
1958 [ [ vf = OPT "virtual"; ctp = class_type_parameters; n = LIDENT; ":";
1960 {MLast.ciLoc = loc; MLast.ciVir = o2b vf; MLast.ciPrm = ctp;
1961 MLast.ciNam = n; MLast.ciExp = ct} ] ]
1963 class_type_declaration:
1964 [ [ vf = OPT "virtual"; ctp = class_type_parameters; n = LIDENT; "=";
1965 cs = class_signature ->
1966 {MLast.ciLoc = loc; MLast.ciVir = o2b vf; MLast.ciPrm = ctp;
1967 MLast.ciNam = n; MLast.ciExp = cs} ] ]
1970 expr: LEVEL "simple"
1972 [ "new"; i = class_longident -> <:expr< new $list:i$ >> ] ]
1975 [ [ e = SELF; "#"; lab = label -> <:expr< $e$ # $lab$ >> ] ]
1977 expr: LEVEL "simple"
1978 [ [ "("; e = SELF; ":"; t = ctyp; ":>"; t2 = ctyp; ")" ->
1979 <:expr< ($e$ : $t$ :> $t2$) >>
1980 | "("; e = SELF; ":>"; t = ctyp; ")" -> <:expr< ($e$ :> $t$) >>
1981 | "{<"; ">}" -> <:expr< {< >} >>
1982 | "{<"; fel = field_expr_list; ">}" -> <:expr< {< $list:fel$ >} >> ] ]
1985 [ [ l = label; "="; e = expr LEVEL "expr1"; ";"; fel = SELF ->
1987 | l = label; "="; e = expr LEVEL "expr1"; ";" -> [(l, e)]
1988 | l = label; "="; e = expr LEVEL "expr1" -> [(l, e)] ] ]
1991 ctyp: LEVEL "simple"
1992 [ [ "#"; id = class_longident -> <:ctyp< # $list:id$ >>
1993 | "<"; (ml, v) = meth_list; ">" -> <:ctyp< < $list:ml$ $opt:v$ > >>
1994 | "<"; ">" -> <:ctyp< < > >> ] ]
1997 [ [ f = field; ";"; (ml, v) = SELF -> ([f :: ml], v)
1998 | f = field; ";" -> ([f], False)
1999 | f = field -> ([f], False)
2000 | ".." -> ([], True) ] ]
2003 [ [ lab = LIDENT; ":"; t = poly_type -> (lab, t) ] ]
2005 (* Polymorphic types *)
2007 [ [ "'"; i = ident -> i ] ]
2010 [ [ test_typevar_list_dot; tpl = LIST1 typevar; "."; t2 = ctyp ->
2011 <:ctyp< ! $list:tpl$ . $t2$ >>
2016 [ [ m = UIDENT; "."; l = SELF -> [m :: l]
2017 | i = LIDENT -> [i] ] ]
2020 [ [ m = UIDENT; "."; l = SELF -> [m :: l]
2021 | i = LIDENT -> [i] ] ]
2026 [ i = lident_colon; t1 = ctyp LEVEL "star"; "->"; t2 = SELF ->
2027 <:ctyp< ( ~ $i$ : $t1$ ) -> $t2$ >>
2028 | i = OPTLABEL; t1 = ctyp LEVEL "star"; "->"; t2 = SELF ->
2029 <:ctyp< ( ? $i$ : $t1$ ) -> $t2$ >>
2030 | i = QUESTIONIDENT; ":"; t1 = ctyp LEVEL "star"; "->"; t2 = SELF ->
2031 <:ctyp< ( ? $i$ : $t1$ ) -> $t2$ >>
2032 | "?"; i=lident_colon;t1 = ctyp LEVEL "star"; "->"; t2 = SELF ->
2033 <:ctyp< ( ? $i$ : $t1$ ) -> $t2$ >> ] ]
2035 ctyp: LEVEL "simple"
2036 [ [ "["; OPT "|"; rfl = LIST1 row_field SEP "|"; "]" ->
2037 <:ctyp< [ = $list:rfl$ ] >>
2038 | "["; ">"; "]" -> <:ctyp< [ > $list:[]$ ] >>
2039 | "["; ">"; OPT "|"; rfl = LIST1 row_field SEP "|"; "]" ->
2040 <:ctyp< [ > $list:rfl$ ] >>
2041 | "[<"; OPT "|"; rfl = LIST1 row_field SEP "|"; "]" ->
2042 <:ctyp< [ < $list:rfl$ ] >>
2043 | "[<"; OPT "|"; rfl = LIST1 row_field SEP "|"; ">";
2044 ntl = LIST1 name_tag; "]" ->
2045 <:ctyp< [ < $list:rfl$ > $list:ntl$ ] >> ] ]
2048 [ [ "`"; i = ident -> MLast.RfTag i True []
2049 | "`"; i = ident; "of"; ao = OPT "&"; l = LIST1 ctyp SEP "&" ->
2050 MLast.RfTag i (o2b ao) l
2051 | t = ctyp -> MLast.RfInh t ] ]
2054 [ [ "`"; i = ident -> i ] ]
2057 [ [ "fun"; p = labeled_patt; e = fun_def -> <:expr< fun $p$ -> $e$ >> ] ]
2061 [ i = LABEL; e = SELF -> <:expr< ~ $i$ : $e$ >>
2062 | i = TILDEIDENT -> <:expr< ~ $i$ >>
2063 | "~"; i = LIDENT -> <:expr< ~ $i$ >>
2064 | i = OPTLABEL; e = SELF -> <:expr< ? $i$ : $e$ >>
2065 | i = QUESTIONIDENT -> <:expr< ? $i$ >>
2066 | "?"; i = LIDENT -> <:expr< ? $i$ >> ] ]
2068 expr: LEVEL "simple"
2069 [ [ "`"; s = ident -> <:expr< ` $s$ >> ] ]
2072 [ [ p = labeled_patt; e = SELF -> <:expr< fun $p$ -> $e$ >> ] ]
2075 [ [ p = labeled_patt; e = SELF -> <:expr< fun $p$ -> $e$ >> ] ]
2078 [ [ i = LABEL; p = simple_patt ->
2079 <:patt< ~ $i$ : $p$ >>
2082 | "~"; i=LIDENT -> <:patt< ~ $i$ >>
2083 | "~"; "("; i = LIDENT; ")" ->
2085 | "~"; "("; i = LIDENT; ":"; t = ctyp; ")" ->
2086 <:patt< ~ $i$ : ($lid:i$ : $t$) >>
2087 | i = OPTLABEL; j = LIDENT ->
2088 <:patt< ? $i$ : ($lid:j$) >>
2089 | i = OPTLABEL; "("; p = patt; "="; e = expr; ")" ->
2090 <:patt< ? $i$ : ( $p$ = $e$ ) >>
2091 | i = OPTLABEL; "("; p = patt; ":"; t = ctyp; ")" ->
2092 <:patt< ? $i$ : ( $p$ : $t$ ) >>
2093 | i = OPTLABEL; "("; p = patt; ":"; t = ctyp; "=";
2095 <:patt< ? $i$ : ( $p$ : $t$ = $e$ ) >>
2096 | i = QUESTIONIDENT -> <:patt< ? $i$ >>
2097 | "?"; i = LIDENT -> <:patt< ? $i$ >>
2098 | "?"; "("; i = LIDENT; "="; e = expr; ")" ->
2099 <:patt< ? ( $lid:i$ = $e$ ) >>
2100 | "?"; "("; i = LIDENT; ":"; t = ctyp; "="; e = expr; ")" ->
2101 <:patt< ? ( $lid:i$ : $t$ = $e$ ) >>
2102 | "?"; "("; i = LIDENT; ")" ->
2104 | "?"; "("; i = LIDENT; ":"; t = ctyp; ")" ->
2105 <:patt< ? ( $lid:i$ : $t$ ) >> ] ]
2108 [ [ i = lident_colon; t = ctyp LEVEL "star"; "->"; ct = SELF ->
2109 <:class_type< [ ~ $i$ : $t$ ] -> $ct$ >>
2110 | i = OPTLABEL; t = ctyp LEVEL "star"; "->"; ct = SELF ->
2111 <:class_type< [ ? $i$ : $t$ ] -> $ct$ >>
2112 | i = QUESTIONIDENT; ":"; t = ctyp LEVEL "star"; "->"; ct = SELF ->
2113 <:class_type< [ ? $i$ : $t$ ] -> $ct$ >>
2114 | "?"; i = LIDENT; ":"; t = ctyp LEVEL "star"; "->"; ct = SELF ->
2115 <:class_type< [ ? $i$ : $t$ ] -> $ct$ >> ] ]
2118 [ [ p = labeled_patt; cfb = SELF -> <:class_expr< fun $p$ -> $cfb$ >> ] ]
2122 (* Main entry points *)
2125 GLOBAL: interf implem use_file top_phrase expr patt;
2127 [ [ si = sig_item_semi; (sil, stopped) = SELF -> ([si :: sil], stopped)
2128 | "#"; n = LIDENT; dp = OPT expr; ";;" ->
2129 ([(<:sig_item< # $n$ $opt:dp$ >>, loc)], True)
2130 | EOI -> ([], False) ] ]
2133 [ [ si = sig_item; OPT ";;" -> (si, loc) ] ]
2136 [ [ si = str_item_semi; (sil, stopped) = SELF -> ([si :: sil], stopped)
2137 | "#"; n = LIDENT; dp = OPT expr; ";;" ->
2138 ([(<:str_item< # $n$ $opt:dp$ >>, loc)], True)
2139 | EOI -> ([], False) ] ]
2142 [ [ si = str_item; OPT ";;" -> (si, loc) ] ]
2145 [ [ ph = phrase; ";;" -> Some ph
2149 [ [ si = str_item; OPT ";;"; (sil, stopped) = SELF ->
2150 ([si :: sil], stopped)
2151 | "#"; n = LIDENT; dp = OPT expr; ";;" ->
2152 ([<:str_item< # $n$ $opt:dp$ >>], True)
2153 | EOI -> ([], False) ] ]
2156 [ [ sti = str_item -> sti
2157 | "#"; n = LIDENT; dp = OPT expr -> <:str_item< # $n$ $opt:dp$ >> ] ]
2161 Pcaml.add_option "-no_quot" (Arg.Set Plexer.no_quotations)
2162 "Don't parse quotations, allowing to use, e.g. \"<:>\" as token";
2166 [[ f = expr; "o"; g = expr -> <:expr< ((o $f$) $g$) >>
2167 | f = expr; "upto"; g = expr -> <:expr< ((upto $f$) $g$) >>
2168 | f = expr; "F_F"; g = expr -> <:expr< ((f_f_ $f$) $g$) >>
2169 | f = expr; "THENC"; g = expr -> <:expr< ((thenc_ $f$) $g$) >>
2170 | f = expr; "THEN"; g = expr -> <:expr< ((then_ $f$) $g$) >>
2171 | f = expr; "THENL"; g = expr -> <:expr< ((thenl_ $f$) $g$) >>
2172 | f = expr; "ORELSE"; g = expr -> <:expr< ((orelse_ $f$) $g$) >>
2173 | f = expr; "ORELSEC"; g = expr -> <:expr< ((orelsec_ $f$) $g$) >>
2174 | f = expr; "THEN_TCL"; g = expr -> <:expr< ((then_tcl_ $f$) $g$) >>
2175 | f = expr; "ORELSE_TCL"; g = expr -> <:expr< ((orelse_tcl_ $f$) $g$) >>
2181 [ [ sti = str_item; ";;" ->
2183 [ <:str_item< $exp:e$ >> -> Some <:str_item< value it = $e$ >>