diff --git a/README.md b/README.md index e21373c..85be2e3 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,5 @@ This will not work: ``` However this will ``` -((IS Instant) union (Color Red) union (Color Green)) +((IS Instant) union (Color Red)) union (Color Green) ``` \ No newline at end of file diff --git a/mtgsearch.cabal b/mtgsearch.cabal index c23455e..792c208 100644 --- a/mtgsearch.cabal +++ b/mtgsearch.cabal @@ -25,7 +25,6 @@ source-repository head library exposed-modules: - Algorithm.BottomQuery Algorithm.Lex Algorithm.Search Config diff --git a/src/Algorithm/BottomQuery.hs b/src/Algorithm/BottomQuery.hs deleted file mode 100644 index e69de29..0000000 diff --git a/src/Algorithm/Lex.hs b/src/Algorithm/Lex.hs index d4ba1b8..e46bbb8 100644 --- a/src/Algorithm/Lex.hs +++ b/src/Algorithm/Lex.hs @@ -9,20 +9,29 @@ import Data.ByteString (count, putStr) lexx :: String -> Token lexx qur = do - let collected = collector qur - let parenthesisFixed = fixSeparators collected - let parenthesis = matchParenthesis parenthesisFixed 0 + let collected = clearRepeatedSpaces $ collector qur + let parenthesises = trace (show $ matchParenthesis collected 0) (matchParenthesis collected 0) + let (parenthesisFixed, parenthesis) = trace (show $ fixSeparators collected parenthesises) fixSeparators collected parenthesises seperator parenthesisFixed parenthesis -fixSeparators :: [String] -> [String] -fixSeparators ("(":values) = "(":values -fixSeparators values = ["("] ++ values++ [")"] +fixSeparators :: [String] -> [(Int, Int)] -> ([String], [(Int, Int)]) +fixSeparators values parenthesis@((start,end):rest) | start == 0 && end == ( length values -1) = (values, parenthesis) +fixSeparators values parenthesis = ( ["("] ++ values ++ [")"], (0, length values + 1):map addOne parenthesis) +addOne (x,y) = (x+1, y+1) isLegal :: Char -> Bool isLegal x = x `notElem` ['(',')',' '] + +clearRepeatedSpaces :: [String] -> [String] +clearRepeatedSpaces (a:as) = case a of + " " -> " ": clearRepeatedSpaces (dropWhile (==" ") as) + b -> b: clearRepeatedSpaces as +clearRepeatedSpaces [] = [] + + collector :: String -> [String] collector [] = [] collector (x:r) | not $ isLegal x = [x] : collector r @@ -45,7 +54,7 @@ findClosing (x:xs) stackCount count = findClosing xs stackCount (count+1) findClosing [] _ _ = error "Unequal number of parenthesis" -data Token = Seperated String Token Token | Queri String deriving Show +data Token = Seperated String Token Token | Queri String String deriving Show seperator :: [String] -> [(Int, Int)] -> Token seperator ("(":rest) ((start,end):points) = case drop (end-start) rest of @@ -55,8 +64,8 @@ seperator ("(":rest) ((start,end):points) = case drop (end-start) rest of -seperator [name, " ", value, ")"] points = Queri (name++" "++value) -seperator a _ = Queri "LOL" --error "Something went wrong tokenizing the input!" +seperator [name, " ", value, ")"] points = Queri name value +seperator a _ = error "Something went wrong tokenizing the input!" diff --git a/src/Algorithm/Search.hs b/src/Algorithm/Search.hs index 75f2650..2fa9a6a 100644 --- a/src/Algorithm/Search.hs +++ b/src/Algorithm/Search.hs @@ -7,6 +7,7 @@ import Config (getDbPath) import Web.Scotty.Internal.Types import Control.Monad import qualified Data.Text as T +import Algorithm.Lex data Card = Card Int T.Text T.Text deriving (Show) diff --git a/src/Site/Host.hs b/src/Site/Host.hs index adb1af9..7adbffc 100644 --- a/src/Site/Host.hs +++ b/src/Site/Host.hs @@ -15,7 +15,7 @@ host = scotty 3000 $ do post "/api/req" $ do query <- param "query" - liftIO (putStrLn $ "\nOutput!" ++ show (lexx query)) + liftIO (putStrLn $ "\nOutput! " ++ show (lexx query)) cards <- liftIO (search query) result <- liftIO (search query) html $ mconcat ["

", pack result, "

"]