Commit 93b36bde authored by Kim Nguyễn's avatar Kim Nguyễn
Browse files

Fine tune the heuristic that detects that a token is a polymorphic variable.

parent fb36149d
......@@ -43,8 +43,8 @@ let parse_char lexbuf base i =
let r = ref 0 in
for i = 0 to String.length s - 1 do
let c = hexa_digit s.[i] in
if c >= base || c < 0
then error (L.lexeme_start lexbuf) (L.lexeme_end lexbuf) "invalid digit";
if c >= base || c < 0 then
error (L.lexeme_start lexbuf) (L.lexeme_end lexbuf) "invalid digit";
r := (!r * base) + c
done;
!r
......@@ -288,6 +288,9 @@ let illegal lexbuf =
let in_comment = ref false
(* used for the heuristic of polymorphic variables *)
let in_list = ref 0
let ident_or_keyword =
let l =
[
......@@ -351,8 +354,12 @@ let rec token lexbuf =
| Plus '0' .. '9' -> INT (L.Utf8.lexeme lexbuf)
| "(" -> LP
| ")" -> RP
| "[" -> LSB
| "]" -> RSB
| "[" ->
incr in_list;
LSB
| "]" ->
decr in_list;
RSB
| "<" -> LT
| ">" -> GT
| "{" -> LCB
......@@ -395,7 +402,8 @@ let rec token lexbuf =
| ";;" -> SEMISEMI
| "'" ->
L.rollback lexbuf;
single_quote lexbuf
if !in_list = 0 then single_quote_outside_list lexbuf
else single_quote_inside_list lexbuf
| '"' | "'" ->
L.rollback lexbuf;
do_string lexbuf
......@@ -422,7 +430,23 @@ and do_string lexbuf =
if double_quote then STRING2 s else STRING1 s
| _ -> assert false
and single_quote lexbuf =
and single_quote_outside_list lexbuf =
match%sedlex lexbuf with
| ( "'",
( '\\', Chars "\\rnt'\""
| Plus (Chars "x0123456789ABCDEFabcdef"), ';'
| Compl (Chars "\'\\") ),
"'" ) ->
L.rollback lexbuf;
do_string lexbuf
| "'", ncname ->
(* then try to read it as variable *)
let s = L.Utf8.sub_lexeme lexbuf 1 (L.lexeme_length lexbuf - 1) in
POLY s
| any -> illegal lexbuf
| _ -> assert false
and single_quote_inside_list lexbuf =
(*
lexing of single quoted string/polymorphic variables
these rules should also be used in the comment lexer.
......@@ -442,8 +466,8 @@ and single_quote lexbuf =
let s = L.Utf8.lexeme lexbuf in
warning lexbuf
(Printf.sprintf
"string literal followed by an identifier ``%s'' is ambiguous. Add a \
space after the second quote."
"string literal followed by an identifier ``%s'' is ambiguous. Add \
a space after the second quote."
s);
L.rollback lexbuf;
do_string lexbuf
......@@ -466,7 +490,7 @@ and comment start lexbuf =
| "*)" -> ()
| "'" ->
L.rollback lexbuf;
ignore (single_quote lexbuf);
ignore (single_quote_inside_list lexbuf);
comment start lexbuf
| '"' | "'" ->
L.rollback lexbuf;
......@@ -487,8 +511,7 @@ and string start double lexbuf =
match%sedlex lexbuf with
| '"' | "'" ->
let d = L.Latin1.lexeme_char lexbuf 0 = '"' in
if d != double
then (
if d != double then (
store_lexeme lexbuf;
string start double lexbuf)
| '\\', Chars "\\\"\'" ->
......@@ -520,6 +543,7 @@ let token lexbuf =
try token lexbuf
with e ->
clear_buff ();
in_list := 0;
in_comment := false;
(* reinit encoding ? *)
raise e
......
......@@ -108,4 +108,4 @@ let [] =
let map2 = init [ (1, "HELLO") (* replaces "A" and "C" *)
(3, "WORLD") (0, "0") ] in
let map3 = merge update (map1, map2) in
iter (fun (x : Any ) : [] = print [ !(string_of x) '\n' ]) map3
\ No newline at end of file
iter (fun (x : 'a ) : [] = print [ !(string_of x) '\n' ]) map3
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment