1 (**************************************************************************)
5 (* François Pottier, INRIA Rocquencourt *)
6 (* Yann Régis-Gianas, PPS, Université Paris Diderot *)
8 (* Copyright 2005-2008 Institut National de Recherche en Informatique *)
9 (* et en Automatique. All rights reserved. This file is distributed *)
10 (* under the terms of the Q Public License version 1.0, with the change *)
11 (* described in file LICENSE. *)
13 (**************************************************************************)
21 (* This wrapper saves the current lexeme start, invokes its argument,
22 and restores it. This allows transmitting better positions to the
25 let savestart lexbuf f =
26 let startp = lexbuf.lex_start_p in
27 let token = f lexbuf in
28 lexbuf.lex_start_p <- startp;
31 (* Updates the line counter, which is used in some error messages. *)
33 let update_loc lexbuf =
34 let pos = lexbuf.lex_curr_p in
35 lexbuf.lex_curr_p <- { pos with
36 pos_lnum = pos.pos_lnum + 1;
37 pos_bol = pos.pos_cnum;
40 (* Extracts a chunk out of the source file. *)
43 let contents = Error.get_file_contents() in
44 let len = ofs2 - ofs1 in
45 String.sub contents ofs1 len
47 (* Extracts a chunk out of the source file, delimited by
48 one position and extending to the end of the file. *)
51 let contents = Error.get_file_contents() in
52 let len = String.length contents - ofs1 in
53 String.sub contents ofs1 len
55 (* Overwrites an old character with a new one at a specified
56 offset in a string. *)
58 let overwrite content offset c1 c2 =
59 assert (content.[offset] = c1);
60 content.[offset] <- c2
62 (* Creates a stretch. *)
64 let mk_stretch parenthesize pos1 pos2 pkeywords =
65 let ofs1 = pos1.pos_cnum
66 and ofs2 = pos2.pos_cnum in
67 let raw_content = chunk ofs1 ofs2 in
68 let content = String.copy raw_content in
69 (* Turn our keywords into valid Objective Caml identifiers
70 by replacing '$', '(', and ')' with '_'. Bloody. *)
71 List.iter (function { value = keyword; position = pos } ->
72 let pos = start_of_position pos in
73 let ofs = pos.pos_cnum - ofs1 in
74 overwrite content ofs '$' '_';
77 | Keyword.Position (Keyword.Left, _, _)
78 | Keyword.PreviousError ->
80 | Keyword.SyntaxError ->
81 (* $syntaxerror is replaced with
83 let source = "(raise _eRR)" in
84 String.blit source 0 content ofs (String.length source)
85 | Keyword.Position (subject, where, _) ->
88 | Keyword.WhereStart ->
93 overwrite content ofslpar '(' '_';
97 | Keyword.RightDollar i ->
98 overwrite content (ofslpar + 1) '$' '_';
99 overwrite content (ofslpar + 2 + String.length (string_of_int i)) ')' '_'
100 | Keyword.RightNamed id ->
101 overwrite content (ofslpar + 1 + String.length id) ')' '_'
103 (* Add whitespace so that the column numbers match those of the source file.
104 If requested, add parentheses so that the semantic action can be inserted
105 into other code without ambiguity. *)
108 (String.make (pos1.pos_cnum - pos1.pos_bol - 1) ' ') ^ "(" ^ content ^ ")"
110 (String.make (pos1.pos_cnum - pos1.pos_bol) ' ') ^ content
113 Stretch.stretch_filename = Error.get_filename();
114 Stretch.stretch_linenum = pos1.pos_lnum;
115 Stretch.stretch_linecount = pos2.pos_lnum - pos1.pos_lnum;
116 Stretch.stretch_content = content;
117 Stretch.stretch_raw_content = raw_content;
118 Stretch.stretch_keywords = pkeywords
121 (* Translates the family of position-related keywords to abstract
124 let mk_keyword lexbuf w f n id =
134 Keyword.FlavorPosition
140 Keyword.RightDollar (int_of_string n)
142 Keyword.RightNamed id
148 let keyword = Keyword.Position (subject, where, flavor) in
149 with_cpos lexbuf keyword
151 (* Objective Caml's reserved words. *)
154 let table = Hashtbl.create 149 in
155 List.iter (fun word -> Hashtbl.add table word ()) [
218 Error.error (Positions.one pos) msg
222 let newline = ('\010' | '\013' | "\013\010")
224 let whitespace = [ ' ' '\t' ';' ]
226 let lowercase = ['a'-'z' '\223'-'\246' '\248'-'\255' '_']
228 let uppercase = ['A'-'Z' '\192'-'\214' '\216'-'\222']
230 let identchar = ['A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246' '\248'-'\255' '0'-'9'] (* '\'' forbidden *)
234 (("start" as w) | "end")
235 (("pos" as f) | "ofs")
236 ( '(' ( '$' (['0'-'9']+ as n) | ((lowercase identchar*) as id)) ')')?
266 { let ofs = lexeme_end lexbuf in
267 PERCENTPERCENT (lazy (echunk ofs)) }
286 | (lowercase identchar *) as id
287 { if Hashtbl.mem reserved id then
289 (Positions.with_poss (lexeme_start_p lexbuf) (lexeme_end_p lexbuf) ())
290 "this is an Objective Caml reserved word."
292 LID (with_pos (cpos lexbuf) id)
294 | (uppercase identchar *) as id
295 { UID (with_pos (cpos lexbuf) id) }
296 | "//" [^ '\010' '\013']* newline (* skip C++ style comment *)
298 { update_loc lexbuf; main lexbuf }
302 { comment (lexeme_start_p lexbuf) lexbuf; main lexbuf }
304 { ocamlcomment (lexeme_start_p lexbuf) lexbuf; main lexbuf }
306 { savestart lexbuf (ocamltype (lexeme_end_p lexbuf)) }
308 { savestart lexbuf (fun lexbuf ->
309 let openingpos = lexeme_end_p lexbuf in
310 let closingpos, _ = action true openingpos [] lexbuf in
311 (* TEMPORARY if keyword list nonempty, issue an error *)
312 HEADER (mk_stretch false openingpos closingpos [])
315 { savestart lexbuf (fun lexbuf ->
316 let openingpos = lexeme_end_p lexbuf in
317 let closingpos, pkeywords = action false openingpos [] lexbuf in
318 let stretch = mk_stretch true openingpos closingpos pkeywords in
319 ACTION (Action.from_stretch stretch)
321 (* TEMPORARY comprendre si la différence entre header et action est bien
322 justifiée et si certains choix comme le parenthésage et le
323 traitement des keywords ne pourraient pas être effectués
328 { error1 (lexeme_start_p lexbuf) "unexpected character(s)." }
330 (* Skip C style comments. *)
332 and comment openingpos = parse
334 { update_loc lexbuf; comment openingpos lexbuf }
338 { error1 openingpos "unterminated comment." }
340 { comment openingpos lexbuf }
342 (* Collect an O'Caml type delimited by angle brackets. Angle brackets can
343 appear as part of O'Caml function types. They might also appear as part
344 of O'Caml variant types, but we ignore that possibility for the moment. *)
346 and ocamltype openingpos = parse
348 { ocamltype openingpos lexbuf }
350 { OCAMLTYPE (Stretch.Declared (mk_stretch true openingpos (lexeme_start_p lexbuf) [])) }
352 { ocamlcomment (lexeme_start_p lexbuf) lexbuf; ocamltype openingpos lexbuf }
354 { update_loc lexbuf; ocamltype openingpos lexbuf }
356 { error1 openingpos "unterminated Objective Caml type." }
358 { ocamltype openingpos lexbuf }
360 (* Collect O'Caml code delimited by curly brackets. Any occurrences of
361 the special ``$i'' identifiers are recorded in the accumulating
362 parameter [pkeywords]. Nested curly brackets must be properly
363 counted. Nested parentheses are also kept track of, so as to better
364 report errors when they are not balanced. *)
366 and action percent openingpos pkeywords = parse
368 { let _, pkeywords = action false (lexeme_end_p lexbuf) pkeywords lexbuf in
369 action percent openingpos pkeywords lexbuf }
370 | ("}" | "%}") as delimiter
371 { match percent, delimiter with
374 (* This is the delimiter we were instructed to look for. *)
375 lexeme_start_p lexbuf, pkeywords
377 (* This is not it. *)
378 error1 openingpos "unbalanced opening brace."
381 { let _, pkeywords = parentheses (lexeme_end_p lexbuf) pkeywords lexbuf in
382 action percent openingpos pkeywords lexbuf }
383 | '$' (['0'-'9']+ as n)
384 { let pkeyword = with_cpos lexbuf (Keyword.Dollar (int_of_string n)) in
385 action percent openingpos (pkeyword :: pkeywords) lexbuf }
387 { let pkeyword = mk_keyword lexbuf w f n id in
388 action percent openingpos (pkeyword :: pkeywords) lexbuf }
390 { let pkeyword = with_cpos lexbuf Keyword.PreviousError in
391 action percent openingpos (pkeyword :: pkeywords) lexbuf }
393 { let pkeyword = with_cpos lexbuf Keyword.SyntaxError in
394 action percent openingpos (pkeyword :: pkeywords) lexbuf }
396 { string (lexeme_start_p lexbuf) lexbuf;
397 action percent openingpos pkeywords lexbuf }
400 action percent openingpos pkeywords lexbuf }
402 { ocamlcomment (lexeme_start_p lexbuf) lexbuf;
403 action percent openingpos pkeywords lexbuf }
406 action percent openingpos pkeywords lexbuf }
409 { error1 openingpos "unbalanced opening brace." }
411 { action percent openingpos pkeywords lexbuf }
413 and parentheses openingpos pkeywords = parse
415 { let _, pkeywords = parentheses (lexeme_end_p lexbuf) pkeywords lexbuf in
416 parentheses openingpos pkeywords lexbuf }
418 { lexeme_start_p lexbuf, pkeywords }
420 { let _, pkeywords = action false (lexeme_end_p lexbuf) pkeywords lexbuf in
421 parentheses openingpos pkeywords lexbuf }
422 | '$' (['0'-'9']+ as n)
423 { let pkeyword = with_cpos lexbuf (Keyword.Dollar (int_of_string n)) in
424 parentheses openingpos (pkeyword :: pkeywords) lexbuf }
426 { let pkeyword = mk_keyword lexbuf w f n id in
427 parentheses openingpos (pkeyword :: pkeywords) lexbuf }
429 { let pkeyword = with_cpos lexbuf Keyword.PreviousError in
430 parentheses openingpos (pkeyword :: pkeywords) lexbuf }
432 { let pkeyword = with_cpos lexbuf Keyword.SyntaxError in
433 parentheses openingpos (pkeyword :: pkeywords) lexbuf }
435 { string (lexeme_start_p lexbuf) lexbuf; parentheses openingpos pkeywords lexbuf }
437 { char lexbuf; parentheses openingpos pkeywords lexbuf }
439 { ocamlcomment (lexeme_start_p lexbuf) lexbuf; parentheses openingpos pkeywords lexbuf }
441 { update_loc lexbuf; parentheses openingpos pkeywords lexbuf }
444 { error1 openingpos "unbalanced opening parenthesis." }
446 { parentheses openingpos pkeywords lexbuf }
448 (* Skip O'Caml comments. Comments can be nested and can contain
449 strings or characters, which must be correctly analyzed. (A string
450 could contain begin-of-comment or end-of-comment sequences, which
451 must be ignored; a character could contain a begin-of-string
454 and ocamlcomment openingpos = parse
458 { ocamlcomment (lexeme_start_p lexbuf) lexbuf; ocamlcomment openingpos lexbuf }
460 { string (lexeme_start_p lexbuf) lexbuf; ocamlcomment openingpos lexbuf }
462 { char lexbuf; ocamlcomment openingpos lexbuf }
464 { update_loc lexbuf; ocamlcomment openingpos lexbuf }
466 { error1 openingpos "unterminated Objective Caml comment." }
468 { ocamlcomment openingpos lexbuf }
470 (* Skip O'Caml strings. *)
472 and string openingpos = parse
477 { update_loc lexbuf; string openingpos lexbuf }
479 (* Upon finding a backslash, skip the character that follows,
480 unless it is a newline. Pretty crude, but should work. *)
481 { string openingpos lexbuf }
483 { error1 openingpos "unterminated Objective Caml string." }
485 { string openingpos lexbuf }
487 (* Skip O'Caml characters. A lone quote character is legal inside
488 a comment, so if we don't recognize the matching closing quote,
489 we simply abandon. *)
493 { update_loc lexbuf }
496 | '\\' ['0'-'9'] ['0'-'9'] ['0'-'9'] "'"
497 | '\\' 'x' ['0'-'9' 'a'-'f' 'A'-'F'] ['0'-'9' 'a'-'f' 'A'-'F'] "'"