1111import org .jruby .ext .coverage .CoverageData ;
1212import org .jruby .management .ParserStats ;
1313import org .jruby .parser .Parser ;
14- import org .jruby .parser .ParserManager ;
1514import org .jruby .parser .ParserType ;
1615import org .jruby .parser .StaticScope ;
1716import org .jruby .runtime .DynamicScope ;
2120import org .jruby .util .ByteList ;
2221import org .jruby .util .CommonByteLists ;
2322import org .jruby .util .io .ChannelHelper ;
24- import org .prism .Nodes ;
25- import org .prism .Nodes .*;
23+ import org .prism .Nodes .ArgumentsNode ;
24+ import org .prism .Nodes .CallNode ;
25+ import org .prism .Nodes .CallNodeFlags ;
26+ import org .prism .Nodes .GlobalVariableReadNode ;
27+ import org .prism .Nodes .GlobalVariableWriteNode ;
28+ import org .prism .Nodes .Node ;
29+ import org .prism .Nodes .ProgramNode ;
30+ import org .prism .Nodes .StatementsNode ;
31+ import org .prism .Nodes .WhileNode ;
2632import org .prism .ParsingOptions ;
27- import org .prism .Prism ;
2833
2934import java .io .ByteArrayInputStream ;
3035import java .io .DataInputStream ;
3944import static org .jruby .parser .ParserType .EVAL ;
4045import static org .jruby .parser .ParserType .MAIN ;
4146
42- public class ParserPrism extends Parser {
43- private boolean parserTiming = org .jruby .util .cli .Options .PARSER_SUMMARY .load ();
47+ public abstract class ParserPrismBase extends Parser {
48+ protected boolean parserTiming = org .jruby .util .cli .Options .PARSER_SUMMARY .load ();
4449
45- private final ParserBindingPrism prismLibrary ;
46-
47- public ParserPrism (Ruby runtime , ParserBindingPrism prismLibrary ) {
50+ public ParserPrismBase (Ruby runtime ) {
4851 super (runtime );
49- this .prismLibrary = prismLibrary ;
5052 }
5153
54+ protected abstract byte [] parse (byte [] source , int sourceLength , byte [] metadata );
55+
5256 @ Override
5357 public ParseResult parse (String fileName , int lineNumber , ByteList content , DynamicScope existingScope , ParserType type ) {
5458 int sourceLength = content .realSize ();
@@ -113,10 +117,10 @@ private ParseResult parseInternal(String fileName, DynamicScope blockScope, byte
113117 coverageMode = runtime .getCoverageData ().getMode ();
114118 }
115119
116- ParseResultPrism result = new ParseResultPrism (fileName , source , (Nodes . ProgramNode ) res .value , res .source , encoding , coverageMode );
120+ ParseResultPrism result = new ParseResultPrism (fileName , source , (ProgramNode ) res .value , res .source , encoding , coverageMode );
117121 if (blockScope != null ) {
118122 if (type == MAIN ) { // update TOPLEVEL_BINDNG
119- RubySymbol [] locals = ((Nodes . ProgramNode ) result .getAST ()).locals ;
123+ RubySymbol [] locals = ((ProgramNode ) result .getAST ()).locals ;
120124 for (int i = 0 ; i < locals .length ; i ++) {
121125 blockScope .getStaticScope ().addVariableThisScope (locals [i ].idString ());
122126 }
@@ -174,36 +178,6 @@ private byte[] loadFully(String fileName, InputStream in) {
174178 }
175179 }
176180
177-
178- private byte [] parse (byte [] source , int sourceLength , byte [] metadata ) {
179- if (ParserManager .PARSER_WASM ) return parseChicory (source , sourceLength , metadata );
180-
181- long time = 0 ;
182- if (parserTiming ) time = System .nanoTime ();
183-
184- ParserBindingPrism .Buffer buffer = new ParserBindingPrism .Buffer (jnr .ffi .Runtime .getRuntime (prismLibrary ));
185- prismLibrary .pm_buffer_init (buffer );
186- prismLibrary .pm_serialize_parse (buffer , source , sourceLength , metadata );
187- if (parserTiming ) {
188- ParserStats stats = runtime .getParserManager ().getParserStats ();
189-
190- stats .addPrismTimeCParseSerialize (System .nanoTime () - time );
191- }
192-
193- int length = buffer .length .intValue ();
194- byte [] src = new byte [length ];
195- buffer .value .get ().get (0 , src , 0 , length );
196-
197- return src ;
198- }
199-
200-
201- private byte [] parseChicory (byte [] source , int sourceLength , byte [] metadata ) {
202- try (Prism prism = new Prism ()) {
203- return prism .serialize (metadata , source , sourceLength );
204- }
205- }
206-
207181 // lineNumber (0-indexed)
208182 private byte [] generateMetadata (String fileName , int lineNumber , Encoding encoding , DynamicScope scope , ParserType type ) {
209183 ByteList metadata = new ByteList ();
@@ -327,23 +301,23 @@ public IRubyObject getLineStub(ThreadContext context, ParseResult arg, int lineC
327301 @ Override
328302 public ParseResult addGetsLoop (Ruby runtime , ParseResult result , boolean printing , boolean processLineEndings , boolean split ) {
329303 var context = runtime .getCurrentContext ();
330- List <Nodes . Node > newBody = new ArrayList <>();
304+ List <Node > newBody = new ArrayList <>();
331305
332306 if (processLineEndings ) {
333- newBody .add (new Nodes . GlobalVariableWriteNode (-1 , 0 , 0 , asSymbol (context , CommonByteLists .DOLLAR_BACKSLASH ),
307+ newBody .add (new GlobalVariableWriteNode (-1 , 0 , 0 , asSymbol (context , CommonByteLists .DOLLAR_BACKSLASH ),
334308 new GlobalVariableReadNode (-1 , 0 , 0 , asSymbol (context , CommonByteLists .DOLLAR_SLASH ))));
335309 }
336310
337- Nodes . GlobalVariableReadNode dollarUnderscore = new GlobalVariableReadNode (-1 , 0 , 0 , asSymbol (context , DOLLAR_UNDERSCORE ));
311+ GlobalVariableReadNode dollarUnderscore = new GlobalVariableReadNode (-1 , 0 , 0 , asSymbol (context , DOLLAR_UNDERSCORE ));
338312
339- List <Nodes . Node > whileBody = new ArrayList <>();
313+ List <Node > whileBody = new ArrayList <>();
340314
341315 if (processLineEndings ) {
342316 whileBody .add (new CallNode (-1 , 0 , 0 , (short ) 0 , dollarUnderscore , asSymbol (context , "chomp!" ), null , null ));
343317 }
344318 if (split ) {
345319 whileBody .add (new GlobalVariableWriteNode (-1 , 0 , 0 , asSymbol (context , "$F" ),
346- new Nodes . CallNode (-1 , 0 , 0 , (short ) 0 , dollarUnderscore , asSymbol (context , "split" ), null , null )));
320+ new CallNode (-1 , 0 , 0 , (short ) 0 , dollarUnderscore , asSymbol (context , "split" ), null , null )));
347321 }
348322
349323 StatementsNode stmts = ((ProgramNode ) result .getAST ()).statements ;
@@ -362,7 +336,7 @@ public ParseResult addGetsLoop(Ruby runtime, ParseResult result, boolean printin
362336
363337 nodes = new Node [newBody .size ()];
364338 newBody .toArray (nodes );
365- Nodes . ProgramNode newRoot = new Nodes . ProgramNode (-1 , 0 , 0 , new RubySymbol [] {}, new StatementsNode (-1 , 0 , 0 , nodes ));
339+ ProgramNode newRoot = new ProgramNode (-1 , 0 , 0 , new RubySymbol [] {}, new StatementsNode (-1 , 0 , 0 , nodes ));
366340
367341 ((ParseResultPrism ) result ).setRoot (newRoot );
368342
0 commit comments