Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.ruby.parser/src/com/oracle/truffle/ruby/parser/JRubyParser.java @ 13982:b167b1838029
mx eclipseinit: take care of working sets that were edited by hand
author | Michael Haupt <michael.haupt@oracle.com> |
---|---|
date | Thu, 20 Feb 2014 11:14:46 +0100 |
parents | 64fa70319890 |
children | 3f27e57439ed |
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 | |
13735
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
30 private final RubyNodeInstrumenter instrumenter; |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
31 |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
32 public JRubyParser() { |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
33 this(new DefaultRubyNodeInstrumenter()); |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
34 } |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
35 |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
36 public JRubyParser(RubyNodeInstrumenter instrumenter) { |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
37 assert instrumenter != null; |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
38 this.instrumenter = instrumenter; |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
39 } |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
40 |
13514 | 41 @Override |
42 public RubyParserResult parse(RubyContext context, Source source, ParserContext parserContext, MaterializedFrame parentFrame) { | |
13735
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
43 |
13514 | 44 // Set up the JRuby parser |
45 | |
46 final org.jrubyparser.Parser parser = new org.jrubyparser.Parser(); | |
47 | |
48 // TODO(cs) should this get a new unique method identifier or not? | |
49 final TranslatorEnvironment environment = new TranslatorEnvironment(context, environmentForFrame(context, parentFrame), this, allocateReturnID(), true, true, new UniqueMethodIdentifier()); | |
50 | |
51 // All parsing contexts have a visibility slot at their top level | |
52 | |
53 environment.addMethodDeclarationSlots(); | |
54 | |
55 final org.jrubyparser.LocalStaticScope staticScope = new org.jrubyparser.LocalStaticScope(null); | |
56 | |
57 if (parentFrame != null) { | |
58 /* | |
59 * Note that jruby-parser will be mistaken about how deep the existing variables are, | |
60 * but that doesn't matter as we look them up ourselves after being told their in some | |
61 * parent scope. | |
62 */ | |
63 | |
64 MaterializedFrame frame = parentFrame; | |
65 | |
66 while (frame != null) { | |
67 for (FrameSlot slot : frame.getFrameDescriptor().getSlots()) { | |
68 if (slot.getIdentifier() instanceof String) { | |
69 final String name = (String) slot.getIdentifier(); | |
70 if (staticScope.exists(name) == -1) { | |
71 staticScope.assign(null, name, null); | |
72 } | |
73 } | |
74 } | |
75 | |
76 frame = frame.getArguments(RubyArguments.class).getDeclarationFrame(); | |
77 } | |
78 } | |
79 | |
13645
497fada09efb
Ruby: remove versioning.
Chris Seaton <chris.seaton@oracle.com>
parents:
13514
diff
changeset
|
80 final org.jrubyparser.parser.ParserConfiguration parserConfiguration = new org.jrubyparser.parser.ParserConfiguration(0, org.jrubyparser.CompatVersion.RUBY2_0, staticScope); |
13514 | 81 |
82 // Parse to the JRuby AST | |
83 | |
84 org.jrubyparser.ast.RootNode node; | |
85 | |
86 try { | |
87 node = (org.jrubyparser.ast.RootNode) parser.parse(source.getName(), new StringReader(source.getCode()), parserConfiguration); | |
88 } catch (UnsupportedOperationException | org.jrubyparser.lexer.SyntaxException e) { | |
89 String message = e.getMessage(); | |
90 | |
91 if (message == null) { | |
92 message = "(no message)"; | |
93 } | |
94 | |
95 throw new RaiseException(new RubyException(context.getCoreLibrary().getSyntaxErrorClass(), message)); | |
96 } | |
97 | |
98 if (context.getConfiguration().getPrintParseTree()) { | |
99 System.err.println(node); | |
100 } | |
101 | |
102 // Translate to Ruby Truffle nodes | |
103 | |
104 final Translator translator; | |
105 | |
106 if (parserContext == RubyParser.ParserContext.MODULE) { | |
107 translator = new ModuleTranslator(context, null, environment, source); | |
108 } else { | |
109 translator = new Translator(context, null, environment, source); | |
110 } | |
111 | |
112 RubyNode truffleNode; | |
113 | |
13732
fbf448929260
Ruby: remove some prototyping code no longer needed
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13645
diff
changeset
|
114 final DebugManager debugManager = context.getDebugManager(); |
13514 | 115 try { |
116 if (debugManager != null) { | |
117 debugManager.notifyStartLoading(source); | |
118 } | |
119 | |
120 if (node.getBody() == null) { | |
121 truffleNode = new NilNode(context, null); | |
122 } else { | |
123 truffleNode = (RubyNode) node.getBody().accept(translator); | |
124 } | |
125 | |
126 // Load flip-flop states | |
127 | |
128 if (environment.getFlipFlopStates().size() > 0) { | |
129 truffleNode = new SequenceNode(context, truffleNode.getSourceSection(), translator.initFlipFlopStates(truffleNode.getSourceSection()), truffleNode); | |
130 } | |
131 | |
132 // Catch next | |
133 | |
134 truffleNode = new CatchNextNode(context, truffleNode.getSourceSection(), truffleNode); | |
135 | |
136 // Catch return | |
137 | |
138 truffleNode = new CatchReturnAsErrorNode(context, truffleNode.getSourceSection(), truffleNode); | |
139 | |
140 // Shell result | |
141 | |
142 if (parserContext == RubyParser.ParserContext.SHELL) { | |
143 truffleNode = new ShellResultNode(context, truffleNode.getSourceSection(), truffleNode); | |
144 } | |
145 | |
146 // Root Node | |
147 | |
148 String indicativeName; | |
149 | |
150 switch (parserContext) { | |
151 case TOP_LEVEL: | |
152 indicativeName = "(main)"; | |
153 break; | |
154 case SHELL: | |
155 indicativeName = "(shell)"; | |
156 break; | |
157 case MODULE: | |
158 indicativeName = "(module)"; | |
159 break; | |
160 default: | |
161 throw new UnsupportedOperationException(); | |
162 } | |
163 | |
13706
232eb6708943
Ruby: required fixes for moving FrameDescriptor to the RootNode.
Christian Humer <christian.humer@gmail.com>
parents:
13645
diff
changeset
|
164 final RootNode root = new RubyRootNode(truffleNode.getSourceSection(), environment.getFrameDescriptor(), indicativeName, truffleNode); |
13514 | 165 |
166 // 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
|
167 return new RubyParserResult(root); |
13514 | 168 } finally { |
169 if (debugManager != null) { | |
170 debugManager.notifyFinishedLoading(source); | |
171 } | |
172 } | |
173 } | |
174 | |
175 public long allocateReturnID() { | |
176 if (nextReturnID == Long.MAX_VALUE) { | |
177 throw new RuntimeException("Return IDs exhausted"); | |
178 } | |
179 | |
180 final long allocated = nextReturnID; | |
181 nextReturnID++; | |
182 return allocated; | |
183 } | |
184 | |
13735
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
185 public RubyNodeInstrumenter getNodeInstrumenter() { |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
186 return instrumenter; |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
187 } |
2c1c805153e6
Ruby: refactor low level instrumentation services
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
13732
diff
changeset
|
188 |
13514 | 189 private TranslatorEnvironment environmentForFrame(RubyContext context, MaterializedFrame frame) { |
190 if (frame == null) { | |
191 return null; | |
192 } else { | |
193 final MaterializedFrame parent = frame.getArguments(RubyArguments.class).getDeclarationFrame(); | |
194 return new TranslatorEnvironment(context, environmentForFrame(context, parent), frame.getFrameDescriptor(), this, allocateReturnID(), true, true, new UniqueMethodIdentifier()); | |
195 } | |
196 } | |
197 | |
198 } |