Skip to content

Commit

Permalink
feat(parsing): Adding variable tests for edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Jan 9, 2024
1 parent 615eb97 commit 75ebc68
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
22 changes: 12 additions & 10 deletions src/Dreamberd/Parsing/Elements/Variable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

module Dreamberd.Parsing.Elements.Variable (parseVar) where

import Data.Char (isSpace)
import Dreamberd.Parsing.Utils (extractValueAndRest, getVariableName)
import Dreamberd.Parsing.Utils (extractValueAndRest, getVariableName, trimSpaces)
import Dreamberd.Parsing.Values (parseAnyValue, parseBool, parseFunctionCall, parseNumber, parseString)
import Dreamberd.Types (AstNode (AssignVariable, Identifier, Operator))

Expand Down Expand Up @@ -33,12 +32,15 @@ parseVar varType code ast =
else
let
(variableName, drop 1 -> afterEqual) = break (== '=') code
strippedName = filter (not . isSpace) variableName
strippedName = trimSpaces variableName
in
getVariableName strippedName >>= \name ->
let
(value, restOfCode) = extractValueAndRest afterEqual
in
parseVarValue varType value >>= \node -> case varType of
Just vt -> Right (restOfCode, ast ++ [AssignVariable vt name node])
Nothing -> Right (restOfCode, ast ++ [Operator "=" (Identifier name) node])
if length (words strippedName) > 1
then Left ("Invalid variable type '" ++ head (words strippedName) ++ "'")
else
getVariableName strippedName >>= \name ->
let
(value, restOfCode) = extractValueAndRest afterEqual
in
parseVarValue varType value >>= \node -> case varType of
Just vt -> Right (restOfCode, ast ++ [AssignVariable vt name node])
Nothing -> Right (restOfCode, ast ++ [Operator "=" (Identifier name) node])
6 changes: 5 additions & 1 deletion src/Dreamberd/Parsing/Utils.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Dreamberd.Parsing.Utils (getVariableName, extractValueAndRest, parseScope) where
module Dreamberd.Parsing.Utils (getVariableName, extractValueAndRest, parseScope, trimSpaces) where

import Data.Char (isSpace)
import Data.List (dropWhileEnd)
import Text.Regex.Posix ((=~))

bannedVariables :: [String]
Expand Down Expand Up @@ -43,3 +44,6 @@ parseScope code =
>>= \validCode ->
let (scope, rest) = extractScopeAndRest validCode 0 []
in Right (drop 1 scope, rest)

trimSpaces :: String -> String
trimSpaces = dropWhileEnd isSpace . dropWhile isSpace
3 changes: 3 additions & 0 deletions test/Unit/Dreamberd/TestDreamberdParsing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ testParseCondition :: Test
testParseCondition =
TestList
[ TestCase (assertEqual "parseCondition basic" (Right ("", [If (Boolean True) [Return (Number 1)] []])) (parseCondition " (true) {return 1;}" []))
, TestCase (assertEqual "parseCondition wrong without scope" (Left "Scope must start with open bracket but empty code found") (parseCondition " (true)" []))
, TestCase (assertEqual "parseCondition wrong without scope but other char" (Left "Code must start with an open bracket but starts with 'a'") (parseCondition " (true) anything" []))
, TestCase (assertEqual "parseCondition wrong if without condition" (Left "Missing condition in if statement") (parseCondition "( ) {return 1;}" []))
, TestCase (assertEqual "parseCondition wrong if with broken close condition parenthesis" (Left "If condition must start with '(' and end with ')'") (parseCondition " ) {return 1;}" []))
, TestCase (assertEqual "parseCondition wrong if with broken open condition parenthesis" (Left "If condition must start with '(' and end with ')'") (parseCondition " ( {return 1;}" []))
Expand All @@ -60,4 +62,5 @@ testParseDreamberd =
, TestCase (assertEqual "parseDreamberd empty code" (Right []) (parseDreamberd " " []))
, TestCase (assertEqual "parseDreamberd invalid variable name" (Left "No variable name found") (parseDreamberd "int * = 4;" []))
, TestCase (assertEqual "parseDreamberd basic loop" (Right [Loop (Boolean True) [AssignVariable "int" "b" (Number 42)] Nothing Nothing]) (parseDreamberd "while (true) {int b = 42;}" []))
, TestCase (assertEqual "parseDreamberd wrong var type" (Left "Unrecognized element") (parseDreamberd "fluid variable = 89;" []))
]

0 comments on commit 75ebc68

Please sign in to comment.