Skip to content

Commit

Permalink
Compiler: js-parser, support #privateName
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Dec 14, 2023
1 parent 7229a4b commit c8ff8d8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion compiler/lib/javascript.ml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ and expression =
| ERegexp of string * string option
| EYield of expression option
| EYieldDelegate of expression option
| EPrivName of identifier
| CoverParenthesizedExpressionAndArrowParameterList of early_error
| CoverCallExpressionAndAsyncArrowHead of early_error

Expand Down Expand Up @@ -397,7 +398,7 @@ and class_element =

and class_element_name =
| PropName of property_name
| PrivName of ident
| PrivName of identifier

and ('a, 'b) list_with_rest =
{ list : 'a list
Expand Down
3 changes: 2 additions & 1 deletion compiler/lib/javascript.mli
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ and expression =
| ERegexp of string * string option
| EYield of expression option
| EYieldDelegate of expression option
| EPrivName of identifier
| CoverParenthesizedExpressionAndArrowParameterList of early_error
| CoverCallExpressionAndAsyncArrowHead of early_error

Expand Down Expand Up @@ -317,7 +318,7 @@ and class_element =

and class_element_name =
| PropName of property_name
| PrivName of ident
| PrivName of identifier

and ('a, 'b) list_with_rest =
{ list : 'a list
Expand Down
10 changes: 7 additions & 3 deletions compiler/lib/js_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ struct
| ENew _
| EClass _
| EYield _
| EYieldDelegate _ -> false
| EYieldDelegate _
| EPrivName _ -> false
| CoverCallExpressionAndAsyncArrowHead e
| CoverParenthesizedExpressionAndArrowParameterList e -> early_error e
in
Expand Down Expand Up @@ -884,6 +885,9 @@ struct
PP.string f ")"
(* There MUST be a space between the yield and its
argument. A line return will not work *)))
| EPrivName (Utf8 i) ->
PP.string f "#";
PP.string f i
| CoverCallExpressionAndAsyncArrowHead e
| CoverParenthesizedExpressionAndArrowParameterList e -> early_error e

Expand Down Expand Up @@ -1737,9 +1741,9 @@ struct
and class_element_name f x =
match x with
| PropName n -> property_name f n
| PrivName i ->
| PrivName (Utf8 i) ->
PP.string f "#";
ident f i
PP.string f i
and program f s = statement_list f s
end
Expand Down
11 changes: 6 additions & 5 deletions compiler/lib/js_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ class_element:

class_property_name:
| property_name { PropName $1 }
| T_POUND ident { PrivName $2 }
| T_POUND id { PrivName $2 }

method_definition(name):
| T_GET name=name args=call_signature "{" b=function_body "}" { name, MethodGet(({async = false; generator = false}, args, b, p $symbolstartpos)) }
Expand Down Expand Up @@ -896,6 +896,8 @@ call_expr(x):
| T_SUPER a=arguments { ECall(vartok $startpos($1) T_SUPER,ANormal, a, p $symbolstartpos) }
| e=call_expr(x) a=access i=method_name
{ EDot (e,a,i) }
| e=call_expr(x) a=access T_POUND i=method_name
{ EDotPrivate (e,a,i) }

new_expr(x):
| e=member_expr(x) { e }
Expand Down Expand Up @@ -926,10 +928,8 @@ member_expr(x):
{ (EDot(vartok $startpos($1) T_SUPER,ak,i)) }
| T_NEW "." T_TARGET
{ (EDot(vartok $startpos($1) T_NEW,ANormal,Stdlib.Utf8_string.of_string_exn "target")) }
| e1=member_expr(x) "." T_POUND i=field_name
{ (EDotPrivate(e1,ANormal,i)) }
| e1=member_expr(x) T_PLING_PERIOD T_POUND i=field_name
{ (EDotPrivate(e1,ANullish,i)) }
| e1=member_expr(x) a=access T_POUND i=field_name
{ (EDotPrivate(e1,a,i)) }
primary_expr(x):
| e=primary_expr_no_braces
| e=x { e }
Expand All @@ -950,6 +950,7 @@ primary_with_stmt:
primary_expr_no_braces:
| T_THIS { EVar (var (p $symbolstartpos) (Stdlib.Utf8_string.of_string_exn "this")) }
| i=ident { EVar i }
| T_POUND id { EPrivName $2 }
| n=null_literal { n }
| b=boolean_literal { b }
| n=numeric_literal { ENum (Num.of_string_unsafe n) }
Expand Down
1 change: 1 addition & 0 deletions compiler/lib/js_simpl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ let rec enot_rec e =
| J.EDotPrivate _
| J.ENew _
| J.EVar _
| J.EPrivName _
| J.EFun _
| J.EArrow _
| J.EStr _
Expand Down
6 changes: 4 additions & 2 deletions compiler/lib/js_traverse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class map : mapper =
method private class_element_name x =
match x with
| PropName n -> PropName (m#property_name n)
| PrivName x -> PrivName (m#ident x)
| PrivName x -> PrivName x

method block l = m#statements l

Expand Down Expand Up @@ -336,6 +336,7 @@ class map : mapper =
| ETemplate t -> ETemplate (m#template t)
| EYield e -> EYield (m#expression_o e)
| EYieldDelegate e -> EYieldDelegate (m#expression_o e)
| EPrivName i -> EPrivName i
| CoverParenthesizedExpressionAndArrowParameterList e ->
CoverParenthesizedExpressionAndArrowParameterList (m#early_error e)
| CoverCallExpressionAndAsyncArrowHead e ->
Expand Down Expand Up @@ -490,7 +491,7 @@ class iter : iterator =
method private class_element_name x =
match x with
| PropName n -> m#property_name n
| PrivName x -> m#ident x
| PrivName (Utf8 _) -> ()

method statement s =
match s with
Expand Down Expand Up @@ -692,6 +693,7 @@ class iter : iterator =
| ETemplate l -> m#template l
| EYield e -> m#expression_o e
| EYieldDelegate e -> m#expression_o e
| EPrivName (Utf8 _) -> ()
| CoverParenthesizedExpressionAndArrowParameterList e -> m#early_error e
| CoverCallExpressionAndAsyncArrowHead e -> m#early_error e

Expand Down

0 comments on commit c8ff8d8

Please sign in to comment.