Skip to content

[WIP] Small cleanup in ResolveSourceFragments.hs #2

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions ParseJS.hs → source/ParseJS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ listToJSASTExpression [(NS (JSUnary operator) srcSpan), (NS (JSDecimal x) _)]
else
EWSS (Value xInt) srcSpan
where
xFloat :: Value
xFloat = JSFloat (-1 * (read x))
xInt :: Value
xInt = JSInt (-1 * (read x))
listToJSASTExpression ((NS (JSUnary operator) srcSpan):x)
| elem operator ["-", "+", "--", "++", "!", "typeof ", "delete ", "~"] =
Expand Down
12 changes: 6 additions & 6 deletions PrettyPrint.hs → source/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
--
-- Top level functions are:
--
-- mapPrintASTWS
-- (jsastListWSMakeSourceFragments
-- (getJSASTWithSource (parseTree program file) file)
-- span)
-- padding
-- printSrc
-- mapPrintASTWS
-- (jsastListWSMakeSourceFragments
-- (getJSASTWithSource (parseTree program file) file)
-- span)
-- padding
-- printSrc
--
-- mapPrintASTChild
-- (label
Expand Down
140 changes: 90 additions & 50 deletions ResolveSourceFragments.hs → source/ResolveSourceFragments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
--
-- Top level function is
-- (jsastListWSMakeSourceFragments (getJSASTWithSource (parseTree program file) file) span)
--


module ResolveSourceFragments
( ExprWithSourceFragment(..)
Expand All @@ -38,7 +40,6 @@ module ResolveSourceFragments
, SourceFragment(..)
, ValueWithSourceFragment(..)
, jsastListWSMakeSourceFragments
, jsastMakeSourceFragment
) where


Expand All @@ -51,23 +52,21 @@ type Row = Int
type Col = Int

-- (FileName, StartRow, StartCol, EndRow, EndCol)
type SourceFragment = (String, Row, Col, Row, Col)
type SourceFragment = (SourceFileName, Row, Col, Row, Col)


-- Represent literal values.
data ValueWithSourceFragment =
WSArray [ExprWithSourceFragment]
| WSBool Bool
-- Double quote strings are never treated differently to normal strings.
-- TODO: Should be merged with JSString
| WSDQString String
| WSFloat Double
| WSInt Int
| WSNull
-- TODO: Comment on what the expressions can be.
| WSObject [ExprWithSourceFragment]
| WSString String
| WSUndefined deriving (Show)


data ExprWSF =
WSArguments [ExprWithSourceFragment]
| WSAssignment Operator ExprWithSourceFragment ExprWithSourceFragment
Expand All @@ -91,16 +90,25 @@ data ExprWSF =
| WSValue ValueWithSourceFragment
| WSVarDeclaration Variable (Maybe ExprWithSourceFragment) deriving (Show)


data JSASTWSF =
WSBlock [JSASTWithSourceFragment]
| WSCase ExprWithSourceFragment JSASTWithSourceFragment
| WSCatch Variable (Maybe ExprWithSourceFragment) JSASTWithSourceFragment
| WSDefault JSASTWithSourceFragment
| WSDoWhile JSASTWithSourceFragment ExprWithSourceFragment
| WSFinally JSASTWithSourceFragment
| WSFor (Maybe ExprWithSourceFragment) (Maybe ExprWithSourceFragment) (Maybe ExprWithSourceFragment) JSASTWithSourceFragment
| WSFor
(Maybe ExprWithSourceFragment)
(Maybe ExprWithSourceFragment)
(Maybe ExprWithSourceFragment)
JSASTWithSourceFragment
| WSForIn [Variable] ExprWithSourceFragment JSASTWithSourceFragment
| WSForVar [ExprWithSourceFragment] (Maybe ExprWithSourceFragment) (Maybe ExprWithSourceFragment) JSASTWithSourceFragment
| WSForVar
[ExprWithSourceFragment]
(Maybe ExprWithSourceFragment)
(Maybe ExprWithSourceFragment)
JSASTWithSourceFragment
| WSForVarIn ExprWithSourceFragment ExprWithSourceFragment JSASTWithSourceFragment
| WSFunctionBody [JSASTWithSourceFragment]
| WSFunctionDeclaration Variable [Variable] JSASTWithSourceFragment
Expand All @@ -122,36 +130,42 @@ data ExprWithSourceFragment =

-- nextSpan is the list's parent's next sibling (or the end of the file, if the parent has no next
-- sibling)
jsastListWSMakeSourceFragments :: ([JSASTWithSourceSpan], SourceFileName) -> SrcSpan -> [JSASTWithSourceFragment]
jsastListWSMakeSourceFragments :: ([JSASTWithSourceSpan], SourceFileName) -> SrcSpan ->
[JSASTWithSourceFragment]
jsastListWSMakeSourceFragments (list, fileName) nextSpan =
jsastListMakeSourceFragments list fileName nextSpan


-- nextSpan is the list's parent's next sibling (or the end of the file, if the parent has no next
-- sibling)
jsastListMakeSourceFragments :: [JSASTWithSourceSpan] -> SourceFileName -> SrcSpan -> [JSASTWithSourceFragment]
jsastListMakeSourceFragments :: [JSASTWithSourceSpan] -> SourceFileName -> SrcSpan ->
[JSASTWithSourceFragment]
jsastListMakeSourceFragments (x:y:z) fileName nextSpan =
(jsastMakeSourceFragment x fileName (jsastGetSpan y)):(jsastListMakeSourceFragments (y:z) fileName nextSpan)
jsastListMakeSourceFragments (x:[]) fileName nextSpan = [jsastMakeSourceFragment x fileName nextSpan]
(jsastMakeSourceFragment x fileName (jsastGetSource y)):
(jsastListMakeSourceFragments (y:z) fileName nextSpan)
jsastListMakeSourceFragments (x:[]) fileName nextSpan =
[jsastMakeSourceFragment x fileName nextSpan]
jsastListMakeSourceFragments [] _ nextSpan = []


jsastGetSpan :: JSASTWithSourceSpan -> SrcSpan
jsastGetSpan (AWSS _ srcSpan) = srcSpan
jsastGetSource :: JSASTWithSourceSpan -> SrcSpan
jsastGetSource (AWSS _ srcSpan) = srcSpan


exprGetSpan :: ExprWithSourceSpan -> SrcSpan
exprGetSpan (EWSS _ srcSpan) = srcSpan
exprGetSource :: ExprWithSourceSpan -> SrcSpan
exprGetSource (EWSS _ srcSpan) = srcSpan


maybeExprGetSpan :: Maybe ExprWithSourceSpan -> Maybe SrcSpan
maybeExprGetSpan (Just (EWSS _ srcSpan)) = Just srcSpan
maybeExprGetSpan Nothing = Nothing
maybeExprGetSource :: Maybe ExprWithSourceSpan -> Maybe SrcSpan
maybeExprGetSource (Just (EWSS _ srcSpan)) = Just srcSpan
maybeExprGetSource Nothing = Nothing


exprListMakeSourceFragments :: [ExprWithSourceSpan] -> SourceFileName -> SrcSpan -> [ExprWithSourceFragment]
exprListMakeSourceFragments :: [ExprWithSourceSpan] -> SourceFileName -> SrcSpan ->
[ExprWithSourceFragment]
exprListMakeSourceFragments (x:y:z) fileName nextSpan =
(exprMakeSourceFragment x fileName (exprGetSpan y)):(exprListMakeSourceFragments (y:z) fileName nextSpan)
(exprMakeSourceFragment x fileName (exprGetSource y)):
(exprListMakeSourceFragments (y:z) fileName nextSpan)
exprListMakeSourceFragments (x:[]) fileName nextSpan = [exprMakeSourceFragment x fileName nextSpan]
exprListMakeSourceFragments [] _ _ = []

Expand All @@ -161,21 +175,19 @@ makeSourceFragment (SpanPoint _ startRow startCol) (SpanPoint _ nextRow nextCol)
(fileName, startRow, startCol, nextRow, nextCol)


-- Here nextSpan is just the end of this fragment
-- Here nextSpan is just the end of this fragment.
-- Still to do
-- WSCase ExprWithSourceFragment JSASTWithSourceFragment
-- WSCatch Variable (Maybe ExprWithSourceFragment) JSASTWithSourceFragment
-- WSDefault JSASTWithSourceFragment
-- WSDoWhile JSASTWithSourceFragment ExprWithSourceFragment
-- WSFinally JSASTWithSourceFragment
-- WSForIn [Variable] ExprWithSourceFragment JSASTWithSourceFragment
-- WSIf ExprWithSourceFragment JSASTWithSourceFragment
-- WSIfElse ExprWithSourceFragment JSASTWithSourceFragment JSASTWithSourceFragment
-- WSLabelled Variable JSASTWithSourceFragment
-- WSSwitch ExprWithSourceFragment JSASTWithSourceFragment
-- WSTry JSASTWithSourceFragment JSASTWithSourceFragment
-- WSWhile ExprWithSourceFragment JSASTWithSourceFragment
jsastMakeSourceFragment :: JSASTWithSourceSpan -> SourceFileName -> SrcSpan -> JSASTWithSourceFragment
jsastMakeSourceFragment :: JSASTWithSourceSpan -> SourceFileName -> SrcSpan ->
JSASTWithSourceFragment
-- jsastMakeSourceFragment (AWSS () srcSpan) fileName nextSpan =
-- AWSF
-- (WS...)
Expand All @@ -190,54 +202,71 @@ jsastMakeSourceFragment (AWSS (For vars cond expr body) srcSpan) fileName nextSp
(WSFor
(maybeExprMakeSourceFragment vars fileName varsNextSpan)
(maybeExprMakeSourceFragment cond fileName condNextSpan)
(maybeExprMakeSourceFragment expr fileName (jsastGetSpan body))
(maybeExprMakeSourceFragment expr fileName (jsastGetSource body))
(jsastMakeSourceFragment body fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
where
justSpanGetSpan (Just ss) = ss
condSrcSpan = maybeExprGetSpan cond
exprSrcSpan = maybeExprGetSpan expr
condSrcSpan :: Maybe SrcSpan
condSrcSpan = maybeExprGetSource cond
exprSrcSpan :: Maybe SrcSpan
exprSrcSpan = maybeExprGetSource expr
varsNextSpan :: SrcSpan
varsNextSpan =
if (not (condSrcSpan == Nothing)) then
justSpanGetSpan condSrcSpan
else if (not (exprSrcSpan == Nothing)) then
justSpanGetSpan exprSrcSpan
else
jsastGetSpan body
jsastGetSource body
condNextSpan :: SrcSpan
condNextSpan =
if (not (exprSrcSpan == Nothing)) then
justSpanGetSpan exprSrcSpan
else
jsastGetSpan body
jsastGetSource body
justSpanGetSpan :: Maybe SrcSpan -> SrcSpan
justSpanGetSpan (Just ss) = ss
jsastMakeSourceFragment (AWSS (ForIn vars obj body) srcSpan) fileName nextSpan =
AWSF
(WSForIn
vars
(exprMakeSourceFragment obj fileName (jsastGetSource body))
(jsastMakeSourceFragment body fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
jsastMakeSourceFragment (AWSS (ForVar vars cond expr body) srcSpan) fileName nextSpan =
AWSF
(WSForVar
(exprListMakeSourceFragments vars fileName varsNextSpan)
(maybeExprMakeSourceFragment cond fileName condNextSpan)
(maybeExprMakeSourceFragment expr fileName (jsastGetSpan body))
(maybeExprMakeSourceFragment expr fileName (jsastGetSource body))
(jsastMakeSourceFragment body fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
where
justSpanGetSpan (Just ss) = ss
condSrcSpan = maybeExprGetSpan cond
exprSrcSpan = maybeExprGetSpan expr
condSrcSpan :: Maybe SrcSpan
condSrcSpan = maybeExprGetSource cond
exprSrcSpan :: Maybe SrcSpan
exprSrcSpan = maybeExprGetSource expr
varsNextSpan :: SrcSpan
varsNextSpan =
if (not (condSrcSpan == Nothing)) then
justSpanGetSpan condSrcSpan
else if (not (exprSrcSpan == Nothing)) then
justSpanGetSpan exprSrcSpan
else
jsastGetSpan body
jsastGetSource body
condNextSpan :: SrcSpan
condNextSpan =
if (not (exprSrcSpan == Nothing)) then
justSpanGetSpan exprSrcSpan
else
jsastGetSpan body
jsastGetSource body
justSpanGetSpan :: Maybe SrcSpan -> SrcSpan
justSpanGetSpan (Just ss) = ss
jsastMakeSourceFragment (AWSS (ForVarIn var obj body) srcSpan) fileName nextSpan =
AWSF
(WSForVarIn
(exprMakeSourceFragment var fileName (exprGetSpan obj))
(exprMakeSourceFragment obj fileName (jsastGetSpan body))
(exprMakeSourceFragment var fileName (exprGetSource obj))
(exprMakeSourceFragment obj fileName (jsastGetSource body))
(jsastMakeSourceFragment body fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
jsastMakeSourceFragment (AWSS (FunctionBody list) srcSpan) fileName nextSpan =
Expand All @@ -250,9 +279,21 @@ jsastMakeSourceFragment (AWSS (FunctionDeclaration var args body) srcSpan) fileN
(WSFunctionDeclaration
var
args
-- The body is the last child of the function declaration,so it has the same end point.
(jsastMakeSourceFragment body fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
jsastMakeSourceFragment (AWSS (If cond body) srcSpan) fileName nextSpan =
AWSF
(WSIf
(exprMakeSourceFragment cond fileName (jsastGetSource body))
(jsastMakeSourceFragment body fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
jsastMakeSourceFragment (AWSS (IfElse cond body elseBody) srcSpan) fileName nextSpan =
AWSF
(WSIfElse
(exprMakeSourceFragment cond fileName (jsastGetSource body))
(jsastMakeSourceFragment body fileName (jsastGetSource elseBody))
(jsastMakeSourceFragment elseBody fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
jsastMakeSourceFragment (AWSS (Return expr) srcSpan) fileName nextSpan =
AWSF
(WSReturn (exprMakeSourceFragment expr fileName nextSpan))
Expand All @@ -267,8 +308,6 @@ valueMakeSourceFragment :: Value -> SourceFileName -> SrcSpan -> ValueWithSource
valueMakeSourceFragment (JSArray list) fileName nextSpan =
WSArray (exprListMakeSourceFragments list fileName nextSpan)
valueMakeSourceFragment (JSBool val) _ _ = WSBool val
-- Double quote strings are never treated differently to normal strings.
-- TODO: Should be merged with JSString
valueMakeSourceFragment (JSDQString val) _ _ = WSDQString val
valueMakeSourceFragment (JSFloat val) _ _ = WSFloat val
valueMakeSourceFragment (JSInt val) _ _ = WSInt val
Expand All @@ -279,7 +318,8 @@ valueMakeSourceFragment (JSString val) _ _ = WSString val
valueMakeSourceFragment JSUndefined _ _ = WSUndefined


maybeExprMakeSourceFragment :: Maybe ExprWithSourceSpan -> SourceFileName -> SrcSpan -> Maybe ExprWithSourceFragment
maybeExprMakeSourceFragment :: Maybe ExprWithSourceSpan -> SourceFileName -> SrcSpan ->
Maybe ExprWithSourceFragment
maybeExprMakeSourceFragment (Just exprWithSourceSpan) fileName srcSpan =
Just (exprMakeSourceFragment exprWithSourceSpan fileName srcSpan)
maybeExprMakeSourceFragment Nothing _ _ = Nothing
Expand All @@ -306,26 +346,26 @@ exprMakeSourceFragment (EWSS (Assignment op expr1 expr2) srcSpan) fileName nextS
EWSF
(WSAssignment
op
(exprMakeSourceFragment expr1 fileName (exprGetSpan expr2))
(exprMakeSourceFragment expr1 fileName (exprGetSource expr2))
(exprMakeSourceFragment expr2 fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
exprMakeSourceFragment (EWSS (Binary op expr1 expr2) srcSpan) fileName nextSpan =
EWSF
(WSBinary
op
(exprMakeSourceFragment expr1 fileName (exprGetSpan expr2))
(exprMakeSourceFragment expr1 fileName (exprGetSource expr2))
(exprMakeSourceFragment expr2 fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
exprMakeSourceFragment (EWSS (Call expr1 expr2) srcSpan) fileName nextSpan =
EWSF
(WSCall
(exprMakeSourceFragment expr1 fileName (exprGetSpan expr2))
(exprMakeSourceFragment expr1 fileName (exprGetSource expr2))
(exprMakeSourceFragment expr2 fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
exprMakeSourceFragment (EWSS (CallExpression expr op callExpr) srcSpan) fileName nextSpan =
EWSF
(WSCallExpression
(exprMakeSourceFragment expr fileName (exprGetSpan callExpr))
(exprMakeSourceFragment expr fileName (exprGetSource callExpr))
op
(exprMakeSourceFragment callExpr fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
Expand All @@ -343,7 +383,7 @@ exprMakeSourceFragment (EWSS (Identifier var) srcSpan) fileName nextSpan =
exprMakeSourceFragment (EWSS (Index expr1 expr2) srcSpan) fileName nextSpan =
EWSF
(WSIndex
(exprMakeSourceFragment expr1 fileName (exprGetSpan expr2))
(exprMakeSourceFragment expr1 fileName (exprGetSource expr2))
(exprMakeSourceFragment expr2 fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
exprMakeSourceFragment (EWSS (List list) srcSpan) fileName nextSpan =
Expand All @@ -360,7 +400,7 @@ exprMakeSourceFragment (EWSS (PropNameValue name expr) srcSpan) fileName nextSpa
exprMakeSourceFragment (EWSS (Reference expr1 expr2) srcSpan) fileName nextSpan =
EWSF
(WSReference
(exprMakeSourceFragment expr1 fileName (exprGetSpan expr2))
(exprMakeSourceFragment expr1 fileName (exprGetSource expr2))
(exprMakeSourceFragment expr2 fileName nextSpan))
(makeSourceFragment srcSpan nextSpan fileName)
exprMakeSourceFragment (EWSS (UnaryPost op expr) srcSpan) fileName nextSpan =
Expand Down
2 changes: 1 addition & 1 deletion TypeRules.hs → source/TypeRules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ exprChildRules (LabReference ex1 ex2, n, sourceFragment) dIDs =
-- bracket notation. There is no way to differentiate between the two using local static analysis.
exprChildRules (LabIndex ex1 ex2, n, sourceFragment) dIDs =
(exprChildRules ex1 dIDs)
-- I don't really want to require the index to be an int literal or a string literal.In the case
-- I don't really want to require the index to be an int literal or a string literal. In the case
-- of arrays, we don't care about the types of particular elements, we justcare that all
-- elements have the same type. Of course this is not the case for objects,since we need to know
-- which property of the object we are modifying so we can check itstype (i.e. the *value* of
Expand Down