Skip to content

Commit b0991aa

Browse files
committed
Add a unit ppx attribute value parser.
1 parent 3e85333 commit b0991aa

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

ppx/ppx_attribute_value.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ let bool ?separated_by:_ ?default:_ loc name s =
181181

182182
Some (Exp.construct ~loc (Location.mkloc (Longident.parse s) loc) None)
183183

184+
let unit ?separated_by:_ ?default:_ loc name s =
185+
if s = "" || s = name then
186+
Some (Ast_convenience.(with_default_loc loc unit))
187+
else
188+
Ppx_common.error loc
189+
{|Value of %s must be %s or "".|}
190+
name name
191+
184192
let int ?separated_by ?default loc name s =
185193
match int_exp loc s with
186194
| Some _ as e -> e

ppx/ppx_attribute_value.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ val bool : parser
9797
(** [bool _ _ s] produces a parse tree for the boolean [true] if [s = "true"]
9898
and [false] if [s = "false"]. *)
9999

100+
val unit : parser
101+
(** [unit _ name s] produces a parse tree for [()]. It fails if [name <> s]. *)
102+
100103
val int : parser
101104
(** [int _ _ s] produces a parse tree for [int_of_string s]. *)
102105

ppx/ppx_reflect.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ let rec to_attribute_parser name = function
9797

9898
| [[%type: character]] -> [%expr char]
9999
| [[%type: bool]] -> [%expr bool]
100+
| [[%type: unit]] -> [%expr nowrap unit]
100101

101102
| [[%type: number]]
102103
| [[%type: pixels]]

test/test_ppx.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ let basics = "ppx basics", tyxml_tests Html.[
6464

6565
]
6666

67+
let attribs = "ppx attribs", tyxml_tests Html.[
68+
69+
"unit absent",
70+
[[%html "<div hidden></div>"]],
71+
[div ~a:[a_hidden ()] []] ;
72+
73+
"unit present",
74+
[[%html "<div hidden=hidden></div>"]],
75+
[div ~a:[a_hidden ()] []] ;
76+
77+
]
78+
6779
let ns_nesting = "namespace nesting" , tyxml_tests Html.[
6880

6981
"html/svg",
@@ -122,6 +134,7 @@ let antiquot = "ppx antiquot", tyxml_tests Html.[
122134

123135
let tests = [
124136
basics ;
137+
attribs ;
125138
ns_nesting ;
126139
antiquot ;
127140
]

0 commit comments

Comments
 (0)