language-javascript 0.4.8 → 0.4.9
raw patch · 6 files changed
+119/−115 lines, 6 files
Files
- README +3/−0
- dist/build/Language/JavaScript/Parser/Grammar5.hs +1/−1
- language-javascript.cabal +2/−2
- src/Language/JavaScript/Parser.hs +14/−14
- src/Language/JavaScript/Parser/AST.hs +56/−55
- src/Language/JavaScript/Parser/SrcLocation.hs +43/−43
README view
@@ -35,6 +35,9 @@ Changes +0.4.9 Make alex and happy versions more explicit. Expose the AlexSpan+ data type, so parse error positions can be reported.+ 0.4.8 Close issue https://github.com/alanz/language-javascript/issues/5 by correcting the lexical analyser for DecimalLiteral
dist/build/Language/JavaScript/Parser/Grammar5.hs view
@@ -16,7 +16,7 @@ import qualified Data.Array as Happy_Data_Array import qualified GHC.Exts as Happy_GHC_Exts --- parser produced by Happy Version 1.18.8+-- parser produced by Happy Version 1.18.9 newtype HappyAbsSyn = HappyAbsSyn HappyAny #if __GLASGOW_HASKELL__ >= 607
language-javascript.cabal view
@@ -1,5 +1,5 @@ Name: language-javascript-Version: 0.4.8+Version: 0.4.9 Synopsis: Parser for JavaScript Description: Parses Javascript into an Abstract Syntax Tree (AST). Initially intended as frontend to hjsmin. Homepage: https://github.com/alanz/language-javascript@@ -30,7 +30,7 @@ , containers >= 0.2 && < 0.5 , utf8-light >= 0.4 && < 1.0 build-tools: alex >= 3.0- , happy+ , happy >= 1.18.5 hs-source-dirs: src Exposed-modules: Language.JavaScript.Parser Language.JavaScript.Parser.Parser
src/Language/JavaScript/Parser.hs view
@@ -1,19 +1,20 @@-module Language.JavaScript.Parser +module Language.JavaScript.Parser ( PA.parse- , PA.readJs - , PA.parseFile - , PA.showStripped - , PA.showStrippedMaybe - , JSNode(..) - , SrcSpan(..) - , Node(..) - , ParseError(..) - -- Source locations + , PA.readJs+ , PA.parseFile+ , PA.showStripped+ , PA.showStrippedMaybe+ , JSNode(..)+ , SrcSpan(..)+ , AlexSpan(..)+ , Node(..)+ , ParseError(..)+ -- Source locations , AlexPosn(..)- -- ParserMonad - , P - , ParseState (..) + -- ParserMonad+ , P+ , ParseState (..) ) where @@ -26,4 +27,3 @@ -- EOF -
src/Language/JavaScript/Parser/AST.hs view
@@ -1,74 +1,75 @@ {-# LANGUAGE DeriveDataTypeable #-}-module Language.JavaScript.Parser.AST +module Language.JavaScript.Parser.AST ( Node (..) , JSNode(..) , SrcSpan (..)- , showStripped + , AlexPosn(..)+ , showStripped ) where import Data.Data import Data.List-import Language.JavaScript.Parser.SrcLocation (SrcSpan(..))+import Language.JavaScript.Parser.SrcLocation (SrcSpan(..), AlexPosn(..)) -- --------------------------------------------------------------------- data JSNode = NS Node SrcSpan deriving (Show, Eq, Read, Data, Typeable) -data Node = JSArguments [[JSNode]] - | JSArrayLiteral [JSNode] - | JSBlock JSNode - | JSBreak [JSNode] [JSNode] - | JSCallExpression String [JSNode] -- type : ., (), []; rest - | JSCase JSNode JSNode - | JSCatch JSNode [JSNode] JSNode - | JSContinue [JSNode] - | JSDecimal String -- Was Integer - | JSDefault JSNode - | JSDoWhile JSNode JSNode JSNode - | JSElision [JSNode] - | JSEmpty JSNode - | JSExpression [JSNode] - | JSExpressionBinary String [JSNode] [JSNode] - | JSExpressionParen JSNode - | JSExpressionPostfix String [JSNode] - | JSExpressionTernary [JSNode] [JSNode] [JSNode] - | JSFinally JSNode - | JSFor [JSNode] [JSNode] [JSNode] JSNode - | JSForIn [JSNode] JSNode JSNode - | JSForVar [JSNode] [JSNode] [JSNode] JSNode - | JSForVarIn JSNode JSNode JSNode +data Node = JSArguments [[JSNode]]+ | JSArrayLiteral [JSNode]+ | JSBlock JSNode+ | JSBreak [JSNode] [JSNode]+ | JSCallExpression String [JSNode] -- type : ., (), []; rest+ | JSCase JSNode JSNode+ | JSCatch JSNode [JSNode] JSNode+ | JSContinue [JSNode]+ | JSDecimal String -- Was Integer+ | JSDefault JSNode+ | JSDoWhile JSNode JSNode JSNode+ | JSElision [JSNode]+ | JSEmpty JSNode+ | JSExpression [JSNode]+ | JSExpressionBinary String [JSNode] [JSNode]+ | JSExpressionParen JSNode+ | JSExpressionPostfix String [JSNode]+ | JSExpressionTernary [JSNode] [JSNode] [JSNode]+ | JSFinally JSNode+ | JSFor [JSNode] [JSNode] [JSNode] JSNode+ | JSForIn [JSNode] JSNode JSNode+ | JSForVar [JSNode] [JSNode] [JSNode] JSNode+ | JSForVarIn JSNode JSNode JSNode | JSFunction JSNode [JSNode] JSNode -- name, parameter list, body- | JSFunctionBody [JSNode] - | JSFunctionExpression [JSNode] [JSNode] JSNode -- name, parameter list, body - | JSHexInteger String -- Was Integer - | JSIdentifier String - | JSIf JSNode JSNode - | JSIfElse JSNode JSNode JSNode - | JSLabelled JSNode JSNode - | JSLiteral String - | JSMemberDot [JSNode] JSNode - | JSMemberSquare [JSNode] JSNode - | JSObjectLiteral [JSNode] - | JSOperator String - | JSPropertyNameandValue JSNode [JSNode] - | JSPropertyAccessor String JSNode [JSNode] JSNode - | JSRegEx String - | JSReturn [JSNode] - | JSSourceElements [JSNode] - | JSSourceElementsTop [JSNode] - | JSStatementBlock JSNode - | JSStatementList [JSNode] - | JSStringLiteral Char [Char] - | JSSwitch JSNode [JSNode] - | JSThrow JSNode - | JSTry JSNode [JSNode] - | JSUnary String - | JSVarDecl JSNode [JSNode] - | JSVariables String [JSNode] - | JSWhile JSNode JSNode - | JSWith JSNode [JSNode] + | JSFunctionBody [JSNode]+ | JSFunctionExpression [JSNode] [JSNode] JSNode -- name, parameter list, body+ | JSHexInteger String -- Was Integer+ | JSIdentifier String+ | JSIf JSNode JSNode+ | JSIfElse JSNode JSNode JSNode+ | JSLabelled JSNode JSNode+ | JSLiteral String+ | JSMemberDot [JSNode] JSNode+ | JSMemberSquare [JSNode] JSNode+ | JSObjectLiteral [JSNode]+ | JSOperator String+ | JSPropertyNameandValue JSNode [JSNode]+ | JSPropertyAccessor String JSNode [JSNode] JSNode+ | JSRegEx String+ | JSReturn [JSNode]+ | JSSourceElements [JSNode]+ | JSSourceElementsTop [JSNode]+ | JSStatementBlock JSNode+ | JSStatementList [JSNode]+ | JSStringLiteral Char [Char]+ | JSSwitch JSNode [JSNode]+ | JSThrow JSNode+ | JSTry JSNode [JSNode]+ | JSUnary String+ | JSVarDecl JSNode [JSNode]+ | JSVariables String [JSNode]+ | JSWhile JSNode JSNode+ | JSWith JSNode [JSNode] deriving (Show, Eq, Read, Data, Typeable) -- Strip out the location info, leaving the original JSNode text representation
src/Language/JavaScript/Parser/SrcLocation.hs view
@@ -1,8 +1,8 @@ {-# LANGUAGE CPP, DeriveDataTypeable #-} ----------------------------------------------------------------------------- -- |--- Module : Language.Python.Common.SrcLocation --- Copyright : (c) 2009 Bernie Pope +-- Module : Language.Python.Common.SrcLocation+-- Copyright : (c) 2009 Bernie Pope -- License : BSD-style -- Maintainer : bjpop@csse.unimelb.edu.au -- Stability : experimental@@ -13,9 +13,9 @@ ----------------------------------------------------------------------------- module Language.JavaScript.Parser.SrcLocation (- -- * Construction + -- * Construction AlexPosn (..),- AlexSpan,+ AlexSpan (..), alexStartPos, alexSpanEmpty, SrcLocation (..),@@ -28,7 +28,7 @@ initialSrcLocation, spanStartPoint, -- * Modification- incColumn, + incColumn, decColumn, incLine, incTab,@@ -64,13 +64,13 @@ -- | A location for a syntactic entity from the source code. -- The location is specified by its filename, and starting row--- and column. -data SrcLocation = +-- and column.+data SrcLocation = Sloc { sloc_filename :: !String , sloc_address :: {-# UNPACK #-} !Int -- address (number of characters preceding the token) , sloc_row :: {-# UNPACK #-} !Int- , sloc_column :: {-# UNPACK #-} !Int - } + , sloc_column :: {-# UNPACK #-} !Int+ } | NoLocation deriving (Eq,Ord,Show,Typeable,Data) {-@@ -89,7 +89,7 @@ instance Span a => Span [a] where getSpan [] = SpanEmpty- getSpan [x] = getSpan x + getSpan [x] = getSpan x getSpan list@(x:_xs) = combineSrcSpans (getSpan x) (getSpan (last list)) instance Span a => Span (Maybe a) where@@ -107,7 +107,7 @@ getSpan = id -- ++AZ++ adding this-instance Span AlexPosn where +instance Span AlexPosn where getSpan ap = toSrcSpan (ap,'\n',"") toSrcSpan :: AlexSpan -> SrcSpan@@ -116,10 +116,10 @@ -- | Construct the initial source location for a file. initialSrcLocation :: String -> SrcLocation-initialSrcLocation filename - = Sloc +initialSrcLocation filename+ = Sloc { sloc_filename = filename- , sloc_address = 1 + , sloc_address = 1 , sloc_row = 1 , sloc_column = 1 }@@ -128,32 +128,32 @@ decColumn :: Int -> SrcLocation -> SrcLocation decColumn n loc | n < col = loc { sloc_column = col - n }- | otherwise = loc + | otherwise = loc where col = sloc_column loc --- | Increment the column of a location. +-- | Increment the column of a location. incColumn :: Int -> SrcLocation -> SrcLocation incColumn n loc@(Sloc { sloc_column = col }) = loc { sloc_column = col + n }-incColumn _ NoLocation = NoLocation +incColumn _ NoLocation = NoLocation -- | Increment the column of a location by one tab stop. incTab :: SrcLocation -> SrcLocation incTab loc@(Sloc { sloc_column = col })- = loc { sloc_column = newCol } + = loc { sloc_column = newCol } where newCol = col + 8 - (col - 1) `mod` 8 incTab NoLocation = NoLocation -- | Increment the line number (row) of a location by one. incLine :: Int -> SrcLocation -> SrcLocation-incLine n loc@(Sloc { sloc_row = row }) +incLine n loc@(Sloc { sloc_row = row }) = loc { sloc_column = 1, sloc_row = row + n } incLine _ NoLocation = NoLocation {--Inspired heavily by compiler/basicTypes/SrcLoc.lhs -A SrcSpan delimits a portion of a text file. +Inspired heavily by compiler/basicTypes/SrcLoc.lhs+A SrcSpan delimits a portion of a text file. -} -- | Source location spanning a contiguous section of a file.@@ -180,26 +180,26 @@ , span_column :: {-# UNPACK #-} !Int } -- | No span information.- | SpanEmpty + | SpanEmpty deriving (Eq,Ord,Show,Read,Typeable,Data) {- instance Show SrcSpan where- show (s@(SpanCoLinear filename row start end)) = + show (s@(SpanCoLinear filename row start end)) = -- showChar '('.showString filename.shows row.shows start.shows end.showChar ')' -- ("foo.txt" 12 4 5) showChar '(' . showChar ')' -- ("foo.txt" 12 4 5) - show (s@(SpanMultiLine filename sr sc er ec)) = + show (s@(SpanMultiLine filename sr sc er ec)) = showChar '('.showString filename.shows sr.shows sc.shows er.shows ec.showChar ')' -- ("foo.txt" 12 4 13 5)- show (s@(SpanPoint filename r c)) = + show (s@(SpanPoint filename r c)) = showChar '('.showString filename.shows r.shows c.showChar ')' -- ("foo.txt" 12 4) show (SpanEmpty) = showString "()" -- () -}- + --instance Read SrcSpan where -- readsPrec _ str = [- -{- ++{- instance Read a => Read Tree a where readsPrec _ str = [(Leave x, t’) | ("Leave", t) <- reads str, (x, t’) <- reads t] ++@@ -209,21 +209,21 @@ (r, t’’) <- reads t’, (d, t’’’) <- reads t’’] -}- + instance Span SrcLocation where getSpan loc@(Sloc {})- = SpanPoint + = SpanPoint { span_filename = sloc_filename loc , span_row = sloc_row loc , span_column = sloc_column loc }- getSpan NoLocation = SpanEmpty + getSpan NoLocation = SpanEmpty -- | Make a point span from the start of a span spanStartPoint :: SrcSpan -> SrcSpan spanStartPoint SpanEmpty = SpanEmpty-spanStartPoint span = - SpanPoint +spanStartPoint span =+ SpanPoint { span_filename = span_filename span , span_row = startRow span , span_column = startCol span@@ -233,13 +233,13 @@ -- arguments are the same, or the left one preceeds the right one. mkSrcSpan :: SrcLocation -> SrcLocation -> SrcSpan mkSrcSpan NoLocation _ = SpanEmpty-mkSrcSpan _ NoLocation = SpanEmpty +mkSrcSpan _ NoLocation = SpanEmpty mkSrcSpan loc1 loc2- | line1 == line2 = - if col2 <= col1 + | line1 == line2 =+ if col2 <= col1 then SpanPoint file line1 col1 else SpanCoLinear file line1 col1 col2- | otherwise = + | otherwise = SpanMultiLine file line1 col1 line2 col2 where line1 = sloc_row loc1@@ -284,14 +284,14 @@ -- | Get the column of the start of a span. startCol :: SrcSpan -> Int-startCol (SpanCoLinear { span_start_column = col }) = col -startCol (SpanMultiLine { span_start_column = col }) = col -startCol (SpanPoint { span_column = col }) = col +startCol (SpanCoLinear { span_start_column = col }) = col+startCol (SpanMultiLine { span_start_column = col }) = col+startCol (SpanPoint { span_column = col }) = col startCol SpanEmpty = error "startCol called on empty span" -- | Get the column of the end of a span. endCol :: SrcSpan -> Int-endCol (SpanCoLinear { span_end_column = col }) = col -endCol (SpanMultiLine { span_end_column = col }) = col -endCol (SpanPoint { span_column = col }) = col +endCol (SpanCoLinear { span_end_column = col }) = col+endCol (SpanMultiLine { span_end_column = col }) = col+endCol (SpanPoint { span_column = col }) = col endCol SpanEmpty = error "endCol called on empty span"