aboutsummaryrefslogtreecommitdiff
path: root/lib/qparser/lex_state.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/qparser/lex_state.ml')
-rw-r--r--lib/qparser/lex_state.ml108
1 files changed, 72 insertions, 36 deletions
diff --git a/lib/qparser/lex_state.ml b/lib/qparser/lex_state.ml
index 37400e7..3cf757d 100644
--- a/lib/qparser/lex_state.ml
+++ b/lib/qparser/lex_state.ml
@@ -1,5 +1,14 @@
+(** This module provide functions used to parse the strings.
+
+ *)
+
exception Out_of_context
+let pr_err buffer =
+ let location, _ = Lexbuf.positions buffer in
+ let line = location.Lexing.pos_lnum and file = location.Lexing.pos_fname in
+ Format.eprintf "Error found at : %s:%d\n" file line
+
let space = [%sedlex.regexp? ' ' | '\t']
let spaces = [%sedlex.regexp? Plus space]
let single_quote = [%sedlex.regexp? '\'']
@@ -12,23 +21,21 @@ let leave_text end_wrapper buf buffer =
Tokens.LITERAL (Buffer.contents buf)
let rec nestedQuotedStringWraper : Lexbuf.stringWraper =
- let rec start_string f buffer =
+ let start_string f buffer =
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
(* There is no more way to add start a quoted string here *)
- | spaces -> start_string f buffer
- | double_quote ->
- Lexbuf.enter_state buffer (Lexbuf.String dQuotedStringWraper);
- Tokens.TEXT_MARKER
- | '{' ->
- Lexbuf.enter_state buffer (Lexbuf.MString 0);
- Tokens.TEXT_MARKER
| _ -> f buffer
in
{
start_string;
- wrap = (fun _ -> raise Out_of_context);
+ wrap =
+ (fun f ?(nested = false) buf buffer ->
+ let lexbuf = Lexbuf.buffer buffer in
+ match%sedlex lexbuf with
+ | "''" -> leave_text nestedQuotedStringWraper buf buffer
+ | _ -> f ~nested buf buffer);
end_string =
(fun buffer ->
let lexbuf = Lexbuf.buffer buffer in
@@ -39,38 +46,61 @@ let rec nestedQuotedStringWraper : Lexbuf.stringWraper =
| _ -> raise Not_found);
}
-and quotedStringWraper : Lexbuf.stringWraper =
- (* This function need to be recursirve in case of successive functions to
- escape *)
- let rec wrap f buf buffer =
+and nestedDquotedStringWraper : Lexbuf.stringWraper =
+ let start_string f buffer =
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
- | "''" ->
- Buffer.add_char buf '\'';
- wrap f buf buffer
- | single_quote -> leave_text quotedStringWraper buf buffer
- | _ -> f buf buffer
+ (* There is no more way to add start a quoted string here *)
+ | _ -> f buffer
in
+ let rec wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
+ let lexbuf = Lexbuf.buffer buffer in
+ match%sedlex lexbuf with
+ | double_quote ->
+ Buffer.add_char buf '"';
+ wrap ~nested:true f buf buffer
+ | double_quote, double_quote ->
+ leave_text nestedDquotedStringWraper buf buffer
+ | _ -> f ~nested buf buffer
+ in
+ {
+ start_string;
+ wrap;
+ end_string =
+ (fun buffer ->
+ let lexbuf = Lexbuf.buffer buffer in
+ match%sedlex lexbuf with
+ | double_quote, double_quote ->
+ Lexbuf.leave_state buffer;
+ TEXT_MARKER
+ | _ -> raise Not_found);
+ }
+
+and quotedStringWraper : Lexbuf.stringWraper =
let rec start_string f buffer =
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
| spaces -> start_string f buffer
- | "''" ->
+ | single_quote -> raise Out_of_context
+ | single_quote, single_quote ->
Lexbuf.enter_state buffer (Lexbuf.String nestedQuotedStringWraper);
Tokens.TEXT_MARKER
- | double_quote ->
- Lexbuf.enter_state buffer (Lexbuf.String dQuotedStringWraper);
- Tokens.TEXT_MARKER
- | '{' ->
- Lexbuf.enter_state buffer (Lexbuf.MString 0);
- Tokens.TEXT_MARKER
| _ -> f buffer
in
+ let rec quoted_wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
+ let lexbuf = Lexbuf.buffer buffer in
+ match%sedlex lexbuf with
+ | single_quote, single_quote ->
+ Buffer.add_char buf '\'';
+ quoted_wrap ~nested f buf buffer
+ | single_quote -> leave_text quotedStringWraper buf buffer
+ | _ -> f ~nested buf buffer
+ in
{
start_string;
- wrap;
+ wrap = quoted_wrap;
end_string =
(fun buffer ->
let lexbuf = Lexbuf.buffer buffer in
@@ -78,29 +108,32 @@ and quotedStringWraper : Lexbuf.stringWraper =
| single_quote ->
Lexbuf.leave_state buffer;
TEXT_MARKER
- | _ -> raise Not_found);
+ | _ ->
+ pr_err buffer;
+ raise Not_found);
}
and dQuotedStringWraper : Lexbuf.stringWraper =
- let rec wrap f buf buffer =
+ let rec wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
- | "\"\"" ->
+ | single_quote ->
+ Buffer.add_char buf '\'';
+ wrap ~nested:true f buf buffer
+ | double_quote, double_quote ->
Buffer.add_char buf '"';
wrap f buf buffer
| double_quote -> leave_text dQuotedStringWraper buf buffer
- | _ -> f buf buffer
+ | _ -> f ~nested buf buffer
in
let rec start_string f buffer =
let lexbuf = Lexbuf.buffer buffer in
match%sedlex lexbuf with
| spaces -> start_string f buffer
- | single_quote ->
- Lexbuf.enter_state buffer (Lexbuf.String nestedQuotedStringWraper);
- Tokens.TEXT_MARKER
- | '{' ->
- Lexbuf.enter_state buffer (Lexbuf.MString 0);
+ | double_quote -> raise Out_of_context
+ | double_quote, double_quote ->
+ Lexbuf.enter_state buffer (Lexbuf.String nestedDquotedStringWraper);
Tokens.TEXT_MARKER
| _ -> f buffer
in
@@ -136,7 +169,10 @@ let defaultWraper : Lexbuf.stringWraper =
in
{
start_string;
- wrap = (fun _f _buf _buffer -> raise Out_of_context);
+ wrap =
+ (fun _f ?nested _buf _buffer ->
+ ignore nested;
+ raise Out_of_context);
end_string = (fun _buffer -> raise Out_of_context);
}