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 *)
|
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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
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
|
|
|
|
|
1998-04-23 01:24:50 -07:00
|
|
|
exception Lexical_error of string * int * int
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
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 =
|
1996-12-03 07:52:50 -08:00
|
|
|
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;
|
1995-05-04 03:15:53 -07:00
|
|
|
!string_buff.[!string_index] <- c;
|
|
|
|
incr string_index
|
|
|
|
|
|
|
|
let get_stored_string () =
|
1996-12-03 07:52:50 -08:00
|
|
|
String.sub !string_buff 0 !string_index
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let char_for_backslash = function
|
1995-08-08 06:37:34 -07:00
|
|
|
'n' -> '\n'
|
|
|
|
| 't' -> '\t'
|
|
|
|
| 'b' -> '\b'
|
|
|
|
| 'r' -> '\r'
|
1995-05-04 03:15:53 -07:00
|
|
|
| c -> c
|
|
|
|
|
|
|
|
let char_for_decimal_code lexbuf i =
|
|
|
|
Char.chr(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))
|
|
|
|
|
1998-04-23 01:24:50 -07:00
|
|
|
let line_num = ref 1
|
|
|
|
let line_start_pos = ref 0
|
|
|
|
|
|
|
|
let handle_lexical_error fn lexbuf =
|
|
|
|
let line = !line_num
|
|
|
|
and column = Lexing.lexeme_start lexbuf - !line_start_pos in
|
|
|
|
try
|
|
|
|
fn lexbuf
|
|
|
|
with Lexical_error(msg, _, _) ->
|
|
|
|
raise(Lexical_error(msg, line, column))
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rule main = parse
|
1998-04-23 01:24:50 -07:00
|
|
|
[' ' '\013' '\009' '\012' ] +
|
1995-05-04 03:15:53 -07:00
|
|
|
{ main lexbuf }
|
1998-04-23 01:24:50 -07:00
|
|
|
| '\010'
|
|
|
|
{ line_start_pos := Lexing.lexeme_end lexbuf;
|
|
|
|
incr line_num;
|
|
|
|
main lexbuf }
|
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 }
|
|
|
|
| ['A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '\'' '_' '0'-'9'] *
|
|
|
|
{ match Lexing.lexeme lexbuf with
|
|
|
|
"rule" -> Trule
|
|
|
|
| "parse" -> Tparse
|
|
|
|
| "and" -> Tand
|
|
|
|
| "eof" -> Teof
|
1998-04-07 05:50:19 -07:00
|
|
|
| "let" -> Tlet
|
1995-05-04 03:15:53 -07:00
|
|
|
| s -> Tident s }
|
|
|
|
| '"'
|
|
|
|
{ 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()) }
|
|
|
|
| "'" [^ '\\'] "'"
|
1997-04-15 12:18:03 -07:00
|
|
|
{ Tchar(Char.code(Lexing.lexeme_char lexbuf 1)) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| "'" '\\' ['\\' '\'' 'n' 't' 'b' 'r'] "'"
|
1997-04-15 12:18:03 -07:00
|
|
|
{ Tchar(Char.code(char_for_backslash (Lexing.lexeme_char lexbuf 2))) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| "'" '\\' ['0'-'9'] ['0'-'9'] ['0'-'9'] "'"
|
1997-04-15 12:18:03 -07:00
|
|
|
{ Tchar(Char.code(char_for_decimal_code lexbuf 2)) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| '{'
|
1998-04-23 01:24:50 -07:00
|
|
|
{ let n1 = Lexing.lexeme_end lexbuf
|
|
|
|
and l1 = !line_num
|
|
|
|
and s1 = !line_start_pos in
|
|
|
|
brace_depth := 1;
|
|
|
|
let n2 = handle_lexical_error action lexbuf in
|
|
|
|
Taction({start_pos = n1; end_pos = n2;
|
|
|
|
start_line = l1; start_col = n1 - s1}) }
|
1995-05-04 03:15:53 -07:00
|
|
|
| '=' { Tequal }
|
|
|
|
| '|' { Tor }
|
|
|
|
| '_' { Tunderscore }
|
|
|
|
| '[' { Tlbracket }
|
|
|
|
| ']' { Trbracket }
|
|
|
|
| '*' { Tstar }
|
|
|
|
| '?' { Tmaybe }
|
|
|
|
| '+' { Tplus }
|
|
|
|
| '(' { Tlparen }
|
|
|
|
| ')' { Trparen }
|
|
|
|
| '^' { Tcaret }
|
|
|
|
| '-' { Tdash }
|
|
|
|
| eof { Tend }
|
|
|
|
| _
|
|
|
|
{ raise(Lexical_error
|
1998-04-23 01:24:50 -07:00
|
|
|
("illegal character " ^ String.escaped(Lexing.lexeme lexbuf),
|
|
|
|
!line_num, Lexing.lexeme_start lexbuf - !line_start_pos)) }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and action = parse
|
|
|
|
'{'
|
|
|
|
{ incr brace_depth;
|
|
|
|
action lexbuf }
|
|
|
|
| '}'
|
|
|
|
{ decr brace_depth;
|
1998-04-23 01:24:50 -07:00
|
|
|
if !brace_depth = 0 then Lexing.lexeme_start lexbuf else action lexbuf }
|
1995-05-04 03:15:53 -07:00
|
|
|
| '"'
|
|
|
|
{ reset_string_buffer();
|
|
|
|
string lexbuf;
|
|
|
|
reset_string_buffer();
|
|
|
|
action lexbuf }
|
1996-05-24 08:17:21 -07:00
|
|
|
| "'" [^ '\\'] "'"
|
|
|
|
{ action lexbuf }
|
|
|
|
| "'" '\\' ['\\' '\'' 'n' 't' 'b' 'r'] "'"
|
1995-05-04 03:15:53 -07:00
|
|
|
{ action lexbuf }
|
1996-05-24 08:17:21 -07:00
|
|
|
| "'" '\\' ['0'-'9'] ['0'-'9'] ['0'-'9'] "'"
|
1995-05-04 03:15:53 -07:00
|
|
|
{ action lexbuf }
|
|
|
|
| "(*"
|
|
|
|
{ comment_depth := 1;
|
|
|
|
comment lexbuf;
|
|
|
|
action lexbuf }
|
|
|
|
| eof
|
1998-04-23 01:24:50 -07:00
|
|
|
{ raise (Lexical_error("unterminated action", 0, 0)) }
|
|
|
|
| '\010'
|
|
|
|
{ line_start_pos := Lexing.lexeme_end lexbuf;
|
|
|
|
incr line_num;
|
|
|
|
action lexbuf }
|
1995-05-04 03:15:53 -07:00
|
|
|
| _
|
|
|
|
{ action lexbuf }
|
|
|
|
|
|
|
|
and string = parse
|
|
|
|
'"'
|
|
|
|
{ () }
|
1998-04-23 01:24:50 -07:00
|
|
|
| '\\' [' ' '\013' '\009' '\012'] * '\010' [' ' '\013' '\009' '\012'] *
|
|
|
|
{ line_start_pos := Lexing.lexeme_end lexbuf;
|
|
|
|
incr line_num;
|
|
|
|
string lexbuf }
|
1995-05-04 03:15:53 -07:00
|
|
|
| '\\' ['\\' '"' '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-04-23 01:24:50 -07:00
|
|
|
{ raise(Lexical_error("unterminated string", 0, 0)) }
|
|
|
|
| '\010'
|
|
|
|
{ store_string_char '\010';
|
|
|
|
line_start_pos := Lexing.lexeme_end lexbuf;
|
|
|
|
incr line_num;
|
|
|
|
string lexbuf }
|
1995-05-04 03:15:53 -07:00
|
|
|
| _
|
|
|
|
{ store_string_char(Lexing.lexeme_char lexbuf 0);
|
|
|
|
string lexbuf }
|
|
|
|
|
|
|
|
and comment = parse
|
|
|
|
"(*"
|
|
|
|
{ incr comment_depth; comment lexbuf }
|
|
|
|
| "*)"
|
|
|
|
{ decr comment_depth;
|
1998-04-23 01:24:50 -07:00
|
|
|
if !comment_depth = 0 then () else comment lexbuf }
|
1995-05-04 03:15:53 -07:00
|
|
|
| '"'
|
|
|
|
{ reset_string_buffer();
|
|
|
|
string lexbuf;
|
|
|
|
reset_string_buffer();
|
|
|
|
comment lexbuf }
|
1995-09-28 11:50:03 -07:00
|
|
|
| "''"
|
|
|
|
{ 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-04-23 01:24:50 -07:00
|
|
|
{ raise(Lexical_error("unterminated comment", 0, 0)) }
|
|
|
|
| '\010'
|
|
|
|
{ line_start_pos := Lexing.lexeme_end lexbuf;
|
|
|
|
incr line_num;
|
|
|
|
comment lexbuf }
|
1995-05-04 03:15:53 -07:00
|
|
|
| _
|
|
|
|
{ comment lexbuf }
|