Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81248 / clex / src / Clex / Lex.hs
diff options
context:
space:
mode:
Commit990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675 (patch)
Author
 Georgi Lyubenov <godzbanebane@gmail.com>
2018-11-07 19:34:16 +0200
Committer
 dtrendafilov <trendafilov.dn@gmail.com>
2018-11-07 19:34:16 +0200
Filesd7f3f8cdc90ea9cc405b7c353d93893c80f15e4d /homework/81248/clex/src/Clex/Lex.hs
IPL-990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675.tar
IPL-990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675.tar.gz
IPL-990a8f5e2c2d6e246acfd8b7a99ac1cbf4fb1675.zip
Parent76b849fbaa276566fa8838d745c0e5fbd2f138bc (diff)
Предаване на първо домашно ("XXX2html") (#12)
* 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
1 files changed, 181 insertions, 0 deletions
-rw-r--r--homework/81248/clex/src/Clex/Lex.hs181
diff --git a/homework/81248/clex/src/Clex/Lex.hs b/homework/81248/clex/src/Clex/Lex.hs
new file mode 100644
index 0000000..ae78c5d
--- /dev/null
+++ b/homework/81248/clex/src/Clex/Lex.hs
@@ -0,0 +1,181 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Clex.Lex
+ ( lexC
+ ) where
+
+
+import Prelude hiding (takeWhile)
+
+import Data.Foldable (asum)
+import Data.Word8 ( Word8, isHexDigit, isOctDigit, isAlpha
+ , _e, _E, _x, _X,_f, _F, _u, _U, _l, _L, _0
+ , _plus, _hyphen, _underscore, _quotesingle
+ , _quotedbl, _backslash, _period)
+
+import Data.Attoparsec.ByteString
+import Data.Attoparsec.ByteString.Char8 (isDigit_w8, isSpace_w8)
+
+import Data.ByteString (ByteString)
+import qualified Data.ByteString as BS
+
+import Control.Applicative ((<|>))
+
+import Clex.Types
+
+
+lexC :: ByteString -> Either String [Token]
+lexC = parseOnly (cParser <* endOfInput)
+
+cParser :: Parser [Token]
+cParser = many' tokenParser
+
+-- Order is important! For example we need to first check for a floating point number
+-- with a leading digit, instead of one without, because the second one will always match the first one.
+tokenParser :: Parser Token
+tokenParser = asum $ concat
+ [ [whitespaceParser]
+ , [ scientificParser, leadingDotParser, dotFloatParser
+ , hexNumParser, octNumParser, numParser, charParser, stringParser
+ ]
+ , map keywordParser allKeyWords
+ , map operParser allOpers
+ , [identifierParser]]
+
+whitespaceParser :: Parser Token
+whitespaceParser = Whitespace <$> takeWhile1 isSpace_w8
+
+keywordParser :: ByteString -> Parser Token
+keywordParser kw = do
+ parsed <- recognize <$> string kw
+ -- we need to check if we still have more letters after the keyword
+ next <- peekWord8
+ let parsed' = pure parsed
+ case next of
+ Nothing -> parsed'
+ (Just x) -> if (not . isAlpha) x then parsed'
+ else fail "more input after keyword"
+
+operParser :: ByteString -> Parser Token
+operParser = (recognize <$>) . string
+
+isDigit :: Word8 -> Bool
+isDigit = isDigit_w8
+
+isLetter :: Word8 -> Bool
+isLetter b = isAlpha b
+ || b == _underscore
+
+exponentParser :: Parser ByteString
+exponentParser = do
+ e <- satisfy (\c -> c == _E || c == _e)
+ sign <- option "" plusMinus
+ digits <- takeWhile1 isDigit_w8
+ pure $ BS.singleton e <> sign <> digits
+ where
+ plusMinus = BS.singleton <$> satisfy (\c -> c == _plus || c == _hyphen) -- plus or minus
+
+float :: Parser Word8
+float = satisfy $
+ \c -> c == _F
+ || c == _f
+ || c == _L
+ || c == _l
+
+unsigned :: Parser ByteString
+unsigned = BS.pack <$> many' (satisfy $
+ \c -> c == _U
+ || c == _u
+ || c == _L
+ || c == _l)
+
+isxX :: Word8 -> Bool
+isxX c = c == _X
+ || c == _x
+
+singleQuote :: Parser Word8
+singleQuote = word8 _quotesingle
+
+doubleQuote :: Parser Word8
+doubleQuote = word8 _quotedbl
+
+charParser :: Parser Token
+charParser = do
+ l <- option "" (BS.singleton <$> word8 _L)
+ _ <- singleQuote
+ str <- BS.concat <$> many1' ((BS.snoc <$> string "\\" <*> anyWord8) <|> notInWeirdClass)
+ sq <- singleQuote
+
+ pure $ CharLit ((l <> (sq `BS.cons` str)) `BS.snoc` sq)
+ where
+ notInWeirdClass = BS.singleton <$>
+ satisfy (\c -> c /= _backslash
+ && c /= _quotesingle)
+
+stringParser :: Parser Token
+stringParser = do
+ l <- option "" (BS.singleton <$> word8 _L)
+ _ <- doubleQuote
+ str <- BS.concat <$> many' ((BS.snoc <$> string "\\" <*> anyWord8) <|> notInWeirdClass)
+ dq <- doubleQuote
+
+ pure $ StringLit ((l <> (dq `BS.cons` str)) `BS.snoc` dq)
+ where
+ notInWeirdClass = BS.singleton <$>
+ satisfy (\c -> c /= _backslash
+ && c /= _quotedbl)
+
+zero :: Parser Word8
+zero = word8 _0
+
+hexNumParser :: Parser Token
+hexNumParser = do
+ z <- zero
+ x <- satisfy isxX
+ hex <- takeWhile1 isHexDigit
+ f <- unsigned
+
+ pure $ HexNumber (z `BS.cons` x `BS.cons` hex <> f)
+
+octNumParser :: Parser Token
+octNumParser = do
+ z <- zero
+ oct <- takeWhile1 isOctDigit
+ f <- unsigned
+
+ pure $ OctNumber (z `BS.cons` oct <> f)
+
+numParser :: Parser Token
+numParser = do
+ num <- takeWhile1 isDigit
+ f <- unsigned
+
+ pure $ Number (num <> f)
+
+scientificParser :: Parser Token
+scientificParser = do
+ digits <- takeWhile1 isDigit
+ expon <- exponentParser
+ f <- option "" (BS.singleton <$> float)
+ pure $ Scientific $ digits <> expon <> f
+
+dotFloatParser :: Parser Token
+dotFloatParser = do
+ leadingDigits <- takeWhile isDigit
+ d <- BS.singleton <$> word8 _period
+ afterDigits <- takeWhile1 isDigit
+ expon <- option "" exponentParser
+ f <- option "" (BS.singleton <$> float)
+ pure $ DotFloat $ leadingDigits <> d <> afterDigits <> expon <> f
+
+leadingDotParser :: Parser Token
+leadingDotParser = do
+ leadingDigits <- takeWhile1 isDigit
+ d <- BS.singleton <$> word8 _period
+ afterDigits <- takeWhile isDigit
+ expon <- option "" exponentParser
+ f <- option "" (BS.singleton <$> float)
+ pure $ LeadingDot $ leadingDigits <> d <> afterDigits <> expon <> f
+
+identifierParser :: Parser Token
+identifierParser = Identifier <$> takeWhile1 isLetter