| 1 |
open Base |
| 2 |
open Ppxlib |
| 3 |
|
| 4 |
module Directive = struct |
| 5 |
type t = |
| 6 |
| If |
| 7 |
| Else |
| 8 |
| Elif |
| 9 |
| Endif |
| 10 |
| Ifdef |
| 11 |
| Ifndef |
| 12 |
| Define |
| 13 |
| Undef |
| 14 |
| Error |
| 15 |
| Warning |
| 16 |
| Import |
| 17 |
| |
| 18 |
Elifdef |
| 19 |
| Elifndef |
| 20 |
|
| 21 |
let matches ~expected matched = |
| 22 |
String.( = ) expected matched || String.( = ) ("optcomp." ^ expected) matched |
| 23 |
;; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
let of_string_opt s = |
| 28 |
match s with |
| 29 |
| "optcomp.if" | "if" -> Some If |
| 30 |
| "optcomp.else" | "else" -> Some Else |
| 31 |
| "optcomp.elif" | "elif" -> Some Elif |
| 32 |
| "optcomp.endif" | "endif" -> Some Endif |
| 33 |
| "optcomp.ifdef" | "ifdef" -> Some Ifdef |
| 34 |
| "optcomp.ifndef" | "ifndef" -> Some Ifndef |
| 35 |
| "optcomp.define" | "define" -> Some Define |
| 36 |
| "optcomp.undef" | "undef" -> Some Undef |
| 37 |
| "optcomp.error" -> Some Error |
| 38 |
| "optcomp.warning" | "warning" -> Some Warning |
| 39 |
| "optcomp.import" | "import" -> Some Import |
| 40 |
| "optcomp.elifdef" | "elifdef" -> Some Elifdef |
| 41 |
| "optcomp.elifndef" | "elifndef" -> Some Elifndef |
| 42 |
| _ -> None |
| 43 |
;; |
| 44 |
end |
| 45 |
|
| 46 |
type 'a t = |
| 47 |
| Block of 'a list |
| 48 |
| Directive of Directive.t * location * payload |
| 49 |
|
| 50 |
let make_directive name loc payload = |
| 51 |
match Directive.of_string_opt name with |
| 52 |
| Some dir -> Directive (dir, loc, payload) |
| 53 |
| None -> Location.raise_errorf ~loc "optcomp: unknown directive" |
| 54 |
;; |
| 55 |
|
| 56 |
let just_directives_exn ~loc ls = |
| 57 |
List.filter_map ls ~f:(fun token -> |
| 58 |
match token with |
| 59 |
| Directive _ as dir -> Some dir |
| 60 |
| Block [] -> None |
| 61 |
| Block _ -> |
| 62 |
Location.raise_errorf |
| 63 |
~loc |
| 64 |
"optcomp: only optcomp directives allowed in this context") |
| 65 |
;; |