summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-06-11 04:06:24 -0400
committerJoe Crayne <joe@jerkface.net>2019-06-11 04:06:24 -0400
commit7303e1d9c65c6d1c91e89bfdf7b4bf3313ea7b06 (patch)
tree3ca11342b9e28258d3bef0a2ccbf511d1109d533
parent33ac9c597f708c0f0d2232c25e1fbbf7840b9610 (diff)
Sabadie parser: support trailing blanks.
-rw-r--r--src/Codec/Wavefront/Token.hs28
-rw-r--r--test/TestSabadie.hs5
2 files changed, 15 insertions, 18 deletions
diff --git a/src/Codec/Wavefront/Token.hs b/src/Codec/Wavefront/Token.hs
index 71691a8..01acd5c 100644
--- a/src/Codec/Wavefront/Token.hs
+++ b/src/Codec/Wavefront/Token.hs
@@ -81,7 +81,7 @@ cleanupTokens = catMaybes
81-- Location ---------------------------------------------------------------------------------------- 81-- Location ----------------------------------------------------------------------------------------
82 82
83location :: Parser Location 83location :: Parser Location
84location = skipSpace *> string "v " *> skipHSpace *> parseXYZW <* eol 84location = string "v " *> skipHSpace *> parseXYZW <* eol
85 where 85 where
86 parseXYZW = do 86 parseXYZW = do
87 xyz <- float `sepBy1` skipHSpace 87 xyz <- float `sepBy1` skipHSpace
@@ -96,7 +96,7 @@ location = skipSpace *> string "v " *> skipHSpace *> parseXYZW <* eol
96-- Normal ------------------------------------------------------------------------------------------ 96-- Normal ------------------------------------------------------------------------------------------
97 97
98normal :: Parser Normal 98normal :: Parser Normal
99normal = skipSpace *> string "vn " *> skipHSpace *> parseIJK <* eol 99normal = string "vn " *> skipHSpace *> parseIJK <* eol
100 where 100 where
101 parseIJK = do 101 parseIJK = do
102 ijk <- float `sepBy1` skipHSpace 102 ijk <- float `sepBy1` skipHSpace
@@ -108,7 +108,7 @@ normal = skipSpace *> string "vn " *> skipHSpace *> parseIJK <* eol
108-- Texture coordinates ----------------------------------------------------------------------------- 108-- Texture coordinates -----------------------------------------------------------------------------
109 109
110texCoord :: Parser TexCoord 110texCoord :: Parser TexCoord
111texCoord = skipSpace *> string "vt " *> skipHSpace *> parseUVW <* eol 111texCoord = string "vt " *> skipHSpace *> parseUVW <* eol
112 where 112 where
113 parseUVW = do 113 parseUVW = do
114 uvw <- float `sepBy1` skipHSpace 114 uvw <- float `sepBy1` skipHSpace
@@ -121,7 +121,7 @@ texCoord = skipSpace *> string "vt " *> skipHSpace *> parseUVW <* eol
121-- Parameter-space coordinates ----------------------------------------------------------------------------- 121-- Parameter-space coordinates -----------------------------------------------------------------------------
122 122
123paramCoord :: Parser ParamCoord 123paramCoord :: Parser ParamCoord
124paramCoord = skipSpace *> string "vp " *> skipHSpace *> parseUVW <* eol 124paramCoord = string "vp " *> skipHSpace *> parseUVW <* eol
125 where 125 where
126 parseUVW = do 126 parseUVW = do
127 uvw <- float `sepBy1` skipHSpace 127 uvw <- float `sepBy1` skipHSpace
@@ -134,13 +134,12 @@ paramCoord = skipSpace *> string "vp " *> skipHSpace *> parseUVW <* eol
134-- Points ------------------------------------------------------------------------------------------ 134-- Points ------------------------------------------------------------------------------------------
135 135
136points :: Parser [Point] 136points :: Parser [Point]
137points = skipSpace *> string "p " *> skipHSpace *> fmap Point decimal `sepBy1` skipHSpace <* eol 137points = string "p " *> skipHSpace *> fmap Point decimal `sepBy1` skipHSpace <* eol
138 138
139---------------------------------------------------------------------------------------------------- 139----------------------------------------------------------------------------------------------------
140-- Lines ------------------------------------------------------------------------------------------- 140-- Lines -------------------------------------------------------------------------------------------
141lines :: Parser [Line] 141lines :: Parser [Line]
142lines = do 142lines = do
143 skipSpace
144 _ <- string "l " 143 _ <- string "l "
145 skipHSpace 144 skipHSpace
146 pointIndices <- parsePointIndices 145 pointIndices <- parsePointIndices
@@ -159,7 +158,6 @@ lines = do
159-- Faces ------------------------------------------------------------------------------------------- 158-- Faces -------------------------------------------------------------------------------------------
160face :: Parser Face 159face :: Parser Face
161face = do 160face = do
162 skipSpace
163 _ <- string "f " 161 _ <- string "f "
164 skipHSpace 162 skipHSpace
165 faceIndices <- parseFaceIndices 163 faceIndices <- parseFaceIndices
@@ -184,37 +182,37 @@ face = do
184-- Groups ------------------------------------------------------------------------------------------ 182-- Groups ------------------------------------------------------------------------------------------
185 183
186groups :: Parser [Text] 184groups :: Parser [Text]
187groups = skipSpace *> string "g " *> skipHSpace *> name `sepBy` skipHSpace <* eol 185groups = string "g " *> skipHSpace *> name `sepBy` skipHSpace <* eol
188 186
189---------------------------------------------------------------------------------------------------- 187----------------------------------------------------------------------------------------------------
190-- Objects ----------------------------------------------------------------------------------------- 188-- Objects -----------------------------------------------------------------------------------------
191 189
192object :: Parser Text 190object :: Parser Text
193object = skipSpace *> string "o " *> skipHSpace *> spacedName <* eol 191object = string "o " *> skipHSpace *> spacedName <* eol
194 192
195---------------------------------------------------------------------------------------------------- 193----------------------------------------------------------------------------------------------------
196-- Material libraries ------------------------------------------------------------------------------ 194-- Material libraries ------------------------------------------------------------------------------
197 195
198mtllib :: Parser [Text] 196mtllib :: Parser [Text]
199mtllib = skipSpace *> string "mtllib " *> skipHSpace *> name `sepBy1` skipHSpace <* eol 197mtllib = string "mtllib " *> skipHSpace *> name `sepBy1` skipHSpace <* eol
200 198
201---------------------------------------------------------------------------------------------------- 199----------------------------------------------------------------------------------------------------
202-- Using materials --------------------------------------------------------------------------------- 200-- Using materials ---------------------------------------------------------------------------------
203 201
204usemtl :: Parser Text 202usemtl :: Parser Text
205usemtl = skipSpace *> string "usemtl " *> skipHSpace *> spacedName <* eol 203usemtl = string "usemtl " *> skipHSpace *> spacedName <* eol
206 204
207---------------------------------------------------------------------------------------------------- 205----------------------------------------------------------------------------------------------------
208-- Smoothing groups -------------------------------------------------------------------------------- 206-- Smoothing groups --------------------------------------------------------------------------------
209smoothingGroup :: Parser Natural 207smoothingGroup :: Parser Natural
210smoothingGroup = skipSpace *> string "s " *> skipHSpace *> offOrIndex <* skipHSpace <* eol 208smoothingGroup = string "s " *> skipHSpace *> offOrIndex <* skipHSpace <* eol
211 where 209 where
212 offOrIndex = string "off" *> pure 0 <|> decimal 210 offOrIndex = string "off" *> pure 0 <|> decimal
213 211
214---------------------------------------------------------------------------------------------------- 212----------------------------------------------------------------------------------------------------
215-- Comments ---------------------------------------------------------------------------------------- 213-- Comments ----------------------------------------------------------------------------------------
216comment :: Parser () 214comment :: Parser ()
217comment = skipSpace *> string "#" *> (() <$ manyTill anyChar eol) 215comment = string "#" *> (() <$ manyTill anyChar eol)
218 216
219---------------------------------------------------------------------------------------------------- 217----------------------------------------------------------------------------------------------------
220-- Special parsers --------------------------------------------------------------------------------- 218-- Special parsers ---------------------------------------------------------------------------------
@@ -250,6 +248,6 @@ untilEnd :: Parser a -> Parser [a]
250untilEnd p = go 248untilEnd p = go
251 where 249 where
252 go = do 250 go = do
253 a <- p 251 skipSpace
254 end <- atEnd 252 end <- atEnd
255 if end then pure [a] else fmap (a:) go 253 if end then pure [] else (:) <$> p <*> go
diff --git a/test/TestSabadie.hs b/test/TestSabadie.hs
index de13678..d8d1949 100644
--- a/test/TestSabadie.hs
+++ b/test/TestSabadie.hs
@@ -3,6 +3,7 @@ module TestSabadie where
3import Codec.Wavefront 3import Codec.Wavefront
4import System.Directory 4import System.Directory
5import System.IO 5import System.IO
6import Text.Show.Pretty (ppShow)
6 7
7getDataDir :: IO FilePath 8getDataDir :: IO FilePath
8getDataDir = do 9getDataDir = do
@@ -14,6 +15,4 @@ getDataDir = do
14main = do 15main = do
15 ddir <- getDataDir 16 ddir <- getDataDir
16 r1 <- fromFile (ddir ++ "/01.obj") 17 r1 <- fromFile (ddir ++ "/01.obj")
17 print r1 18 either print (putStrLn . ppShow) r1
18 -- Failed: ` vp ` [...]: Failed reading: empty
19 -- Reason: blank lines