Commit 9b94eb11 authored by Julien Lopez's avatar Julien Lopez

We don't need parentheses for type variables in regexp anymore, the syntax is

now as such:
	- ['a] is the regexp matching the type variable "a";
	- ['a'] is the regexp matching the string "a";
	- ['a' 'a] is the regexp matching the string "a" followed by the type
	variable "a";
	- ['a''a] is a syntax error (a string in single quotes in a regexp MUST
	be followed by a character that cannot be the beginning of a name for a
	type variable, a blank character will do).

Bug: At this point, when using CDuce at toplevel, one will have to press enter
twice when typing a regexp with type variables.
parent c31e7efd
......@@ -310,6 +310,7 @@ let catch_exn ppf_err exn =
Format.fprintf ppf_err "@."
let parse rule input =
Ulexer.toplevel := !toplevel;
try Parser.localize_exn (fun () -> rule input)
with e -> Parser.sync (); raise e
......
......@@ -138,6 +138,8 @@ module L = Ulexing
exception Error of int * int * string
let toplevel = ref false
let error i j s = raise (Error (i,j,s))
(* Buffer for string literals *)
......@@ -182,6 +184,8 @@ let regexp ncname_char =
let regexp ncname = ( xml_letter ncname_char* ) | ('_' ncname_char+)
let regexp qname = (ncname ':')? ncname
(* Should be [^ xml_letter ] *)
let regexp not_xml_letter = [^ 'A'-'Z' 'a'-'z' '0'-'9' '_' ]
let illegal lexbuf =
error
......@@ -276,7 +280,8 @@ and token2 = lexer
string (L.lexeme_start lexbuf) '"' lexbuf;
let s = get_stored_string () in
return_loc start (L.lexeme_end lexbuf) (STRING s)
| "'" [^ '\'']+ "'" ->
| "'" "\\"? _ "'--'" "\\"? _ "'"
| "'" [^ '\'']+ "'" not_xml_letter ->
L.rollback lexbuf;
(fun _ -> lexer
| "'" -> let start = L.lexeme_start lexbuf in
......@@ -284,9 +289,9 @@ and token2 = lexer
let s = get_stored_string () in
return_loc start (L.lexeme_end lexbuf) (STRING2 s)
| _ -> assert false) () lexbuf
| "('" ncname ")" ->
| "'" ncname ->
let s = L.utf8_lexeme lexbuf in
let s = String.sub s 2 (String.length s - 3) in
let s = String.sub s 1 (String.length s - 1) in
return lexbuf (PTYPE s)
| "(*" ->
in_comment := true;
......@@ -303,18 +308,60 @@ and token2 = lexer
| _ ->
illegal lexbuf
and unescape s start =
let rec aux acc start = function
| "" -> acc
| s ->
let len = String.length s in
if s.[0] = '\\' then if len != 1
then aux (acc ^ String.make 1 s.[1]) start (String.sub s 2 (len - 2))
else error start (start+1) "Unterminated string"
else
let tmp = String.sub s 1 (len - 1) in
aux (acc ^ String.make 1 s.[0]) start tmp in
aux "" start s
and token2toplevel = lexer
| xml_blank+ -> token2toplevel lexbuf
| qname ->
let s = L.utf8_lexeme lexbuf in
return lexbuf (IDENT s)
| ncname ":*" ->
let s = L.utf8_sub_lexeme lexbuf 0 (L.lexeme_length lexbuf - 2) in
return lexbuf (ANY_IN_NS s)
| ".:*" ->
return lexbuf (ANY_IN_NS "")
| '-'? ['0'-'9']+ ->
return lexbuf (INT (L.utf8_lexeme lexbuf))
| [ "<>=.,:;+-*/@&{}()|?`!$" ]
| "->" | "::" | ";;" | "--" | "//" | "/@" | ":=" | "\\" | "++"
| "<=" | ">=" | "<<" | ">>" | "||" | "&&" | "**" | "_"
| ".."
| ["?+*"] "?" | "#" ->
return lexbuf (KEYWORD (L.utf8_lexeme lexbuf))
| "[" -> in_brackets := !in_brackets + 1;
return lexbuf (KEYWORD (L.utf8_lexeme lexbuf))
| "]" -> in_brackets := !in_brackets - 1;
return lexbuf (KEYWORD (L.utf8_lexeme lexbuf))
| '"' ->
let start = L.lexeme_start lexbuf in
string (L.lexeme_start lexbuf) '"' lexbuf;
let s = get_stored_string () in
return_loc start (L.lexeme_end lexbuf) (STRING s)
| "'" "\\"? _ "'--'" "\\"? _ "'"
| "'" [^ "\n\'"]+ "'" not_xml_letter ->
L.rollback lexbuf;
(fun _ -> lexer
| "'" -> let start = L.lexeme_start lexbuf in
string (L.lexeme_start lexbuf) '\'' lexbuf;
let s = get_stored_string () in
return_loc start (L.lexeme_end lexbuf) (STRING2 s)
| _ -> assert false) () lexbuf
| "'" ncname ->
let s = L.utf8_lexeme lexbuf in
let s = String.sub s 1 (String.length s - 1) in
return lexbuf (PTYPE s)
| "(*" ->
in_comment := true;
comment (L.lexeme_start lexbuf) lexbuf;
in_comment := false;
token2toplevel lexbuf
| "/*" ->
in_comment := true;
tcomment (L.lexeme_start lexbuf) lexbuf;
in_comment := false;
token2toplevel lexbuf
| eof ->
return lexbuf EOI
| _ ->
illegal lexbuf
and comment start = lexer
| "(*" ->
......@@ -353,7 +400,8 @@ and string start endchar = lexer
| eof -> error start (start+1) "Unterminated string"
| _ -> store_lexeme lexbuf; string start endchar lexbuf
let token lexbuf = if !in_brackets = 0 then token lexbuf else token2 lexbuf
let token lexbuf = if !in_brackets = 0 then token lexbuf
else if !toplevel then token2toplevel lexbuf else token2 lexbuf
let lexbuf = ref None
let last_tok = ref (KEYWORD "DUMMY")
......
......@@ -13,6 +13,8 @@ type token =
exception Error of int * int * string
val toplevel : bool ref
module Loc : Loc with type t = int * int
module Token : Token with module Loc = Loc and type t = token
module Error : Error
......
This diff is collapsed.
......@@ -34,11 +34,11 @@ let iteri (f : (Int -> 'a -> []))(l : [('a)*]) : [] =
| [el; rest] -> f pos el; aux f rest (pos + 1)
in
aux f l 0
(*
let mapf (f : 'a -> 'b)(l : [('a)*]) : [('b)*] =
let aux (f : 'a -> 'b)(l : [('a)*])(acc : [('b)*]) : [('b)*] = match l with
| [] -> acc
| [el; rest] -> aux f rest (acc @ [(f el)]) in
aux f l []
*)
(* List scanning *)
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