Import path parsing

This commit is contained in:
VegOwOtenks 2025-02-18 00:20:55 +01:00
parent 99dcfbd976
commit a19bcf0ad5
3 changed files with 27 additions and 12 deletions

View file

@ -22,10 +22,12 @@ description: Please see the README on GitHub at <https://github.com/gith
dependencies: dependencies:
- base >= 4.7 && < 5 - base >= 4.7 && < 5
- containers - containers
- parsec
- filepath
- path
- directory - directory
- exceptions
- filepath
- os-string
- path
- parsec
ghc-options: ghc-options:
- -Wall - -Wall

View file

@ -3,17 +3,22 @@ module Ubc.Parse.Syntax.Import
parse) parse)
where where
import Control.Monad.Catch ( SomeException () )
import Data.Functor ((<&>))
import Data.List.NonEmpty (NonEmpty)
import Text.Parsec (sepBy1, ParsecT, anyChar, many, char, choice, notFollowedBy, oneOf) import Text.Parsec (sepBy1, ParsecT, anyChar, many, char, choice, notFollowedBy, oneOf)
import OsPath as OsPath import Path as Path ( Path, File, parseRelFile, Rel, parseRelDir, Dir )
import qualified Ubc.Parse.Syntax.Language as UbcLanguage import qualified Ubc.Parse.Syntax.Language as UbcLanguage
import System.OsPath (encodeUtf, OsPath) import qualified Data.List.NonEmpty as NonEmpty
import System.OsPath.Encoding (EncodingException (EncodingError)) import Control.Monad (liftM2)
import Control.Exception (SomeException(SomeException))
importPath :: Monad m => ParsecT String u m [String] importPath :: Monad m => ParsecT String u m (NonEmpty String)
importPath = UbcLanguage.angles (many importChar `sepBy1` char '/') importPath = UbcLanguage.angles (many importChar `sepBy1` char '/')
<&> NonEmpty.fromList
importChar :: (Monad m) => ParsecT String u m Char importChar :: (Monad m) => ParsecT String u m Char
importChar = choice importChar = choice
@ -25,9 +30,11 @@ parse :: ParsecT String u IO [String]
parse = do parse = do
UbcLanguage.reserved "import" UbcLanguage.reserved "import"
fragments <- importPath fragments <- importPath
case mapM encodeUtf fragments :: Either EncodingException [OsPath] of
Left err -> pure ()
Right success -> pure ()
pure [] let name = Path.parseRelFile $ NonEmpty.last fragments :: Either SomeException (Path Rel File)
let dirPath = mapM Path.parseRelDir . NonEmpty.init $ fragments :: Either SomeException [Path Rel Dir]
case liftM2 (,) name dirPath of
Left err -> pure []
Right success -> pure []

View file

@ -48,7 +48,9 @@ library
base >=4.7 && <5 base >=4.7 && <5
, containers , containers
, directory , directory
, exceptions
, filepath , filepath
, os-string
, parsec , parsec
, path , path
default-language: Haskell2010 default-language: Haskell2010
@ -66,7 +68,9 @@ executable ubcc-exe
base >=4.7 && <5 base >=4.7 && <5
, containers , containers
, directory , directory
, exceptions
, filepath , filepath
, os-string
, parsec , parsec
, path , path
, ubcc , ubcc
@ -86,7 +90,9 @@ test-suite ubcc-test
base >=4.7 && <5 base >=4.7 && <5
, containers , containers
, directory , directory
, exceptions
, filepath , filepath
, os-string
, parsec , parsec
, path , path
, ubcc , ubcc