summaryrefslogtreecommitdiff
path: root/source/Syntax/LexiconFile.hs
diff options
context:
space:
mode:
authorSimon-Kor <52245124+Simon-Kor@users.noreply.github.com>2024-05-07 18:08:34 +0200
committerGitHub <noreply@github.com>2024-05-07 18:08:34 +0200
commitfcaffbf3cb44e804fe6df25b32f09d33e1afbabb (patch)
treecf00f0039e78882353706553100398b24fd32f39 /source/Syntax/LexiconFile.hs
parent08019dcdaf3b13bb8ce554dfd5377690bb508c6d (diff)
parentb2f9f7900ccb4a569ed23e9ecf327564dbba2b7d (diff)
Merge branch 'adelon:main' into main
Diffstat (limited to 'source/Syntax/LexiconFile.hs')
-rw-r--r--source/Syntax/LexiconFile.hs58
1 files changed, 58 insertions, 0 deletions
diff --git a/source/Syntax/LexiconFile.hs b/source/Syntax/LexiconFile.hs
new file mode 100644
index 0000000..b700621
--- /dev/null
+++ b/source/Syntax/LexiconFile.hs
@@ -0,0 +1,58 @@
+module Syntax.LexiconFile where
+
+import Base hiding (many)
+import Syntax.Adapt
+import Syntax.LexicalPhrase
+import Syntax.Abstract
+
+import Data.Char (isAlphaNum, isAsciiLower, isLetter, isDigit)
+import Data.Text qualified as Text
+import Data.Text.IO qualified as Text
+import Text.Earley.Mixfix (Holey)
+import Text.Megaparsec hiding (Token, Label, label)
+import Text.Megaparsec.Char qualified as Char
+import UnliftIO.Directory
+import System.FilePath
+
+type LexiconFileParser = Parsec Void Text
+
+parseLexiconFile :: IO [ScannedLexicalItem]
+parseLexiconFile = do
+ currentDir <- getCurrentDirectory
+ let csvPath = (currentDir </> "library" </> "lexicon.csv")
+ csv <- Text.readFile csvPath
+ case runParser lexiconFile csvPath csv of
+ Left err -> fail (errorBundlePretty err)
+ Right entries -> pure entries
+
+lexiconFile :: LexiconFileParser [ScannedLexicalItem]
+lexiconFile = many line <* eof
+
+line :: LexiconFileParser ScannedLexicalItem
+line = do
+ c <- satisfy isAsciiLower
+ cs <- takeWhileP Nothing (\x -> isAsciiLower x || isDigit x || x == '_')
+ let marker = Marker (Text.cons c cs)
+ Char.char ','
+ kind <- takeWhile1P Nothing isLetter
+ Char.char ','
+ item <- case kind of
+ "adj" -> do
+ entry <- takeWhile1P Nothing (\x -> isAlphaNum x || x == '\'' || x == '-' || x == ' ')
+ pure (ScanAdj (unsafeReadPhrase (Text.unpack entry)) marker)
+ "rel" -> do
+ entry <- tokenSingle
+ pure (ScanRelationSymbol entry marker)
+ "const" -> do
+ entry <- tokenPattern
+ pure (ScanFunctionSymbol entry marker)
+ _ -> error "Unrecognized lexical item kind in lexicon file."
+ optional Char.eol
+ pure item
+
+tokenSingle :: LexiconFileParser Token
+tokenSingle = Command <$> (single '\\' *> takeWhile1P Nothing (\x -> isAlphaNum x))
+
+-- TODO allow spaces
+tokenPattern :: LexiconFileParser (Holey Token)
+tokenPattern = some (Just <$> tokenSingle <|> Nothing <$ single '?')