Add PathParser

This allows us to parser user input in order to traverse
the QuadTree from the Algorithms.RangeSearch module.
This commit is contained in:
hasufell 2014-11-14 21:23:43 +01:00
parent 53eac4fc5c
commit b9c8207981
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020

46
Parser/PathParser.hs Normal file
View File

@ -0,0 +1,46 @@
{-# OPTIONS_HADDOCK ignore-exports #-}
module Parser.PathParser where
import Control.Applicative
import Parser.Core
import Algorithms.RangeSearch.Core (Quad(NW, NE, SW, SE), Orient(North, South, West, East))
-- |Can either be an Orientation or a Quad, corresponding to the
-- Algorithms.RangeSearch module.
data QuadOrOrient = Orient Orient
| Quad Quad
deriving (Show)
-- |Parse a string such as "ne, n, sw, e" into
-- [Quad NE, Orient North, Quad SW, Orient East].
stringToQuads :: String -> [QuadOrOrient]
stringToQuads str = case runParser parsePath str of
Nothing -> []
Just xs -> fst xs
where
parsePath = zeroOrMore ((parseQuad <|> parseOrient)
<* zeroOrMore (char ',')
<* spaces)
-- |Parses a string that represents a single squad into the
-- QuadOrOrient ADT.
parseQuad :: Parser QuadOrOrient
parseQuad =
const (Quad NW) <$> (string "nw" <|> string "NW")
<|> const (Quad NE) <$> (string "ne" <|> string "NE")
<|> const (Quad SW) <$> (string "sw" <|> string "SW")
<|> const (Quad SE) <$> (string "se" <|> string "SE")
-- |Parses a string that represents a single Orientation into the
-- QuadOrOrient ADT.
parseOrient :: Parser QuadOrOrient
parseOrient =
const (Orient North) <$> (string "n" <|> string "N")
<|> const (Orient South) <$> (string "s" <|> string "S")
<|> const (Orient West) <$> (string "w" <|> string "W")
<|> const (Orient East) <$> (string "e" <|> string "E")