-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathslangSolidityParser.ts
More file actions
28 lines (24 loc) · 1.03 KB
/
slangSolidityParser.ts
File metadata and controls
28 lines (24 loc) · 1.03 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 { createParser } from './slang-utils/create-parser.js';
import { locStart } from './slang-utils/loc.js';
import { SourceUnit } from './slang-nodes/SourceUnit.js';
import type { ParserOptions } from 'prettier';
import type { Comment, PrintableNode } from './slang-nodes/types.d.ts';
export default function parse(
text: string,
options: ParserOptions<PrintableNode>
): PrintableNode {
const { parser, parseOutput } = createParser(text, options);
// We update the compiler version with the inferred one.
options.compiler = parser.languageVersion;
const comments: Comment[] = [];
const parsed = new SourceUnit(
new SlangSourceUnit(parseOutput.tree.asNonterminalNode()),
{ offsets: new Map<number, number>(), comments },
options
);
// Comments are extracted in nested order; sort them by location.
parsed.comments = comments.sort((a, b) => locStart(a) - locStart(b));
return parsed;
}