Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for parsing expressions and statements. #90

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/Language/Cimple/Ast.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
module Language.Cimple.Ast
( AssignOp (..)
, BinaryOp (..)
Expand Down
3 changes: 1 addition & 2 deletions src/Language/Cimple/DescribeAst.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
module Language.Cimple.DescribeAst
( HasLocation (..)
, describeLexeme
Expand Down Expand Up @@ -34,7 +33,7 @@ describeNode node = case unFix node of
PreprocIf{} -> "#if/#endif block"
PreprocIfdef{} -> "#ifdef/#endif block"
PreprocIfndef{} -> "#ifndef/#endif block"
_ -> show $ (const ellipsis) <$> unFix node
_ -> show $ ellipsis <$ unFix node
where
ellipsis :: String
ellipsis = "..."
Expand Down
1 change: 0 additions & 1 deletion src/Language/Cimple/Diagnostics.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
module Language.Cimple.Diagnostics
( Diagnostics
, HasDiagnostics (..)
Expand Down
3 changes: 1 addition & 2 deletions src/Language/Cimple/Flatten.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeOperators #-}
module Language.Cimple.Flatten (lexemes) where

Expand Down Expand Up @@ -81,7 +80,7 @@ instance GenConcatsFlatten (Fix (CommentF a)) a where
gconcatsFlatten (Fix (DocRParen x)) = gconcatsFlatten x
gconcatsFlatten (Fix (DocSee r x)) = r : gconcatsFlatten x
gconcatsFlatten (Fix (DocSentence x p)) = gconcatsFlatten x ++ [p]
gconcatsFlatten (Fix (DocULItem i x)) = concat [gconcatsFlatten i, gconcatsFlatten x]
gconcatsFlatten (Fix (DocULItem i x)) = gconcatsFlatten i ++ gconcatsFlatten x
gconcatsFlatten (Fix (DocWord x)) = [x]

instance GenConcatsFlatten t a => GenConcats (Rec0 t) a where
Expand Down
20 changes: 15 additions & 5 deletions src/Language/Cimple/IO.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE Strict #-}
module Language.Cimple.IO
( parseFile
( parseExpr
, parseFile
, parseFiles
, parseProgram
, parseStmt
, parseText
) where

Expand All @@ -16,7 +17,7 @@ import qualified Data.Map.Strict as Map
import Data.Text (Text)
import qualified Data.Text.Encoding as Text
import Language.Cimple.Ast (Node)
import Language.Cimple.Lexer (Lexeme, runAlex)
import Language.Cimple.Lexer (Alex, Lexeme, runAlex)
import Language.Cimple.MapAst (TextActions, mapAst,
textActions)
import qualified Language.Cimple.Parser as Parser
Expand All @@ -43,8 +44,17 @@ cacheText textAst =
return text


runText :: Alex a -> Text -> Either String a
runText f = flip runAlex f . LBS.fromStrict . Text.encodeUtf8

parseExpr :: Text -> Either String TextNode
parseExpr = runText Parser.parseStmt

parseStmt :: Text -> Either String TextNode
parseStmt = runText Parser.parseStmt

parseText :: Text -> Either String [TextNode]
parseText = fmap cacheText . flip runAlex Parser.parseTranslationUnit . LBS.fromStrict . Text.encodeUtf8
parseText = fmap cacheText . runText Parser.parseTranslationUnit

parseBytes :: LBS.ByteString -> Either String [TextNode]
parseBytes = flip runAlex Parser.parseTranslationUnit
Expand Down
1 change: 0 additions & 1 deletion src/Language/Cimple/MapAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
module Language.Cimple.MapAst
( mapAst
Expand Down
6 changes: 5 additions & 1 deletion src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
{-# LANGUAGE OverloadedStrings #-}
module Language.Cimple.Parser
( parseTranslationUnit
( parseExpr
, parseStmt
, parseTranslationUnit
) where

import Data.Fix (Fix (..))
Expand All @@ -20,6 +22,8 @@ import Language.Cimple.Tokens (LexemeClass (..))
%expect 2

%name parseTranslationUnit TranslationUnit
%name parseExpr Expr
%name parseStmt Stmt

%error {parseError}
%errorhandlertype explist
Expand Down
7 changes: 5 additions & 2 deletions src/Language/Cimple/Pretty.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# OPTIONS_GHC -Wno-missing-signatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{- HLINT ignore "Functor law" -}
{- HLINT ignore "Use <$" -}
module Language.Cimple.Pretty
( plain
, render
Expand All @@ -20,16 +22,16 @@
UnaryOp (..), lexemeLine,
lexemeText)
import Prelude hiding ((<$>))
import Text.PrettyPrint.ANSI.Leijen

Check warning on line 25 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

Module ‘Text.PrettyPrint.ANSI.Leijen’ is deprecated:

Check warning on line 25 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

Module ‘Text.PrettyPrint.ANSI.Leijen’ is deprecated:

indentWidth :: Int
indentWidth = 2

kwBreak = dullred $ text "break"

Check warning on line 30 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 30 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):

Check warning on line 30 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 30 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):
kwCase = dullred $ text "case"

Check warning on line 31 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 31 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):

Check warning on line 31 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 31 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):
kwConst = dullgreen $ text "const"

Check warning on line 32 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullgreen’

Check warning on line 32 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):

Check warning on line 32 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullgreen’

Check warning on line 32 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):
kwContinue = dullred $ text "continue"

Check warning on line 33 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 33 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):

Check warning on line 33 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 33 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):
kwDefault = dullred $ text "default"

Check warning on line 34 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 34 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’
kwDo = dullred $ text "do"
kwElse = dullred $ text "else"
kwEnum = dullgreen $ text "enum"
Expand Down Expand Up @@ -275,8 +277,9 @@
ppCommentInfo :: Comment (Lexeme Text) -> Doc
ppCommentInfo = foldFix go
where
ppBody = vcat . zipWith (<>) ( repeat (dullyellow (text " * " )))
ppIndented = vcat . zipWith (<>) (empty : repeat (dullyellow (text " * ")))
commentStart t = repeat (dullyellow (text t))
ppBody = vcat . zipWith (<>) ( commentStart " * " )
ppIndented = vcat . zipWith (<>) (empty : commentStart " * ")
ppRef = underline . cyan . ppLexeme
ppAttr = maybe empty (blue . ppLexeme)

Expand Down
3 changes: 1 addition & 2 deletions src/Language/Cimple/SemCheck/Includes.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE Strict #-}
module Language.Cimple.SemCheck.Includes
( collectIncludes
, normaliseIncludes
Expand Down
1 change: 0 additions & 1 deletion src/Language/Cimple/TraverseAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
module Language.Cimple.TraverseAst
( traverseAst
Expand Down
3 changes: 2 additions & 1 deletion tools/count-tokens.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE Strict #-}
module Main (main) where

import Data.Bifunctor (bimap)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as Text
import Data.Time.Clock (diffUTCTime, getCurrentTime)
Expand All @@ -27,7 +28,7 @@ processFile source = do
Right ok -> return ok

processFiles :: [FilePath] -> IO (Int, Int)
processFiles = fmap ((\(a, b) -> (sum a, sum b)) . unzip) . mapM processFile
processFiles = fmap (bimap sum sum . unzip) . mapM processFile

main :: IO ()
main = do
Expand Down
Loading