@@ -2,6 +2,7 @@ import { NonterminalKind } from '@nomicfoundation/slang/cst';
22import { Parser } from '@nomicfoundation/slang/parser' ;
33import { LanguageFacts } from '@nomicfoundation/slang/utils' ;
44import { maxSatisfying } from 'semver' ;
5+ import { slangParserId , slangYulParserId } from '../constants.js' ;
56
67import type { ParseOutput } from '@nomicfoundation/slang/parser' ;
78import type { ParserOptions } from 'prettier' ;
@@ -12,13 +13,25 @@ const supportedLength = supportedVersions.length;
1213
1314function parserAndOutput (
1415 text : string ,
15- version : string
16+ version : string ,
17+ { parser : optionsParser } : ParserOptions < AstNode >
1618) : { parser : Parser ; parseOutput : ParseOutput } {
19+ let rootKind ;
20+ switch ( optionsParser ) {
21+ case slangParserId :
22+ rootKind = NonterminalKind . SourceUnit ;
23+ break ;
24+ case slangYulParserId :
25+ rootKind = NonterminalKind . YulBlock ;
26+ break ;
27+ default :
28+ throw new Error (
29+ `Parser '${ optionsParser as string } ' is not supported for Language Inference.`
30+ ) ;
31+ }
32+
1733 const parser = Parser . create ( version ) ;
18- return {
19- parser,
20- parseOutput : parser . parseNonterminal ( NonterminalKind . SourceUnit , text )
21- } ;
34+ return { parser, parseOutput : parser . parseNonterminal ( rootKind , text ) } ;
2235}
2336
2437function createError (
@@ -36,7 +49,7 @@ export function createParser(
3649) : { parser : Parser ; parseOutput : ParseOutput } {
3750 const compiler = maxSatisfying ( supportedVersions , options . compiler ) ;
3851 if ( compiler ) {
39- const result = parserAndOutput ( text , compiler ) ;
52+ const result = parserAndOutput ( text , compiler , options ) ;
4053
4154 if ( ! result . parseOutput . isValid ( ) )
4255 throw createError (
@@ -55,7 +68,8 @@ export function createParser(
5568 if ( inferredLength === 0 || inferredLength === supportedLength ) {
5669 const result = parserAndOutput (
5770 text ,
58- supportedVersions [ supportedLength - 1 ]
71+ supportedVersions [ supportedLength - 1 ] ,
72+ options
5973 ) ;
6074
6175 if ( ! result . parseOutput . isValid ( ) )
@@ -70,7 +84,8 @@ export function createParser(
7084
7185 const result = parserAndOutput (
7286 text ,
73- inferredRanges [ inferredRanges . length - 1 ]
87+ inferredRanges [ inferredLength - 1 ] ,
88+ options
7489 ) ;
7590
7691 if ( ! result . parseOutput . isValid ( ) )
0 commit comments