Skip to content

Commit

Permalink
chore: add some test cases and rename some functions
Browse files Browse the repository at this point in the history
* add some test cases for newline

* chore: add some test cases and rename some symbols
  • Loading branch information
xieyuschen authored Aug 2, 2024
1 parent cb07709 commit ae57c4f
Show file tree
Hide file tree
Showing 19 changed files with 329 additions and 214 deletions.
4 changes: 2 additions & 2 deletions src/Lib/AST/Contract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pContractDefinition = do
>> pMany1Spaces
*> pIdentifier
<* pManySpaces
inheriSpeciciers <-
inherSpecifiers <-
optionMaybe $
pOneKeyword "is"
*> pMany1Spaces
Expand All @@ -53,6 +53,6 @@ pContractDefinition = do
ContractDefinition
{ contractName = contractName,
contractIsAbstract = isJust abs,
contractInheritanceSpecifiers = fromMaybe [] inheriSpeciciers,
contractInheritanceSpecifiers = fromMaybe [] inherSpecifiers,
contractBody = body
}
10 changes: 5 additions & 5 deletions src/Lib/AST/Definition.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Data.Either
import Data.Functor (($>))
import Data.Maybe (fromMaybe, listToMaybe)
import Lib.AST.Expr (pFnCallArgs)
import Lib.AST.Function (pFnDeclModifierInvocation, pFunction)
import Lib.AST.Model (ConstructorDefinition (..), ConstructorMutability (..), ContractBody (..), ContractBodyFieldSum (..), ErrorDefinition (..), ErrorParameter (ErrorParameter, errParamName, errParamType), EventDefinition (..), EventParameter (..), FnDecorator (..), FnModifierInvocation, FnName (..), Function (fname), InheritanceSpecifier (..), InterfaceDefinition (..), LibraryDefinition (..), ModifierDefinition (..), extractFnDecOs, leftCurlyBrace, leftParenthesis, rightCurlyBrace, rightParenthesis, semicolon)
import Lib.AST.Function (pFnDeclModifierInvocation, pFunctionDefinition)
import Lib.AST.Model (ConstructorDefinition (..), ConstructorMutability (..), ContractBody (..), ContractBodyFieldSum (..), ErrorDefinition (..), ErrorParameter (ErrorParameter, errParamName, errParamType), EventDefinition (..), EventParameter (..), FnDecorator (..), FnModifierInvocation, FnName (..), FunctionDefinition (fnDefName), InheritanceSpecifier (..), InterfaceDefinition (..), LibraryDefinition (..), ModifierDefinition (..), extractFnDecOs, leftCurlyBrace, leftParenthesis, rightCurlyBrace, rightParenthesis, semicolon)
import Lib.AST.Pragma (pComment, pUsingDirective)
import Lib.AST.Stat (pState, pStateVariable)
import Lib.AST.Type (pInt, pType, pTypeEnum, pTypeStruct, pUserDefinedValueTypeDefinition)
Expand Down Expand Up @@ -167,7 +167,7 @@ pContractBody = do
( try pComment $> CBFSSumComments
<|> CBFSSumUsingDirective <$> try pUsingDirective
<|> CBFSSumConstructor <$> try pConstructorDefinition
<|> CBFSSumFunction <$> try pFunction
<|> CBFSSumFunction <$> try pFunctionDefinition
<|> CBFSSumModifierDefinition <$> try pModifierDefinition
<|> CBFSSumStructure <$> try pTypeStruct
<|> CBFSSumSTypeEnum <$> try pTypeEnum
Expand All @@ -191,8 +191,8 @@ pContractBody = do
ctBodyEventDefinitions = [v | CBFSSumEventDefinition v <- all],
ctBodyErrorDefinitions = [v | CBFSSumErrorDefinition v <- all],
ctBodyUsingDirectives = [v | CBFSSumUsingDirective v <- all],
ctBodyReceiveFunctions = filter (\f -> fname f == FnReceive) [v | CBFSSumFunction v <- all],
ctBodyFallbackFunctions = filter (\f -> fname f == FnFallback) [v | CBFSSumFunction v <- all],
ctBodyReceiveFunctions = filter (\f -> fnDefName f == FnReceive) [v | CBFSSumFunction v <- all],
ctBodyFallbackFunctions = filter (\f -> fnDefName f == FnFallback) [v | CBFSSumFunction v <- all],
ctBodyAllFields = all
}

Expand Down
9 changes: 6 additions & 3 deletions src/Lib/AST/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ pUnaryExpr = do
-- don't use pExpression here because we don't want to parse the whole expression after unary
-- for example, -(x-1) && 234, we should parse the -(x-1) as an expression only
operand <-
pParenthesizedExpr
<|> SExprL <$> pLiteral
<|> SExprVar <$> pIdentifier
pManySpaces
>> ( pParenthesizedExpr -- it's allowed to have space after the unary operator
<|> SExprL <$> pLiteral
<|> SExprVar <$> pIdentifier
<|> SExprU <$> pUnaryExpr -- double unary expression is possible, such as '- - 1'
)
return
ExprUnary
{ uOperand = operand,
Expand Down
17 changes: 8 additions & 9 deletions src/Lib/AST/Function.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Lib.AST.Model
FnName (..),
FnStateMutability (..),
FnVisibility (..),
Function (..),
FunctionDefinition (..),
OverrideSpecifier,
SType,
StateVariable,
Expand All @@ -52,23 +52,22 @@ import Lib.AST.Util
import Lib.Parser
import Text.Parsec

pFunction :: Parser Function
pFunction = do
pFunctionDefinition :: Parser FunctionDefinition
pFunctionDefinition = do
name <-
pManySpaces
>> pOneKeyword keywordFunction
>> pMany1Spaces
>> pFunctionName
args <- pManySpaces >> pFnDeclArgsInParentheses

-- todo: support custom modifiers as well
decorators <- pFunctionDecorators

-- guards the decorators satisify the function declaration specification
-- guards the decorators satisfy the function declaration specification
let visibility = extractFnDecV decorators
states = extractFnDecS decorators
mis = extractFnDecMI decorators
ospecifier = extractFnDecOs decorators
oSpecifier = extractFnDecOs decorators
guard $ length visibility <= 1
guard $ length states <= 1
guard $ length states <= 1
Expand All @@ -86,14 +85,14 @@ pFunction = do

-- why 'many anyChar' doesn't work?
return
( Function
{ fname = name,
( FunctionDefinition
{ fnDefName = name,
fargs = args,
fnIsVirtual = FnDecVirtual `elem` decorators,
fnVisibility = fromMaybe FnInternal $ listToMaybe visibility,
fnState = fromMaybe FnStateDefault $ listToMaybe states,
fnModifierInvocations = mis,
fnFnOverrideSpecifier = listToMaybe ospecifier,
fnFnOverrideSpecifier = listToMaybe oSpecifier,
fnReturnTyp = optReturns,
fnBody = fnBody
}
Expand Down
37 changes: 12 additions & 25 deletions src/Lib/AST/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ leftSquareBracket = "["
rightSquareBracket :: Text
rightSquareBracket = "]"

data AST
= ASTSPDXComment SPDXComment
| ASTComment Comment
| ASTPragma Pragma
| ASTType SType
| ASTFunction Function
| ASTModifier FnVisibility
| ASTVariable StateVariable
| Struct
{ name :: Text
}
deriving (Show, Eq)

-- // SPDX-License-Identifier: MIT
type SPDXComment = Text

Expand Down Expand Up @@ -127,8 +114,8 @@ data FnName
| FnReceive -- function receive
deriving (Show, Eq)

data Function = Function
{ fname :: FnName,
data FunctionDefinition = FunctionDefinition
{ fnDefName :: FnName,
fnState :: FnStateMutability,
fnVisibility :: FnVisibility,
fnModifierInvocations :: [FnModifierInvocation],
Expand Down Expand Up @@ -314,10 +301,10 @@ data Operator
| ArithmeticDivision -- /
| ArithmeticModulus -- %
| ArithmeticExp -- **
| ComparisionLessEqual -- <=
| ComparisionLess -- <
| ComparisionMoreEqual -- >=
| ComparisionMore -- >
| ComparisonLessEqual -- <=
| ComparisonLess -- <
| ComparisonMoreEqual -- >=
| ComparisonMore -- >
| BitAnd -- &
| BitOr -- |
| BitExor -- ^
Expand All @@ -327,7 +314,7 @@ data Operator
| CompoundAddition -- +=
| CompoundMinus -- -=
| CompoundMultiply -- '*='
| CompoundDevision -- /=
| CompoundDivision -- /=
| CompoundModulus -- %=
| CompoundAnd -- '&='
| CompoundOr -- '|='
Expand Down Expand Up @@ -569,15 +556,15 @@ data InheritanceSpecifier = InheritanceSpecifier
deriving (Show, Eq)

data ContractBodyField
= CtFunction Function
= CtFunction FunctionDefinition
| CtVariable StateVariable
| CtComment Comment
| CtEmptyLine
deriving (Show, Eq)

data ContractBodyFieldSum
= CBFSSumConstructor ConstructorDefinition
| CBFSSumFunction Function
| CBFSSumFunction FunctionDefinition
| CBFSSumModifierDefinition ModifierDefinition
| CBFSSumStructure Structure
| CBFSSumSTypeEnum STypeEnum
Expand All @@ -592,10 +579,10 @@ data ContractBodyFieldSum

data ContractBody = ContractBody
{ ctBodyConstructor :: Maybe ConstructorDefinition,
ctBodyFunctions :: [Function],
ctBodyFunctions :: [FunctionDefinition],
ctBodyModifiers :: [ModifierDefinition],
ctBodyFallbackFunctions :: [Function],
ctBodyReceiveFunctions :: [Function],
ctBodyFallbackFunctions :: [FunctionDefinition],
ctBodyReceiveFunctions :: [FunctionDefinition],
ctBodyStructDefinitions :: [Structure],
ctBodyEnumDefinitions :: [STypeEnum],
ctBodyUserDefinedValueTypeDefinition :: [UserDefinedValueTypeDefinition],
Expand Down
29 changes: 19 additions & 10 deletions src/Lib/AST/Oper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ pOperator2Char = do
"--" -> return Decrement
"**" -> return ArithmeticExp
"*=" -> return CompoundMultiply
"/=" -> return CompoundDevision
"/=" -> return CompoundDivision
"%=" -> return CompoundModulus
"!=" -> return LogicalInequal
"&&" -> return LogicalAnd
"&=" -> return CompoundAnd
"||" -> return LogicalOr
"|=" -> return CompoundOr
"^=" -> return CompoundExor
"<=" -> return ComparisionLessEqual
"<=" -> return ComparisonLessEqual
"<<" -> return ShiftLeft
">=" -> return ComparisionMoreEqual
">=" -> return ComparisonMoreEqual
">>" -> return ShiftRight
"==" -> return LogicalEqual
_ -> fail "unsupport operator in two characters"
_ -> fail "un-support operator in two characters"

pOperator1Char :: Parser Operator
pOperator1Char = do
Expand All @@ -62,9 +62,9 @@ pOperator1Char = do
"~" -> return BitNeg
"&" -> return BitAnd
"|" -> return BitOr
"<" -> return ComparisionLess
">" -> return ComparisionMore
_ -> fail "unsupport operator in one character"
"<" -> return ComparisonLess
">" -> return ComparisonMore
_ -> fail "un-support operator in one character"

pOperator :: Parser Operator
pOperator = do
Expand All @@ -74,40 +74,49 @@ pOperator = do
<|> try pOperator1Char
)

-- we use the same rank mentioned in the documentation to refre the precedences among different
-- we use the same rank mentioned in the documentation to refer the precedences among different
-- operators, in the format of pOpRank{n} such as pOpRank1 and so on
-- https://docs.soliditylang.org/en/latest/types.html#order-of-precedence-of-operators

-- todo: support all the cases in rank1
-- todo: support the delete cases
-- todo: think about the unary minus precedence
opRank2 :: [Operator]
opRank2 = [Increment, Decrement, LogicalNegation, BitNeg]

opRank3 :: [Operator]
opRank3 = [ArithmeticExp]

opRank6 :: [Operator]
opRank6 = [ShiftLeft, ShiftRight]

opRank7 :: [Operator]
opRank7 = [BitAnd]

opRank8 :: [Operator]
opRank8 = [BitExor]

opRank9 :: [Operator]
opRank9 = [BitOr]

opRank10 = [ComparisionLessEqual, ComparisionLess, ComparisionMoreEqual, ComparisionMore]
opRank10 :: [Operator]
opRank10 = [ComparisonLessEqual, ComparisonLess, ComparisonMoreEqual, ComparisonMore]

opRank11 :: [Operator]
opRank11 = [LogicalEqual, LogicalInequal]

opRank12 :: [Operator]
opRank12 = [LogicalAnd]

opRank13 :: [Operator]
opRank13 = [LogicalOr]

-- todo: support op14 Ternary operator, and assignment
opRank14 =
[ CompoundAddition,
CompoundMinus,
CompoundMultiply,
CompoundDevision,
CompoundDivision,
CompoundModulus,
CompoundAnd,
CompoundOr,
Expand Down
16 changes: 8 additions & 8 deletions src/Lib/AST/Pragma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import Lib.AST.Model
BitExor,
BitNeg,
BitOr,
ComparisionLess,
ComparisionLessEqual,
ComparisionMore,
ComparisionMoreEqual,
ComparisonLess,
ComparisonLessEqual,
ComparisonMore,
ComparisonMoreEqual,
LogicalEqual,
LogicalInequal,
Minus
Expand Down Expand Up @@ -225,10 +225,10 @@ toUserDefinableOperator o =
ArithmeticMultiplication,
ArithmeticDivision,
ArithmeticModulus,
ComparisionLessEqual,
ComparisionLess,
ComparisionMoreEqual,
ComparisionMore,
ComparisonLessEqual,
ComparisonLess,
ComparisonMoreEqual,
ComparisonMore,
BitAnd,
BitOr,
BitExor,
Expand Down
5 changes: 4 additions & 1 deletion src/Lib/AST/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import Lib.AST.Model
UserDefinedValueTypeDefinition (..),
aSize,
leftCurlyBrace,
leftParenthesis,
leftSquareBracket,
rightSquareBracket,
semicolon,
Expand Down Expand Up @@ -96,7 +97,9 @@ pTypeMapping :: Parser Mapping
pTypeMapping = do
keyTyp <-
pManySpaces
>> pOneKeyword "mapping("
>> pOneKeyword "mapping"
>> pManySpaces
>> pOneKeyword leftParenthesis
>> pManySpaces
>> pType
<* pManySpaces
Expand Down
16 changes: 6 additions & 10 deletions src/Lib/AST/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pFunctionArgs =
)
pType
(optionMaybe $ pManySpaces >> pLocationModifier)
(optionMaybe $ pManySpaces >> pIdentifier)
(optionMaybe $ pManySpaces >> pIdentifier <* pManySpaces)

pStateVariableConstrain :: Parser StateVariableConstrain
pStateVariableConstrain =
Expand All @@ -77,15 +77,11 @@ pFnDeclVisibility =
-- parse the '(name: uint)' as so on. it will consume the following spaces
pFnDeclArgsInParentheses :: Parser [FnDeclArg]
pFnDeclArgsInParentheses = do
fmap (fromMaybe []) $
pManySpaces
>> pOneKeyword leftParenthesis
>> pManySpaces
>> optionMaybe pFunctionArgs
<* ( pManySpaces
>> pOneKeyword rightParenthesis
>> pManySpaces
)
fromMaybe []
<$> between
(pManySpaces >> pOneKeyword leftParenthesis >> pManySpaces)
(pManySpaces >> pOneKeyword rightParenthesis >> pManySpaces)
(optionMaybe pFunctionArgs)

-- whether the function is decorated by the 'virtual' keyword
pFnDeclVirtual :: Parser FnDecorator
Expand Down
1 change: 1 addition & 0 deletions src/Lib/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pOneKeyword s = T.pack <$> string (T.unpack s)
pReadline :: Parser Text
pReadline = T.pack <$> manyTill anyChar (newline <|> crlf)

-- consume any Unicode space character, and the control characters \t, \n, \r, \f, \v
pManySpaces :: Parser ()
pManySpaces = skipMany space

Expand Down
2 changes: 1 addition & 1 deletion tests/Lib/AST/ContractSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ parseContractSpec = do
let testCase =
( "contract Counter { \
\ uint256 public count;\
\ // Function to get the current count \n \
\ // function to get the current count \n \
\ function get() public view returns (uint256) {\
\ return count;\
\ } \
Expand Down
Loading

0 comments on commit ae57c4f

Please sign in to comment.