Mercurial > hg > graal-jvmci-8
annotate graal/com.oracle.truffle.ruby.parser/src/com/oracle/truffle/ruby/parser/JRubyParser.java @ 13706:232eb6708943
Ruby: required fixes for moving FrameDescriptor to the RootNode.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 20 Jan 2014 13:44:54 +0100 |
parents | 497fada09efb |
children | 64fa70319890 |
rev | line source |
---|---|
13514 | 1 /* |
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This | |
3 * code is released under a tri EPL/GPL/LGPL license. You can use it, | |
4 * redistribute it and/or modify it under the terms of the: | |
5 * | |
6 * Eclipse Public License version 1.0 | |
7 * GNU General Public License version 2 | |
8 * GNU Lesser General Public License version 2.1 | |
9 */ | |
10 package com.oracle.truffle.ruby.parser; | |
11 | |
12 import java.io.*; | |
13 | |
14 import com.oracle.truffle.api.*; | |
15 import com.oracle.truffle.api.frame.*; | |
16 import com.oracle.truffle.api.nodes.*; | |
17 import com.oracle.truffle.ruby.nodes.*; | |
18 import com.oracle.truffle.ruby.nodes.control.*; | |
19 import com.oracle.truffle.ruby.nodes.literal.*; | |
20 import com.oracle.truffle.ruby.nodes.methods.*; | |
21 import com.oracle.truffle.ruby.runtime.*; | |
22 import com.oracle.truffle.ruby.runtime.control.*; | |
23 import com.oracle.truffle.ruby.runtime.core.*; | |
24 import com.oracle.truffle.ruby.runtime.debug.*; | |
25 import com.oracle.truffle.ruby.runtime.methods.*; | |
26 | |
27 public class JRubyParser implements RubyParser { | |
28 | |
29 private long nextReturnID = 0; | |
30 | |
31 @Override | |
32 public RubyParserResult parse(RubyContext context, Source source, ParserContext parserContext, MaterializedFrame parentFrame) { | |
33 // Set up the JRuby parser | |
34 | |
35 final org.jrubyparser.Parser parser = new org.jrubyparser.Parser(); | |
36 | |
37 // TODO(cs) should this get a new unique method identifier or not? | |
38 final TranslatorEnvironment environment = new TranslatorEnvironment(context, environmentForFrame(context, parentFrame), this, allocateReturnID(), true, true, new UniqueMethodIdentifier()); | |
39 | |
40 // All parsing contexts have a visibility slot at their top level | |
41 | |
42 environment.addMethodDeclarationSlots(); | |
43 | |
44 final org.jrubyparser.LocalStaticScope staticScope = new org.jrubyparser.LocalStaticScope(null); | |
45 | |
46 if (parentFrame != null) { | |
47 /* | |
48 * Note that jruby-parser will be mistaken about how deep the existing variables are, | |
49 * but that doesn't matter as we look them up ourselves after being told their in some | |
50 * parent scope. | |
51 */ | |
52 | |
53 MaterializedFrame frame = parentFrame; | |
54 | |
55 while (frame != null) { | |
56 for (FrameSlot slot : frame.getFrameDescriptor().getSlots()) { | |
57 if (slot.getIdentifier() instanceof String) { | |
58 final String name = (String) slot.getIdentifier(); | |
59 if (staticScope.exists(name) == -1) { | |
60 staticScope.assign(null, name, null); | |
61 } | |
62 } | |
63 } | |
64 | |
65 frame = frame.getArguments(RubyArguments.class).getDeclarationFrame(); | |
66 } | |
67 } | |
68 | |
13645
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
69 final org.jrubyparser.parser.ParserConfiguration parserConfiguration = new org.jrubyparser.parser.ParserConfiguration(0, org.jrubyparser.CompatVersion.RUBY2_0, staticScope); |
13514 | 70 |
71 // Parse to the JRuby AST | |
72 | |
73 org.jrubyparser.ast.RootNode node; | |
74 | |
75 try { | |
76 node = (org.jrubyparser.ast.RootNode) parser.parse(source.getName(), new StringReader(source.getCode()), parserConfiguration); | |
77 } catch (UnsupportedOperationException | org.jrubyparser.lexer.SyntaxException e) { | |
78 String message = e.getMessage(); | |
79 | |
80 if (message == null) { | |
81 message = "(no message)"; | |
82 } | |
83 | |
84 throw new RaiseException(new RubyException(context.getCoreLibrary().getSyntaxErrorClass(), message)); | |
85 } | |
86 | |
87 if (context.getConfiguration().getPrintParseTree()) { | |
88 System.err.println(node); | |
89 } | |
90 | |
91 // Translate to Ruby Truffle nodes | |
92 | |
93 final Translator translator; | |
94 | |
95 if (parserContext == RubyParser.ParserContext.MODULE) { | |
96 translator = new ModuleTranslator(context, null, environment, source); | |
97 } else { | |
98 translator = new Translator(context, null, environment, source); | |
99 } | |
100 | |
101 RubyNode truffleNode; | |
102 | |
103 final RubyDebugManager debugManager = context.getDebugManager(); | |
104 try { | |
105 if (debugManager != null) { | |
106 debugManager.notifyStartLoading(source); | |
107 } | |
108 | |
109 if (node.getBody() == null) { | |
110 truffleNode = new NilNode(context, null); | |
111 } else { | |
112 truffleNode = (RubyNode) node.getBody().accept(translator); | |
113 } | |
114 | |
115 // Load flip-flop states | |
116 | |
117 if (environment.getFlipFlopStates().size() > 0) { | |
118 truffleNode = new SequenceNode(context, truffleNode.getSourceSection(), translator.initFlipFlopStates(truffleNode.getSourceSection()), truffleNode); | |
119 } | |
120 | |
121 // Catch next | |
122 | |
123 truffleNode = new CatchNextNode(context, truffleNode.getSourceSection(), truffleNode); | |
124 | |
125 // Catch return | |
126 | |
127 truffleNode = new CatchReturnAsErrorNode(context, truffleNode.getSourceSection(), truffleNode); | |
128 | |
129 // Shell result | |
130 | |
131 if (parserContext == RubyParser.ParserContext.SHELL) { | |
132 truffleNode = new ShellResultNode(context, truffleNode.getSourceSection(), truffleNode); | |
133 } | |
134 | |
135 // Root Node | |
136 | |
137 String indicativeName; | |
138 | |
139 switch (parserContext) { | |
140 case TOP_LEVEL: | |
141 indicativeName = "(main)"; | |
142 break; | |
143 case SHELL: | |
144 indicativeName = "(shell)"; | |
145 break; | |
146 case MODULE: | |
147 indicativeName = "(module)"; | |
148 break; | |
149 default: | |
150 throw new UnsupportedOperationException(); | |
151 } | |
152 | |
13706
232eb6708943
Ruby: required fixes for moving FrameDescriptor to the RootNode.
Christian Humer <christian.humer@gmail.com>
parents:
13645
diff
changeset
|
153 final RootNode root = new RubyRootNode(truffleNode.getSourceSection(), environment.getFrameDescriptor(), indicativeName, truffleNode); |
13514 | 154 |
155 // Return the root and the frame descriptor | |
13706
232eb6708943
Ruby: required fixes for moving FrameDescriptor to the RootNode.
Christian Humer <christian.humer@gmail.com>
parents:
13645
diff
changeset
|
156 return new RubyParserResult(root); |
13514 | 157 } finally { |
158 if (debugManager != null) { | |
159 debugManager.notifyFinishedLoading(source); | |
160 } | |
161 } | |
162 } | |
163 | |
164 public long allocateReturnID() { | |
165 if (nextReturnID == Long.MAX_VALUE) { | |
166 throw new RuntimeException("Return IDs exhausted"); | |
167 } | |
168 | |
169 final long allocated = nextReturnID; | |
170 nextReturnID++; | |
171 return allocated; | |
172 } | |
173 | |
174 private TranslatorEnvironment environmentForFrame(RubyContext context, MaterializedFrame frame) { | |
175 if (frame == null) { | |
176 return null; | |
177 } else { | |
178 final MaterializedFrame parent = frame.getArguments(RubyArguments.class).getDeclarationFrame(); | |
179 return new TranslatorEnvironment(context, environmentForFrame(context, parent), frame.getFrameDescriptor(), this, allocateReturnID(), true, true, new UniqueMethodIdentifier()); | |
180 } | |
181 } | |
182 | |
183 } |