Skip to content

Support Import Statements - WIP #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions src/Language/JavaScript/Parser/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ data JSStatement
| JSTry !JSAnnot !JSBlock ![JSTryCatch] !JSTryFinally -- ^try,block,catches,finally
| JSVariable !JSAnnot !(JSCommaList JSExpression) !JSSemi -- ^var|const, decl, autosemi
| JSWhile !JSAnnot !JSAnnot !JSExpression !JSAnnot !JSStatement -- ^while,lb,expr,rb,stmt
| JSImport !JSAnnot !(Maybe JSStatement) !JSAnnot JSExpression !JSSemi -- ^import
| JSWith !JSAnnot !JSAnnot !JSExpression !JSAnnot !JSStatement !JSSemi -- ^with,lb,expr,rb,stmt list
deriving (Data, Eq, Show, Typeable)

Expand Down Expand Up @@ -271,6 +272,8 @@ instance ShowStripped JSStatement where
ss (JSReturn _ (Just me) s) = "JSReturn " ++ ss me ++ " " ++ ss s
ss (JSReturn _ Nothing s) = "JSReturn " ++ ss s
ss (JSSwitch _ _lp x _rp _lb x2 _rb _) = "JSSwitch (" ++ ss x ++ ") " ++ ss x2
ss (JSImport _ Nothing _ x1 _) = "JSImport (" ++ ss x1 ++ ")"
ss (JSImport _ (Just f) _ x1 _) = "JSImport (" ++ ss f ++ ") (" ++ ss x1 ++ ")"
ss (JSThrow _ x _) = "JSThrow (" ++ ss x ++ ")"
ss (JSTry _ xt1 xtc xtf) = "JSTry (" ++ ss xt1 ++ "," ++ ss xtc ++ "," ++ ss xtf ++ ")"
ss (JSVariable _ xs _as) = "JSVariable " ++ ss xs
Expand Down
2 changes: 1 addition & 1 deletion src/Language/JavaScript/Parser/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ keywordNames =
, ( "export", FutureToken )
, ( "extends", FutureToken )

, ( "import", FutureToken )
, ( "import", ImportToken )
, ( "super", FutureToken )


Expand Down
1 change: 1 addition & 0 deletions src/Language/JavaScript/Parser/Token.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ data Token
| VarToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| VoidToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| WhileToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| ImportToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
| WithToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
-- Future reserved words
| FutureToken { tokenSpan :: !TokenPosn, tokenLiteral :: !String, tokenComment :: ![CommentAnnotation] }
Expand Down
5 changes: 4 additions & 1 deletion test/Test/Language/Javascript/StatementParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ testStatementParser = describe "Parse statements:" $ do
it "if" $
testStmt "if (1) {}" `shouldBe` "Right (JSAstStatement (JSIf (JSDecimal '1') (JSStatementBlock [])))"

it "import" $ do
testStmt "import 'a';" `shouldBe` "Right (JSAstStatement (JSImport (JSStringLiteral 'a')))"
testStmt "import a from 'test';" `shouldBe` "Right (JSAstStatement (JSImport (JSIdentifier 'a') (JSStringLiteral 'test')))"

it "if/else" $ do
testStmt "if (1) {} else {}" `shouldBe` "Right (JSAstStatement (JSIfElse (JSDecimal '1') (JSStatementBlock []) (JSStatementBlock [])))"
testStmt "if (1) x=1; else {}" `shouldBe` "Right (JSAstStatement (JSIfElse (JSDecimal '1') (JSOpAssign ('=',JSIdentifier 'x',JSDecimal '1'),JSSemicolon) (JSStatementBlock [])))"
Expand Down Expand Up @@ -101,4 +105,3 @@ testStatementParser = describe "Parse statements:" $ do

testStmt :: String -> String
testStmt str = showStrippedMaybe (parseUsing parseStatement str "src")