-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathParserGenerator.java
More file actions
220 lines (197 loc) · 8.58 KB
/
ParserGenerator.java
File metadata and controls
220 lines (197 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*******************************************************************************
* Copyright (c) 2009-2019 CWI
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* * Jurgen J. Vinju - Jurgen.Vinju@cwi.nl - CWI
* * Anya Helene Bagge - anya@ii.uib.no (Univ. Bergen)
* * Arnold Lankamp - Arnold.Lankamp@cwi.nl
*******************************************************************************/
package org.rascalmpl.parser;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collections;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.exceptions.ImplementationError;
import org.rascalmpl.exceptions.Throw;
import org.rascalmpl.interpreter.Configuration;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.interpreter.utils.JavaBridge;
import org.rascalmpl.parser.gtd.IGTD;
import org.rascalmpl.runtime.ModuleStore;
import org.rascalmpl.runtime.RascalExecutionContext;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.parsetrees.ITree;
import rascal.lang.rascal.grammar.$ParserGenerator;
import rascal.lang.rascal.grammar.$ConcreteSyntax;
import rascal.lang.rascal.grammar.definition.$Modules;
import rascal.lang.rascal.grammar.definition.$Priorities;
import rascal.lang.rascal.grammar.definition.$Regular;
import rascal.lang.rascal.grammar.definition.$Keywords;
import rascal.lang.rascal.grammar.definition.$Literals;
import rascal.lang.rascal.grammar.definition.$Symbols;
import rascal.lang.rascal.grammar.definition.$Parameters;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IInteger;
import io.usethesource.vallang.IMap;
import io.usethesource.vallang.ISet;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValueFactory;
public class ParserGenerator {
private final $ParserGenerator pgen;
private final $ConcreteSyntax concreteSyntax;
private final $Modules modules;
private final $Priorities priorities;
private final $Regular regular;
private final $Keywords keywords;
private final $Literals literals;
private final $Parameters parameters;
private final $Symbols symbols;
private final JavaBridge bridge;
private final IValueFactory vf;
private static final String packageName = "org.rascalmpl.java.parser.object";
private static final boolean debug = false;
public ParserGenerator(IRascalMonitor monitor, PrintWriter out, IValueFactory factory, Configuration config) {
var rex = new RascalExecutionContext(null, out, out, null, null, $ParserGenerator.class);
ModuleStore ms = rex.getModuleStore();
pgen = new $ParserGenerator(rex);
concreteSyntax = ms.getModule($ConcreteSyntax.class);
modules = ms.getModule($Modules.class);
priorities = ms.getModule($Priorities.class);
symbols = ms.getModule($Symbols.class);
regular = ms.getModule($Regular.class);
literals = ms.getModule($Literals.class);
parameters = ms.getModule($Parameters.class);
keywords = ms.getModule($Keywords.class);
this.bridge = new JavaBridge(Collections.singletonList(Evaluator.class.getClassLoader()), factory, config);
this.vf = factory;
}
private void debugOutput(Object thing, String file) {
if (debug) {
String classString = thing.toString();
FileOutputStream s = null;
try {
System.err.println("Writing parser to " + file);
s = new FileOutputStream(file);
s.write(classString.getBytes());
s.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public IConstructor getGrammarFromModules(IRascalMonitor monitor, String main, IMap mods) {
return modules.modules2grammar(vf.string(main), mods);
}
public IConstructor getExpandedGrammar(IRascalMonitor monitor, String main, IMap definition) {
IConstructor g = getGrammarFromModules(monitor, main, definition);
g = keywords.expandKeywords(g);
g = (IConstructor) regular.makeRegularStubs(g); // why is the return type IValue here?
g = regular.expandRegularSymbols(g);
g = parameters.expandParameterizedSymbols(g);
g = literals.literals(g);
return g;
}
public ISet getNestingRestrictions(IRascalMonitor monitor, IConstructor g) {
return priorities.doNotNest(g);
}
/**
* Produces the name generated by the parser generator for a parse method for the given symbol
*/
public String getParserMethodName(IConstructor symbol) {
return pgen.getParserMethodName(symbol).getValue();
}
/**
* Converts the parse tree of a symbol to a UPTR symbol
*/
public IConstructor symbolTreeToSymbol(IConstructor symbol) {
return symbols.sym2symbol(symbol);
}
/**
* Generate a parser from a Rascal syntax definition (a set of production rules).
*
* @param monitor a progress monitor; this method will contribute 100 work units
* @param loc a location for error reporting
* @param name the name of the parser for use in code generation and for later reference
* @param definition a map of syntax definitions (which are imports in the Rascal grammar)
* @return A parser class, ready for instantiation
*/
public Class<IGTD<IConstructor, ITree, ISourceLocation>> getNewParser(IRascalMonitor monitor, ISourceLocation loc, String name, IMap definition) {
try {
IConstructor grammar = IRascalValueFactory.getInstance().grammar(definition);
debugOutput(grammar, System.getProperty("java.io.tmpdir") + "/grammar.trm");
return getNewParser(monitor, loc, name, grammar);
}
catch (ClassCastException e) {
throw new ImplementationError("parser generator:" + e.getMessage(), e);
}
catch (Throw e) {
throw new ImplementationError("parser generator: " + e.getMessage() + e.getTrace());
}
}
/**
* Generate a parser from a Rascal grammar.
*
* @param monitor a progress monitor; this method will contribute 100 work units
* @param loc a location for error reporting
* @param name the name of the parser for use in code generation and for later reference
* @param grammar a grammar
* @return A parser class, ready for instantiation
*/
public Class<IGTD<IConstructor, ITree, ISourceLocation>> getNewParser(IRascalMonitor monitor, ISourceLocation loc, String name, IConstructor grammar) {
try {
String normName = name.replaceAll("::", "_").replaceAll("\\\\", "_");
IString classString = pgen.newGenerate(vf.string(packageName), vf.string(normName), grammar);
debugOutput(classString, System.getProperty("java.io.tmpdir") + "/parser.java");
return bridge.compileJava(loc, packageName + "." + normName, classString.getValue());
} catch (ClassCastException e) {
throw new ImplementationError("parser generator:" + e.getMessage(), e);
} catch (Throw e) {
throw new ImplementationError("parser generator: " + e.getMessage() + e.getTrace());
}
}
/**
* Generate a parser from a Rascal grammar and write it to disk
*
* @param monitor a progress monitor; this method will contribute 100 work units
* @param loc a location for error reporting
* @param name the name of the parser for use in code generation and for later reference
* @param grammar a grammar
* @return A parser class, ready for instantiation
* @throws IOException
*/
public void writeNewParser(IRascalMonitor monitor, ISourceLocation loc, String name, IMap definition, ISourceLocation target) throws IOException {
try (OutputStream out = URIResolverRegistry.getInstance().getOutputStream(target, false)) {
String normName = name.replaceAll("::", "_").replaceAll("\\\\", "_");
IConstructor grammar = IRascalValueFactory.getInstance().grammar(definition);
IString classString = pgen.newGenerate(vf.string(packageName), vf.string(normName), grammar);
debugOutput(classString, System.getProperty("java.io.tmpdir") + "/parser.java");
bridge.compileJava(loc, packageName + "." + normName, classString.getValue(), out);
} catch (ClassCastException e) {
throw new ImplementationError("parser generator:" + e.getMessage(), e);
} catch (Throw e) {
throw new ImplementationError("parser generator: " + e.getMessage() + e.getTrace());
}
}
public IString createHole(IConstructor part, IInteger size) {
return concreteSyntax.createHole(part, size);
}
}