summaryrefslogtreecommitdiffstats
path: root/src/parser.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.hs')
-rw-r--r--src/parser.hs36
1 files changed, 28 insertions, 8 deletions
diff --git a/src/parser.hs b/src/parser.hs
index 79c30be..f6883da 100644
--- a/src/parser.hs
+++ b/src/parser.hs
@@ -2,7 +2,7 @@ module Parser where
import qualified Data.Bifunctor as B
import qualified Data.Hashable as DH
-import Data.List (isPrefixOf)
+import Data.List (intercalate, isInfixOf, isPrefixOf)
import Data.Map (Map, (!))
import qualified Data.Map as M
import qualified Data.Text as T
@@ -21,26 +21,46 @@ removeComments source =
let lines_ = lines source $> map (T.pack .> T.splitOn (T.pack "--") .> head .> T.unpack)
in unlines lines_
-processStringLiterals source =
- let refMap = M.empty
- in processStringLiterals' Nothing refMap source
+processStringLiterals = processStringLiterals' Nothing M.empty
-processStringLiterals' :: Maybe String -> Map String String -> String -> (String, Map String String)
+processStringLiterals' :: Maybe String -> Map String [LWord] -> String -> (String, Map String [LWord])
processStringLiterals' currentM refMap [] = case currentM of
Just _ -> error "[error] nonterminated string literal"
Nothing -> ([], refMap)
processStringLiterals' currentM refMap (c : source) = case c of
'"' -> case currentM of
Just str ->
+ -- string literal ends
let hash = DH.hash str $> show
- newRefMap = M.insert hash str refMap
+ strP = str $> map LChar .> LPhrase
+ interpolated = processSLInterpolations strP
+ newRefMap = M.insert hash interpolated refMap
(resSource, resRefMap) = processStringLiterals' Nothing newRefMap source
in ("##" ++ hash ++ resSource, resRefMap)
Nothing ->
+ -- string literal starts
processStringLiterals' (Just "") refMap source
other -> case currentM of
- Just str -> processStringLiterals' (Just $ str ++ [c]) refMap source
- Nothing -> processStringLiterals' Nothing refMap source $> B.first (c :)
+ Just str -> processStringLiterals' (Just $ str ++ [c]) refMap source -- add char to current string literal
+ Nothing -> processStringLiterals' Nothing refMap source $> B.first (c :) -- proceed normally
+
+processSLInterpolations :: LWord -> [LWord]
+processSLInterpolations strP@(LPhrase lChars)
+ | "%%" `isInfixOf` chars = [LPhrase interpolated, LSymbol "unphrase"]
+ | otherwise = [strP]
+ where
+ chars = lChars $> map (\(LChar c) -> c)
+ tChars = T.pack chars
+ separatedByMarker = T.pack chars $> T.splitOn (T.pack "%%") .> map (T.unpack .> map LChar)
+ labelIndices = [0 .. length separatedByMarker - 1 - 1]
+ varStores = labelIndices $> map (\n -> [LLabel ("$__" ++ show n), LSymbol "!"]) .> reverse
+ part1 = concat varStores
+ symbolLookups = labelIndices $> map (\n -> [LLabel ("$__" ++ show n), LSymbol "@", LSymbol "unphrase"])
+ part2 = LSymbol "'[" : concat (mix separatedByMarker symbolLookups) ++ [LSymbol "']", LSymbol "phrase"]
+ varForgets = labelIndices $> map (\n -> [LLabel ("$__" ++ show n), LSymbol "forget"])
+ part3 = concat varForgets
+ interpolated = part1 ++ part2 ++ part3
+processSLInterpolations p = error $ "[error] non-phrase in processSLInterpolations: " ++ show p
parseSource source =
let woComments = source $> removeComments