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

@ -3,17 +3,22 @@ module Ubc.Parse.Syntax.Import
parse)
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 OsPath as OsPath
import Path as Path ( Path, File, parseRelFile, Rel, parseRelDir, Dir )
import qualified Ubc.Parse.Syntax.Language as UbcLanguage
import System.OsPath (encodeUtf, OsPath)
import System.OsPath.Encoding (EncodingException (EncodingError))
import Control.Exception (SomeException(SomeException))
import qualified Data.List.NonEmpty as NonEmpty
import Control.Monad (liftM2)
importPath :: Monad m => ParsecT String u m [String]
importPath :: Monad m => ParsecT String u m (NonEmpty String)
importPath = UbcLanguage.angles (many importChar `sepBy1` char '/')
<&> NonEmpty.fromList
importChar :: (Monad m) => ParsecT String u m Char
importChar = choice
@ -25,9 +30,11 @@ parse :: ParsecT String u IO [String]
parse = do
UbcLanguage.reserved "import"
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 []