-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP. Aiming at getting parsing infrastructure in place, based on lang…
…uage-python.
- Loading branch information
Showing
9 changed files
with
586 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/dist/build/Language/JavaScript/Parser/ParserMonad.hi | ||
/dist/build/Language/JavaScript/Parser/ParserMonad.o | ||
/dist/build/autogen/Paths_language_javascript.hs | ||
/dist/build/autogen/cabal_macros.h | ||
/dist/package.conf.inplace | ||
/dist/setup-config | ||
/language-javascript.cabal~ | ||
/src/Language/JavaScript/Parser/Lexer.x~ | ||
/src/Language/JavaScript/Parser/Parser.y~ | ||
/src/Language/JavaScript/Parser/ParserMonad.hs~ | ||
/dist/build/Language/JavaScript/Parser/Lexer.hs | ||
/dist/build/Language/JavaScript/Parser/Parser.hs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Copyright (c)2010, Alan Zimmerman | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
|
||
* Neither the name of Alan Zimmerman nor the names of other | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Parser for JavaScript | ||
|
||
Based (loosely) on language-python | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
cabal clean && cabal configure && cabal build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Name: language-javascript | ||
Version: 0.0.1 | ||
Synopsis: Parser for JavaScript | ||
-- Description: | ||
Homepage: https://github.com/alanz/language-javascript | ||
License: BSD3 | ||
License-file: LICENSE | ||
Author: Alan Zimmerman | ||
Maintainer: alan.zimm@gmail.com | ||
Copyright: (c) 2010 Alan Zimmerman | ||
Category: Language | ||
Build-type: Simple | ||
-- Extra-source-files: | ||
Cabal-version: >=1.2 | ||
|
||
Library | ||
Build-depends: base >= 4 && < 5 | ||
, array >= 0.3 && < 0.5 | ||
, mtl >= 1.1 && < 2 | ||
hs-source-dirs: src | ||
Exposed-modules: Language.JavaScript.Parser.Parser | ||
Language.JavaScript.Parser.Lexer | ||
-- Other-modules: | ||
Build-tools: happy, alex | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{ | ||
|
||
module Language.JavaScript.Parser.Lexer (Token(..), alexScanTokens) where | ||
|
||
import Language.JavaScript.Parser.ParserMonad | ||
|
||
} | ||
|
||
%wrapper "basic" | ||
|
||
$digit = 0-9 -- digits | ||
$alpha = [a-zA-Z] -- alphabetic characters | ||
|
||
tokens :- | ||
|
||
$white+ { \s -> White } | ||
"--".* { \s -> Comment } | ||
let { \s -> Let } | ||
in { \s -> In } | ||
$digit+ { \s -> Int (read s) } | ||
[\=\+\-\*\/\(\)] { \s -> Sym (head s) } | ||
$alpha [$alpha $digit \_ \']* { \s -> Var s } | ||
|
||
{ | ||
-- Each right-hand side has type :: String -> Token | ||
|
||
lexToken :: P Token | ||
lexToken = do | ||
location <- getLocation | ||
input <- getInput | ||
startCode <- getStartCode | ||
case alexScan (location, input) startCode of | ||
AlexEOF -> return endOfFileToken | ||
AlexError _ -> lexicalError | ||
AlexSkip (nextLocation, rest) len -> do | ||
setLocation nextLocation | ||
setInput rest | ||
lexToken | ||
AlexToken (nextLocation, rest) len action -> do | ||
setLocation nextLocation | ||
setInput rest | ||
token <- action (mkSrcSpan location $ decColumn 1 nextLocation) len input | ||
setLastToken token | ||
return token | ||
|
||
-- This is called by the Happy parser. | ||
lexCont :: (Token -> P a) -> P a | ||
lexCont cont = do | ||
lexLoop | ||
where | ||
-- lexLoop :: P a | ||
lexLoop = do | ||
tok <- lexToken | ||
case tok of | ||
{- | ||
CommentToken {} -> do | ||
addComment tok | ||
lexLoop | ||
LineJoinToken {} -> lexLoop | ||
-} | ||
_other -> cont tok | ||
|
||
-- lexer :: String -> [Token] | ||
-- lexer [] = [] | ||
-- lexer (c:cs) | ||
-- | isSpace c = lexer cs | ||
-- | isAlpha c = lexVar (c:cs) | ||
-- | isDigit c = lexNum (c:cs) | ||
-- lexer ('=':cs) = TokenEq : lexer cs | ||
-- lexer ('+':cs) = TokenPlus : lexer cs | ||
-- lexer ('-':cs) = TokenMinus : lexer cs | ||
-- lexer ('*':cs) = TokenTimes : lexer cs | ||
-- lexer ('/':cs) = TokenDiv : lexer cs | ||
-- lexer ('(':cs) = TokenOB : lexer cs | ||
-- lexer (')':cs) = TokenCB : lexer cs | ||
|
||
-- lexNum cs = TokenInt (read num) : lexer rest | ||
-- where (num,rest) = span isDigit cs | ||
|
||
-- lexVar cs = | ||
-- case span isAlpha cs of | ||
-- ("let",rest) -> TokenLet : lexer rest | ||
-- ("in",rest) -> TokenIn : lexer rest | ||
-- (var,rest) -> TokenVar var : lexer rest | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
|
||
{ | ||
module Language.JavaScript.Parser.Parser where | ||
|
||
import Data.Char | ||
import Language.JavaScript.Parser.Lexer | ||
|
||
} | ||
|
||
-- The name of the generated function to be exported from the module | ||
%name parse | ||
%tokentype { Token } | ||
%error { parseError } | ||
%monad { P } { thenP } { returnP } | ||
%lexer { lexCont } { EOFToken {} } | ||
|
||
|
||
%token | ||
let { TokenLet } | ||
in { TokenIn } | ||
int { TokenInt $$ } | ||
var { TokenVar $$ } | ||
'=' { TokenEq } | ||
'+' { TokenPlus } | ||
'-' { TokenMinus } | ||
'*' { TokenTimes } | ||
'/' { TokenDiv } | ||
'(' { TokenOB } | ||
')' { TokenCB } | ||
|
||
%% | ||
|
||
Exp : let var '=' Exp in Exp { Let $2 $4 $6 } | ||
| Exp1 { Exp1 $1 } | ||
|
||
Exp1 : Exp1 '+' Term { Plus $1 $3 } | ||
| Exp1 '-' Term { Minus $1 $3 } | ||
| Term { Term $1 } | ||
|
||
Term : Term '*' Factor { Times $1 $3 } | ||
| Term '/' Factor { Div $1 $3 } | ||
| Factor { Factor $1 } | ||
|
||
Factor | ||
: int { Int $1 } | ||
| var { Var $1 } | ||
| '(' Exp ')' { Brack $2 } | ||
|
||
|
||
{ | ||
|
||
parseError :: [Token] -> a | ||
parseError _ = error "Parse error" | ||
|
||
data Exp | ||
= Let String Exp Exp | ||
| Exp1 Exp1 | ||
deriving Show | ||
|
||
data Exp1 | ||
= Plus Exp1 Term | ||
| Minus Exp1 Term | ||
| Term Term | ||
deriving Show | ||
|
||
data Term | ||
= Times Term Factor | ||
| Div Term Factor | ||
| Factor Factor | ||
deriving Show | ||
|
||
data Factor | ||
= Int Int | ||
| Var String | ||
| Brack Exp | ||
deriving Show | ||
|
||
|
||
|
||
data Token | ||
= TokenLet | ||
| TokenIn | ||
| TokenInt Int | ||
| TokenVar String | ||
| TokenEq | ||
| TokenPlus | ||
| TokenMinus | ||
| TokenTimes | ||
| TokenDiv | ||
| TokenOB | ||
| TokenCB | ||
deriving Show | ||
|
||
lexer :: String -> [Token] | ||
lexer [] = [] | ||
lexer (c:cs) | ||
| isSpace c = lexer cs | ||
| isAlpha c = lexVar (c:cs) | ||
| isDigit c = lexNum (c:cs) | ||
lexer ('=':cs) = TokenEq : lexer cs | ||
lexer ('+':cs) = TokenPlus : lexer cs | ||
lexer ('-':cs) = TokenMinus : lexer cs | ||
lexer ('*':cs) = TokenTimes : lexer cs | ||
lexer ('/':cs) = TokenDiv : lexer cs | ||
lexer ('(':cs) = TokenOB : lexer cs | ||
lexer (')':cs) = TokenCB : lexer cs | ||
|
||
lexNum cs = TokenInt (read num) : lexer rest | ||
where (num,rest) = span isDigit cs | ||
|
||
lexVar cs = | ||
case span isAlpha cs of | ||
("let",rest) -> TokenLet : lexer rest | ||
("in",rest) -> TokenIn : lexer rest | ||
(var,rest) -> TokenVar var : lexer rest | ||
|
||
|
||
|
||
main = getContents >>= print . parse . lexer | ||
|
||
} | ||
|
||
|
Oops, something went wrong.