@@ -18272,6 +18272,8 @@ val iter_directive_built_in_value :
18272
18272
(** semantic version predicate *)
18273
18273
val semver : Location.t -> string -> string -> bool
18274
18274
18275
+ val filter_directive_from_lexbuf : Lexing.lexbuf -> (int * int) list
18276
+
18275
18277
end = struct
18276
18278
#1 "lexer.ml"
18277
18279
# 15 "parsing/lexer.mll"
@@ -21006,6 +21008,94 @@ and __ocaml_lex_skip_sharp_bang_rec lexbuf __ocaml_lex_state =
21006
21008
21007
21009
and docstring = Docstrings.docstring
21008
21010
21011
+ let interpret_directive lexbuf cont look_ahead =
21012
+ let if_then_else = !if_then_else in
21013
+ begin match token_with_comments lexbuf, if_then_else with
21014
+ | IF, Dir_out ->
21015
+ let rec skip_from_if_false () =
21016
+ let token = token_with_comments lexbuf in
21017
+ if token = EOF then
21018
+ raise (Error (Unterminated_if, Location.curr lexbuf)) else
21019
+ if token = SHARP && at_bol lexbuf then
21020
+ begin
21021
+ let token = token_with_comments lexbuf in
21022
+ match token with
21023
+ | END ->
21024
+ begin
21025
+ update_if_then_else Dir_out;
21026
+ cont lexbuf
21027
+ end
21028
+ | ELSE ->
21029
+ begin
21030
+ update_if_then_else Dir_if_false;
21031
+ cont lexbuf
21032
+ end
21033
+ | IF ->
21034
+ raise (Error (Unexpected_directive, Location.curr lexbuf))
21035
+ | _ ->
21036
+ if is_elif token &&
21037
+ directive_parse token_with_comments lexbuf then
21038
+ begin
21039
+ update_if_then_else Dir_if_true;
21040
+ cont lexbuf
21041
+ end
21042
+ else skip_from_if_false ()
21043
+ end
21044
+ else skip_from_if_false () in
21045
+ if directive_parse token_with_comments lexbuf then
21046
+ begin
21047
+ update_if_then_else Dir_if_true (* Next state: ELSE *);
21048
+ cont lexbuf
21049
+ end
21050
+ else
21051
+ skip_from_if_false ()
21052
+ | IF, (Dir_if_false | Dir_if_true)->
21053
+ raise (Error(Unexpected_directive, Location.curr lexbuf))
21054
+ | LIDENT "elif", (Dir_if_false | Dir_out)
21055
+ -> (* when the predicate is false, it will continue eating `elif` *)
21056
+ raise (Error(Unexpected_directive, Location.curr lexbuf))
21057
+ | (LIDENT "elif" | ELSE as token), Dir_if_true ->
21058
+ (* looking for #end, however, it can not see #if anymore *)
21059
+ let rec skip_from_if_true else_seen =
21060
+ let token = token_with_comments lexbuf in
21061
+ if token = EOF then
21062
+ raise (Error (Unterminated_else, Location.curr lexbuf)) else
21063
+ if token = SHARP && at_bol lexbuf then
21064
+ begin
21065
+ let token = token_with_comments lexbuf in
21066
+ match token with
21067
+ | END ->
21068
+ begin
21069
+ update_if_then_else Dir_out;
21070
+ cont lexbuf
21071
+ end
21072
+ | IF ->
21073
+ raise (Error (Unexpected_directive, Location.curr lexbuf))
21074
+ | ELSE ->
21075
+ if else_seen then
21076
+ raise (Error (Unexpected_directive, Location.curr lexbuf))
21077
+ else
21078
+ skip_from_if_true true
21079
+ | _ ->
21080
+ if else_seen && is_elif token then
21081
+ raise (Error (Unexpected_directive, Location.curr lexbuf))
21082
+ else
21083
+ skip_from_if_true else_seen
21084
+ end
21085
+ else skip_from_if_true else_seen in
21086
+ skip_from_if_true (token = ELSE)
21087
+ | ELSE, Dir_if_false
21088
+ | ELSE, Dir_out ->
21089
+ raise (Error(Unexpected_directive, Location.curr lexbuf))
21090
+ | END, (Dir_if_false | Dir_if_true ) ->
21091
+ update_if_then_else Dir_out;
21092
+ cont lexbuf
21093
+ | END, Dir_out ->
21094
+ raise (Error(Unexpected_directive, Location.curr lexbuf))
21095
+ | token, (Dir_if_true | Dir_if_false | Dir_out) ->
21096
+ look_ahead token
21097
+ end
21098
+
21009
21099
let token lexbuf =
21010
21100
let post_pos = lexeme_end_p lexbuf in
21011
21101
let attach lines docs pre_pos =
@@ -21052,93 +21142,10 @@ and __ocaml_lex_skip_sharp_bang_rec lexbuf __ocaml_lex_state =
21052
21142
| BlankLine -> BlankLine
21053
21143
in
21054
21144
loop lines' docs lexbuf
21055
- | SHARP when at_bol lexbuf ->
21056
- let if_then_else = !if_then_else in
21057
- begin match token_with_comments lexbuf, if_then_else with
21058
- | IF, Dir_out ->
21059
- let rec skip_from_if_false () : Parser.token =
21060
- let token = token_with_comments lexbuf in
21061
- if token = EOF then
21062
- raise (Error (Unterminated_if, Location.curr lexbuf)) else
21063
- if token = SHARP && at_bol lexbuf then
21064
- begin
21065
- let token = token_with_comments lexbuf in
21066
- match token with
21067
- | END ->
21068
- begin
21069
- update_if_then_else Dir_out;
21070
- loop lines docs lexbuf
21071
- end
21072
- | ELSE ->
21073
- begin
21074
- update_if_then_else Dir_if_false;
21075
- loop lines docs lexbuf
21076
- end
21077
- | IF ->
21078
- raise (Error (Unexpected_directive, Location.curr lexbuf))
21079
- | _ ->
21080
- if is_elif token &&
21081
- directive_parse token_with_comments lexbuf then
21082
- begin
21083
- update_if_then_else Dir_if_true;
21084
- loop lines docs lexbuf
21085
- end
21086
- else skip_from_if_false ()
21087
- end
21088
- else skip_from_if_false () in
21089
- if directive_parse token_with_comments lexbuf then
21090
- begin
21091
- update_if_then_else Dir_if_true (* Next state: ELSE *);
21092
- loop lines docs lexbuf
21093
- end
21094
- else
21095
- skip_from_if_false ()
21096
- | IF, (Dir_if_false | Dir_if_true)->
21097
- raise (Error(Unexpected_directive, Location.curr lexbuf))
21098
- | LIDENT "elif", (Dir_if_false | Dir_out)
21099
- -> (* when the predicate is false, it will continue eating `elif` *)
21100
- raise (Error(Unexpected_directive, Location.curr lexbuf))
21101
- | (LIDENT "elif" | ELSE as token), Dir_if_true ->
21102
- (* looking for #end, however, it can not see #if anymore *)
21103
- let rec skip_from_if_true else_seen =
21104
- let token = token_with_comments lexbuf in
21105
- if token = EOF then
21106
- raise (Error (Unterminated_else, Location.curr lexbuf)) else
21107
- if token = SHARP && at_bol lexbuf then
21108
- begin
21109
- let token = token_with_comments lexbuf in
21110
- match token with
21111
- | END ->
21112
- begin
21113
- update_if_then_else Dir_out;
21114
- loop lines docs lexbuf
21115
- end
21116
- | IF ->
21117
- raise (Error (Unexpected_directive, Location.curr lexbuf))
21118
- | ELSE ->
21119
- if else_seen then
21120
- raise (Error (Unexpected_directive, Location.curr lexbuf))
21121
- else
21122
- skip_from_if_true true
21123
- | _ ->
21124
- if else_seen && is_elif token then
21125
- raise (Error (Unexpected_directive, Location.curr lexbuf))
21126
- else
21127
- skip_from_if_true else_seen
21128
- end
21129
- else skip_from_if_true else_seen in
21130
- skip_from_if_true (token = ELSE)
21131
- | ELSE, Dir_if_false
21132
- | ELSE, Dir_out ->
21133
- raise (Error(Unexpected_directive, Location.curr lexbuf))
21134
- | END, (Dir_if_false | Dir_if_true ) ->
21135
- update_if_then_else Dir_out;
21136
- loop lines docs lexbuf
21137
- | END, Dir_out ->
21138
- raise (Error(Unexpected_directive, Location.curr lexbuf))
21139
- | token, (Dir_if_true | Dir_if_false | Dir_out) ->
21140
- sharp_look_ahead := Some token; SHARP
21141
- end
21145
+ | SHARP when at_bol lexbuf ->
21146
+ interpret_directive lexbuf
21147
+ (fun lexbuf -> loop lines docs lexbuf)
21148
+ (fun token -> sharp_look_ahead := Some token; SHARP)
21142
21149
| DOCSTRING doc ->
21143
21150
add_docstring_comment doc;
21144
21151
let docs' =
@@ -21174,12 +21181,32 @@ and __ocaml_lex_skip_sharp_bang_rec lexbuf __ocaml_lex_state =
21174
21181
| None -> ()
21175
21182
| Some (init, _preprocess) -> init ()
21176
21183
21184
+ let rec filter_directive pos acc lexbuf : (int * int ) list =
21185
+ match token_with_comments lexbuf with
21186
+ | SHARP when at_bol lexbuf ->
21187
+ (* ^[start_pos]#if ... #then^[end_pos] *)
21188
+ let start_pos = Lexing.lexeme_start lexbuf in
21189
+ interpret_directive lexbuf
21190
+ (fun lexbuf ->
21191
+ filter_directive
21192
+ (Lexing.lexeme_end lexbuf)
21193
+ ((pos, start_pos) :: acc)
21194
+ lexbuf
21195
+
21196
+ )
21197
+ (fun _token -> filter_directive pos acc lexbuf )
21198
+ | EOF -> (pos, Lexing.lexeme_end lexbuf) :: acc
21199
+ | _ -> filter_directive pos acc lexbuf
21200
+
21201
+ let filter_directive_from_lexbuf lexbuf =
21202
+ List.rev (filter_directive 0 [] lexbuf )
21203
+
21177
21204
let set_preprocessor init preprocess =
21178
21205
escaped_newlines := true;
21179
21206
preprocessor := Some (init, preprocess)
21180
21207
21181
21208
21182
- # 2907 "parsing/lexer.ml"
21209
+ # 2932 "parsing/lexer.ml"
21183
21210
21184
21211
end
21185
21212
module Bs_conditional_initial : sig
0 commit comments