|
1 | 1 | { |
| 2 | +open Import |
| 3 | + |
2 | 4 | type 'command block = |
3 | 5 | | Command of 'command |
4 | 6 | | Comment of string list |
| 7 | + |
| 8 | +(* Gets position before consuming newline *) |
| 9 | +let pos_before_newline lexbuf = |
| 10 | + let pos = Lexing.lexeme_end_p lexbuf in |
| 11 | + { pos with pos_cnum = pos.pos_cnum - 1 } |
| 12 | + |
| 13 | +(* Creates comment from string list accumulator *) |
| 14 | +let create_comment_from_acc ~start ~stop acc = |
| 15 | + match acc with |
| 16 | + | [] -> None |
| 17 | + | _ -> Some (Loc.create ~start ~stop, Comment (List.rev acc)) |
| 18 | + |
| 19 | +(* Helper for extracting start/stop positions *) |
| 20 | +let with_span lexbuf f = |
| 21 | + let start = Lexing.lexeme_start_p lexbuf in |
| 22 | + let stop = Lexing.lexeme_end_p lexbuf in |
| 23 | + f ~start ~stop |
| 24 | + |
| 25 | +(* Forward declarations for lexer rules *) |
| 26 | +let eol_fdecl = Fdecl.create Dyn.opaque |
| 27 | +let command_cont_fdecl = Fdecl.create Dyn.opaque |
| 28 | + |
| 29 | +(* Helper for position extraction + eol + recursive call *) |
| 30 | +let with_span_eol lexbuf content next_rule = |
| 31 | + with_span lexbuf (fun ~start ~stop -> |
| 32 | + ignore (Fdecl.get eol_fdecl lexbuf); |
| 33 | + next_rule start stop content lexbuf) |
| 34 | + |
| 35 | +(* Handles command continuation with eol checking *) |
| 36 | +let handle_command_continuation ~start ~content ~acc lexbuf = |
| 37 | + let stop = Lexing.lexeme_end_p lexbuf in |
| 38 | + match Fdecl.get eol_fdecl lexbuf with |
| 39 | + | true -> Fdecl.get command_cont_fdecl start stop (content :: acc) lexbuf |
| 40 | + | false -> (Loc.create ~start ~stop, Command (List.rev (content :: acc))) |
| 41 | + |
| 42 | +(* Processes " $ " command start pattern *) |
| 43 | +let process_command_start str lexbuf = |
| 44 | + let start = |
| 45 | + let pos = Lexing.lexeme_start_p lexbuf in |
| 46 | + { pos with pos_cnum = pos.pos_cnum + 2 } |
| 47 | + in |
| 48 | + let stop = Lexing.lexeme_end_p lexbuf in |
| 49 | + match Fdecl.get eol_fdecl lexbuf with |
| 50 | + | true -> Some (Fdecl.get command_cont_fdecl start stop [ str ] lexbuf) |
| 51 | + | false -> Some (Loc.create ~start ~stop, Command [ str ]) |
5 | 52 | } |
6 | 53 |
|
7 | | -let eol = '\n' | eof |
| 54 | +let nonspace = [^' ' '\n'] |
| 55 | +let not_nl = [^'\n'] |
8 | 56 |
|
9 | | -let blank = [' ' '\t' '\r' '\012'] |
| 57 | +rule eol = parse |
| 58 | + | '\n' { Lexing.new_line lexbuf; true } |
| 59 | + | eof { false } |
10 | 60 |
|
11 | | -rule block = parse |
| 61 | +and block = parse |
12 | 62 | | eof { None } |
13 | | - | " $ " ([^'\n']* as str) eol |
14 | | - { Some (command_cont [str] lexbuf) } |
15 | | - | " " [^'\n']* eol |
16 | | - { output [] lexbuf } |
17 | | - | ' '? as str eol |
18 | | - { comment [str] lexbuf } |
19 | | - | ' '? [^' ' '\n'] [^'\n']* as str eol |
20 | | - { comment [str] lexbuf } |
21 | | - |
22 | | -and comment acc = parse |
| 63 | + | " $ " ([^'\n']* as str) |
| 64 | + { process_command_start str lexbuf } |
| 65 | + | " > " ([^'\n']* as str) |
| 66 | + { with_span_eol lexbuf [ " > " ^ str ] comment } |
| 67 | + | " >" |
| 68 | + { with_span_eol lexbuf [ " >" ] comment } |
| 69 | + | " " [^'\n']* |
| 70 | + { with_span_eol lexbuf [] (fun start _ content lexbuf -> output start content lexbuf) } |
| 71 | + | ' ' ((nonspace not_nl*) as rest) |
| 72 | + { with_span_eol lexbuf [ " " ^ rest ] comment } |
| 73 | + | ' ' '\n' |
| 74 | + { let start = Lexing.lexeme_start_p lexbuf in |
| 75 | + let stop = pos_before_newline lexbuf in |
| 76 | + Lexing.new_line lexbuf; |
| 77 | + comment start stop [ " " ] lexbuf } |
| 78 | + | ' ' |
| 79 | + { with_span lexbuf (fun ~start ~stop -> comment start stop [ " " ] lexbuf) } |
| 80 | + | '\n' |
| 81 | + { let start = Lexing.lexeme_start_p lexbuf in |
| 82 | + let stop = Lexing.lexeme_start_p lexbuf in |
| 83 | + Lexing.new_line lexbuf; |
| 84 | + comment start stop [ "" ] lexbuf } |
| 85 | + | nonspace not_nl* as str |
| 86 | + { with_span_eol lexbuf [str] comment } |
| 87 | + |
| 88 | +and comment start last_content_stop acc = parse |
23 | 89 | | eof |
24 | | - { match acc with |
25 | | - | [] -> None |
26 | | - | _ -> Some (Comment (List.rev acc)) |
27 | | - } |
28 | | - | ' '? as str eol |
29 | | - { comment (str :: acc) lexbuf } |
30 | | - | ' '? [^' ' '\n'] [^'\n']* as str eol |
31 | | - { comment (str :: acc) lexbuf } |
32 | | - | "" |
33 | | - { Some (Comment (List.rev acc)) } |
| 90 | + { create_comment_from_acc ~start ~stop:last_content_stop acc } |
| 91 | + | ' ' ((nonspace not_nl*) as rest) |
| 92 | + { with_span_eol lexbuf [(" " ^ rest)] (fun _start stop content lexbuf -> |
| 93 | + comment start stop (content @ acc) lexbuf) } |
| 94 | + | ' ' '\n' |
| 95 | + { let content_stop = pos_before_newline lexbuf in |
| 96 | + Lexing.new_line lexbuf; |
| 97 | + comment start content_stop (" " :: acc) lexbuf } |
| 98 | + | '\n' |
| 99 | + { let content_stop = Lexing.lexeme_start_p lexbuf in |
| 100 | + Lexing.new_line lexbuf; |
| 101 | + comment start content_stop ("" :: acc) lexbuf } |
| 102 | + | nonspace not_nl* as str |
| 103 | + { with_span_eol lexbuf [str] (fun _start stop content lexbuf -> |
| 104 | + comment start stop (content @ acc) lexbuf) } |
| 105 | + | "" { create_comment_from_acc ~start ~stop:last_content_stop acc } |
34 | 106 |
|
35 | | -and output maybe_comment = parse |
| 107 | + |
| 108 | +and output block_start maybe_comment = parse |
36 | 109 | | eof |
37 | | - { match maybe_comment with |
38 | | - | [] -> None |
39 | | - | l -> Some (Comment (List.rev l)) |
40 | | - } |
41 | | - | ' ' eof |
42 | | - { Some (Comment (List.rev (" " :: maybe_comment))) } |
43 | | - | " "? eof |
44 | | - { None } |
45 | | - | " " eol |
46 | | - { output [] lexbuf } |
47 | | - | ' '? as s eol |
48 | | - { output (s :: maybe_comment) lexbuf } |
49 | | - | " $" eol |
50 | | - { output [] lexbuf } |
51 | | - | " " '$' [^' ' '\n'] [^'\n']* eol |
52 | | - { output [] lexbuf } |
53 | | - | " " [^'$' '\n'] [^'\n']* eol |
54 | | - { output [] lexbuf } |
| 110 | + { create_comment_from_acc ~start:block_start ~stop:(Lexing.lexeme_start_p lexbuf) maybe_comment } |
| 111 | + | " $ " ([^'\n']* as str) |
| 112 | + { process_command_start str lexbuf } |
| 113 | + | ' ' ((nonspace not_nl*) as rest) |
| 114 | + { match eol lexbuf with |
| 115 | + | true -> output block_start ((" " ^ rest) :: maybe_comment) lexbuf |
| 116 | + | false -> |
| 117 | + Some (Loc.create ~start:block_start ~stop:(Lexing.lexeme_start_p lexbuf) |
| 118 | + , Comment (List.rev ((" " ^ rest) :: maybe_comment))) } |
| 119 | + | ' ' '\n' |
| 120 | + { Lexing.new_line lexbuf; |
| 121 | + output block_start (" " :: maybe_comment) lexbuf } |
| 122 | + | " " [^'\n']* |
| 123 | + { with_span_eol lexbuf [] (fun _start _stop _content lexbuf -> |
| 124 | + output block_start maybe_comment lexbuf) } |
55 | 125 | | "" |
56 | 126 | { match maybe_comment with |
57 | | - | [] -> block lexbuf |
58 | | - | l -> comment l lexbuf |
59 | | - } |
60 | | - |
61 | | -and command_cont acc = parse |
62 | | - | " > " ([^'\n']* as str) eol |
63 | | - { command_cont (str :: acc) lexbuf } |
64 | | - | " >" eol |
65 | | - { command_cont ("" :: acc) lexbuf } |
| 127 | + | [] -> block lexbuf |
| 128 | + | l -> comment block_start (Lexing.lexeme_start_p lexbuf) l lexbuf } |
| 129 | + |
| 130 | +and command_cont start last_stop acc = parse |
| 131 | + | " > " ([^'\n']* as str) |
| 132 | + { handle_command_continuation ~start ~content:str ~acc lexbuf } |
| 133 | + | " >" |
| 134 | + { handle_command_continuation ~start ~content:"" ~acc lexbuf } |
66 | 135 | | "" |
67 | | - { Command (List.rev acc) } |
| 136 | + { (Loc.create ~start ~stop:last_stop, Command (List.rev acc)) } |
| 137 | + |
| 138 | +{ |
| 139 | +let () = |
| 140 | + Fdecl.set eol_fdecl eol; |
| 141 | + Fdecl.set command_cont_fdecl command_cont |
| 142 | +} |
0 commit comments