Skip to content

Commit 11637d8

Browse files
committed
refactor
1 parent b8f1156 commit 11637d8

File tree

6 files changed

+112
-37
lines changed

6 files changed

+112
-37
lines changed

jscomp/core/classify_function.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@ let classify_exp (prog : _ Flow_ast.Expression.t ) : Js_raw_info.exp =
8383
- in parsing
8484
- in code generation
8585
*)
86-
let classify (prog : string) : Js_raw_info.exp =
86+
let classify ?check (prog : string) : Js_raw_info.exp =
8787
let prog, errors =
8888
Parser_flow.parse_expression
8989
(Parser_env.init_env None prog) false in
90-
if errors <> [] then
91-
assert false (* in the calling API, we already asked flow to raise*)
92-
else classify_exp prog
90+
(match check with
91+
| Some (loc,offset) ->
92+
Bs_flow_ast_utils.check_flow_errors
93+
~loc ~offset errors
94+
| None -> ());
95+
classify_exp prog
9396

9497

9598
let classify_stmt (prog : string) : Js_raw_info.stmt =

jscomp/core/classify_function.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525

2626
val classify :
27-
string -> Js_raw_info.exp
27+
?check:(Location.t * int) ->
28+
string ->
29+
Js_raw_info.exp
2830

2931
val classify_stmt :
3032
string -> Js_raw_info.stmt

jscomp/syntax/ast_attributes.ml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,24 @@ let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
278278
(Bs_ast_invariant.mark_used_bs_attribute attr ;
279279
match Ast_payload.is_single_int payload with
280280
| None ->
281-
begin match Ast_payload.is_single_string payload with
282-
| Some (s,None) ->
283-
st := Some (Str (s))
284-
| Some (s, Some "json") ->
285-
st := Some (Js_literal_str s )
286-
| None | Some (_, Some _) ->
281+
begin match payload with
282+
| PStr [ {
283+
pstr_desc =
284+
Pstr_eval (
285+
{pexp_desc =
286+
Pexp_constant
287+
(Pconst_string(s, (None | Some "json" as dec)))
288+
;
289+
_},_);
290+
_}] ->
291+
if dec = None then
292+
st := Some (Str (s))
293+
else
294+
st := Some (Js_literal_str s )
295+
| _ ->
287296
Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal
288297
end
289-
| Some v->
298+
| Some v->
290299
st := (Some (Int v))
291300
)
292301
else

jscomp/syntax/ast_payload.ml

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,37 +66,15 @@ let is_single_int (x : t ) : int option =
6666
| _ -> None
6767

6868

69-
let offset_pos
70-
({pos_lnum; pos_bol; pos_cnum} as loc : Lexing.position)
71-
({line; column} : Loc.position) first_line_offset : Lexing.position =
72-
if line = 1 then
73-
{loc with pos_cnum = pos_cnum + column + first_line_offset }
74-
else {
75-
loc with
76-
pos_lnum = pos_lnum + line - 1;
77-
pos_cnum = pos_bol + column
78-
}
69+
7970

8071
let flow_deli_off_set deli =
8172
(match deli with
8273
| None -> 1 (* length of '"'*)
8374
| Some deli ->
8475
String.length deli + 2 (* length of "{|"*)
8576
)
86-
(* Here the loc is the payload loc *)
87-
let check_flow_errors ~(loc : Location.t)
88-
~offset(errors : (Loc.t * Parse_error.t) list) =
89-
match errors with
90-
| [] -> ()
91-
| ({start ;
92-
_end },first_error) :: _ ->
93-
94-
Location.raise_errorf ~loc:{loc with
95-
loc_start = offset_pos loc.loc_start start
96-
offset ;
97-
loc_end = offset_pos loc.loc_start _end
98-
offset } "%s"
99-
(Parse_error.PP.error first_error)
77+
10078
;;
10179
let raw_as_string_exp_exn
10280
~(kind: Js_raw_info.raw_kind)
@@ -111,7 +89,7 @@ let raw_as_string_exp_exn
11189
;
11290
pexp_loc = loc} as e ,_);
11391
_}] ->
114-
check_flow_errors ~loc ~offset:(flow_deli_off_set deli) (match kind with
92+
Bs_flow_ast_utils.check_flow_errors ~loc ~offset:(flow_deli_off_set deli) (match kind with
11593
| Raw_re
11694
| Raw_exp ->
11795
let (_loc,e),errors = (Parser_flow.parse_expression (Parser_env.init_env None str) false) in

jscomp/syntax/bs_flow_ast_utils.ml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(* Copyright (C) 2020 - Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
let offset_pos
27+
({pos_lnum; pos_bol; pos_cnum} as loc : Lexing.position)
28+
({line; column} : Loc.position)
29+
first_line_offset : Lexing.position =
30+
if line = 1 then
31+
{loc with pos_cnum = pos_cnum + column + first_line_offset }
32+
else {
33+
loc with
34+
pos_lnum = pos_lnum + line - 1;
35+
pos_cnum = pos_bol + column
36+
}
37+
38+
(* Here the loc is the payload loc *)
39+
let check_flow_errors ~(loc : Location.t)
40+
~offset
41+
(errors : (Loc.t * Parse_error.t) list) =
42+
match errors with
43+
| [] -> ()
44+
| ({start ;
45+
_end },first_error) :: _ ->
46+
let loc_start = loc.loc_start in
47+
Location.raise_errorf
48+
~loc:{loc with
49+
loc_start = offset_pos loc_start start
50+
offset ;
51+
loc_end = offset_pos loc_start _end
52+
offset } "%s"
53+
(Parse_error.PP.error first_error)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(* Copyright (C) 2020 - Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
val check_flow_errors:
27+
loc:Location.t ->
28+
offset:int ->
29+
(Loc.t * Parse_error.t) list ->
30+
unit

0 commit comments

Comments
 (0)