-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathslangYulParser.ts
More file actions
28 lines (24 loc) · 1012 Bytes
/
slangYulParser.ts
File metadata and controls
28 lines (24 loc) · 1012 Bytes
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 { YulBlock as SlangYulBlock } from '@nomicfoundation/slang/ast';
import { createParser } from './slang-utils/create-parser.js';
import { YulBlock } from './slang-nodes/YulBlock.js';
import type { ParserOptions } from 'prettier';
import type { AstNode, Comment } 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 comments: Comment[] = [];
const parsed = new YulBlock(
new SlangYulBlock(parseOutput.tree.asNonterminalNode()),
{ offsets: new Map<number, number>(), comments },
options
);
// Because of comments being extracted like a Russian doll, the order needs
// to be fixed at the end.
parsed.comments = comments.sort((a, b) => a.loc.start - b.loc.start);
return parsed;
}