| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
module Unshadow = struct |
| 6 |
module Parser = Parser |
| 7 |
end |
| 8 |
|
| 9 |
open Ppxlib |
| 10 |
open Unshadow |
| 11 |
module Parsing = Stdlib.Parsing |
| 12 |
|
| 13 |
type lexer = Lexing.lexbuf -> Parser.token |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
let located x lexbuf = { Location.txt = x; loc = Location.of_lexbuf lexbuf } |
| 20 |
|
| 21 |
let parse parsing_fun lexer lexbuf = |
| 22 |
try parsing_fun lexer lexbuf with |
| 23 |
| Parsing.Parse_error | Syntaxerr.Escape_error -> |
| 24 |
let loc = Location.of_lexbuf lexbuf in |
| 25 |
raise (Syntaxerr.Error (Syntaxerr.Other loc)) |
| 26 |
;; |
| 27 |
|
| 28 |
let fetch_directive_argument (lexer : lexer) lexbuf = |
| 29 |
let rec loop acc (brackets : Parser.token list) = |
| 30 |
match lexer lexbuf, brackets with |
| 31 |
| EOF, _ | EOL, [] -> located Parser.EOF lexbuf :: acc |
| 32 |
| (EOL | COMMENT _), _ -> loop acc brackets |
| 33 |
| token, _ -> |
| 34 |
let acc = located token lexbuf :: acc in |
| 35 |
(match token, brackets with |
| 36 |
| BEGIN, _ -> loop acc (END :: brackets) |
| 37 |
| DO, _ -> loop acc (DONE :: brackets) |
| 38 |
| LPAREN, _ -> loop acc (RPAREN :: brackets) |
| 39 |
| LBRACE, _ -> loop acc (RBRACE :: brackets) |
| 40 |
| LBRACELESS, _ -> loop acc (GREATERRBRACE :: brackets) |
| 41 |
| LBRACKETLESS, _ -> loop acc (GREATERRBRACKET :: brackets) |
| 42 |
| LBRACKETBAR, _ -> loop acc (BARRBRACKET :: brackets) |
| 43 |
| ( ( LBRACKET |
| 44 |
| LBRACKETGREATER |
| 45 |
| LBRACKETPERCENT |
| 46 |
| LBRACKETPERCENTPERCENT |
| 47 |
| LBRACKETAT |
| 48 |
| LBRACKETATAT |
| 49 |
| LBRACKETATATAT ) |
| 50 |
, _ ) -> loop acc (RBRACKET :: brackets) |
| 51 |
| _, closing :: brackets when token = closing -> loop acc brackets |
| 52 |
| _ -> loop acc brackets) |
| 53 |
in |
| 54 |
let start_pos = Lexing.lexeme_end_p lexbuf in |
| 55 |
match loop [] [] |> List.rev with |
| 56 |
| [] -> None |
| 57 |
| tokens -> |
| 58 |
let tokens = ref tokens in |
| 59 |
let fake_lexer (lexbuf : Lexing.lexbuf) : Parser.token = |
| 60 |
match !tokens with |
| 61 |
| [] -> EOF |
| 62 |
| token :: rest -> |
| 63 |
tokens := rest; |
| 64 |
lexbuf.lex_start_p <- token.loc.loc_start; |
| 65 |
lexbuf.lex_curr_p <- token.loc.loc_end; |
| 66 |
token.txt |
| 67 |
in |
| 68 |
let fake_lexbuf = Lexing.from_function (fun _ _ -> assert false) in |
| 69 |
fake_lexbuf.lex_curr_p <- start_pos; |
| 70 |
(match |
| 71 |
Parse.Of_ocaml.copy_structure (parse Parser.implementation fake_lexer fake_lexbuf) |
| 72 |
with |
| 73 |
| [] -> None |
| 74 |
| [ st ] -> |
| 75 |
assert_no_attributes_in#structure_item st; |
| 76 |
Some st |
| 77 |
| _ :: st :: _ -> |
| 78 |
Location.raise_errorf ~loc:st.pstr_loc "optcomp: too many structure items") |
| 79 |
;; |
| 80 |
|
| 81 |
let parse_directive (lexer : lexer) lexbuf : 'a Token.t = |
| 82 |
let token = located (lexer lexbuf) lexbuf in |
| 83 |
let arg = fetch_directive_argument lexer lexbuf in |
| 84 |
let loc = { token.loc with loc_end = Lexing.lexeme_end_p lexbuf } in |
| 85 |
let payload = |
| 86 |
match arg with |
| 87 |
| Some st_item -> PStr [ st_item ] |
| 88 |
| None -> PStr [] |
| 89 |
in |
| 90 |
match token.txt with |
| 91 |
| IF -> Token.make_directive "if" loc payload |
| 92 |
| ELSE -> Token.make_directive "else" loc payload |
| 93 |
| LIDENT s -> Token.make_directive s loc payload |
| 94 |
| _ -> Location.raise_errorf ~loc "optcomp: unknown token" |
| 95 |
;; |
| 96 |
|
| 97 |
let parse_loop lexbuf = |
| 98 |
let is_beginning_of_line lexbuf = |
| 99 |
let pos = Lexing.lexeme_start_p lexbuf in |
| 100 |
pos.pos_cnum = pos.pos_bol |
| 101 |
in |
| 102 |
let rec parse_loop_aux acc = |
| 103 |
match Lexer.token_with_comments lexbuf with |
| 104 |
| HASH when is_beginning_of_line lexbuf -> |
| 105 |
let acc = parse_directive Lexer.token_with_comments lexbuf :: acc in |
| 106 |
parse_loop_aux acc |
| 107 |
| EOF -> acc |
| 108 |
| _ -> parse_loop_aux acc |
| 109 |
in |
| 110 |
List.rev (parse_loop_aux []) |
| 111 |
;; |