1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* The lexical analyzer for lexer definitions. Bootstrapped! *)
|
|
|
|
|
|
|
|
{
|
|
|
|
open Syntax
|
|
|
|
open Parser
|
|
|
|
|
|
|
|
(* Auxiliaries for the lexical analyzer *)
|
|
|
|
|
|
|
|
let brace_depth = ref 0
|
|
|
|
and comment_depth = ref 0
|
|
|
|
|
2002-10-31 06:21:20 -08:00
|
|
|
let in_pattern () = !brace_depth = 0 && !comment_depth = 0
|
|
|
|
|
2004-04-21 16:26:06 -07:00
|
|
|
exception Lexical_error of string * string * int * int
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-10-31 06:21:20 -08:00
|
|
|
let string_buff = Buffer.create 256
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2006-01-04 08:55:50 -08:00
|
|
|
let reset_string_buffer () = Buffer.clear string_buff
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-10-31 06:21:20 -08:00
|
|
|
let store_string_char c = Buffer.add_char string_buff c
|
2014-01-22 05:31:54 -08:00
|
|
|
let store_string_chars s = Buffer.add_string string_buff s
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-10-31 06:21:20 -08:00
|
|
|
let get_stored_string () = Buffer.contents string_buff
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let char_for_backslash = function
|
2012-05-02 07:41:55 -07:00
|
|
|
'n' -> '\010'
|
|
|
|
| 'r' -> '\013'
|
|
|
|
| 'b' -> '\008'
|
|
|
|
| 't' -> '\009'
|
1995-05-04 03:15:53 -07:00
|
|
|
| c -> c
|
|
|
|
|
2004-04-21 16:26:06 -07:00
|
|
|
let raise_lexical_error lexbuf msg =
|
|
|
|
let p = Lexing.lexeme_start_p lexbuf in
|
|
|
|
raise (Lexical_error (msg,
|
|
|
|
p.Lexing.pos_fname,
|
|
|
|
p.Lexing.pos_lnum,
|
|
|
|
p.Lexing.pos_cnum - p.Lexing.pos_bol + 1))
|
|
|
|
;;
|
1998-04-23 01:24:50 -07:00
|
|
|
|
|
|
|
let handle_lexical_error fn lexbuf =
|
2004-04-21 16:26:06 -07:00
|
|
|
let p = Lexing.lexeme_start_p lexbuf in
|
|
|
|
let line = p.Lexing.pos_lnum
|
|
|
|
and column = p.Lexing.pos_cnum - p.Lexing.pos_bol + 1
|
|
|
|
and file = p.Lexing.pos_fname
|
|
|
|
in
|
1998-04-23 01:24:50 -07:00
|
|
|
try
|
|
|
|
fn lexbuf
|
2004-04-21 16:26:06 -07:00
|
|
|
with Lexical_error (msg, "", 0, 0) ->
|
|
|
|
raise(Lexical_error(msg, file, line, column))
|
2002-10-31 06:21:20 -08:00
|
|
|
|
|
|
|
let get_input_name () = Sys.argv.(Array.length Sys.argv - 1)
|
2006-01-04 08:55:50 -08:00
|
|
|
|
2002-10-31 06:21:20 -08:00
|
|
|
let warning lexbuf msg =
|
2004-04-21 16:26:06 -07:00
|
|
|
let p = Lexing.lexeme_start_p lexbuf in
|
2002-10-31 06:21:20 -08:00
|
|
|
Printf.eprintf "ocamllex warning:\nFile \"%s\", line %d, character %d: %s.\n"
|
2004-04-21 16:26:06 -07:00
|
|
|
p.Lexing.pos_fname p.Lexing.pos_lnum
|
|
|
|
(p.Lexing.pos_cnum - p.Lexing.pos_bol + 1) msg;
|
2002-10-31 06:21:20 -08:00
|
|
|
flush stderr
|
|
|
|
|
|
|
|
let decimal_code c d u =
|
|
|
|
100 * (Char.code c - 48) + 10 * (Char.code d - 48) + (Char.code u - 48)
|
|
|
|
|
|
|
|
let char_for_hexadecimal_code d u =
|
|
|
|
let d1 = Char.code d in
|
|
|
|
let val1 = if d1 >= 97 then d1 - 87
|
|
|
|
else if d1 >= 65 then d1 - 55
|
|
|
|
else d1 - 48
|
|
|
|
in
|
|
|
|
let d2 = Char.code u in
|
|
|
|
let val2 = if d2 >= 97 then d2 - 87
|
|
|
|
else if d2 >= 65 then d2 - 55
|
|
|
|
else d2 - 48
|
|
|
|
in
|
|
|
|
Char.chr (val1 * 16 + val2)
|
|
|
|
|
2004-04-21 16:26:06 -07:00
|
|
|
let incr_loc lexbuf delta =
|
|
|
|
let pos = lexbuf.Lexing.lex_curr_p in
|
|
|
|
lexbuf.Lexing.lex_curr_p <- { pos with
|
|
|
|
Lexing.pos_lnum = pos.Lexing.pos_lnum + 1;
|
|
|
|
Lexing.pos_bol = pos.Lexing.pos_cnum - delta;
|
|
|
|
}
|
|
|
|
;;
|
|
|
|
|
|
|
|
let update_loc lexbuf opt_file line =
|
|
|
|
let pos = lexbuf.Lexing.lex_curr_p in
|
|
|
|
let new_file = match opt_file with
|
|
|
|
| None -> pos.Lexing.pos_fname
|
|
|
|
| Some f -> f
|
|
|
|
in
|
|
|
|
lexbuf.Lexing.lex_curr_p <- { pos with
|
|
|
|
Lexing.pos_fname = new_file;
|
|
|
|
Lexing.pos_lnum = line;
|
|
|
|
Lexing.pos_bol = pos.Lexing.pos_cnum;
|
|
|
|
}
|
|
|
|
;;
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2002-10-31 06:21:20 -08:00
|
|
|
let identstart =
|
|
|
|
['A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246' '\248'-'\255']
|
|
|
|
let identbody =
|
|
|
|
['A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246' '\248'-'\255' '\'' '0'-'9']
|
|
|
|
let backslash_escapes =
|
2012-05-02 07:41:55 -07:00
|
|
|
['\\' '\'' '"' 'n' 't' 'b' 'r' ' ']
|
2002-10-31 06:21:20 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
rule main = parse
|
2006-01-04 08:55:50 -08:00
|
|
|
[' ' '\013' '\009' '\012' ] +
|
1995-05-04 03:15:53 -07:00
|
|
|
{ main lexbuf }
|
1998-04-23 01:24:50 -07:00
|
|
|
| '\010'
|
2004-04-21 16:26:06 -07:00
|
|
|
{ incr_loc lexbuf 0;
|
1998-04-23 01:24:50 -07:00
|
|
|
main lexbuf }
|
2004-04-21 16:26:06 -07:00
|
|
|
| "#" [' ' '\t']* (['0'-'9']+ as num) [' ' '\t']*
|
|
|
|
('\"' ([^ '\010' '\013' '\"']* as name) '\"')?
|
|
|
|
[^ '\010' '\013']* '\010'
|
|
|
|
{ update_loc lexbuf name (int_of_string num);
|
|
|
|
main lexbuf
|
|
|
|
}
|
2006-01-04 08:55:50 -08:00
|
|
|
| "(*"
|
1995-05-04 03:15:53 -07:00
|
|
|
{ comment_depth := 1;
|
1998-04-23 01:24:50 -07:00
|
|
|
handle_lexical_error comment lexbuf;
|
1995-05-04 03:15:53 -07:00
|
|
|
main lexbuf }
|
2002-10-31 06:21:20 -08:00
|
|
|
| '_' { Tunderscore }
|
|
|
|
| identstart identbody *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ match Lexing.lexeme lexbuf with
|
|
|
|
"rule" -> Trule
|
|
|
|
| "parse" -> Tparse
|
2002-10-28 08:46:50 -08:00
|
|
|
| "shortest" -> Tparse_shortest
|
1995-05-04 03:15:53 -07:00
|
|
|
| "and" -> Tand
|
|
|
|
| "eof" -> Teof
|
1998-04-07 05:50:19 -07:00
|
|
|
| "let" -> Tlet
|
2002-10-28 08:46:50 -08:00
|
|
|
| "as" -> Tas
|
2014-03-14 08:39:32 -07:00
|
|
|
| "refill" -> Trefill
|
1995-05-04 03:15:53 -07:00
|
|
|
| s -> Tident s }
|
2006-01-04 08:55:50 -08:00
|
|
|
| '"'
|
1995-05-04 03:15:53 -07:00
|
|
|
{ reset_string_buffer();
|
1998-04-23 01:24:50 -07:00
|
|
|
handle_lexical_error string lexbuf;
|
1995-05-04 03:15:53 -07:00
|
|
|
Tstring(get_stored_string()) }
|
2006-01-04 08:55:50 -08:00
|
|
|
(* note: ''' is a valid character literal (by contrast with the compiler) *)
|
|
|
|
| "'" [^ '\\'] "'"
|
1997-04-15 12:18:03 -07:00
|
|
|
{ Tchar(Char.code(Lexing.lexeme_char lexbuf 1)) }
|
2006-01-04 08:55:50 -08:00
|
|
|
| "'" '\\' backslash_escapes "'"
|
1997-04-15 12:18:03 -07:00
|
|
|
{ Tchar(Char.code(char_for_backslash (Lexing.lexeme_char lexbuf 2))) }
|
2006-01-04 08:55:50 -08:00
|
|
|
| "'" '\\' (['0'-'9'] as c) (['0'-'9'] as d) (['0'-'9'] as u)"'"
|
2002-10-31 06:21:20 -08:00
|
|
|
{ let v = decimal_code c d u in
|
|
|
|
if v > 255 then
|
2004-04-21 16:26:06 -07:00
|
|
|
raise_lexical_error lexbuf
|
|
|
|
(Printf.sprintf "illegal escape sequence \\%c%c%c" c d u)
|
2002-10-31 06:21:20 -08:00
|
|
|
else
|
|
|
|
Tchar v }
|
|
|
|
| "'" '\\' 'x'
|
|
|
|
(['0'-'9' 'a'-'f' 'A'-'F'] as d) (['0'-'9' 'a'-'f' 'A'-'F'] as u) "'"
|
|
|
|
{ Tchar(Char.code(char_for_hexadecimal_code d u)) }
|
|
|
|
| "'" '\\' (_ as c)
|
2004-04-21 16:26:06 -07:00
|
|
|
{ raise_lexical_error lexbuf
|
|
|
|
(Printf.sprintf "illegal escape sequence \\%c" c)
|
2002-10-31 06:21:20 -08:00
|
|
|
}
|
2006-01-04 08:55:50 -08:00
|
|
|
| '{'
|
2004-04-21 16:26:06 -07:00
|
|
|
{ let p = Lexing.lexeme_end_p lexbuf in
|
2013-09-04 08:12:37 -07:00
|
|
|
let f = p.Lexing.pos_fname in
|
2004-04-21 16:26:06 -07:00
|
|
|
let n1 = p.Lexing.pos_cnum
|
|
|
|
and l1 = p.Lexing.pos_lnum
|
|
|
|
and s1 = p.Lexing.pos_bol in
|
1998-04-23 01:24:50 -07:00
|
|
|
brace_depth := 1;
|
|
|
|
let n2 = handle_lexical_error action lexbuf in
|
2013-09-04 08:12:37 -07:00
|
|
|
Taction({loc_file = f; start_pos = n1; end_pos = n2;
|
1998-04-23 01:24:50 -07:00
|
|
|
start_line = l1; start_col = n1 - s1}) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| '=' { Tequal }
|
|
|
|
| '|' { Tor }
|
|
|
|
| '[' { Tlbracket }
|
|
|
|
| ']' { Trbracket }
|
|
|
|
| '*' { Tstar }
|
|
|
|
| '?' { Tmaybe }
|
|
|
|
| '+' { Tplus }
|
|
|
|
| '(' { Tlparen }
|
|
|
|
| ')' { Trparen }
|
|
|
|
| '^' { Tcaret }
|
|
|
|
| '-' { Tdash }
|
2004-04-29 04:12:49 -07:00
|
|
|
| '#' { Tsharp }
|
1995-05-04 03:15:53 -07:00
|
|
|
| eof { Tend }
|
|
|
|
| _
|
2004-04-21 16:26:06 -07:00
|
|
|
{ raise_lexical_error lexbuf
|
|
|
|
("illegal character " ^ String.escaped(Lexing.lexeme lexbuf))
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-10-31 06:21:20 -08:00
|
|
|
|
|
|
|
(* String parsing comes from the compiler lexer *)
|
1995-05-04 03:15:53 -07:00
|
|
|
and string = parse
|
2006-01-04 08:55:50 -08:00
|
|
|
'"'
|
1995-05-04 03:15:53 -07:00
|
|
|
{ () }
|
2014-01-22 05:31:54 -08:00
|
|
|
| '\\' ('\013'* '\010') ([' ' '\009'] * as spaces)
|
2004-04-21 16:26:06 -07:00
|
|
|
{ incr_loc lexbuf (String.length spaces);
|
1998-04-23 01:24:50 -07:00
|
|
|
string lexbuf }
|
2002-10-31 06:21:20 -08:00
|
|
|
| '\\' (backslash_escapes as c)
|
|
|
|
{ store_string_char(char_for_backslash c);
|
|
|
|
string lexbuf }
|
|
|
|
| '\\' (['0'-'9'] as c) (['0'-'9'] as d) (['0'-'9'] as u)
|
|
|
|
{ let v = decimal_code c d u in
|
|
|
|
if in_pattern () && v > 255 then
|
|
|
|
warning lexbuf
|
|
|
|
(Printf.sprintf
|
2014-01-22 05:31:54 -08:00
|
|
|
"illegal backslash escape in string: '\\%c%c%c'" c d u) ;
|
2002-10-31 06:21:20 -08:00
|
|
|
store_string_char (Char.chr v);
|
1995-05-04 03:15:53 -07:00
|
|
|
string lexbuf }
|
2002-10-31 06:21:20 -08:00
|
|
|
| '\\' 'x' (['0'-'9' 'a'-'f' 'A'-'F'] as d) (['0'-'9' 'a'-'f' 'A'-'F'] as u)
|
|
|
|
{ store_string_char (char_for_hexadecimal_code d u) ;
|
|
|
|
string lexbuf }
|
|
|
|
| '\\' (_ as c)
|
|
|
|
{if in_pattern () then
|
|
|
|
warning lexbuf
|
2014-01-22 05:31:54 -08:00
|
|
|
(Printf.sprintf "illegal backslash escape in string: '\\%c'" c) ;
|
2002-10-31 06:21:20 -08:00
|
|
|
store_string_char '\\' ;
|
|
|
|
store_string_char c ;
|
1995-05-04 03:15:53 -07:00
|
|
|
string lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| eof
|
2004-04-21 16:26:06 -07:00
|
|
|
{ raise(Lexical_error("unterminated string", "", 0, 0)) }
|
2014-01-22 05:31:54 -08:00
|
|
|
| '\013'* '\010' as s
|
|
|
|
{ warning lexbuf (Printf.sprintf "unescaped newline in string") ;
|
|
|
|
store_string_chars s;
|
2004-04-21 16:26:06 -07:00
|
|
|
incr_loc lexbuf 0;
|
1998-04-23 01:24:50 -07:00
|
|
|
string lexbuf }
|
2002-10-31 06:21:20 -08:00
|
|
|
| _ as c
|
|
|
|
{ store_string_char c;
|
1995-05-04 03:15:53 -07:00
|
|
|
string lexbuf }
|
|
|
|
|
2002-10-31 06:21:20 -08:00
|
|
|
(*
|
|
|
|
Lexers comment and action are quite similar,
|
|
|
|
they should lex both strings and characters,
|
|
|
|
in order not to be confused by what is inside then
|
|
|
|
*)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
and comment = parse
|
2006-01-04 08:55:50 -08:00
|
|
|
"(*"
|
1995-05-04 03:15:53 -07:00
|
|
|
{ incr comment_depth; comment lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| "*)"
|
1995-05-04 03:15:53 -07:00
|
|
|
{ decr comment_depth;
|
1998-04-23 01:24:50 -07:00
|
|
|
if !comment_depth = 0 then () else comment lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| '"'
|
1995-05-04 03:15:53 -07:00
|
|
|
{ reset_string_buffer();
|
|
|
|
string lexbuf;
|
|
|
|
reset_string_buffer();
|
|
|
|
comment lexbuf }
|
2002-10-31 06:21:20 -08:00
|
|
|
| "'"
|
|
|
|
{ skip_char lexbuf ;
|
|
|
|
comment lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| eof
|
2004-04-21 16:26:06 -07:00
|
|
|
{ raise(Lexical_error("unterminated comment", "", 0, 0)) }
|
1998-04-23 01:24:50 -07:00
|
|
|
| '\010'
|
2004-04-21 16:26:06 -07:00
|
|
|
{ incr_loc lexbuf 0;
|
1998-04-23 01:24:50 -07:00
|
|
|
comment lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| _
|
1995-05-04 03:15:53 -07:00
|
|
|
{ comment lexbuf }
|
2002-10-31 06:21:20 -08:00
|
|
|
|
|
|
|
and action = parse
|
2006-01-04 08:55:50 -08:00
|
|
|
'{'
|
2002-10-31 06:21:20 -08:00
|
|
|
{ incr brace_depth;
|
|
|
|
action lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| '}'
|
2002-10-31 06:21:20 -08:00
|
|
|
{ decr brace_depth;
|
|
|
|
if !brace_depth = 0 then Lexing.lexeme_start lexbuf else action lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| '"'
|
2002-10-31 06:21:20 -08:00
|
|
|
{ reset_string_buffer();
|
|
|
|
handle_lexical_error string lexbuf;
|
|
|
|
reset_string_buffer();
|
|
|
|
action lexbuf }
|
|
|
|
| "'"
|
|
|
|
{ skip_char lexbuf ;
|
|
|
|
action lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| "(*"
|
2002-10-31 06:21:20 -08:00
|
|
|
{ comment_depth := 1;
|
|
|
|
comment lexbuf;
|
|
|
|
action lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| eof
|
2004-04-21 16:26:06 -07:00
|
|
|
{ raise (Lexical_error("unterminated action", "", 0, 0)) }
|
2002-10-31 06:21:20 -08:00
|
|
|
| '\010'
|
2004-04-21 16:26:06 -07:00
|
|
|
{ incr_loc lexbuf 0;
|
2002-10-31 06:21:20 -08:00
|
|
|
action lexbuf }
|
2006-01-04 08:55:50 -08:00
|
|
|
| _
|
2002-10-31 06:21:20 -08:00
|
|
|
{ action lexbuf }
|
|
|
|
|
|
|
|
and skip_char = parse
|
|
|
|
| '\\'? '\010' "'"
|
2004-04-21 16:26:06 -07:00
|
|
|
{ incr_loc lexbuf 1;
|
|
|
|
}
|
2002-10-31 06:21:20 -08:00
|
|
|
| [^ '\\' '\''] "'" (* regular character *)
|
|
|
|
(* one character and numeric escape sequences *)
|
|
|
|
| '\\' _ "'"
|
|
|
|
| '\\' ['0'-'9'] ['0'-'9'] ['0'-'9'] "'"
|
|
|
|
| '\\' 'x' ['0'-'9' 'a'-'f' 'A'-'F'] ['0'-'9' 'a'-'f' 'A'-'F'] "'"
|
|
|
|
{()}
|
|
|
|
(* A dieu va ! *)
|
2006-01-04 08:55:50 -08:00
|
|
|
| "" {()}
|