Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.ruby.parser/src/com/oracle/truffle/ruby/parser/JRubyParser.java @ 13514:0fbee3eb71f0
Ruby: import project.
author | Chris Seaton <chris.seaton@oracle.com> |
---|---|
date | Mon, 06 Jan 2014 17:12:09 +0000 |
parents | |
children | 497fada09efb |
comparison
equal
deleted
inserted
replaced
13513:64a23ce736a0 | 13514:0fbee3eb71f0 |
---|---|
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 org.jrubyparser.CompatVersion parserVersion = null; | |
38 | |
39 switch (context.getConfiguration().getRubyVersion()) { | |
40 case RUBY_18: | |
41 parserVersion = org.jrubyparser.CompatVersion.RUBY1_8; | |
42 break; | |
43 case RUBY_19: | |
44 parserVersion = org.jrubyparser.CompatVersion.RUBY1_9; | |
45 break; | |
46 case RUBY_20: | |
47 parserVersion = org.jrubyparser.CompatVersion.RUBY2_0; | |
48 break; | |
49 case RUBY_21: | |
50 parserVersion = org.jrubyparser.CompatVersion.RUBY2_0; | |
51 break; | |
52 } | |
53 | |
54 // TODO(cs) should this get a new unique method identifier or not? | |
55 final TranslatorEnvironment environment = new TranslatorEnvironment(context, environmentForFrame(context, parentFrame), this, allocateReturnID(), true, true, new UniqueMethodIdentifier()); | |
56 | |
57 // All parsing contexts have a visibility slot at their top level | |
58 | |
59 environment.addMethodDeclarationSlots(); | |
60 | |
61 final org.jrubyparser.LocalStaticScope staticScope = new org.jrubyparser.LocalStaticScope(null); | |
62 | |
63 if (parentFrame != null) { | |
64 /* | |
65 * Note that jruby-parser will be mistaken about how deep the existing variables are, | |
66 * but that doesn't matter as we look them up ourselves after being told their in some | |
67 * parent scope. | |
68 */ | |
69 | |
70 MaterializedFrame frame = parentFrame; | |
71 | |
72 while (frame != null) { | |
73 for (FrameSlot slot : frame.getFrameDescriptor().getSlots()) { | |
74 if (slot.getIdentifier() instanceof String) { | |
75 final String name = (String) slot.getIdentifier(); | |
76 if (staticScope.exists(name) == -1) { | |
77 staticScope.assign(null, name, null); | |
78 } | |
79 } | |
80 } | |
81 | |
82 frame = frame.getArguments(RubyArguments.class).getDeclarationFrame(); | |
83 } | |
84 } | |
85 | |
86 final org.jrubyparser.parser.ParserConfiguration parserConfiguration = new org.jrubyparser.parser.ParserConfiguration(0, parserVersion, staticScope); | |
87 | |
88 // Parse to the JRuby AST | |
89 | |
90 org.jrubyparser.ast.RootNode node; | |
91 | |
92 try { | |
93 node = (org.jrubyparser.ast.RootNode) parser.parse(source.getName(), new StringReader(source.getCode()), parserConfiguration); | |
94 } catch (UnsupportedOperationException | org.jrubyparser.lexer.SyntaxException e) { | |
95 String message = e.getMessage(); | |
96 | |
97 if (message == null) { | |
98 message = "(no message)"; | |
99 } | |
100 | |
101 throw new RaiseException(new RubyException(context.getCoreLibrary().getSyntaxErrorClass(), message)); | |
102 } | |
103 | |
104 if (context.getConfiguration().getPrintParseTree()) { | |
105 System.err.println(node); | |
106 } | |
107 | |
108 // Translate to Ruby Truffle nodes | |
109 | |
110 final Translator translator; | |
111 | |
112 if (parserContext == RubyParser.ParserContext.MODULE) { | |
113 translator = new ModuleTranslator(context, null, environment, source); | |
114 } else { | |
115 translator = new Translator(context, null, environment, source); | |
116 } | |
117 | |
118 RubyNode truffleNode; | |
119 | |
120 final RubyDebugManager debugManager = context.getDebugManager(); | |
121 try { | |
122 if (debugManager != null) { | |
123 debugManager.notifyStartLoading(source); | |
124 } | |
125 | |
126 if (node.getBody() == null) { | |
127 truffleNode = new NilNode(context, null); | |
128 } else { | |
129 truffleNode = (RubyNode) node.getBody().accept(translator); | |
130 } | |
131 | |
132 // Load flip-flop states | |
133 | |
134 if (environment.getFlipFlopStates().size() > 0) { | |
135 truffleNode = new SequenceNode(context, truffleNode.getSourceSection(), translator.initFlipFlopStates(truffleNode.getSourceSection()), truffleNode); | |
136 } | |
137 | |
138 // Catch next | |
139 | |
140 truffleNode = new CatchNextNode(context, truffleNode.getSourceSection(), truffleNode); | |
141 | |
142 // Catch return | |
143 | |
144 truffleNode = new CatchReturnAsErrorNode(context, truffleNode.getSourceSection(), truffleNode); | |
145 | |
146 // Shell result | |
147 | |
148 if (parserContext == RubyParser.ParserContext.SHELL) { | |
149 truffleNode = new ShellResultNode(context, truffleNode.getSourceSection(), truffleNode); | |
150 } | |
151 | |
152 // Root Node | |
153 | |
154 String indicativeName; | |
155 | |
156 switch (parserContext) { | |
157 case TOP_LEVEL: | |
158 indicativeName = "(main)"; | |
159 break; | |
160 case SHELL: | |
161 indicativeName = "(shell)"; | |
162 break; | |
163 case MODULE: | |
164 indicativeName = "(module)"; | |
165 break; | |
166 default: | |
167 throw new UnsupportedOperationException(); | |
168 } | |
169 | |
170 final RootNode root = new RubyRootNode(truffleNode.getSourceSection(), indicativeName, truffleNode); | |
171 | |
172 // Return the root and the frame descriptor | |
173 | |
174 return new RubyParserResult(root, environment.getFrameDescriptor()); | |
175 } finally { | |
176 if (debugManager != null) { | |
177 debugManager.notifyFinishedLoading(source); | |
178 } | |
179 } | |
180 } | |
181 | |
182 public long allocateReturnID() { | |
183 if (nextReturnID == Long.MAX_VALUE) { | |
184 throw new RuntimeException("Return IDs exhausted"); | |
185 } | |
186 | |
187 final long allocated = nextReturnID; | |
188 nextReturnID++; | |
189 return allocated; | |
190 } | |
191 | |
192 private TranslatorEnvironment environmentForFrame(RubyContext context, MaterializedFrame frame) { | |
193 if (frame == null) { | |
194 return null; | |
195 } else { | |
196 final MaterializedFrame parent = frame.getArguments(RubyArguments.class).getDeclarationFrame(); | |
197 return new TranslatorEnvironment(context, environmentForFrame(context, parent), frame.getFrameDescriptor(), this, allocateReturnID(), true, true, new UniqueMethodIdentifier()); | |
198 } | |
199 } | |
200 | |
201 } |