From 990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675 Mon Sep 17 00:00:00 2001 From: Georgi Lyubenov Date: Wed, 7 Nov 2018 19:34:16 +0200 Subject: Предаване на първо домашно ("XXX2html") (#12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Initial commit of clex new file: .gitignore new file: Setup.hs new file: app/Main.hs new file: keyword-strings.txt new file: package.yaml new file: sample.c new file: src/Boilerplate.hs new file: src/Lex.hs new file: src/Lib.hs new file: stack.yaml new file: test/Spec.hs Need to implement ctohtml functionality now. And maybe tests for the lexer. * Clean-up modified: app/Main.hs modified: package.yaml modified: src/Boilerplate.hs modified: src/Lex.hs deleted: src/Lib.hs Clean-up warnings (unused stuff). Remove unused Lib.hs * Add swap files to gitignore modified: .gitignore * Add comments, fix keyword parsing modified: src/Lex.hs Previously keyword parsing would only work if you followed the keyword with a whitespace. Now we just include all non-alphanumerir characters as ends of a keyword. * Fix escaped character parsing in strings modified: src/Lex.hs Had previously only fixed it for characters. * Add explicit exports and comments to Boilerplate.hs modified: src/Boilerplate.hs * Remove chars written as numbers modified: src/Lex.hs * Rename Boilerplate to Types modified: Lex.hs renamed: Boilerplate.hs -> Types.hs * Fix forgotten shift assigns, implement tokenToBS modified: src/Types.hs * Add generic tests, fix a couple of bugs modified: package.yaml renamed: src/Lex.hs -> src/Clex/Lex.hs renamed: src/Types.hs -> src/Clex/Types.hs modified: stack.yaml new file: test/Clex/LexSpec.hs new file: test/Clex/TypesSpec.hs modified: test/Spec.hs Added pretty basic tests that generate tokens and then lex to and from bytestrings. Fixed a bug where I didn't realise unsigned could be 0 or more instances, and not a single char. Fixed a bug where (tokenToBS Not) == "!=", instead of "!". Fixed a bug where I was parsing Numbers before Floating numbers, and as a result "0.123" would show up as [Number _, DotFloat _] instead of [LeadingDot _]. * Remove executable info from clex lib deleted: app/Main.hs modified: package.yaml * Add cToHtml tool new file: .gitignore new file: Setup.hs new file: app/Main.hs new file: package.yaml new file: sample.c new file: src/Html.hs new file: stack.yaml * Add instructions and global stack.yaml for ease of use new file: .gitignore new file: README.md new file: stack.yaml * Add a bit of docs to cToHtml new file: README.md modified: package.yaml * Add comment on comments and the CPP modified: README.md * Apply hlint, remove ChangeLog.md, add *.swp modified: .gitignore new file: cToHtml/app/.Main.hs.swp modified: cToHtml/app/Main.hs modified: cToHtml/package.yaml modified: clex/src/Clex/Lex.hs modified: clex/test/Clex/LexSpec.hs modified: clex/test/Clex/TypesSpec.hs * Don't need Lucid for lexer modified: clex/package.yaml * Fix comma typo modified: README.md * Fix typo and misinformation modified: cToHtml/package.yaml modified: clex/package.yaml --- homework/81248/clex/test/Clex/LexSpec.hs | 58 ++++++++++ homework/81248/clex/test/Clex/TypesSpec.hs | 177 +++++++++++++++++++++++++++++ homework/81248/clex/test/Spec.hs | 1 + 3 files changed, 236 insertions(+) create mode 100644 homework/81248/clex/test/Clex/LexSpec.hs create mode 100644 homework/81248/clex/test/Clex/TypesSpec.hs create mode 100644 homework/81248/clex/test/Spec.hs (limited to 'homework/81248/clex/test') diff --git a/homework/81248/clex/test/Clex/LexSpec.hs b/homework/81248/clex/test/Clex/LexSpec.hs new file mode 100644 index 0000000..c5fec81 --- /dev/null +++ b/homework/81248/clex/test/Clex/LexSpec.hs @@ -0,0 +1,58 @@ +module Clex.LexSpec + ( spec + ) where + +import qualified Data.ByteString as BS +import Data.ByteString (ByteString) +import Data.ByteString.Char8 (pack) + +import Clex.Types +import Clex.Lex (lexC) + +import Test.QuickCheck +import Test.Hspec +import Test.Hspec.QuickCheck +import Clex.TypesSpec () + +spec :: Spec +spec = describe "lexing" $ do + prop "tokensToBS after lexC is id" $ + \x -> let x' = unValidC x in + case lexC x' of + (Left _) -> False + (Right x'') -> x' == tokensToBS x'' + prop "lexing after tokensToBS is id" $ + \x -> let x' = unTokenC x in + case lexC $ tokensToBS x' of + (Left _) -> False + (Right x'') -> x' == x'' + +newtype TokenC = TokenC {unTokenC :: [Token]} + deriving (Eq, Show) + +newtype ValidC = ValidC {unValidC :: ByteString} + deriving (Eq, Show) + +instance Arbitrary TokenC where + arbitrary = TokenC . packWhitespaceTokens . concat <$> + listOf1 (do + x <- arbitrary + whitespace <- whitespaceGen + pure [x, whitespace]) + where + whitespaceGen = Whitespace . pack <$> do listOf1 $ elements ['\t', ' ', '\n'] + +instance Arbitrary ValidC where + arbitrary = ValidC . BS.concat . map tokenToBS <$> listOf1 (frequency + [ (30, arbitrary) + , (60, whitespaceGen) + ]) + where + whitespaceGen = Whitespace . pack <$> do listOf1 $ elements ['\t', ' ', '\n'] + +packWhitespaceTokens :: [Token] -> [Token] +packWhitespaceTokens [] = [] +packWhitespaceTokens [x] = [x] +packWhitespaceTokens (Whitespace x : Whitespace y : xs) + = packWhitespaceTokens $ Whitespace (x <> y) : xs +packWhitespaceTokens (x:xs) = x : packWhitespaceTokens xs diff --git a/homework/81248/clex/test/Clex/TypesSpec.hs b/homework/81248/clex/test/Clex/TypesSpec.hs new file mode 100644 index 0000000..805affc --- /dev/null +++ b/homework/81248/clex/test/Clex/TypesSpec.hs @@ -0,0 +1,177 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS -fno-warn-orphans #-} + +module Clex.TypesSpec + ( spec + ) where + +import Data.ByteString.Char8 (pack) + +import Clex.Types + +import Test.QuickCheck +import Test.Hspec +import Test.Hspec.QuickCheck +import Data.ByteString.Arbitrary + +spec :: Spec +spec = describe "types" + conversionSpec + +conversionSpec :: Spec +conversionSpec = describe "ByteString conversions" $ do + prop "recognize is inverse to tokenToBS" $ + \x -> (not . isLiteral) x ==> x == recognize (tokenToBS x) + prop "tokenToBS is inverse to recognize" $ + \x -> let x' = fromABS x in x' == tokenToBS (recognize x') + +isLiteral :: Token -> Bool +isLiteral (HexNumber _) = True +isLiteral (OctNumber _) = True +isLiteral (Number _) = True +isLiteral (CharLit _) = True +isLiteral (Scientific _) = True +isLiteral (DotFloat _) = True +isLiteral (LeadingDot _) = True +isLiteral (StringLit _) = True +isLiteral _ = False + +instance Arbitrary Token where + arbitrary = frequency + [ (30, nullaryGen) + , (30, identifierGen) + , (10, literalGen) + ] + where + nullaryGen = elements + [ Auto + , Break + , Case + , Char + , Const + , Continue + , Default + , Do + , Double + , Else + , Enum + , Extern + , Float + , For + , Goto + , If + , Int + , Long + , Register + , Return + , Short + , Signed + , Sizeof + , Static + , Struct + , Switch + , Typedef + , Union + , Unsigned + , Void + , Volatile + , While + , Ellipsis + , RightShiftAssign + , LeftShiftAssign + , AddAssign + , SubAssign + , MulAssign + , DivAssign + , ModAssign + , AndAssign + , XorAssign + , OrAssign + , Incr + , Decr + , Ptr + , Dot + , And + , Or + , Not + , LtEq + , GtEq + , Eq + , NEq + , Lt + , Gt + , Semicolon + , LeftCurly + , RightCurly + , Comma + , Colon + , Assign + , LeftBracket + , RightBracket + , LeftSquare + , RightSquare + , RightShift + , LeftShift + , BitAnd + , BitOr + , BitNot + , Sub + , Add + , Mul + , Div + , Mod + , Xor + , Ternary + ] + identifierGen = Identifier <$> (pack <$> do listOf1 $ elements $ '_' : ['a'..'z'] ++ ['A'..'Z']) + `suchThat` (not . reserved) + reserved = (`elem` allKeyWords) + -- don't feel like overdoing this right now + literalGen = oneof + [ elements + [ HexNumber "0xb23abcf4" + , HexNumber "0x123ab5f4ul" + , HexNumber "0X123a4cf4" + , HexNumber "0x1e3ebcf4lull" + + , OctNumber "0234" + , OctNumber "012354ul" + , OctNumber "012744" + , OctNumber "0134lull" + + , Number "234" + , Number "12354ul" + , Number "12744" + , Number "134lull" + ] + , elements + [ CharLit "'c'" + , CharLit "L'c'" + , CharLit "'\n'" + , CharLit "'\t'" + , CharLit "'a'" + ] + , elements + [ DotFloat ".213E+10f" + , DotFloat ".213e-10" + , DotFloat ".213e-10l" + , DotFloat ".213e10l" + , DotFloat ".213l" + , DotFloat ".213" + , DotFloat ".213e9L" + + , LeadingDot "213.2E+10f" + , LeadingDot "213.3e-10" + , LeadingDot "213.e-10l" + , LeadingDot "213.e10l" + , LeadingDot "213.e9L" + , LeadingDot "213.9L" + , LeadingDot "213.L" + ] + , elements + [ StringLit "\"\"" + , StringLit "L\"asdfwqe\"" + , StringLit "\"asd\nfw\tqe\"" + , StringLit "L\"\"" + ] + ] diff --git a/homework/81248/clex/test/Spec.hs b/homework/81248/clex/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/homework/81248/clex/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} -- cgit v1.3.1-26-gb76a