Remove usage of withNextId

This commit is contained in:
Ng Zhi An 2018-06-07 21:30:20 -07:00
parent 48ac654a93
commit e496b413bd
1 changed files with 11 additions and 34 deletions

View File

@ -136,31 +136,6 @@ almostSpace =
char c char c
return ' ' return ' '
withNextId :: Monad m => SCParser m (Id -> b) -> SCParser m b
withNextId p = do
start <- getPosition
id <- createId
fn <- p
let t = fn id
end <- getPosition
setPos id start end
return t
where
createId = do
state <- getState
let id = incId (lastId state)
putState $ state {
lastId = id
}
return id
where incId (Id n) = Id $ n+1
setPos id start end = do
state <- getState
let newMap = Map.insert id (start, Just end) (positionMap state)
putState $ state {
positionMap = newMap
}
--------- Message/position annotation on top of user state --------- Message/position annotation on top of user state
data Note = Note Id Severity Code String deriving (Show, Eq) data Note = Note Id Severity Code String deriving (Show, Eq)
data ParseNote = ParseNote SourcePos SourcePos Severity Code String deriving (Show, Eq) data ParseNote = ParseNote SourcePos SourcePos Severity Code String deriving (Show, Eq)
@ -197,7 +172,6 @@ noteToParseNote map (Note id severity code message) =
getLastId = lastId <$> getState getLastId = lastId <$> getState
-- Deprecated by withNextId
getNextIdAt sourcepos = do getNextIdAt sourcepos = do
state <- getState state <- getState
let newId = incId (lastId state) let newId = incId (lastId state)
@ -209,7 +183,6 @@ getNextIdAt sourcepos = do
return newId return newId
where incId (Id n) = Id $ n+1 where incId (Id n) = Id $ n+1
-- Deprecated by withNextId
getNextId :: Monad m => SCParser m Id getNextId :: Monad m => SCParser m Id
getNextId = do getNextId = do
pos <- getPosition pos <- getPosition
@ -1217,7 +1190,8 @@ prop_readDoubleQuoted7 = isOk readSimpleCommand "echo \"${ ls;}bar\""
prop_readDoubleQuoted8 = isWarning readDoubleQuoted "\"\x201Chello\x201D\"" prop_readDoubleQuoted8 = isWarning readDoubleQuoted "\"\x201Chello\x201D\""
prop_readDoubleQuoted9 = isWarning readDoubleQuoted "\"foo\\n\"" prop_readDoubleQuoted9 = isWarning readDoubleQuoted "\"foo\\n\""
prop_readDoubleQuoted10 = isOk readDoubleQuoted "\"foo\\\\n\"" prop_readDoubleQuoted10 = isOk readDoubleQuoted "\"foo\\\\n\""
readDoubleQuoted = called "double quoted string" $ withNextId $ do readDoubleQuoted = called "double quoted string" $ do
id <- getNextId
startPos <- getPosition startPos <- getPosition
doubleQuote doubleQuote
x <- many doubleQuotedPart x <- many doubleQuotedPart
@ -1227,7 +1201,7 @@ readDoubleQuoted = called "double quoted string" $ withNextId $ do
try . lookAhead $ suspectCharAfterQuotes <|> oneOf "$\"" try . lookAhead $ suspectCharAfterQuotes <|> oneOf "$\""
when (any hasLineFeed x && not (startsWithLineFeed x)) $ when (any hasLineFeed x && not (startsWithLineFeed x)) $
suggestForgotClosingQuote startPos endPos "double quoted string" suggestForgotClosingQuote startPos endPos "double quoted string"
return $ \id -> T_DoubleQuoted id x return $ T_DoubleQuoted id x
where where
startsWithLineFeed (T_Literal _ ('\n':_):_) = True startsWithLineFeed (T_Literal _ ('\n':_):_) = True
startsWithLineFeed _ = False startsWithLineFeed _ = False
@ -1571,13 +1545,14 @@ prop_readDollarVariable4 = isWarning (readDollarVariable >> string "[@]") "$arr[
prop_readDollarVariable5 = isWarning (readDollarVariable >> string "[f") "$arr[f" prop_readDollarVariable5 = isWarning (readDollarVariable >> string "[f") "$arr[f"
readDollarVariable :: Monad m => SCParser m Token readDollarVariable :: Monad m => SCParser m Token
readDollarVariable = withNextId $ do readDollarVariable = do
id <- getNextId
pos <- getPosition pos <- getPosition
let singleCharred p = do let singleCharred p = do
n <- p n <- p
value <- wrap [n] value <- wrap [n]
return $ \id -> (T_DollarBraced id value) return $ (T_DollarBraced id value)
let positional = do let positional = do
value <- singleCharred digit value <- singleCharred digit
@ -1590,15 +1565,17 @@ readDollarVariable = withNextId $ do
let regular = do let regular = do
name <- readVariableName name <- readVariableName
value <- wrap name value <- wrap name
return (\id -> (T_DollarBraced id value)) `attempting` do return (T_DollarBraced id value) `attempting` do
lookAhead $ char '[' lookAhead $ char '['
parseNoteAt pos ErrorC 1087 "Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet)." parseNoteAt pos ErrorC 1087 "Use braces when expanding arrays, e.g. ${array[idx]} (or ${var}[.. to quiet)."
try $ char '$' >> (positional <|> special <|> regular) try $ char '$' >> (positional <|> special <|> regular)
where where
wrap s = withNextId $ withNextId $ do wrap s = do
return $ \x y -> T_NormalWord x [T_Literal y s] x <- getNextId
y <- getNextId
return $ T_NormalWord x [T_Literal y s]
readVariableName = do readVariableName = do
f <- variableStart f <- variableStart