Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions packages/parser/lib/Lexer.re
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,21 @@ let escape = [%sedlex.regexp? '\\'];

let alpha = [%sedlex.regexp? 'a' .. 'z' | 'A' .. 'Z'];


// https://drafts.csswg.org/css-syntax-3/#non-ascii-ident-code-point
let non_ascii = [%sedlex.regexp?
0x00B7
| 0x00C0 .. 0x00D6
| 0x00D8 .. 0x00F6
| 0x00F8 .. 0x037D
| 0x037F .. 0x1FFF
| 0x200C
| 0x200D
| 0x203F
| 0x2040
| 0x2070 .. 0x218F
| 0x2C00 .. 0x2FEF
| 0x3001 .. 0xD7FF
| 0xF900 .. 0xFDCF
| 0xFDF0 .. 0xFFFD
| Compl (0x0000 .. 0x10000)]
0x00B7 | 0x00C0 .. 0x00D6 | 0x00D8 .. 0x00F6 | 0x00F8 .. 0x037D |
0x037F .. 0x1FFF |
0x200C |
0x200D |
0x203F |
0x2040 |
0x2070 .. 0x218F |
0x2C00 .. 0x2FEF |
0x3001 .. 0xD7FF |
0xF900 .. 0xFDCF |
0xFDF0 .. 0xFFFD |
Compl(0x0000 .. 0x10000)
];

let ident_start = [%sedlex.regexp? '_' | alpha | '$' | non_ascii | escape];

Expand Down Expand Up @@ -123,9 +120,7 @@ let at_rule_without_body = [%sedlex.regexp?
let at_rule = [%sedlex.regexp? ("@", ident)];
let at_keyframes = [%sedlex.regexp? ("@", "keyframes")];

let identifier_start_code_point = [%sedlex.regexp?
alpha | non_ascii | '_'
];
let identifier_start_code_point = [%sedlex.regexp? alpha | non_ascii | '_'];
let starts_with_a_valid_escape = [%sedlex.regexp? ('\\', Sub(any, '\n'))];

let starts_an_identifier = [%sedlex.regexp?
Expand Down
4 changes: 1 addition & 3 deletions packages/ppx/src/Css_to_runtime.re
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ let string_to_const = (~loc, s) => {
};
};
let render_variable = (~loc, v) => {
open Ppxlib;
let txt = v |> String.concat(".") |> Longident.parse;
Helper.Exp.ident({
loc,
txt,
txt: v |> String.concat(".") |> Ppxlib.Longident.parse,
});
};

Expand Down
25 changes: 25 additions & 0 deletions packages/ppx/src/Error.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Builder = Ppxlib.Ast_builder.Default

let make ~description ~examples ~link =
let examples =
match examples with
| Some examples ->
Printf.sprintf "\n\nExample:\n %s" (String.concat "\n " examples)
| None -> ""
in
let link =
match link with
| Some link -> Printf.sprintf "\n\nMore info: %s" link
| None -> ""
in
Printf.sprintf "%s%s%s" description examples link

let expr ~loc ?examples ?link description =
let message = make ~description ~examples ~link in
Builder.pexp_extension ~loc
@@ Ppxlib.Location.Error.to_extension
@@ Ppxlib.Location.Error.make ~loc message ~sub:[]

let raise ~loc ?examples ?link description =
let message = make ~description ~examples ~link in
raise @@ Ppxlib.Location.raise_errorf ~loc "%s" message
17 changes: 17 additions & 0 deletions packages/ppx/src/Error.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
val expr :
loc:Ppxlib.location ->
?examples:string list ->
?link:string ->
string ->
Ppxlib.expression

(** This is a wrapper around `Ppxlib.Location.raise_errorf` that allows for
examples and a link to the documentation, similarly to
`Ppxlib.Location.raise_errorf` shouldn't be used much, prefer to use
`Error.expr` instead. *)
val raise :
loc:Ppxlib.location ->
?examples:string list ->
?link:string ->
string ->
'raises
30 changes: 0 additions & 30 deletions packages/ppx/src/Error.re

This file was deleted.

12 changes: 5 additions & 7 deletions packages/ppx/src/generate.re
Original file line number Diff line number Diff line change
Expand Up @@ -924,10 +924,10 @@ let rec getArgs = (expr, list) => {
| Pexp_fun(arg, _, pattern, _) when !getIsLabelled(arg) =>
Error.raise(
~loc=pattern.ppat_loc,
~description="Dynamic components are defined with labeled arguments.",
~example=Some("[%styled.div (~a, ~b) => {}]"),
~examples=["[%styled.div (~a, ~b) => {}]"],
~link=
"https://reasonml.org/docs/manual/latest/function#labeled-arguments",
"Dynamic components are defined with labeled arguments.",
)
| _ => (expr, list)
};
Expand All @@ -951,20 +951,18 @@ let getLabeledArgs = (label, defaultValue, param, expr) => {
if (getIsEmpty(param)) {
Error.raise(
~loc=param.ppat_loc,
~description=
"A dynamic component without props doesn't make much sense. This component should be static.",
~example=None,
~link="https://styled-ppx.vercel.app/usage/dynamic-components",
"A dynamic component without props doesn't make much sense. This component should be static.",
);
};

if (getNotLabelled(label)) {
Error.raise(
~loc=param.ppat_loc,
~description="Dynamic components are defined with labeled arguments.",
~example=Some("[%styled.div (~a, ~b) => {}]"),
~examples=["[%styled.div (~a, ~b) => {}]"],
~link=
"https://reasonml.org/docs/manual/latest/function#labeled-arguments",
"Dynamic components are defined with labeled arguments.",
);
};

Expand Down
Loading
Loading