From a9d28f95c1c9ef11194f85396a3153d634fe9734 Mon Sep 17 00:00:00 2001 From: Rick Winfrey Date: Wed, 12 Oct 2016 12:35:20 -0500 Subject: [PATCH] Break apart else clauses for If syntax nodes - This is an intermediate step and not complete. We currently only handle one level of else-if, rather than arbitrary levels of else-if. --- src/Language/JavaScript.hs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Language/JavaScript.hs b/src/Language/JavaScript.hs index 0f3c7e9a9..0d7207523 100644 --- a/src/Language/JavaScript.hs +++ b/src/Language/JavaScript.hs @@ -49,8 +49,10 @@ termConstructor source sourceSpan name range children ("case", [ expr, body ]) -> S.Case expr body ("object", _) -> S.Object $ foldMap toTuple children ("pair", _) -> S.Fixed children - ("if_statement", [ expr, clause1, clause2 ]) -> S.If expr clause1 (Just clause2) - ("if_statement", [ expr, clause ]) -> S.If expr clause Nothing + ("if_statement", [ expr, thenClause, elseClause ]) -> case unwrap elseClause of + S.If{} -> S.If expr thenClause [(toElseIf elseClause)] + _ -> S.If expr thenClause [elseClause] + ("if_statement", [ expr, thenClause ]) -> S.If expr thenClause [] ("while_statement", [ expr, body ]) -> S.While expr body ("do_statement", [ expr, body ]) -> S.DoWhile expr body ("throw_statement", [ expr ]) -> S.Throw expr @@ -156,6 +158,9 @@ categoryForJavaScriptProductionName name = case name of toVarDecl :: (HasField fields Category) => Term (S.Syntax Text) (Record fields) -> Term (S.Syntax Text) (Record fields) toVarDecl child = cofree $ setCategory (extract child) VarDecl :< S.VarDecl child +toElseIf :: (HasField fields Category) => Term (S.Syntax Text) (Record fields) -> Term (S.Syntax Text) (Record fields) +toElseIf child = cofree $ setCategory (extract child) If :< S.If child child [] + toTuple :: Term (S.Syntax Text) (Record fields) -> [Term (S.Syntax Text) (Record fields)] toTuple child | S.Indexed [key,value] <- unwrap child = [cofree (extract child :< S.Pair key value)] toTuple child | S.Fixed [key,value] <- unwrap child = [cofree (extract child :< S.Pair key value)]