1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
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 *)
|
1998-11-05 10:53:28 -08:00
|
|
|
(* en Automatique. Distributed only by permission. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* The lexer definition *)
|
|
|
|
|
|
|
|
{
|
|
|
|
open Misc
|
|
|
|
open Parser
|
|
|
|
|
|
|
|
type error =
|
|
|
|
Illegal_character
|
|
|
|
| Unterminated_comment
|
|
|
|
| Unterminated_string
|
|
|
|
|
|
|
|
exception Error of error * int * int
|
|
|
|
|
|
|
|
(* For nested comments *)
|
|
|
|
|
|
|
|
let comment_depth = ref 0
|
|
|
|
|
|
|
|
(* The table of keywords *)
|
|
|
|
|
|
|
|
let keyword_table =
|
|
|
|
create_hashtable 149 [
|
|
|
|
"and", AND;
|
|
|
|
"as", AS;
|
1997-05-13 11:28:25 -07:00
|
|
|
"assert", ASSERT;
|
1995-05-04 03:15:53 -07:00
|
|
|
"begin", BEGIN;
|
1996-04-22 04:15:41 -07:00
|
|
|
"class", CLASS;
|
|
|
|
"constraint", CONSTRAINT;
|
1995-05-04 03:15:53 -07:00
|
|
|
"do", DO;
|
|
|
|
"done", DONE;
|
|
|
|
"downto", DOWNTO;
|
|
|
|
"else", ELSE;
|
|
|
|
"end", END;
|
|
|
|
"exception", EXCEPTION;
|
|
|
|
"external", EXTERNAL;
|
|
|
|
"false", FALSE;
|
|
|
|
"for", FOR;
|
|
|
|
"fun", FUN;
|
|
|
|
"function", FUNCTION;
|
|
|
|
"functor", FUNCTOR;
|
|
|
|
"if", IF;
|
|
|
|
"in", IN;
|
|
|
|
"include", INCLUDE;
|
1996-04-22 04:15:41 -07:00
|
|
|
"inherit", INHERIT;
|
1998-06-24 12:22:26 -07:00
|
|
|
"initializer", INITIALIZER;
|
1997-10-14 06:17:48 -07:00
|
|
|
"lazy", LAZY;
|
1995-05-04 03:15:53 -07:00
|
|
|
"let", LET;
|
|
|
|
"match", MATCH;
|
1996-04-22 04:15:41 -07:00
|
|
|
"method", METHOD;
|
1995-05-04 03:15:53 -07:00
|
|
|
"module", MODULE;
|
|
|
|
"mutable", MUTABLE;
|
1996-04-22 04:15:41 -07:00
|
|
|
"new", NEW;
|
1998-06-24 12:22:26 -07:00
|
|
|
"object", OBJECT;
|
1995-05-04 03:15:53 -07:00
|
|
|
"of", OF;
|
|
|
|
"open", OPEN;
|
|
|
|
"or", OR;
|
1995-11-09 03:08:48 -08:00
|
|
|
"parser", PARSER;
|
1996-04-22 04:15:41 -07:00
|
|
|
"private", PRIVATE;
|
1995-05-04 03:15:53 -07:00
|
|
|
"rec", REC;
|
|
|
|
"sig", SIG;
|
|
|
|
"struct", STRUCT;
|
|
|
|
"then", THEN;
|
|
|
|
"to", TO;
|
|
|
|
"true", TRUE;
|
|
|
|
"try", TRY;
|
|
|
|
"type", TYPE;
|
|
|
|
"val", VAL;
|
1996-04-22 04:15:41 -07:00
|
|
|
"virtual", VIRTUAL;
|
1995-05-04 03:15:53 -07:00
|
|
|
"when", WHEN;
|
|
|
|
"while", WHILE;
|
|
|
|
"with", WITH;
|
|
|
|
|
|
|
|
"mod", INFIXOP3("mod");
|
|
|
|
"land", INFIXOP3("land");
|
|
|
|
"lor", INFIXOP3("lor");
|
|
|
|
"lxor", INFIXOP3("lxor");
|
|
|
|
"lsl", INFIXOP4("lsl");
|
|
|
|
"lsr", INFIXOP4("lsr");
|
|
|
|
"asr", INFIXOP4("asr")
|
|
|
|
]
|
|
|
|
|
|
|
|
(* To buffer string literals *)
|
|
|
|
|
|
|
|
let initial_string_buffer = String.create 256
|
|
|
|
let string_buff = ref initial_string_buffer
|
|
|
|
let string_index = ref 0
|
|
|
|
|
|
|
|
let reset_string_buffer () =
|
|
|
|
string_buff := initial_string_buffer;
|
|
|
|
string_index := 0
|
|
|
|
|
|
|
|
let store_string_char c =
|
|
|
|
if !string_index >= String.length (!string_buff) then begin
|
|
|
|
let new_buff = String.create (String.length (!string_buff) * 2) in
|
|
|
|
String.blit (!string_buff) 0 new_buff 0 (String.length (!string_buff));
|
|
|
|
string_buff := new_buff
|
|
|
|
end;
|
|
|
|
String.unsafe_set (!string_buff) (!string_index) c;
|
|
|
|
incr string_index
|
|
|
|
|
|
|
|
let get_stored_string () =
|
|
|
|
let s = String.sub (!string_buff) 0 (!string_index) in
|
|
|
|
string_buff := initial_string_buffer;
|
|
|
|
s
|
|
|
|
|
|
|
|
(* To translate escape sequences *)
|
|
|
|
|
1996-11-02 10:03:23 -08:00
|
|
|
let char_for_backslash =
|
1996-11-07 03:01:05 -08:00
|
|
|
match Sys.os_type with
|
1996-11-02 10:03:23 -08:00
|
|
|
| "Unix" | "Win32" ->
|
|
|
|
begin function
|
|
|
|
| 'n' -> '\010'
|
|
|
|
| 'r' -> '\013'
|
|
|
|
| 'b' -> '\008'
|
|
|
|
| 't' -> '\009'
|
|
|
|
| c -> c
|
1997-01-05 06:03:14 -08:00
|
|
|
end
|
1996-11-02 10:03:23 -08:00
|
|
|
| "MacOS" ->
|
|
|
|
begin function
|
|
|
|
| 'n' -> '\013'
|
|
|
|
| 'r' -> '\010'
|
|
|
|
| 'b' -> '\008'
|
|
|
|
| 't' -> '\009'
|
|
|
|
| c -> c
|
1997-01-05 06:03:14 -08:00
|
|
|
end
|
1996-11-07 03:01:05 -08:00
|
|
|
| x -> fatal_error "Lexer: unknown system type"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let char_for_decimal_code lexbuf i =
|
1995-08-08 06:37:34 -07:00
|
|
|
let c = 100 * (Char.code(Lexing.lexeme_char lexbuf i) - 48) +
|
|
|
|
10 * (Char.code(Lexing.lexeme_char lexbuf (i+1)) - 48) +
|
|
|
|
(Char.code(Lexing.lexeme_char lexbuf (i+2)) - 48) in
|
|
|
|
Char.chr(c land 0xFF)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-11-05 10:53:28 -08:00
|
|
|
(* To store the position of the beginning of a string and comment *)
|
|
|
|
let string_start_pos = ref 0
|
|
|
|
and comment_start_pos = ref 0
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Error report *)
|
|
|
|
|
|
|
|
open Format
|
|
|
|
|
|
|
|
let report_error = function
|
|
|
|
Illegal_character ->
|
|
|
|
print_string "Illegal character"
|
|
|
|
| Unterminated_comment ->
|
|
|
|
print_string "Comment not terminated"
|
|
|
|
| Unterminated_string ->
|
|
|
|
print_string "String literal not terminated"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1998-04-07 05:50:19 -07:00
|
|
|
let blank = [' ' '\010' '\013' '\009' '\012']
|
|
|
|
let lowercase = ['a'-'z' '\223'-'\246' '\248'-'\255' '_']
|
|
|
|
let uppercase = ['A'-'Z' '\192'-'\214' '\216'-'\222']
|
|
|
|
let identchar =
|
|
|
|
['A'-'Z' 'a'-'z' '_' '\192'-'\214' '\216'-'\246' '\248'-'\255' '\'' '0'-'9']
|
|
|
|
let symbolchar =
|
|
|
|
['!' '$' '%' '&' '*' '+' '-' '.' '/' ':' '<' '=' '>' '?' '@' '^' '|' '~']
|
|
|
|
let decimal_literal = ['0'-'9']+
|
|
|
|
let hex_literal = '0' ['x' 'X'] ['0'-'9' 'A'-'F' 'a'-'f']+
|
|
|
|
let oct_literal = '0' ['o' 'O'] ['0'-'7']+
|
|
|
|
let bin_literal = '0' ['b' 'B'] ['0'-'1']+
|
|
|
|
let float_literal =
|
|
|
|
['0'-'9']+ ('.' ['0'-'9']*)? (['e' 'E'] ['+' '-']? ['0'-'9']+)?
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
rule token = parse
|
1998-04-07 05:50:19 -07:00
|
|
|
blank +
|
1995-05-04 03:15:53 -07:00
|
|
|
{ token lexbuf }
|
1998-04-07 05:50:19 -07:00
|
|
|
| "_"
|
|
|
|
{ UNDERSCORE }
|
|
|
|
| lowercase identchar *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ let s = Lexing.lexeme lexbuf in
|
|
|
|
try
|
|
|
|
Hashtbl.find keyword_table s
|
|
|
|
with Not_found ->
|
|
|
|
LIDENT s }
|
1998-04-07 05:50:19 -07:00
|
|
|
| uppercase identchar *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ UIDENT(Lexing.lexeme lexbuf) } (* No capitalized keywords *)
|
1998-04-07 05:50:19 -07:00
|
|
|
| decimal_literal | hex_literal | oct_literal | bin_literal
|
1995-05-04 03:15:53 -07:00
|
|
|
{ INT (int_of_string(Lexing.lexeme lexbuf)) }
|
1998-04-07 05:50:19 -07:00
|
|
|
| float_literal
|
1995-05-04 03:15:53 -07:00
|
|
|
{ FLOAT (Lexing.lexeme lexbuf) }
|
|
|
|
| "\""
|
|
|
|
{ reset_string_buffer();
|
|
|
|
let string_start = Lexing.lexeme_start lexbuf in
|
1998-11-05 10:53:28 -08:00
|
|
|
string_start_pos := string_start;
|
1995-05-04 03:15:53 -07:00
|
|
|
string lexbuf;
|
1996-02-25 06:45:47 -08:00
|
|
|
lexbuf.Lexing.lex_start_pos <-
|
|
|
|
string_start - lexbuf.Lexing.lex_abs_pos;
|
1995-05-04 03:15:53 -07:00
|
|
|
STRING (get_stored_string()) }
|
|
|
|
| "'" [^ '\\' '\''] "'"
|
|
|
|
{ CHAR(Lexing.lexeme_char lexbuf 1) }
|
|
|
|
| "'" '\\' ['\\' '\'' 'n' 't' 'b' 'r'] "'"
|
|
|
|
{ CHAR(char_for_backslash (Lexing.lexeme_char lexbuf 2)) }
|
|
|
|
| "'" '\\' ['0'-'9'] ['0'-'9'] ['0'-'9'] "'"
|
|
|
|
{ CHAR(char_for_decimal_code lexbuf 2) }
|
|
|
|
| "(*"
|
|
|
|
{ comment_depth := 1;
|
1998-11-05 10:53:28 -08:00
|
|
|
comment_start_pos := Lexing.lexeme_start lexbuf;
|
1995-05-04 03:15:53 -07:00
|
|
|
comment lexbuf;
|
|
|
|
token lexbuf }
|
1997-02-04 07:40:22 -08:00
|
|
|
| "#" [' ' '\t']* ['0'-'9']+ [' ' '\t']* "\"" [^ '\n' '\r'] *
|
|
|
|
('\n' | '\r' | "\r\n")
|
1997-01-01 07:36:18 -08:00
|
|
|
(* # linenum "filename" flags \n *)
|
|
|
|
{ token lexbuf }
|
1996-04-30 07:53:58 -07:00
|
|
|
| "#" { SHARP }
|
|
|
|
| "&" { AMPERSAND }
|
1995-12-15 02:20:24 -08:00
|
|
|
| "&&" { AMPERAMPER }
|
1996-04-30 07:53:58 -07:00
|
|
|
| "'" { QUOTE }
|
|
|
|
| "(" { LPAREN }
|
|
|
|
| ")" { RPAREN }
|
|
|
|
| "*" { STAR }
|
|
|
|
| "," { COMMA }
|
|
|
|
| "?" { QUESTION }
|
1995-05-04 03:15:53 -07:00
|
|
|
| "->" { MINUSGREATER }
|
1996-04-30 07:53:58 -07:00
|
|
|
| "." { DOT }
|
1995-05-04 03:15:53 -07:00
|
|
|
| ".." { DOTDOT }
|
1996-04-30 07:53:58 -07:00
|
|
|
| ":" { COLON }
|
1995-05-04 03:15:53 -07:00
|
|
|
| "::" { COLONCOLON }
|
|
|
|
| ":=" { COLONEQUAL }
|
1996-04-22 04:15:41 -07:00
|
|
|
| ":>" { COLONGREATER }
|
1996-04-30 07:53:58 -07:00
|
|
|
| ";" { SEMI }
|
1995-05-04 03:15:53 -07:00
|
|
|
| ";;" { SEMISEMI }
|
1996-04-30 07:53:58 -07:00
|
|
|
| "<" { LESS }
|
1995-05-04 03:15:53 -07:00
|
|
|
| "<-" { LESSMINUS }
|
1996-04-30 07:53:58 -07:00
|
|
|
| "=" { EQUAL }
|
|
|
|
| "[" { LBRACKET }
|
1995-05-04 03:15:53 -07:00
|
|
|
| "[|" { LBRACKETBAR }
|
1995-11-28 07:00:53 -08:00
|
|
|
| "[<" { LBRACKETLESS }
|
1996-04-30 07:53:58 -07:00
|
|
|
| "]" { RBRACKET }
|
|
|
|
| "{" { LBRACE }
|
1996-04-22 04:15:41 -07:00
|
|
|
| "{<" { LBRACELESS }
|
1996-04-30 07:53:58 -07:00
|
|
|
| "|" { BAR }
|
1995-12-15 02:20:24 -08:00
|
|
|
| "||" { BARBAR }
|
1995-05-04 03:15:53 -07:00
|
|
|
| "|]" { BARRBRACKET }
|
1996-04-30 07:53:58 -07:00
|
|
|
| ">" { GREATER }
|
1995-11-28 07:00:53 -08:00
|
|
|
| ">]" { GREATERRBRACKET }
|
1996-04-30 07:53:58 -07:00
|
|
|
| "}" { RBRACE }
|
1996-04-22 04:15:41 -07:00
|
|
|
| ">}" { GREATERRBRACE }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-30 07:53:58 -07:00
|
|
|
| "!=" { INFIXOP0 "!=" }
|
|
|
|
| "-" { SUBTRACTIVE "-" }
|
|
|
|
| "-." { SUBTRACTIVE "-." }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-04-07 05:50:19 -07:00
|
|
|
| ['!' '?' '~'] symbolchar *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ PREFIXOP(Lexing.lexeme lexbuf) }
|
1998-04-07 05:50:19 -07:00
|
|
|
| ['=' '<' '>' '|' '&' '$'] symbolchar *
|
1996-04-30 07:53:58 -07:00
|
|
|
{ INFIXOP0(Lexing.lexeme lexbuf) }
|
1998-04-07 05:50:19 -07:00
|
|
|
| ['@' '^'] symbolchar *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ INFIXOP1(Lexing.lexeme lexbuf) }
|
1998-04-07 05:50:19 -07:00
|
|
|
| ['+' '-'] symbolchar *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ INFIXOP2(Lexing.lexeme lexbuf) }
|
1998-04-07 05:50:19 -07:00
|
|
|
| "**" symbolchar *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ INFIXOP4(Lexing.lexeme lexbuf) }
|
1998-04-07 05:50:19 -07:00
|
|
|
| ['*' '/' '%'] symbolchar *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ INFIXOP3(Lexing.lexeme lexbuf) }
|
|
|
|
| eof { EOF }
|
|
|
|
| _
|
|
|
|
{ raise (Error(Illegal_character,
|
|
|
|
Lexing.lexeme_start lexbuf, Lexing.lexeme_end lexbuf)) }
|
|
|
|
|
|
|
|
and comment = parse
|
|
|
|
"(*"
|
|
|
|
{ comment_depth := succ !comment_depth; comment lexbuf }
|
|
|
|
| "*)"
|
|
|
|
{ comment_depth := pred !comment_depth;
|
|
|
|
if !comment_depth > 0 then comment lexbuf }
|
1995-08-08 06:37:34 -07:00
|
|
|
| "\""
|
|
|
|
{ reset_string_buffer();
|
1998-11-05 10:53:28 -08:00
|
|
|
string_start_pos := Lexing.lexeme_start lexbuf;
|
1995-08-08 06:37:34 -07:00
|
|
|
string lexbuf;
|
|
|
|
string_buff := initial_string_buffer;
|
|
|
|
comment lexbuf }
|
|
|
|
| "''"
|
|
|
|
{ comment lexbuf }
|
|
|
|
| "'" [^ '\\' '\''] "'"
|
|
|
|
{ comment lexbuf }
|
|
|
|
| "'\\" ['\\' '\'' 'n' 't' 'b' 'r'] "'"
|
|
|
|
{ comment lexbuf }
|
|
|
|
| "'\\" ['0'-'9'] ['0'-'9'] ['0'-'9'] "'"
|
|
|
|
{ comment lexbuf }
|
1995-05-04 03:15:53 -07:00
|
|
|
| eof
|
1998-11-05 10:53:28 -08:00
|
|
|
{ raise (Error (Unterminated_comment,
|
|
|
|
!comment_start_pos, !comment_start_pos+2)) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| _
|
|
|
|
{ comment lexbuf }
|
|
|
|
|
|
|
|
and string = parse
|
|
|
|
'"'
|
|
|
|
{ () }
|
1996-02-01 07:04:05 -08:00
|
|
|
| '\\' ("\010" | "\013" | "\013\010") [' ' '\009'] *
|
1995-05-04 03:15:53 -07:00
|
|
|
{ string lexbuf }
|
|
|
|
| '\\' ['\\' '"' 'n' 't' 'b' 'r']
|
|
|
|
{ store_string_char(char_for_backslash(Lexing.lexeme_char lexbuf 1));
|
|
|
|
string lexbuf }
|
|
|
|
| '\\' ['0'-'9'] ['0'-'9'] ['0'-'9']
|
|
|
|
{ store_string_char(char_for_decimal_code lexbuf 1);
|
|
|
|
string lexbuf }
|
|
|
|
| eof
|
1998-11-05 10:53:28 -08:00
|
|
|
{ raise (Error (Unterminated_string,
|
|
|
|
!string_start_pos, !string_start_pos+1)) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| _
|
|
|
|
{ store_string_char(Lexing.lexeme_char lexbuf 0);
|
|
|
|
string lexbuf }
|