|
34 | 34 | import com.oracle.truffle.api.source.SourceSection;
|
35 | 35 | import com.oracle.truffle.api.strings.TruffleString;
|
36 | 36 | import org.graalvm.collections.Pair;
|
| 37 | +import org.prism.ParseResult; |
37 | 38 | import org.truffleruby.Layouts;
|
38 | 39 | import org.truffleruby.RubyLanguage;
|
39 | 40 | import org.truffleruby.annotations.CoreMethod;
|
@@ -256,19 +257,48 @@ Object ast(Object code,
|
256 | 257 | @Cached TruffleString.FromJavaStringNode fromJavaStringNode) {
|
257 | 258 | var codeString = new TStringWithEncoding(RubyGuards.asTruffleStringUncached(code),
|
258 | 259 | RubyStringLibrary.getUncached().getEncoding(code));
|
| 260 | + |
| 261 | + var rubySource = createRubySource(codeString); |
| 262 | + var parseResult = getParseResult(getLanguage(), rubySource); |
| 263 | + var ast = parseResult.value; |
| 264 | + |
| 265 | + return createString(fromJavaStringNode, ast.toString(), Encodings.UTF_8); |
| 266 | + } |
| 267 | + |
| 268 | + private static RubySource createRubySource(TStringWithEncoding code) { |
259 | 269 | String name = "<parse_ast>";
|
260 |
| - var source = Source.newBuilder("ruby", new ByteBasedCharSequence(codeString), name).build(); |
261 |
| - var rubySource = new RubySource(source, name); |
| 270 | + var source = Source.newBuilder("ruby", new ByteBasedCharSequence(code), name).build(); |
| 271 | + return new RubySource(source, name); |
| 272 | + } |
262 | 273 |
|
263 |
| - var language = getLanguage(); |
| 274 | + private static ParseResult getParseResult(RubyLanguage language, RubySource rubySource) { |
264 | 275 | var yarpSource = YARPTranslatorDriver.createYARPSource(rubySource.getBytes());
|
265 | 276 | String sourcePath = rubySource.getSourcePath(language).intern();
|
266 | 277 |
|
267 |
| - var parseResult = YARPTranslatorDriver.parseToYARPAST(rubySource, sourcePath, yarpSource, |
| 278 | + return YARPTranslatorDriver.parseToYARPAST(rubySource, sourcePath, yarpSource, |
268 | 279 | Collections.emptyList(), language.options.FROZEN_STRING_LITERALS);
|
269 |
| - var ast = parseResult.value; |
| 280 | + } |
| 281 | + } |
270 | 282 |
|
271 |
| - return createString(fromJavaStringNode, ast.toString(), Encodings.UTF_8); |
| 283 | + @CoreMethod(names = "profile_translator", onSingleton = true, required = 2, lowerFixnum = 2) |
| 284 | + public abstract static class ProfileTranslatorNode extends CoreMethodArrayArgumentsNode { |
| 285 | + @TruffleBoundary |
| 286 | + @Specialization |
| 287 | + Object profileTranslator(Object code, int repeat) { |
| 288 | + var codeString = new TStringWithEncoding(RubyGuards.asTruffleStringUncached(code), |
| 289 | + RubyStringLibrary.getUncached().getEncoding(code)); |
| 290 | + |
| 291 | + var rubySource = ParseASTNode.createRubySource(codeString); |
| 292 | + var parseResult = ParseASTNode.getParseResult(getLanguage(), rubySource); |
| 293 | + |
| 294 | + var translator = new YARPTranslatorDriver(getContext()); |
| 295 | + |
| 296 | + for (int i = 0; i < repeat; i++) { |
| 297 | + translator.parse(rubySource, ParserContext.TOP_LEVEL, null, null, getContext().getRootLexicalScope(), |
| 298 | + this, parseResult); |
| 299 | + } |
| 300 | + |
| 301 | + return nil; |
272 | 302 | }
|
273 | 303 | }
|
274 | 304 |
|
|
0 commit comments