66import org .jruby .Ruby ;
77import org .jruby .RubyArray ;
88import org .jruby .RubyFile ;
9- import org .jruby .RubyIO ;
109import org .jruby .RubyInstanceConfig ;
1110import org .jruby .RubySymbol ;
1211import org .jruby .ext .coverage .CoverageData ;
1918import org .jruby .runtime .builtin .IRubyObject ;
2019import org .jruby .runtime .load .LoadServiceResourceInputStream ;
2120import org .jruby .util .ByteList ;
22- import org .jruby .util .CommonByteLists ;
23- import org .jruby .util .io .ChannelHelper ;
2421import org .jruby .util .io .SeekableByteChannelImpl ;
25- import org .ruby_lang .prism .Nodes .ArgumentsNode ;
26- import org .ruby_lang .prism .Nodes .CallNode ;
27- import org .ruby_lang .prism .Nodes .CallNodeFlags ;
28- import org .ruby_lang .prism .Nodes .GlobalVariableReadNode ;
29- import org .ruby_lang .prism .Nodes .GlobalVariableWriteNode ;
30- import org .ruby_lang .prism .Nodes .Node ;
3122import org .ruby_lang .prism .Nodes .ProgramNode ;
32- import org .ruby_lang .prism .Nodes .StatementsNode ;
33- import org .ruby_lang .prism .Nodes .WhileNode ;
3423import org .ruby_lang .prism .ParsingOptions ;
3524
3625import java .io .ByteArrayInputStream ;
3726import java .io .DataInputStream ;
3827import java .io .IOException ;
3928import java .io .InputStream ;
40- import java .nio .channels .SeekableByteChannel ;
4129import java .util .ArrayList ;
4230import java .util .Arrays ;
4331import java .util .EnumSet ;
44- import java .util .List ;
4532import java .util .stream .IntStream ;
4633
47- import static org .jruby .api .Convert .asSymbol ;
48- import static org .jruby .lexer .LexingCommon .DOLLAR_UNDERSCORE ;
4934import static org .jruby .parser .ParserType .EVAL ;
35+ import static org .jruby .parser .ParserType .INLINE ;
5036import static org .jruby .parser .ParserType .MAIN ;
5137
5238public abstract class ParserPrismBase extends Parser {
@@ -62,7 +48,7 @@ public ParserPrismBase(Ruby runtime) {
6248 public ParseResult parse (String fileName , int lineNumber , ByteList content , DynamicScope existingScope , ParserType type ) {
6349 int sourceLength = content .realSize ();
6450 byte [] source = content .begin () == 0 ? content .unsafeBytes () : content .bytes ();
65- byte [] metadata = generateMetadata (fileName , lineNumber , content .getEncoding (), existingScope );
51+ byte [] metadata = generateMetadata (fileName , lineNumber , content .getEncoding (), existingScope , type );
6652 byte [] serialized = parse (source , sourceLength , metadata );
6753 return parseInternal (fileName , existingScope , source , serialized , type );
6854 }
@@ -155,7 +141,7 @@ private void populateScriptData(byte[] source, Encoding encoding, RubyArray line
155141 protected ParseResult parse (String fileName , int lineNumber , InputStream in , Encoding encoding ,
156142 DynamicScope existingScope , ParserType type ) {
157143 byte [] source = getSourceAsBytes (fileName , in );
158- byte [] metadata = generateMetadata (fileName , lineNumber , encoding , existingScope );
144+ byte [] metadata = generateMetadata (fileName , lineNumber , encoding , existingScope , type );
159145 byte [] serialized = parse (source , source .length , metadata );
160146 return parseInternal (fileName , existingScope , source , serialized , type );
161147 }
@@ -182,13 +168,13 @@ private byte[] loadFully(String fileName, InputStream in) {
182168 }
183169
184170 // lineNumber (0-indexed)
185- private byte [] generateMetadata (String fileName , int lineNumber , Encoding encoding , DynamicScope scope ) {
171+ private byte [] generateMetadata (String fileName , int lineNumber , Encoding encoding , DynamicScope scope , ParserType type ) {
186172 return ParsingOptions .serialize (
187173 fileName .getBytes (),
188174 lineNumber + 1 ,
189175 encoding .getName (),
190176 (runtime .getInstanceConfig ().isFrozenStringLiteral () instanceof Boolean bool && bool ),
191- commandLineFromConfig (runtime .getInstanceConfig ()),
177+ commandLineFromConfig (runtime .getInstanceConfig (), type == MAIN || type == INLINE ),
192178 ParsingOptions .SyntaxVersion .V4_0 ,
193179 false ,
194180 true ,
@@ -222,15 +208,17 @@ private void evalScopesRecursive(StaticScope scope, ArrayList<ParsingOptions.Sco
222208 .toArray (ParsingOptions .Forwarding []::new )));
223209 }
224210
225- private EnumSet <ParsingOptions .CommandLine > commandLineFromConfig (RubyInstanceConfig config ) {
211+ private EnumSet <ParsingOptions .CommandLine > commandLineFromConfig (RubyInstanceConfig config , boolean isMain ) {
226212 var list = new ArrayList <ParsingOptions .CommandLine >();
227-
228- if (config .isSplit ()) list .add (ParsingOptions .CommandLine .A ); // -a
229- if (config .isInlineScript ()) list .add (ParsingOptions .CommandLine .E ); // -e
230- if (config .isProcessLineEnds ()) list .add (ParsingOptions .CommandLine .L ); // -l
231- if (config .isAssumeLoop ()) list .add (ParsingOptions .CommandLine .N ); // -n
232- if (config .isAssumePrinting ()) list .add (ParsingOptions .CommandLine .P ); // -p
233- if (config .isXFlag ()) list .add (ParsingOptions .CommandLine .X ); // -x
213+
214+ if (isMain ) {
215+ if (config .isSplit ()) list .add (ParsingOptions .CommandLine .A ); // -a
216+ if (config .isInlineScript ()) list .add (ParsingOptions .CommandLine .E ); // -e
217+ if (config .isProcessLineEnds ()) list .add (ParsingOptions .CommandLine .L ); // -l
218+ if (config .isAssumeLoop ()) list .add (ParsingOptions .CommandLine .N ); // -n
219+ if (config .isAssumePrinting ()) list .add (ParsingOptions .CommandLine .P ); // -p
220+ if (config .isXFlag ()) list .add (ParsingOptions .CommandLine .X ); // -x
221+ }
234222
235223 return list .isEmpty () ? EnumSet .noneOf (ParsingOptions .CommandLine .class ) : EnumSet .copyOf (list );
236224 }
@@ -305,50 +293,9 @@ public IRubyObject getLineStub(ThreadContext context, ParseResult arg, int lineC
305293 return lineStubs ;
306294 }
307295
308- // It looks weird to see 0 everywhere but these are all virtual instrs and if they raise during execution it will
309- // show it happening on line 1 (which is what it should do).
296+ // Prism generates this for us.
310297 @ Override
311298 public ParseResult addGetsLoop (Ruby runtime , ParseResult result , boolean printing , boolean processLineEndings , boolean split ) {
312- var context = runtime .getCurrentContext ();
313- List <Node > newBody = new ArrayList <>();
314-
315- if (processLineEndings ) {
316- newBody .add (new GlobalVariableWriteNode (0 , 0 , asSymbol (context , CommonByteLists .DOLLAR_BACKSLASH ),
317- new GlobalVariableReadNode (0 , 0 , asSymbol (context , CommonByteLists .DOLLAR_SLASH ))));
318- }
319-
320- GlobalVariableReadNode dollarUnderscore = new GlobalVariableReadNode (0 , 0 , asSymbol (context , DOLLAR_UNDERSCORE ));
321-
322- List <Node > whileBody = new ArrayList <>();
323-
324- if (processLineEndings ) {
325- whileBody .add (new CallNode (0 , 0 , (short ) 0 , dollarUnderscore , asSymbol (context , "chomp!" ), null , null ));
326- }
327- if (split ) {
328- whileBody .add (new GlobalVariableWriteNode (0 , 0 , asSymbol (context , "$F" ),
329- new CallNode (0 , 0 , (short ) 0 , dollarUnderscore , asSymbol (context , "split" ), null , null )));
330- }
331-
332- StatementsNode stmts = ((ProgramNode ) result .getAST ()).statements ;
333- if (stmts != null && stmts .body != null ) whileBody .addAll (Arrays .asList (stmts .body ));
334-
335- ArgumentsNode args = new ArgumentsNode (0 , 0 , (short ) 0 , new Node [] { dollarUnderscore });
336- if (printing ) whileBody .add (new CallNode (0 , 0 , (short ) 0 , null , asSymbol (context , "print" ), args , null ));
337-
338- Node [] nodes = new Node [whileBody .size ()];
339- whileBody .toArray (nodes );
340- StatementsNode statements = new StatementsNode (0 , 0 , nodes );
341-
342- newBody .add (new WhileNode (0 , 0 , (short ) 0 ,
343- new CallNode (0 , 0 , CallNodeFlags .VARIABLE_CALL , null , asSymbol (context , "gets" ), null , null ),
344- statements ));
345-
346- nodes = new Node [newBody .size ()];
347- newBody .toArray (nodes );
348- ProgramNode newRoot = new ProgramNode (0 , 0 , new RubySymbol [] {}, new StatementsNode (0 , 0 , nodes ));
349-
350- ((ParseResultPrism ) result ).setRoot (newRoot );
351-
352299 return result ;
353300 }
354301}
0 commit comments