|
1 | 1 | { |
| 2 | +open Import |
| 3 | + |
2 | 4 | type 'command block = |
3 | 5 | | Command of 'command |
4 | 6 | | Comment of string list |
5 | | -} |
6 | 7 |
|
7 | | -let eol = '\n' | eof |
| 8 | +(* Creates location from lexbuf *) |
| 9 | +let loc lexbuf = |
| 10 | + Loc.create ~start:(Lexing.lexeme_start_p lexbuf) ~stop:(Lexing.lexeme_end_p lexbuf) |
| 11 | +[@@inline] |
| 12 | + |
| 13 | +(* Adjusts the start position by an offset *) |
| 14 | +let adjust_start_cnum offset loc = |
| 15 | + let start = Loc.start loc in |
| 16 | + let start = { start with pos_cnum = start.pos_cnum + offset } in |
| 17 | + Loc.set_start loc start |
| 18 | +[@@inline] |
8 | 19 |
|
9 | | -let blank = [' ' '\t' '\r' '\012'] |
| 20 | +(* Creates location for " $ " pattern, adjusting past the " $ " prefix *) |
| 21 | +let loc_of_dollar lexbuf = |
| 22 | + loc lexbuf |> adjust_start_cnum 2 |
| 23 | +[@@inline] |
| 24 | + |
| 25 | +(* Gets position before consuming newline *) |
| 26 | +let pos_before_newline lexbuf = |
| 27 | + let pos = Lexing.lexeme_end_p lexbuf in |
| 28 | + { pos with pos_cnum = pos.pos_cnum - 1 } |
| 29 | +[@@inline] |
| 30 | + |
| 31 | +(* Creates comment from string list accumulator *) |
| 32 | +let create_comment_from_acc loc acc = |
| 33 | + match acc with |
| 34 | + | [] -> None |
| 35 | + | _ -> Some (loc, Comment (List.rev acc)) |
| 36 | +[@@inline] |
| 37 | +} |
| 38 | + |
| 39 | +let nonspace = [^' ' '\n'] |
| 40 | +let not_nl = [^'\n'] |
10 | 41 |
|
11 | 42 | rule block = parse |
12 | 43 | | 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 |
| 44 | + | " $ " ([^'\n']* as str) |
| 45 | + { eol_then_command_or_finish (loc_of_dollar lexbuf) str lexbuf } |
| 46 | + | " > " ([^'\n']* as str) |
| 47 | + { after_comment_line (loc lexbuf) [ " > " ^ str ] lexbuf } |
| 48 | + | " >" |
| 49 | + { after_comment_line (loc lexbuf) [ " >" ] lexbuf } |
| 50 | + | " " [^'\n']* |
| 51 | + { after_output_line (loc lexbuf) lexbuf } |
| 52 | + | ' ' ((nonspace not_nl*) as rest) |
| 53 | + { after_comment_line (loc lexbuf) [ " " ^ rest ] lexbuf } |
| 54 | + | ' ' '\n' |
| 55 | + { let loc = loc lexbuf in |
| 56 | + let loc = Loc.set_stop loc (pos_before_newline lexbuf) in |
| 57 | + Lexing.new_line lexbuf; |
| 58 | + comment loc [ " " ] lexbuf } |
| 59 | + | ' ' |
| 60 | + { comment (loc lexbuf) [ " " ] lexbuf } |
| 61 | + | '\n' |
| 62 | + { let loc = loc lexbuf in |
| 63 | + let loc = Loc.set_stop loc (Loc.start loc) in |
| 64 | + Lexing.new_line lexbuf; |
| 65 | + comment loc [ "" ] lexbuf } |
| 66 | + | nonspace not_nl* as str |
| 67 | + { after_comment_line (loc lexbuf) [str] lexbuf } |
| 68 | + |
| 69 | +and after_comment_line loc content = parse |
| 70 | + | '\n' { Lexing.new_line lexbuf; comment loc content lexbuf } |
| 71 | + | eof { comment loc content lexbuf } |
| 72 | + |
| 73 | +and after_output_line loc = parse |
| 74 | + | '\n' { Lexing.new_line lexbuf; output loc [] lexbuf } |
| 75 | + | eof { output loc [] lexbuf } |
| 76 | + |
| 77 | +and eol_then_command_or_finish loc str = parse |
| 78 | + | '\n' { Lexing.new_line lexbuf; Some (command_cont loc [ str ] lexbuf) } |
| 79 | + | eof { Some (loc, Command [ str ]) } |
| 80 | + |
| 81 | +and comment loc acc = parse |
23 | 82 | | 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)) } |
| 83 | + { create_comment_from_acc loc acc } |
| 84 | + | ' ' ((nonspace not_nl*) as rest) |
| 85 | + { let loc = Loc.set_stop loc (Lexing.lexeme_end_p lexbuf) in |
| 86 | + after_comment_line_in_comment loc (" " ^ rest) acc lexbuf } |
| 87 | + | ' ' '\n' |
| 88 | + { let loc = Loc.set_stop loc (pos_before_newline lexbuf) in |
| 89 | + Lexing.new_line lexbuf; |
| 90 | + comment loc (" " :: acc) lexbuf } |
| 91 | + | '\n' |
| 92 | + { let loc = Loc.set_stop loc (Lexing.lexeme_start_p lexbuf) in |
| 93 | + Lexing.new_line lexbuf; |
| 94 | + comment loc ("" :: acc) lexbuf } |
| 95 | + | nonspace not_nl* as str |
| 96 | + { let loc = Loc.set_stop loc (Lexing.lexeme_end_p lexbuf) in |
| 97 | + after_comment_line_in_comment loc str acc lexbuf } |
| 98 | + | "" { create_comment_from_acc loc acc } |
34 | 99 |
|
35 | | -and output maybe_comment = parse |
| 100 | +and after_comment_line_in_comment loc str acc = parse |
| 101 | + | '\n' { Lexing.new_line lexbuf; comment loc (str :: acc) lexbuf } |
| 102 | + | eof { comment loc (str :: acc) lexbuf } |
| 103 | + |
| 104 | +and output loc maybe_comment = parse |
36 | 105 | | 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 } |
| 106 | + { let loc = Loc.set_stop loc (Lexing.lexeme_start_p lexbuf) in |
| 107 | + create_comment_from_acc loc maybe_comment } |
| 108 | + | " $ " ([^'\n']* as str) |
| 109 | + { eol_then_command_or_finish (loc_of_dollar lexbuf) str lexbuf } |
| 110 | + | ' ' ((nonspace not_nl*) as rest) |
| 111 | + { check_eol_for_output loc ((" " ^ rest) :: maybe_comment) lexbuf } |
| 112 | + | ' ' '\n' |
| 113 | + { Lexing.new_line lexbuf; |
| 114 | + output loc (" " :: maybe_comment) lexbuf } |
| 115 | + | " " [^'\n']* |
| 116 | + { after_output_line_in_output loc maybe_comment lexbuf } |
55 | 117 | | "" |
56 | 118 | { 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 } |
| 119 | + | [] -> block lexbuf |
| 120 | + | l -> |
| 121 | + let loc = Loc.set_stop loc (Lexing.lexeme_start_p lexbuf) in |
| 122 | + comment loc l lexbuf } |
| 123 | + |
| 124 | +and after_output_line_in_output loc maybe_comment = parse |
| 125 | + | '\n' { Lexing.new_line lexbuf; output loc maybe_comment lexbuf } |
| 126 | + | eof { output loc maybe_comment lexbuf } |
| 127 | + |
| 128 | +and check_eol_for_output loc maybe_comment = parse |
| 129 | + | '\n' { Lexing.new_line lexbuf; output loc maybe_comment lexbuf } |
| 130 | + | eof { |
| 131 | + let loc = Loc.set_stop loc (Lexing.lexeme_start_p lexbuf) in |
| 132 | + Some (loc, Comment (List.rev maybe_comment)) } |
| 133 | + |
| 134 | +and command_cont loc acc = parse |
| 135 | + | " > " ([^'\n']* as str) |
| 136 | + { let loc = Loc.set_stop loc (Lexing.lexeme_end_p lexbuf) in |
| 137 | + eol_then_continue_or_finish loc str acc lexbuf } |
| 138 | + | " >" |
| 139 | + { let loc = Loc.set_stop loc (Lexing.lexeme_end_p lexbuf) in |
| 140 | + eol_then_continue_or_finish loc "" acc lexbuf } |
66 | 141 | | "" |
67 | | - { Command (List.rev acc) } |
| 142 | + { (loc, Command (List.rev acc)) } |
| 143 | + |
| 144 | +and eol_then_continue_or_finish loc content acc = parse |
| 145 | + | '\n' { Lexing.new_line lexbuf; command_cont loc (content :: acc) lexbuf } |
| 146 | + | eof { (loc, Command (List.rev (content :: acc))) } |
0 commit comments