Skip to content

Commit 1866c85

Browse files
eregonandrykonchin
authored andcommitted
Save the ParserContext in ParseEnvironment for convenience
1 parent 07aa2ae commit 1866c85

File tree

6 files changed

+17
-22
lines changed

6 files changed

+17
-22
lines changed

src/main/java/org/truffleruby/language/RubyInlineParsingRequestNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public RubyInlineParsingRequestNode(
4747
final RubySource rubySource = new RubySource(source, language.getSourcePath(source), null, true, 0);
4848

4949
// We use the current frame as the lexical scope to parse, but then we may run with a new frame in the future
50-
final TranslatorDriver translator = new TranslatorDriver(context, rubySource);
50+
final TranslatorDriver translator = new TranslatorDriver(context);
5151
final RootCallTarget callTarget = translator.parse(
5252
rubySource,
5353
ParserContext.INLINE,

src/main/java/org/truffleruby/language/RubyParsingRequestNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public RubyParsingRequestNode(RubyLanguage language, RubyContext context, Source
4242

4343
final RubySource rubySource = new RubySource(source, language.getSourcePath(source));
4444

45-
final TranslatorDriver translator = new TranslatorDriver(context, rubySource);
45+
final TranslatorDriver translator = new TranslatorDriver(context);
4646
callTarget = translator.parse(
4747
rubySource,
4848
ParserContext.TOP_LEVEL,

src/main/java/org/truffleruby/language/loader/CodeLoader.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ public RootCallTarget parse(RubySource source,
7777
MaterializedFrame parentFrame,
7878
LexicalScope lexicalScope,
7979
Node currentNode) {
80-
final TranslatorDriver translator = new TranslatorDriver(context, source);
81-
return translator
82-
.parse(source, parserContext, null, parentFrame, lexicalScope, currentNode);
80+
final TranslatorDriver translator = new TranslatorDriver(context);
81+
return translator.parse(source, parserContext, null, parentFrame, lexicalScope, currentNode);
8382
}
8483

8584
@TruffleBoundary
@@ -89,7 +88,7 @@ public RootCallTarget parseWithYARP(Object code,
8988
LexicalScope lexicalScope,
9089
Node currentNode) {
9190
RubySource rubySource = YARPTranslatorDriver.createRubySource(code);
92-
final YARPTranslatorDriver translator = new YARPTranslatorDriver(context, rubySource);
91+
final YARPTranslatorDriver translator = new YARPTranslatorDriver(context);
9392
return translator.parse(rubySource, parserContext, null, parentFrame, lexicalScope, currentNode);
9493
}
9594

src/main/java/org/truffleruby/parser/ParseEnvironment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
public final class ParseEnvironment {
2323

2424
public final Source source;
25+
public final ParserContext parserContext;
2526
private final boolean inCore;
2627
private final boolean coverageEnabled;
2728

@@ -30,8 +31,9 @@ public final class ParseEnvironment {
3031
// Set once after parsing and before translating
3132
public Boolean allowTruffleRubyPrimitives = null;
3233

33-
public ParseEnvironment(RubyLanguage language, RubySource rubySource) {
34+
public ParseEnvironment(RubyLanguage language, RubySource rubySource, ParserContext parserContext) {
3435
this.source = rubySource.getSource();
36+
this.parserContext = parserContext;
3537
this.inCore = RubyLanguage.getPath(source).startsWith(language.corePath);
3638
this.coverageEnabled = RubyLanguage.MIME_TYPE_COVERAGE.equals(rubySource.getSource().getMimeType());
3739
}

src/main/java/org/truffleruby/parser/TranslatorDriver.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,22 @@ public final class TranslatorDriver {
9595
/** May be null, see {@link ParserCache#parse} */
9696
private final RubyContext context;
9797
private final RubyLanguage language;
98-
private final ParseEnvironment parseEnvironment;
98+
private ParseEnvironment parseEnvironment;
9999

100-
public TranslatorDriver(RubyContext context, RubySource rubySource) {
100+
public TranslatorDriver(RubyContext context) {
101101
this.context = context;
102102
this.language = context.getLanguageSlow();
103-
this.parseEnvironment = new ParseEnvironment(language, rubySource);
104103
}
105104

106105
public RootCallTarget parse(RubySource rubySource, ParserContext parserContext, String[] argumentNames,
107106
MaterializedFrame parentFrame, LexicalScope staticLexicalScope, Node currentNode) {
107+
this.parseEnvironment = new ParseEnvironment(language, rubySource, parserContext);
108+
108109
if (language.options.PRISM) {
109-
return new YARPTranslatorDriver(context, rubySource).parse(rubySource, parserContext, argumentNames,
110+
return new YARPTranslatorDriver(context).parse(rubySource, parserContext, argumentNames,
110111
parentFrame, staticLexicalScope, currentNode);
111112
}
112113

113-
if (rubySource.getSource() != parseEnvironment.source) {
114-
throw CompilerDirectives.shouldNotReachHere("TranslatorDriver used with a different Source");
115-
}
116-
117114
if (parserContext.isTopLevel() != (parentFrame == null)) {
118115
throw CompilerDirectives.shouldNotReachHere(
119116
"A frame should be given iff the context is not toplevel: " + parserContext + " " + parentFrame);

src/main/java/org/truffleruby/parser/YARPTranslatorDriver.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,18 @@ public final class YARPTranslatorDriver {
100100
/** May be null, see {@link ParserCache#parse} */
101101
private final RubyContext context;
102102
private final RubyLanguage language;
103-
private final ParseEnvironment parseEnvironment;
103+
private ParseEnvironment parseEnvironment;
104104

105-
public YARPTranslatorDriver(RubyContext context, RubySource rubySource) {
105+
public YARPTranslatorDriver(RubyContext context) {
106106
this.context = context;
107107
this.language = context.getLanguageSlow();
108-
this.parseEnvironment = new ParseEnvironment(language, rubySource);
109108
}
110109

111110
public RootCallTarget parse(RubySource rubySource, ParserContext parserContext, String[] argumentNames,
112111
MaterializedFrame parentFrame, LexicalScope staticLexicalScope, Node currentNode) {
113-
assert rubySource.isEval() == parserContext.isEval();
112+
this.parseEnvironment = new ParseEnvironment(language, rubySource, parserContext);
114113

115-
if (rubySource.getSource() != parseEnvironment.source) {
116-
throw CompilerDirectives.shouldNotReachHere("TranslatorDriver used with a different Source");
117-
}
114+
assert rubySource.isEval() == parserContext.isEval();
118115

119116
if (parserContext.isTopLevel() != (parentFrame == null)) {
120117
throw CompilerDirectives.shouldNotReachHere(

0 commit comments

Comments
 (0)