Skip to content

Commit c9c8635

Browse files
committed
Smaller grammar tweaks and optimization
1 parent 4b024cb commit c9c8635

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

Diff for: src/types/typeStrings/typeString_parser_header.ts

+35-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @ts-nocheck
33
import {
44
ASTNode,
5+
ASTNodeConstructor,
56
ContractDefinition,
67
DataLocation,
78
EnumDefinition,
@@ -34,30 +35,40 @@ import {
3435
} from "../ast";
3536

3637
function getFunctionAttributes(
37-
rawDecorators: string[]
38+
decorators: string[]
3839
): [FunctionVisibility, FunctionStateMutability] {
3940
let visiblity: FunctionVisibility | undefined;
4041
let mutability: FunctionStateMutability | undefined;
4142

42-
for (const decorator of rawDecorators) {
43-
if (["external", "internal"].includes(decorator)) {
43+
const visiblities = new Set<string>([
44+
FunctionVisibility.Internal,
45+
FunctionVisibility.External
46+
]);
47+
48+
const mutabilities = new Set<string>([
49+
FunctionStateMutability.Pure,
50+
FunctionStateMutability.View,
51+
FunctionStateMutability.NonPayable,
52+
FunctionStateMutability.Payable
53+
]);
54+
55+
for (const decorator of decorators) {
56+
if (visiblities.has(decorator)) {
4457
if (visiblity !== undefined) {
4558
throw new Error(
4659
`Multiple visiblity decorators specified: ${decorator} conflicts with ${visiblity}`
4760
);
4861
}
4962

50-
visiblity = decorator;
51-
}
52-
53-
if (["pure", "view", "nonpayable", "payable"].includes(decorator)) {
63+
visiblity = decorator as FunctionVisibility;
64+
} else if (mutabilities.has(decorator)) {
5465
if (mutability !== undefined) {
5566
throw new Error(
5667
`Multiple mutability decorators specified: ${decorator} conflicts with ${mutability}`
5768
);
5869
}
5970

60-
mutability = decorator;
71+
mutability = decorator as FunctionStateMutability;
6172
}
6273
}
6374

@@ -75,29 +86,34 @@ function getFunctionAttributes(
7586
}
7687

7788
/**
78-
* Return the `TypeNode` corresponding to `arg`, where `arg` is a node with a type string (`Expression` or `VariableDeclaration`).
79-
* The function uses a parser to parse the type string, while resolving and user-defined type refernces in the context of `arg`.
89+
* Return the `TypeNode` corresponding to `node`, where `node` is an AST node
90+
* with a type string (`Expression` or `VariableDeclaration`).
91+
*
92+
* The function uses a parser to process the type string,
93+
* while resolving and user-defined type references in the context of `node`.
8094
*
81-
* @param arg - either a type string, or a node with a type string (`Expression` or `VariableDeclaration`)
82-
* @param version - compiler version to be used. Useful as resolution rules changed betwee 0.4.x and 0.5.x
83-
* @returns
95+
* @param arg - an AST node with a type string (`Expression` or `VariableDeclaration`)
96+
* @param version - compiler version to be used. Useful as resolution rules changed between 0.4.x and 0.5.x.
8497
*/
8598
export function getNodeType(node: Expression | VariableDeclaration, version: string): TypeNode {
8699
return parse(node.typeString, { ctx: node, version }) as TypeNode;
87100
}
88101

89102
/**
90-
* Return the `TypeNode` corresponding to `arg`, where `arg` is either a raw type string, or a node with a type string (`Expression` or `VariableDeclaration`).
91-
* The function uses a parser to parse the type string, while resolving and user-defined type refernces in the context of `ctx`.
103+
* Return the `TypeNode` corresponding to `arg`, where `arg` is either a raw type string,
104+
* or an AST node with a type string (`Expression` or `VariableDeclaration`).
105+
*
106+
* The function uses a parser to process the type string,
107+
* while resolving and user-defined type references in the context of `ctx`.
92108
*
93109
* @param arg - either a type string, or a node with a type string (`Expression` or `VariableDeclaration`)
94-
* @param version - compiler version to be used. Useful as resolution rules changed betwee 0.4.x and 0.5.x
110+
* @param version - compiler version to be used. Useful as resolution rules changed between 0.4.x and 0.5.x.
95111
* @param ctx - `ASTNode` representing the context in which a type string is to be parsed
96-
* @returns
97112
*/
98113
export function getNodeTypeInCtx(arg: Expression | VariableDeclaration | string, version: string, ctx: ASTNode): TypeNode {
99-
const typeString = typeof arg === 'string' ? arg : arg.typeString;
100-
return parse( typeString, { ctx, version }) as TypeNode;
114+
const typeString = typeof arg === "string" ? arg : arg.typeString;
115+
116+
return parse(typeString, { ctx, version }) as TypeNode;
101117
}
102118

103119
function makeUserDefinedType<T extends ASTNode>(

0 commit comments

Comments
 (0)