Skip to content

Commit 0bec1b3

Browse files
committed
Udate remaining logic
- using provided `asNonterminalNode` method instead of manually casting the tree. - using Parser instead of Language - some accessors became methods and some methods became accessors - using isNodeTerminal() since Node.type was removed - cst.children used to return Node[], now it returns Edge[]
1 parent fd25110 commit 0bec1b3

5 files changed

Lines changed: 32 additions & 35 deletions

File tree

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
RequiredOptions,
1717
SupportLanguage
1818
} from 'prettier';
19-
import type { AstNode } from './slang-nodes';
19+
import type { AstNode } from './slang-nodes/index.d.ts';
2020

2121
const slangParserId = 'slang-solidity';
2222
const antlrParserId = 'solidity-parse';

src/slang-utils/infer-language.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { VersionExpressionSets as SlangVersionExpressionSets } from '@nomicfoundation/slang/ast/index.js';
2-
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
3-
import { Language } from '@nomicfoundation/slang/language/index.js';
4-
import { Query } from '@nomicfoundation/slang/query/index.js';
1+
import { VersionExpressionSets as SlangVersionExpressionSets } from '@nomicfoundation/slang/ast';
2+
import { NonterminalKind, Query } from '@nomicfoundation/slang/cst';
3+
import { Parser } from '@nomicfoundation/slang/parser';
54
import {
65
maxSatisfying,
76
minSatisfying,
@@ -12,9 +11,7 @@ import {
1211
} from 'semver';
1312
import { VersionExpressionSets } from '../slang-nodes/VersionExpressionSets.js';
1413

15-
import type { NonterminalNode } from '@nomicfoundation/slang/cst';
16-
17-
const supportedVersions = Language.supportedVersions();
14+
const supportedVersions = Parser.supportedVersions();
1815

1916
const milestoneVersions = Array.from(
2017
supportedVersions.reduce((minorRanges, version) => {
@@ -29,7 +26,7 @@ const milestoneVersions = Array.from(
2926
return versions;
3027
}, []);
3128

32-
export function inferLanguage(text: string): Language {
29+
export function inferLanguage(text: string): Parser {
3330
let inferredRange = '';
3431

3532
for (const version of milestoneVersions) {
@@ -48,12 +45,12 @@ export function inferLanguage(text: string): Language {
4845
const maxSatisfyingVersion = maxSatisfying(supportedVersions, inferredRange);
4946

5047
return maxSatisfyingVersion
51-
? new Language(maxSatisfyingVersion)
52-
: new Language(supportedVersions[supportedVersions.length - 1]);
48+
? Parser.create(maxSatisfyingVersion)
49+
: Parser.create(supportedVersions[supportedVersions.length - 1]);
5350
}
5451

5552
function tryToCollectPragmas(text: string, version: string): string {
56-
const language = new Language(version);
53+
const language = Parser.create(version);
5754
const parseOutput = language.parse(NonterminalKind.SourceUnit, text);
5855
const query = Query.parse(
5956
'[VersionPragma @versionRanges [VersionExpressionSets]]'
@@ -64,7 +61,7 @@ function tryToCollectPragmas(text: string, version: string): string {
6461
let match;
6562
while ((match = matches.next())) {
6663
const versionRange = new SlangVersionExpressionSets(
67-
match.captures.versionRanges[0].node() as NonterminalNode
64+
match.captures.versionRanges[0].node.asNonterminalNode()!
6865
);
6966
ranges.push(
7067
// Replace all comments that could be in the expression with whitespace
@@ -78,7 +75,7 @@ function tryToCollectPragmas(text: string, version: string): string {
7875
// Check if we found pragmas.
7976
if (ranges.length === 0) {
8077
// If we didn't find pragmas but succeeded parsing the source we keep it.
81-
if (parseOutput.isValid) {
78+
if (parseOutput.isValid()) {
8279
return version;
8380
}
8481
// Otherwise we throw.

src/slang-utils/metadata.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { TerminalKind } from '@nomicfoundation/slang/kinds/index.js';
2-
import { NodeType } from '@nomicfoundation/slang/cst/index.js';
1+
import { TerminalKind } from '@nomicfoundation/slang/cst';
32
import { createKindCheckFunction } from './create-kind-check-function.js';
43
import { MultiLineComment } from '../slang-nodes/MultiLineComment.js';
54
import { MultiLineNatSpecComment } from '../slang-nodes/MultiLineNatSpecComment.js';
65
import { SingleLineComment } from '../slang-nodes/SingleLineComment.js';
76
import { SingleLineNatSpecComment } from '../slang-nodes/SingleLineNatSpecComment.js';
87

98
import type { Node } from '@nomicfoundation/slang/cst';
10-
import type { Comment, StrictAstNode } from '../slang-nodes';
11-
import type { Metadata, SlangAstNode } from '../types';
9+
import type { Comment, StrictAstNode } from '../slang-nodes/index.d.ts';
10+
import type { Metadata, SlangAstNode } from '../types.d.ts';
1211

1312
const isCommentOrWhiteSpace = createKindCheckFunction([
1413
TerminalKind.MultiLineComment,
@@ -22,7 +21,7 @@ const isCommentOrWhiteSpace = createKindCheckFunction([
2221
function getLeadingOffset(children: Node[]): number {
2322
let offset = 0;
2423
for (const child of children) {
25-
if (child.type === NodeType.Nonterminal) {
24+
if (child.isNonterminalNode()) {
2625
// The node's content starts when we find the first non-terminal token.
2726
return offset;
2827
} else if (!isCommentOrWhiteSpace(child)) {
@@ -44,14 +43,16 @@ export function getNodeMetadata(
4443
throw new Error("Can't initiate metadata with an undefined initialOffset");
4544
}
4645

47-
const children = ast.cst.children();
46+
const children = ast.cst.children.map((child) => {
47+
return child.node;
48+
});
4849

4950
let offset = initialOffset;
5051

5152
const comments: Comment[] = [];
5253

5354
const offsets = children.reduce((offsetsArray: number[], child) => {
54-
if (child.type === NodeType.Nonterminal) {
55+
if (child.isNonterminalNode()) {
5556
offsetsArray.push(offset);
5657
} else {
5758
switch (child.kind) {

src/slangPrinter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { isBlockComment } from './slang-utils/is-comment.js';
33
import { locEnd, locStart } from './slang-utils/loc.js';
44

55
import type { AstPath, Doc, ParserOptions } from 'prettier';
6-
import type { AstNode, StrictAstNode } from './slang-nodes';
7-
import type { PrintFunction } from './types';
6+
import type { AstNode, StrictAstNode } from './slang-nodes/index.d.ts';
7+
import type { PrintFunction } from './types.d.ts';
88

99
function once<T>(factory: () => T): () => T {
1010
let value: T;

src/slangSolidityParser.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
// https://prettier.io/docs/en/plugins.html#parsers
2-
import { Language } from '@nomicfoundation/slang/language/index.js';
3-
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
4-
import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast/index.js';
2+
import { Parser } from '@nomicfoundation/slang/parser';
3+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
4+
import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast';
55
import { maxSatisfying } from 'semver';
66
import { inferLanguage } from './slang-utils/infer-language.js';
77
import { printWarning } from './slang-utils/print-warning.js';
88
import { SourceUnit } from './slang-nodes/SourceUnit.js';
99

10-
import type { NonterminalNode } from '@nomicfoundation/slang/cst';
11-
import type { Parser, ParserOptions } from 'prettier';
12-
import type { AstNode } from './slang-nodes/index.js';
10+
import type { Parser as PrettierParser, ParserOptions } from 'prettier';
11+
import type { AstNode } from './slang-nodes/index.d.ts';
1312

14-
const supportedVersions = Language.supportedVersions();
13+
const supportedVersions = Parser.supportedVersions();
1514

1615
export default function parse(
1716
text: string,
18-
_parsers: Parser[] | ParserOptions<AstNode>,
17+
_parsers: PrettierParser[] | ParserOptions<AstNode>,
1918
options = _parsers as ParserOptions<AstNode>
2019
): AstNode {
2120
const compiler = maxSatisfying(supportedVersions, options.compiler);
2221

2322
const language =
2423
compiler && supportedVersions.includes(compiler)
25-
? new Language(compiler)
24+
? Parser.create(compiler)
2625
: inferLanguage(text);
2726

2827
const parseOutput = language.parse(NonterminalKind.SourceUnit, text);
@@ -32,14 +31,14 @@ export default function parse(
3231
: `Inferred version ${language.version} based on the pragma statements in the code.`
3332
);
3433

35-
if (parseOutput.isValid) {
34+
if (parseOutput.isValid()) {
3635
// We update the compiler version by the inferred one.
3736
options.compiler = language.version;
3837
return new SourceUnit(
39-
new SlangSourceUnit(parseOutput.tree() as NonterminalNode),
38+
new SlangSourceUnit(parseOutput.tree.asNonterminalNode()!),
4039
0,
4140
options
4241
);
4342
}
44-
throw new Error(parseOutput.errors()[0].message());
43+
throw new Error(parseOutput.errors[0].message);
4544
}

0 commit comments

Comments
 (0)