Cleaned input and removed bracket around entire statement

This commit is contained in:
Rolf Martin Glomsrud 2023-04-28 21:39:04 +02:00
parent 5f8cf39aa7
commit 3fe066c6af
6 changed files with 21 additions and 12 deletions

View file

@ -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)
```

View file

@ -25,7 +25,6 @@ source-repository head
library
exposed-modules:
Algorithm.BottomQuery
Algorithm.Lex
Algorithm.Search
Config

View file

@ -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!"

View file

@ -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)

View file

@ -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 ["<h1>", pack result, "</h1>"]