Skip to content

Commit 7977cfc

Browse files
committed
refactor createParser
1 parent 32fca7d commit 7977cfc

1 file changed

Lines changed: 22 additions & 47 deletions

File tree

src/slang-utils/create-parser.ts

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@ import type { PrintableNode } from '../slang-nodes/types.d.ts';
99

1010
const supportedVersions = LanguageFacts.allVersions();
1111
const supportedLength = supportedVersions.length;
12+
const latestSupportedVersion = LanguageFacts.latestVersion();
1213

1314
function parserAndOutput(
1415
text: string,
15-
version: string
16+
version: string,
17+
reason: string
1618
): { parser: Parser; parseOutput: ParseOutput } {
1719
const parser = Parser.create(version);
18-
return {
19-
parser,
20-
parseOutput: parser.parseNonterminal(NonterminalKind.SourceUnit, text)
21-
};
22-
}
20+
const parseOutput = parser.parseNonterminal(NonterminalKind.SourceUnit, text);
2321

24-
function createError(
25-
result: { parseOutput: ParseOutput },
26-
reason: string
27-
): Error {
28-
return new Error(
29-
`We encountered the following syntax error:\n\n\t${result.parseOutput.errors()[0].message}\n\n${reason}`
30-
);
22+
if (!parseOutput.isValid())
23+
throw new Error(
24+
`We encountered the following syntax error:\n\n\t${parseOutput.errors()[0].message}\n\n${reason}`
25+
);
26+
27+
return { parser, parseOutput };
3128
}
3229

3330
export function createParser(
@@ -36,50 +33,28 @@ export function createParser(
3633
): { parser: Parser; parseOutput: ParseOutput } {
3734
const compiler = maxSatisfying(supportedVersions, options.compiler);
3835
if (compiler) {
39-
const result = parserAndOutput(text, compiler);
40-
41-
if (!result.parseOutput.isValid())
42-
throw createError(
43-
result,
44-
`Based on the compiler option provided, we inferred your code to be using Solidity version ${
45-
result.parser.languageVersion
46-
}. If you would like to change that, specify a different version in your \`.prettierrc\` file.`
47-
);
48-
49-
return result;
36+
return parserAndOutput(
37+
text,
38+
compiler,
39+
`Based on the compiler option provided, we inferred your code to be using Solidity version ${compiler}. If you would like to change that, specify a different version in your \`.prettierrc\` file.`
40+
);
5041
}
5142

5243
const inferredRanges: string[] = LanguageFacts.inferLanguageVersions(text);
5344
const inferredLength = inferredRanges.length;
5445

5546
if (inferredLength === 0 || inferredLength === supportedLength) {
56-
const result = parserAndOutput(
47+
return parserAndOutput(
5748
text,
58-
supportedVersions[supportedLength - 1]
49+
latestSupportedVersion,
50+
`We couldn't infer a Solidity version based on the pragma statements in your code so we defaulted to ${latestSupportedVersion}. You might be attempting to use a syntax not yet supported by Slang or you might want to specify a version in your \`.prettierrc\` file.`
5951
);
60-
61-
if (!result.parseOutput.isValid())
62-
throw createError(
63-
result,
64-
`We couldn't infer a Solidity version based on the pragma statements in your code so we defaulted to ${
65-
result.parser.languageVersion
66-
}. You might be attempting to use a syntax not yet supported by Slang or you might want to specify a version in your \`.prettierrc\` file.`
67-
);
68-
return result;
6952
}
7053

71-
const result = parserAndOutput(
54+
const inferredVersion = inferredRanges[inferredLength - 1];
55+
return parserAndOutput(
7256
text,
73-
inferredRanges[inferredRanges.length - 1]
57+
inferredVersion,
58+
`Based on the pragma statements, we inferred your code to be using Solidity version ${inferredVersion}. If you would like to change that, update the pragmas in your source file, or specify a version in your \`.prettierrc\` file.`
7459
);
75-
76-
if (!result.parseOutput.isValid())
77-
throw createError(
78-
result,
79-
`Based on the pragma statements, we inferred your code to be using Solidity version ${
80-
result.parser.languageVersion
81-
}. If you would like to change that, update the pragmas in your source file, or specify a version in your \`.prettierrc\` file.`
82-
);
83-
84-
return result;
8560
}

0 commit comments

Comments
 (0)