summaryrefslogtreecommitdiff
path: root/src/Language/Fiddle/Compiler
diff options
context:
space:
mode:
Diffstat (limited to 'src/Language/Fiddle/Compiler')
-rw-r--r--src/Language/Fiddle/Compiler/Stage0.hs61
-rw-r--r--src/Language/Fiddle/Compiler/Stage1.hs9
2 files changed, 70 insertions, 0 deletions
diff --git a/src/Language/Fiddle/Compiler/Stage0.hs b/src/Language/Fiddle/Compiler/Stage0.hs
new file mode 100644
index 0000000..d00d7cb
--- /dev/null
+++ b/src/Language/Fiddle/Compiler/Stage0.hs
@@ -0,0 +1,61 @@
+module Language.Fiddle.Compiler.Stage0 (toStage0, toStage1) where
+
+import Control.Monad.Identity (Identity)
+import Control.Monad.Writer
+import qualified Data.Text
+import Language.Fiddle.Ast
+import Language.Fiddle.Compiler
+import qualified Language.Fiddle.Parser
+import Language.Fiddle.Types (Commented, SourceSpan(..))
+import Text.Parsec (ParseError, errorPos)
+import Text.Parsec.Error (errorMessages, showErrorMessages)
+
+newtype Stage0Diagnostic = SyntaxError String
+
+toStage0 ::
+ String ->
+ Data.Text.Text ->
+ Compile () (FiddleUnit Stage1 (Either ParseError) (Commented SourceSpan))
+toStage0 filePath text =
+ case Language.Fiddle.Parser.parseFiddleText filePath text of
+ Left pe -> do
+ tell [parseErrorToDiagnostic pe]
+ hoistMaybe Nothing
+ Right a -> return a
+
+-- Gets the AST ready for Stage1 processing .This will report primarily
+-- SyntaxErrors and errors parsing the tree.
+--
+-- In the process, the tree is un-deferred and all parts of the
+toStage1 ::
+ FiddleUnit Stage1 (Either ParseError) a ->
+ Compile () (FiddleUnit Stage1 Identity a)
+toStage1 ast = do
+ alter
+ ( \case
+ (Left l) -> do
+ tell [parseErrorToDiagnostic l]
+ return (Left l)
+ r -> return r
+ )
+ return
+ ast
+
+ hoistMaybe $
+ case squeeze ast of
+ (Left _) -> Nothing
+ (Right a) -> Just a
+
+parseErrorToDiagnostic :: ParseError -> Diagnostic
+parseErrorToDiagnostic pe =
+ Diagnostic
+ Error
+ ( showErrorMessages
+ "or"
+ "unknown"
+ "expecting"
+ "unexpected"
+ "end of body or input (maybe a missing semicolon?)"
+ (errorMessages pe)
+ )
+ (SourceSpan (errorPos pe) (errorPos pe))
diff --git a/src/Language/Fiddle/Compiler/Stage1.hs b/src/Language/Fiddle/Compiler/Stage1.hs
new file mode 100644
index 0000000..d3f3e10
--- /dev/null
+++ b/src/Language/Fiddle/Compiler/Stage1.hs
@@ -0,0 +1,9 @@
+module Language.Fiddle.Compiler.Stage1 (toStage2) where
+
+import Control.Monad.Identity (Identity)
+import Language.Fiddle.Ast
+
+-- The second stage is a simplified version of the AST without anonymous
+-- declarations.
+toStage2 :: FiddleUnit Stage1 Identity a -> FiddleUnit Stage2 Identity a
+toStage2 = undefined