lex_state.ml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. (** This module provide functions used to parse the strings.
  2. *)
  3. exception Out_of_context
  4. let space = [%sedlex.regexp? ' ' | '\t']
  5. let spaces = [%sedlex.regexp? Plus space]
  6. let single_quote = [%sedlex.regexp? '\'']
  7. let double_quote = [%sedlex.regexp? '"']
  8. let leave_text end_wrapper buf buffer =
  9. Lexbuf.leave_state buffer;
  10. Lexbuf.enter_state buffer (Lexbuf.EndString end_wrapper);
  11. Lexbuf.rollback buffer;
  12. Tokens.LITERAL (Buffer.contents buf)
  13. let rec nestedQuotedStringWraper : Lexbuf.stringWraper =
  14. let start_string f buffer =
  15. let lexbuf = Lexbuf.buffer buffer in
  16. match%sedlex lexbuf with
  17. (* There is no more way to add start a quoted string here *)
  18. | _ -> f buffer
  19. in
  20. {
  21. start_string;
  22. wrap =
  23. (fun f ?(nested = false) buf buffer ->
  24. let lexbuf = Lexbuf.buffer buffer in
  25. match%sedlex lexbuf with
  26. | "''" -> leave_text nestedQuotedStringWraper buf buffer
  27. | _ -> f ~nested buf buffer);
  28. end_string =
  29. (fun buffer ->
  30. let lexbuf = Lexbuf.buffer buffer in
  31. match%sedlex lexbuf with
  32. | "''" ->
  33. Lexbuf.leave_state buffer;
  34. TEXT_MARKER
  35. | _ -> raise Not_found);
  36. }
  37. and nestedDquotedStringWraper : Lexbuf.stringWraper =
  38. let start_string f buffer =
  39. let lexbuf = Lexbuf.buffer buffer in
  40. match%sedlex lexbuf with
  41. (* There is no more way to add start a quoted string here *)
  42. | _ -> f buffer
  43. in
  44. let rec wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
  45. let lexbuf = Lexbuf.buffer buffer in
  46. match%sedlex lexbuf with
  47. | double_quote ->
  48. Buffer.add_char buf '"';
  49. wrap ~nested:true f buf buffer
  50. | double_quote, double_quote ->
  51. leave_text nestedDquotedStringWraper buf buffer
  52. | _ -> f ~nested buf buffer
  53. in
  54. {
  55. start_string;
  56. wrap;
  57. end_string =
  58. (fun buffer ->
  59. let lexbuf = Lexbuf.buffer buffer in
  60. match%sedlex lexbuf with
  61. | double_quote, double_quote ->
  62. Lexbuf.leave_state buffer;
  63. TEXT_MARKER
  64. | _ -> raise Not_found);
  65. }
  66. and quotedStringWraper : Lexbuf.stringWraper =
  67. let rec start_string f buffer =
  68. let lexbuf = Lexbuf.buffer buffer in
  69. match%sedlex lexbuf with
  70. | spaces -> start_string f buffer
  71. | single_quote -> raise Out_of_context
  72. | single_quote, single_quote ->
  73. Lexbuf.enter_state buffer (Lexbuf.String nestedQuotedStringWraper);
  74. Tokens.TEXT_MARKER
  75. | _ -> f buffer
  76. in
  77. let rec quoted_wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
  78. let lexbuf = Lexbuf.buffer buffer in
  79. match%sedlex lexbuf with
  80. | single_quote, single_quote ->
  81. Buffer.add_char buf '\'';
  82. quoted_wrap ~nested f buf buffer
  83. | single_quote -> leave_text quotedStringWraper buf buffer
  84. | _ -> f ~nested buf buffer
  85. in
  86. {
  87. start_string;
  88. wrap = quoted_wrap;
  89. end_string =
  90. (fun buffer ->
  91. let lexbuf = Lexbuf.buffer buffer in
  92. match%sedlex lexbuf with
  93. | single_quote ->
  94. Lexbuf.leave_state buffer;
  95. TEXT_MARKER
  96. | _ -> raise Not_found);
  97. }
  98. and dQuotedStringWraper : Lexbuf.stringWraper =
  99. let rec wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
  100. let lexbuf = Lexbuf.buffer buffer in
  101. match%sedlex lexbuf with
  102. | single_quote ->
  103. Buffer.add_char buf '\'';
  104. wrap ~nested:true f buf buffer
  105. | double_quote, double_quote ->
  106. Buffer.add_char buf '"';
  107. wrap f buf buffer
  108. | double_quote -> leave_text dQuotedStringWraper buf buffer
  109. | _ -> f ~nested buf buffer
  110. in
  111. let rec start_string f buffer =
  112. let lexbuf = Lexbuf.buffer buffer in
  113. match%sedlex lexbuf with
  114. | spaces -> start_string f buffer
  115. | double_quote -> raise Out_of_context
  116. | double_quote, double_quote ->
  117. Lexbuf.enter_state buffer (Lexbuf.String nestedDquotedStringWraper);
  118. Tokens.TEXT_MARKER
  119. | _ -> f buffer
  120. in
  121. {
  122. start_string;
  123. wrap;
  124. end_string =
  125. (fun buffer ->
  126. let lexbuf = Lexbuf.buffer buffer in
  127. match%sedlex lexbuf with
  128. | double_quote ->
  129. Lexbuf.leave_state buffer;
  130. TEXT_MARKER
  131. | _ -> raise Not_found);
  132. }
  133. let defaultWraper : Lexbuf.stringWraper =
  134. let rec start_string f buffer =
  135. let lexbuf = Lexbuf.buffer buffer in
  136. match%sedlex lexbuf with
  137. | spaces -> start_string f buffer
  138. | single_quote ->
  139. Lexbuf.enter_state buffer (Lexbuf.String quotedStringWraper);
  140. Tokens.TEXT_MARKER
  141. | double_quote ->
  142. Lexbuf.enter_state buffer (Lexbuf.String dQuotedStringWraper);
  143. Tokens.TEXT_MARKER
  144. | '{' ->
  145. Lexbuf.enter_state buffer (Lexbuf.MString 0);
  146. Tokens.TEXT_MARKER
  147. | _ -> f buffer
  148. in
  149. {
  150. start_string;
  151. wrap =
  152. (fun _f ?nested _buf _buffer ->
  153. ignore nested;
  154. raise Out_of_context);
  155. end_string = (fun _buffer -> raise Out_of_context);
  156. }
  157. let readLongStringWraper : Lexbuf.stringWraper =
  158. {
  159. defaultWraper with
  160. end_string =
  161. (fun buffer ->
  162. let lexbuf = Lexbuf.buffer buffer in
  163. match%sedlex lexbuf with
  164. | "}" ->
  165. Lexbuf.leave_state buffer;
  166. TEXT_MARKER
  167. | _ -> raise Not_found);
  168. }