Skip to content
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
29 changes: 29 additions & 0 deletions Kong/src/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ updateLineCount _ (l, c) = (l, c + 1)
getRest :: Char -> String -> LineCount -> Rest
getRest c str lc = (str, updateLineCount c lc)

parseEscapeSequence :: Char -> Maybe Char
parseEscapeSequence 'n' = Just '\n'
parseEscapeSequence 't' = Just '\t'
parseEscapeSequence 'r' = Just '\r'
parseEscapeSequence '\\' = Just '\\'
parseEscapeSequence '\"' = Just '\"'
parseEscapeSequence '\'' = Just '\''
parseEscapeSequence _ = Nothing

fatal :: String -> String -> Parser a
fatal scope detail = Parser f
where
Expand Down Expand Up @@ -104,6 +113,9 @@ generateError c d = Parser $ \(_, lc) -> Left (False, c, d, lc)
parseChar :: Char -> Parser Char
parseChar c = Parser $ \(s, lc) -> case s of
[] -> Left (False, "Error parsing char \"" <> [c] <> "\"", "String empty", lc)
('\\' : x : xs) -> case parseEscapeSequence x of
Just escaped | escaped == c -> Right (escaped, getRest escaped xs lc)
_ -> Left (False, "Error parsing char \"" <> [c] <> "\"", "Char not found", lc)
(x : xs)
| c == x -> Right (x, getRest x xs lc)
| otherwise -> Left (False, "Error parsing char \"" <> [c] <> "\"", "Char not found", lc)
Expand All @@ -124,18 +136,35 @@ isAnyNotChar str = Parser $ \(s, lc) -> case s of
parseNotChar :: Char -> Parser Char
parseNotChar c = Parser $ \(s, lc) -> case s of
[] -> Left (False, "Error parsing not char \"" <> [c] <> "\"", "String empty", lc)
('\\' : x : xs) -> case parseEscapeSequence x of
Just escaped
| escaped == c -> Left (False, "Error parsing not char \"" <> [c] <> "\"", "Char found", lc)
| otherwise -> Right (escaped, getRest escaped xs lc)
Nothing
| x == c -> Left (False, "Error parsing not char \"" <> [c] <> "\"", "Char found", lc)
| otherwise -> Right (x, getRest x xs lc)
(x : xs)
| c == x -> Left (False, "Error parsing not char \"" <> [c] <> "\"", "Char found", lc)
| otherwise -> Right (x, getRest x xs lc)

parseCharAny :: Parser Char
parseCharAny = Parser $ \(s, lc) -> case s of
('\\' : x : xs) -> case parseEscapeSequence x of
Just escaped -> Right (escaped, getRest escaped xs lc)
Nothing -> Right (x, getRest x xs lc)
(x : xs) -> Right (x, getRest x xs lc)
[] -> Left (False, "Error parsing any char", "String empty", lc)

parseAnyNotChar :: String -> Parser Char
parseAnyNotChar str = Parser $ \(s, lc) -> case s of
[] -> Left (False, "Error parsing any not char \"" <> str <> "\"", "String empty", lc)
('\\' : x : xs) -> case parseEscapeSequence x of
Just escaped
| escaped `elem` str -> Left (False, "Error parsing any not char \"" <> str <> "\"", "Character found in string", lc)
| otherwise -> Right (escaped, getRest escaped xs lc)
Nothing
| x `elem` str -> Left (False, "Error parsing any not char \"" <> str <> "\"", "Character found in string", lc)
| otherwise -> Right (x, getRest x xs lc)
(x : xs)
| x `elem` str -> Left (False, "Error parsing any not char \"" <> str <> "\"", "Character found in string", lc)
| otherwise -> Right (x, getRest x xs lc)
Expand Down
2 changes: 1 addition & 1 deletion Kong/test/BaseParsingTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ testParseRefType :: Test
testParseRefType = TestCase (assertBool "should parse reference type" (parseSucceeds parseType "Int&"))

testParseFunctionType :: Test
testParseFunctionType = TestCase (assertBool "should parse function type" (parseSucceeds parseType "(Int,Bool)->Float"))
testParseFunctionType = TestCase (assertBool "should parse function type" (parseSucceeds parseType "((Int,Bool)->Float)"))

testParseCustomType :: Test
testParseCustomType = TestCase (assertBool "should parse custom type" (parseSucceeds parseType "MyType"))
Expand Down
16 changes: 0 additions & 16 deletions Kong/test/KongCompilerErrorTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -359,21 +359,6 @@ testWrongReturnType =
)
)

testArgumentCountMismatch :: Test
testArgumentCountMismatch =
TestCase
( assertBool
"should detect argument count mismatch in builtin function"
( case compileWithEnv emptyEnv (wrapAst (AExpress (wrapExpr (ACall
(wrapExpr (AValue (wrapValue (AVarCall "+"))))
[wrapExpr (AValue (wrapValue (ANumber (AInteger 1))))]
)))) of
Left (ArgumentCountMismatch _ _ _) -> True
Left (InvalidArguments _ _) -> True
_ -> False
)
)

testArgumentTypeMismatch :: Test
testArgumentTypeMismatch =
TestCase
Expand Down Expand Up @@ -491,7 +476,6 @@ kongCompilerErrorTests =
TestLabel "duplicate function" testDuplicateFunction,
TestLabel "missing return" testMissingReturn,
TestLabel "wrong return type" testWrongReturnType,
TestLabel "argument count mismatch" testArgumentCountMismatch,
TestLabel "argument type mismatch" testArgumentTypeMismatch,
TestLabel "call non function" testCallNonFunction,
TestLabel "duplicate variable" testDuplicateVariable,
Expand Down