Skip to content

Commit

Permalink
[parser] let/const parser cleanup
Browse files Browse the repository at this point in the history
Summary: there was duplication in Statement_parser, and Declaration_parser.variable was statement-specific and didn't belong there.

Reviewed By: panagosg7

Differential Revision: D10493287

fbshipit-source-id: d848975918884f0ef68200e51545afac55e3dea9
  • Loading branch information
mroch authored and facebook-github-bot committed Oct 23, 2018
1 parent 771548f commit b9e43a4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
16 changes: 0 additions & 16 deletions src/parser/declaration_parser.ml
Expand Up @@ -23,8 +23,6 @@ module type DECLARATION = sig
val is_simple_function_params: (Loc.t, Loc.t) Ast.Function.Params.t -> bool
val strict_post_check: env -> strict:bool -> simple:bool -> Loc.t Identifier.t option -> (Loc.t, Loc.t) Ast.Function.Params.t -> unit
val concise_function_body: env -> async:bool -> generator:bool -> (Loc.t, Loc.t) Function.body * bool
val variable: env -> (Loc.t, Loc.t) Statement.t * (Loc.t * Error.t) list
val variable_declaration_list: env -> (Loc.t, Loc.t) Statement.VariableDeclaration.Declarator.t list * (Loc.t * Error.t) list
val let_: env -> (Loc.t, Loc.t) Statement.VariableDeclaration.t * (Loc.t * Error.t) list
val const: env -> (Loc.t, Loc.t) Statement.VariableDeclaration.t * (Loc.t * Error.t) list
val var: env -> (Loc.t, Loc.t) Statement.VariableDeclaration.t * (Loc.t * Error.t) list
Expand Down Expand Up @@ -329,18 +327,4 @@ module Declaration
let let_ env =
let env = env |> with_no_let true in
declarations T_LET Statement.VariableDeclaration.Let env

let variable env =
let loc, (decl, errs) = with_loc (fun env ->
let variable, errs = match Peek.token env with
| T_CONST -> const env
| T_LET -> let_ env
| T_VAR -> var env
| _ ->
error_unexpected env;
(* We need to return something. This is as good as anything else *)
var env in
Statement.VariableDeclaration variable, errs
) env in
(loc, decl), errs
end
4 changes: 2 additions & 2 deletions src/parser/parser_flow.ml
Expand Up @@ -148,7 +148,7 @@ module rec Parse : PARSER = struct
(* Remember kids, these look like statements but they're not
* statements... (see section 13) *)
| T_LET -> let_ env
| T_CONST -> var_or_const env
| T_CONST -> const env
| _ when Peek.is_function env -> Declaration._function env
| _ when Peek.is_class env -> class_declaration env decorators
| T_INTERFACE -> interface env
Expand All @@ -164,7 +164,7 @@ module rec Parse : PARSER = struct
Peek.loc env, Ast.Statement.Empty
| T_SEMICOLON -> empty env
| T_LCURLY -> block env
| T_VAR -> var_or_const env
| T_VAR -> var env
| T_BREAK -> break env
| T_CONTINUE -> continue env
| T_DEBUGGER -> debugger env
Expand Down
27 changes: 14 additions & 13 deletions src/parser/statement_parser.ml
Expand Up @@ -41,7 +41,8 @@ module type STATEMENT = sig
val switch: env -> (Loc.t, Loc.t) Statement.t
val throw: env -> (Loc.t, Loc.t) Statement.t
val type_alias: env -> (Loc.t, Loc.t) Statement.t
val var_or_const: env -> (Loc.t, Loc.t) Statement.t
val var: env -> (Loc.t, Loc.t) Statement.t
val const: env -> (Loc.t, Loc.t) Statement.t
end

module Statement
Expand Down Expand Up @@ -424,25 +425,25 @@ module Statement
}
)

and var_or_const = with_loc (fun env ->
let (_loc, declaration), errs = Declaration.variable env in
and var = with_loc (fun env ->
let declaration, errs = Declaration.var env in
Eat.semicolon env;
errs |> List.iter (error_at env);
declaration
Statement.VariableDeclaration declaration
)

and const = with_loc (fun env ->
let declaration, errs = Declaration.const env in
Eat.semicolon env;
errs |> List.iter (error_at env);
Statement.VariableDeclaration declaration
)

and let_ = with_loc (fun env ->
Expect.token env T_LET;
(* Let declaration *)
let declarations, errs = Declaration.variable_declaration_list (env |> with_no_let true) in
let declaration =
Ast.(Statement.VariableDeclaration Statement.VariableDeclaration.({
declarations;
kind = Let;
})) in
let declaration, errs = Declaration.let_ env in
Eat.semicolon env;
errs |> List.iter (error_at env);
declaration
Statement.VariableDeclaration declaration
)

and while_ = with_loc (fun env ->
Expand Down

0 comments on commit b9e43a4

Please sign in to comment.