forked from prettier-solidity/prettier-plugin-solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslangSolidityParser.ts
More file actions
28 lines (24 loc) · 1 KB
/
slangSolidityParser.ts
File metadata and controls
28 lines (24 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// https://prettier.io/docs/en/plugins.html#parsers
import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast';
import { clearComments, clearOffsets } from './slang-nodes/SlangNode.js';
import { createParser } from './slang-utils/create-parser.js';
import { SourceUnit } from './slang-nodes/SourceUnit.js';
import type { ParserOptions } from 'prettier';
import type { AstNode } from './slang-nodes/types.d.ts';
export default function parse(
text: string,
options: ParserOptions<AstNode>
): AstNode {
const { parser, parseOutput } = createParser(text, options);
// We update the compiler version by the inferred one.
options.compiler = parser.languageVersion;
const parsed = new SourceUnit(
new SlangSourceUnit(parseOutput.tree.asNonterminalNode()),
options
);
// Because of comments being extracted like a Russian doll, the order needs
// to be fixed at the end.
parsed.comments = clearComments().sort((a, b) => a.loc.start - b.loc.start);
clearOffsets();
return parsed;
}