Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.ruby.parser/src/com/oracle/truffle/ruby/parser/JRubyParser.java @ 13732:fbf448929260
Ruby: remove some prototyping code no longer needed
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Sat, 18 Jan 2014 22:12:42 -0800 |
parents | 497fada09efb |
children | 2c1c805153e6 |
rev | line source |
---|---|
13514 | 1 /* |
13732
fbf448929260
Ruby: remove some prototyping code no longer needed
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13645
diff
changeset
|
2 * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This |
13514 | 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.methods.*; | |
25 | |
26 public class JRubyParser implements RubyParser { | |
27 | |
28 private long nextReturnID = 0; | |
29 | |
30 @Override | |
31 public RubyParserResult parse(RubyContext context, Source source, ParserContext parserContext, MaterializedFrame parentFrame) { | |
32 // Set up the JRuby parser | |
33 | |
34 final org.jrubyparser.Parser parser = new org.jrubyparser.Parser(); | |
35 | |
36 // TODO(cs) should this get a new unique method identifier or not? | |
37 final TranslatorEnvironment environment = new TranslatorEnvironment(context, environmentForFrame(context, parentFrame), this, allocateReturnID(), true, true, new UniqueMethodIdentifier()); | |
38 | |
39 // All parsing contexts have a visibility slot at their top level | |
40 | |
41 environment.addMethodDeclarationSlots(); | |
42 | |
43 final org.jrubyparser.LocalStaticScope staticScope = new org.jrubyparser.LocalStaticScope(null); | |
44 | |
45 if (parentFrame != null) { | |
46 /* | |
47 * Note that jruby-parser will be mistaken about how deep the existing variables are, | |
48 * but that doesn't matter as we look them up ourselves after being told their in some | |
49 * parent scope. | |
50 */ | |
51 | |
52 MaterializedFrame frame = parentFrame; | |
53 | |
54 while (frame != null) { | |
55 for (FrameSlot slot : frame.getFrameDescriptor().getSlots()) { | |
56 if (slot.getIdentifier() instanceof String) { | |
57 final String name = (String) slot.getIdentifier(); | |
58 if (staticScope.exists(name) == -1) { | |
59 staticScope.assign(null, name, null); | |
60 } | |
61 } | |
62 } | |
63 | |
64 frame = frame.getArguments(RubyArguments.class).getDeclarationFrame(); | |
65 } | |
66 } | |
67 | |
13645
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
68 final org.jrubyparser.parser.ParserConfiguration parserConfiguration = new org.jrubyparser.parser.ParserConfiguration(0, org.jrubyparser.CompatVersion.RUBY2_0, staticScope); |
13514 | 69 |
70 // Parse to the JRuby AST | |
71 | |
72 org.jrubyparser.ast.RootNode node; | |
73 | |
74 try { | |
75 node = (org.jrubyparser.ast.RootNode) parser.parse(source.getName(), new StringReader(source.getCode()), parserConfiguration); | |
76 } catch (UnsupportedOperationException | org.jrubyparser.lexer.SyntaxException e) { | |
77 String message = e.getMessage(); | |
78 | |
79 if (message == null) { | |
80 message = "(no message)"; | |
81 } | |
82 | |
83 throw new RaiseException(new RubyException(context.getCoreLibrary().getSyntaxErrorClass(), message)); | |
84 } | |
85 | |
86 if (context.getConfiguration().getPrintParseTree()) { | |
87 System.err.println(node); | |
88 } | |
89 | |
90 // Translate to Ruby Truffle nodes | |
91 | |
92 final Translator translator; | |
93 | |
94 if (parserContext == RubyParser.ParserContext.MODULE) { | |
95 translator = new ModuleTranslator(context, null, environment, source); | |
96 } else { | |
97 translator = new Translator(context, null, environment, source); | |
98 } | |
99 | |
100 RubyNode truffleNode; | |
101 | |
13732
fbf448929260
Ruby: remove some prototyping code no longer needed
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13645
diff
changeset
|
102 final DebugManager debugManager = context.getDebugManager(); |
13514 | 103 try { |
104 if (debugManager != null) { | |
105 debugManager.notifyStartLoading(source); | |
106 } | |
107 | |
108 if (node.getBody() == null) { | |
109 truffleNode = new NilNode(context, null); | |
110 } else { | |
111 truffleNode = (RubyNode) node.getBody().accept(translator); | |
112 } | |
113 | |
114 // Load flip-flop states | |
115 | |
116 if (environment.getFlipFlopStates().size() > 0) { | |
117 truffleNode = new SequenceNode(context, truffleNode.getSourceSection(), translator.initFlipFlopStates(truffleNode.getSourceSection()), truffleNode); | |
118 } | |
119 | |
120 // Catch next | |
121 | |
122 truffleNode = new CatchNextNode(context, truffleNode.getSourceSection(), truffleNode); | |
123 | |
124 // Catch return | |
125 | |
126 truffleNode = new CatchReturnAsErrorNode(context, truffleNode.getSourceSection(), truffleNode); | |
127 | |
128 // Shell result | |
129 | |
130 if (parserContext == RubyParser.ParserContext.SHELL) { | |
131 truffleNode = new ShellResultNode(context, truffleNode.getSourceSection(), truffleNode); | |
132 } | |
133 | |
134 // Root Node | |
135 | |
136 String indicativeName; | |
137 | |
138 switch (parserContext) { | |
139 case TOP_LEVEL: | |
140 indicativeName = "(main)"; | |
141 break; | |
142 case SHELL: | |
143 indicativeName = "(shell)"; | |
144 break; | |
145 case MODULE: | |
146 indicativeName = "(module)"; | |
147 break; | |
148 default: | |
149 throw new UnsupportedOperationException(); | |
150 } | |
151 | |
152 final RootNode root = new RubyRootNode(truffleNode.getSourceSection(), indicativeName, truffleNode); | |
153 | |
154 // Return the root and the frame descriptor | |
155 | |
156 return new RubyParserResult(root, environment.getFrameDescriptor()); | |
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 } |