Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Compiles. Time to add a test.

  • Loading branch information...
commit 1388d58195ad51a09f9c1b8e24ec41ec692356b1 1 parent c512d2b
Alan Zimmerman authored
4 .gitignore
View
@@ -10,3 +10,7 @@
/src/Language/JavaScript/Parser/StringEscape.hs~
/src/Language/JavaScript/Parser/Token.hs~
/src/Language/JavaScript/Parser/AST.hs~
+/build.sh~
+/src/Language/JavaScript/Parser/Parser.hs~
+/src/Language/JavaScript/Parser/Parser/Lexer.x~
+/src/Language/JavaScript/Parser/Parser/Parser.y~
3  build.sh
View
@@ -0,0 +1,3 @@
+
+cabal build
+
4 language-javascript.cabal
View
@@ -17,9 +17,11 @@ Library
Build-depends: base >= 4 && < 5
, array >= 0.3 && < 0.5
, mtl >= 1.1 && < 2
+ , containers >= 0.2 && < 0.5
hs-source-dirs: src
Exposed-modules: Language.JavaScript.Parser.Parser
- Language.JavaScript.Parser.Lexer
+ Language.JavaScript.Parser.Parser.Lexer
+ Language.JavaScript.Parser.Parser.Parser
-- Other-modules:
Build-tools: happy, alex
27 src/Language/JavaScript/Parser/Parser.hs
View
@@ -0,0 +1,27 @@
+module Language.JavaScript.Parser.Parser (
+ -- * Parsing modules
+ -- parseModule,
+ -- * Parsing statements
+ parseStmt,
+ -- * Parsing expressions
+ -- parseExpr)
+ ) where
+
+import Language.JavaScript.Parser.ParseError
+import Language.JavaScript.Parser.Parser.Parser
+import Language.JavaScript.Parser.Parser.Lexer
+import Language.JavaScript.Parser.ParserMonad
+import Language.JavaScript.Parser.SrcLocation
+import Language.JavaScript.Parser.Token
+import qualified Language.JavaScript.Parser.AST as AST
+
+-- | Parse one compound statement, or a sequence of simple statements. Generally used for interactive input, such as from the command line of an interpreter. Return comments in addition to the parsed statements.
+parseStmt :: String -- ^ The input stream (python statement source code).
+ -> String -- ^ The name of the python source (filename or input device).
+ -- -> Either ParseError ([StatementSpan], [Token]) -- ^ An error or maybe the abstract syntax tree (AST) of zero or more python statements, plus comments.
+ -> Either ParseError (AST.JSNode, [Token])
+parseStmt input srcName =
+ execParserKeepComments parse state
+ where
+ initLoc = initialSrcLocation srcName
+ state = initialState initLoc input initStartCodeStack
51 src/Language/JavaScript/Parser/Lexer.x → src/Language/JavaScript/Parser/Parser/Lexer.x
View
@@ -1,19 +1,28 @@
{
-module Language.JavaScript.Parser.Lexer (Token(..),lexCont {-, alexScanTokens-}) where
+module Language.JavaScript.Parser.Parser.Lexer (Token(..),lexCont {-, alexScanTokens-},initStartCodeStack) where
import Language.JavaScript.Parser.LexerUtils
import Language.JavaScript.Parser.ParserMonad
import Language.JavaScript.Parser.SrcLocation
import Language.JavaScript.Parser.Token
+import qualified Data.Map as Map
+
}
--%wrapper "basic"
+-- character sets
+$lf = \n -- line feed
+$cr = \r -- carriage return
$digit = 0-9 -- digits
$alpha = [a-zA-Z] -- alphabetic characters
$digit = 0-9
$non_zero_digit = 1-9
+$ident_letter = [a-zA-Z_]
+@eol_pattern = $lf | $cr $lf | $cr $lf
+
+
@reservedid =
break|case|catch|const|continue|
@@ -30,6 +39,16 @@ $non_zero_digit = 1-9
tokens :-
+-- beginning of file
+<bof> {
+ @eol_pattern ;
+ -- @eol_pattern { endOfLine lexToken }
+ -- () { indentation lexToken dedent BOF }
+}
+
+<0> $ident_letter($ident_letter|$digit)* { \loc len str -> keywordOrIdent (take len str) loc }
+
+
<0> {
";" { symbolToken SemiColonToken}
"," { symbolToken CommaToken}
@@ -92,7 +111,13 @@ tokens :-
}
+
{
+
+-- The lexer starts off in the beginning of file state (bof)
+initStartCodeStack :: [Int]
+initStartCodeStack = [bof,0]
+
-- Each right-hand side has type :: String -> Token
lexToken :: P Token
@@ -153,6 +178,30 @@ lexCont cont = do
-- ("let",rest) -> TokenLet : lexer rest
-- ("in",rest) -> TokenIn : lexer rest
-- (var,rest) -> TokenVar var : lexer rest
+
+-- ---------------------------------------------------------------------
+
+-- a keyword or an identifier (the syntax overlaps)
+keywordOrIdent :: String -> SrcSpan -> P Token
+keywordOrIdent str location
+ = return $ case Map.lookup str keywords of
+ Just symbol -> symbol location
+ Nothing -> IdentifierToken location str
+
+-- mapping from strings to keywords
+keywords :: Map.Map String (SrcSpan -> Token)
+keywords = Map.fromList keywordNames
+
+keywordNames :: [(String, SrcSpan -> Token)]
+keywordNames =
+ [ ("False", FalseToken), ("class", ClassToken), ("finally", FinallyToken), ("is", IsToken), ("return", ReturnToken)
+ , ("None", NoneToken), ("continue", ContinueToken), ("for", ForToken), ("lambda", LambdaToken), ("try", TryToken)
+ , ("True", TrueToken), ("def", DefToken), ("from", FromToken), ("nonlocal", NonLocalToken), ("while", WhileToken)
+ , ("and", AndToken), ("del", DeleteToken), ("global", GlobalToken), ("not", NotToken), ("with", WithToken)
+ , ("as", AsToken), ("elif", ElifToken), ("if", IfToken), ("or", OrToken), ("yield", YieldToken)
+ , ("assert", AssertToken), ("else", ElseToken), ("import", ImportToken), ("pass", PassToken)
+ , ("break", BreakToken), ("except", ExceptToken), ("in", InToken), ("raise", RaiseToken)
+ ]
}
21 src/Language/JavaScript/Parser/Parser.y → src/Language/JavaScript/Parser/Parser/Parser.y
View
@@ -1,17 +1,19 @@
{
-module Language.JavaScript.Parser.Parser where
+module Language.JavaScript.Parser.Parser.Parser (parse) where
import Control.Monad.Error.Class (throwError)
import Data.Char
-import Language.JavaScript.Parser.Lexer
+import Language.JavaScript.Parser.Parser.Lexer
import Language.JavaScript.Parser.ParserMonad
import Language.JavaScript.Parser.SrcLocation
+import qualified Language.JavaScript.Parser.AST as AST
}
-- The name of the generated function to be exported from the module
-%name parse
+%name parse Id
+
%tokentype { Token }
%error { parseError }
%monad { P } { thenP } { returnP }
@@ -20,6 +22,17 @@ import Language.JavaScript.Parser.SrcLocation
%token
+ ';' { SemiColonToken {} }
+ ',' { CommaToken {} }
+ '?' { HookToken {} }
+ ':' { ColonToken {} }
+ '||' { OrToken {} }
+ '&&' { AndToken {} }
+ '|' { BitwiseOrToken {}}
+
+ 'ident' { IdentifierToken {} }
+
+
let { TokenLet {} }
in { TokenIn {} }
int { TokenInt {} {-$$-} }
@@ -34,6 +47,8 @@ import Language.JavaScript.Parser.SrcLocation
%%
+Id : 'ident' { AST.JSIdentifier (token_literal $1) }
+
Foo : '(' '+' ')' {}
{-
Please sign in to comment.
Something went wrong with that request. Please try again.