summaryrefslogtreecommitdiff
path: root/src/Language/Fiddle/Parser.hs
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-09-22 22:49:17 -0600
committerJosh Rahm <joshuarahm@gmail.com>2024-09-22 22:49:17 -0600
commit0c6ada2f5c8a3ac900fabd0384af558fb6bd334a (patch)
tree5c1d69c3ac15c90c1b64598196cc12e23de09c7a /src/Language/Fiddle/Parser.hs
parent0d2095b5d42989639c1861d7213c182abd064672 (diff)
downloadfiddle-0c6ada2f5c8a3ac900fabd0384af558fb6bd334a.tar.gz
fiddle-0c6ada2f5c8a3ac900fabd0384af558fb6bd334a.tar.bz2
fiddle-0c6ada2f5c8a3ac900fabd0384af558fb6bd334a.zip
Add import statements, add using statements, properly cross-package
symbols.
Diffstat (limited to 'src/Language/Fiddle/Parser.hs')
-rw-r--r--src/Language/Fiddle/Parser.hs35
1 files changed, 25 insertions, 10 deletions
diff --git a/src/Language/Fiddle/Parser.hs b/src/Language/Fiddle/Parser.hs
index f3ad744..b44a9a1 100644
--- a/src/Language/Fiddle/Parser.hs
+++ b/src/Language/Fiddle/Parser.hs
@@ -10,6 +10,7 @@ where
import Control.Monad (void)
import Data.Functor.Identity
import Data.Kind (Type)
+import Data.List.NonEmpty (NonEmpty (..))
import Data.Text (Text)
import qualified Data.Text
import Debug.Trace
@@ -107,19 +108,24 @@ fiddleUnit = do
<* many comment
stringToken :: P Text
-stringToken = token (\case
- (TokString str) -> Just str
- _ -> Nothing)
+stringToken =
+ token
+ ( \case
+ (TokString str) -> Just str
+ _ -> Nothing
+ )
importList :: PaS ImportList
importList = withMeta $ do
tok TokLParen
- ImportList <$> many (ident <* (tok TokComma <|> lookAhead (tok TokRParen)))
- <* tok TokRParen
+ ImportList
+ <$> many (ident <* (tok TokComma <|> lookAhead (tok TokRParen)))
+ <* tok TokRParen
importStatement :: PaS ImportStatement
-importStatement = withMeta $
- ImportStatement <$> stringToken <*> optionMaybe importList
+importStatement =
+ withMeta $
+ ImportStatement <$> stringToken <*> optionMaybe importList
fiddleDecl :: Pa FiddleDecl
fiddleDecl = do
@@ -129,8 +135,9 @@ fiddleDecl = do
KWOption -> OptionDecl <$> nextText <*> nextText
KWPackage ->
PackageDecl
- <$> ident
+ <$> name
<*> defer body packageBody
+ KWUsing -> UsingDecl <$> name
KWLocation -> LocationDecl <$> ident <*> (tok TokEq >> expression)
KWBits -> BitsDecl <$> ident <*> (tok TokColon >> bitType)
KWImport -> ImportDecl <$> importStatement
@@ -185,7 +192,7 @@ objType = do
baseObj :: P (A -> ObjType Stage1 F A)
baseObj =
- (ReferencedObjType <$> ident)
+ (ReferencedObjType <$> name)
<|> ( do
t <- bodyType
AnonymousObjType <$> defer body (objTypeBody t)
@@ -285,7 +292,7 @@ registerBitsTypeRef = do
withMeta $
(RegisterBitsJustBits <$> exprInParen)
<|> (RegisterBitsAnonymousType <$> anonymousBitsType)
- <|> (RegisterBitsReference <$> ident)
+ <|> (RegisterBitsReference <$> name)
anonymousBitsType :: Pa AnonymousBitsType
anonymousBitsType = withMeta $ do
@@ -390,6 +397,14 @@ ident =
(TokIdent id) -> Just (Identifier id)
_ -> Nothing
+name :: PaS Name
+name = withMeta $ do
+ i <- ident
+ is <- many $ do
+ tok TokDot
+ ident
+ return $ Name (i :| is)
+
-- Takes a some parsable thing p and automatically parses the comments before
-- and after and sets the positions and adds it to the annotation.
withMeta :: P (Commented SourceSpan -> b) -> P b