@Override protected void addForLineNumber(int $line, java.util.List<AbstractAST> $result) { if (getLocation().getBeginLine() == $line) { $result.add(this); } ISourceLocation $l; $l = constant.getLocation(); if ($l.hasLineColumn() && $l.getBeginLine() <= $line && $l.getEndLine() >= $line) { constant.addForLineNumber($line, $result); } if ($l.getBeginLine() > $line) { return; } }
private IValue read(Type resultType, ISourceLocation loc, IMap options, IEvaluatorContext ctx) { setOptions(options); Reader reader = null; try { reader = ctx.getResolverRegistry().getCharacterReader(loc.getURI()); List<Record> records = loadRecords(reader); if (resultType == null) { resultType = inferType(records, ctx); ctx.getStdOut().println("readCSV inferred the relation type: " + resultType); ctx.getStdOut().flush(); } else if (header) { records.remove(0); } return buildCollection(resultType, records, ctx); } catch (IOException e) { throw RuntimeExceptionFactory.io( values.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace()); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw RuntimeExceptionFactory.io( values.string(e.getMessage()), ctx.getCurrentAST(), ctx.getStackTrace()); } } } }
@Override public InputStream getInputStream(ISourceLocation uri) throws IOException { String contents = modules.get(uri.getPath()); if (contents != null) { return new ByteArrayInputStream(contents.getBytes()); } return null; }
@Override public String getMessage() { if (loc != null) { URI url = loc.getURI(); return (url.getScheme().equals("file") ? (url.getAuthority() + url.getPath()) : url) + ":" + loc.getBeginLine() + "," + loc.getBeginColumn() + ": " + super.getMessage(); } // TODO remove once all errors have locations return super.getMessage(); }
public void report() { List<Map.Entry<AbstractAST, Count>> sortedData = sortData(); int maxURL = 1; long nTicks = 0; for (Map.Entry<AbstractAST, Count> e : sortedData) { URI url = e.getKey().getLocation().getURI(); int sz = url.toString().length(); if (sz > maxURL) maxURL = sz; nTicks += e.getValue().getTicks(); } PrintWriter out = eval.getStdOut(); String URLFormat = "%" + maxURL + "s"; out.printf( "PROFILE: %d data points, %d ticks, tick = %d milliSecs\n", data.size(), nTicks, resolution); out.printf(URLFormat + "%11s%8s%9s %s\n", " Source File", "Lines", "Ticks", "%", "Source"); for (Map.Entry<AbstractAST, Count> e : sortedData) { ISourceLocation L = e.getKey().getLocation(); String uri = L.getURI().toString(); String filePrefix = "file://"; if (uri.startsWith(filePrefix)) uri = uri.substring(filePrefix.length()); int bgn = L.getBeginLine(); int end = L.getEndLine(); String range = (end == bgn) ? Integer.toString(bgn) : bgn + ".." + end; int ticks = e.getValue().getTicks(); double perc = (ticks * 100.0) / nTicks; String source = String.format( "%-30.30s", e.getKey().toString().replaceFirst("^[\\s]+", "").replaceAll("[\\s]+", " ")); out.printf(URLFormat + "%11s%8d%8.1f%% %s\n", uri, range, ticks, perc, source); } // Make sure that our output is seen: out.flush(); }
@Override protected String getPath(ISourceLocation uri) { String path = uri.getPath(); if (path == null || path.isEmpty() || path.equals("/")) { return ""; } while (path.startsWith("/")) { path = path.substring(1); } return path; }
public static IConstructor loadForRascal(IValueFactory vf, IString src, ISourceLocation loc) { IGTD<IConstructor, IConstructor, ISourceLocation> gtd = new ClojureParser(); IConstructor file = (IConstructor) gtd.parse( START_SORT, loc.getURI(), src.getValue().toCharArray(), new DefaultNodeFlattener<IConstructor, IConstructor, ISourceLocation>(), new UPTRNodeFactory()); String path = loc.getURI().getPath(); IConstructor[] fileRef = new IConstructor[] {file}; // ignore the clojure result ISetWriter errors = vf.setWriter(Message.Message); new UPTRCompiler(vf, errors).loadPT(fileRef, path, new File(path).getName()); // return the updated parse tree. return fileRef[0].setAnnotation("messages", errors.done()); }
@Override protected IStatus run(IProgressMonitor monitor) { monitor.beginTask("Parsing", 105); try { handler.clearMessages(); IGTD<IConstructor, IConstructor, ISourceLocation> gtd = new ClojureParser(); parseTree = (IConstructor) gtd.parse( START_SORT, URI.create(path.toString()), input.toCharArray(), new DefaultNodeFlattener<IConstructor, IConstructor, ISourceLocation>(), new UPTRNodeFactory()); } catch (ParseError pe) { int offset = pe.getOffset(); if (offset == input.length()) --offset; handler.handleSimpleMessage( "parse error", offset, offset + pe.getLength(), pe.getBeginColumn(), pe.getEndColumn(), pe.getBeginLine() + 1, pe.getEndLine() + 1); } catch (Throw e) { IValue exc = e.getException(); if (exc.getType() == RuntimeExceptionFactory.Exception) { if (((IConstructor) exc).getConstructorType() == RuntimeExceptionFactory.ParseError) { ISourceLocation loc = (ISourceLocation) ((IConstructor) e.getException()).get(0); handler.handleSimpleMessage( "parse error: " + loc, loc.getOffset(), loc.getOffset() + loc.getLength(), loc.getBeginColumn(), loc.getEndColumn(), loc.getBeginLine(), loc.getEndLine()); } else { Activator.getInstance().logException(e.getMessage(), e); } } } catch (Throwable e) { Activator.getInstance().logException("parsing failed: " + e.getMessage(), e); } finally { monitor.done(); } return Status.OK_STATUS; }
private JarEntry getEntry(JarInputStream stream, ISourceLocation subPath) throws IOException { int pos = ((JarInputStreamTreeHierachy) getFileHierchyCache(new File(subPath.getPath()))) .getPosition(getPath(subPath)); if (pos != -1) { JarEntry entry; do { entry = stream.getNextJarEntry(); } while (pos-- > 0); return entry; } return null; }
public void writeTextJSonFile(ISourceLocation loc, IValue value, IEvaluatorContext ctx) { OutputStream out = null; try { out = ctx.getResolverRegistry().getOutputStream(loc.getURI(), false); new JSonWriter().write(value, new OutputStreamWriter(out, "UTF8")); } catch (IOException e) { throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); } finally { if (out != null) { try { out.close(); } catch (IOException ioex) { throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null); } } } }
public IValue readTextJSonFile(IValue type, ISourceLocation loc, IEvaluatorContext ctx) { // TypeStore store = new TypeStore(); TypeStore store = ctx.getCurrentEnvt().getStore(); Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store); Reader read = null; try { read = ctx.getResolverRegistry().getCharacterReader(loc.getURI()); return new JSonReader().read(values, store, start, read); } catch (IOException e) { throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); } finally { if (read != null) { try { read.close(); } catch (IOException ioex) { throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null); } } } }
public IValue readTextATermFile(IValue type, ISourceLocation loc, IEvaluatorContext ctx) { TypeStore store = new TypeStore(); Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store); InputStream in = null; try { in = ctx.getResolverRegistry().getInputStream(loc.getURI()); return new ATermReader().read(values, store, start, in); } catch (IOException e) { throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); } finally { if (in != null) { try { in.close(); } catch (IOException ioex) { throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null); } } } }
public void generateTypedInterfaceInternal( IString tag, ISourceLocation uriLoc, IEvaluatorContext ctx) { IResource handler = Resource.getResourceHandler(uriLoc); String tagStr = tag.getValue(); Type t = handler.getResourceType(ctx, uriLoc); PrintWriter currentOutStream = ctx.getStdOut(); // Declare an alias to the type of the resource TypeStore ts = ctx.getCurrentEnvt().getStore(); Type alias2t = TypeFactory.getInstance().aliasType(ts, tagStr + "Type", t); currentOutStream.println( "Generated type alias " + alias2t.toString() + ": " + alias2t.getAliased().toString()); currentOutStream.flush(); // Declare a function that just uses the given URI. This way, if we provide // the complete URI up front, we don't need to keep providing it later. StringBuilder sb = new StringBuilder(); sb.append("public ").append(tagStr).append("Type ").append(tagStr).append("() { "); sb.append(" return getTypedResource(") .append(uriLoc.toString()) .append(",#") .append(tagStr) .append("Type); }"); IConstructor declTree = ctx.getEvaluator() .parseCommand( ctx.getEvaluator().getMonitor(), sb.toString(), ctx.getCurrentAST().getLocation().getURI()); Command cmd = ctx.getEvaluator().getBuilder().buildCommand(declTree); Environment env = ctx.getCurrentEnvt(); ctx.setCurrentEnvt(env.getRoot()); Result<IValue> fun0 = ctx.getEvaluator().eval(ctx.getEvaluator().getMonitor(), cmd); ctx.unwind(env); currentOutStream.println("Generated function " + fun0.toString()); currentOutStream.flush(); }
@Override public boolean exists(ISourceLocation uri) { return modules.containsKey(uri.getPath()); }
@Override protected File getJar(ISourceLocation uri) throws IOException { return new File(jarURI.getPath()); }
public void write(IValue rel, ISourceLocation loc, IEvaluatorContext ctx) { OutputStream out = null; Type paramType = ctx.getCurrentEnvt().getTypeBindings().get(types.parameterType("T")); if (!paramType.isRelation() && !paramType.isListRelation()) { throw RuntimeExceptionFactory.illegalTypeArgument( "A relation type is required instead of " + paramType, ctx.getCurrentAST(), ctx.getStackTrace()); } try { boolean isListRel = rel instanceof IList; out = ctx.getResolverRegistry().getOutputStream(loc.getURI(), false); ISet irel = null; IList lrel = null; if (isListRel) { lrel = (IList) rel; } else { irel = (ISet) rel; } int nfields = isListRel ? lrel.asRelation().arity() : irel.asRelation().arity(); if (header) { for (int i = 0; i < nfields; i++) { if (i > 0) out.write(separator); String label = paramType.getFieldName(i); if (label == null || label.isEmpty()) label = "field" + i; writeString(out, label); } out.write('\n'); } String separatorAsString = new String(Character.toChars(separator)); for (IValue v : (isListRel ? lrel : irel)) { ITuple tup = (ITuple) v; int sep = 0; for (IValue w : tup) { if (sep == 0) sep = separator; else out.write(sep); if (w.getType().isString()) { String s = ((IString) w).getValue(); if (s.contains(separatorAsString) || s.contains("\n") || s.contains("\r") || s.contains("\"")) { s = s.replaceAll("\"", "\"\""); out.write('"'); writeString(out, s); out.write('"'); } else writeString(out, s); } else { writeString(out, w.toString()); } } out.write('\n'); } out.flush(); out.close(); } catch (IOException e) { throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); } finally { if (out != null) { try { out.flush(); out.close(); } catch (IOException ioex) { throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()), null, null); } } } }
public Object loadPT(IConstructor[] fileRef, String sourcePath, String sourceName) { IConstructor file = fileRef[0]; ISourceLocation loc = TreeAdapter.getLocation(file); int lineNumber = loc.getBeginLine(); Object ret = null; Var.pushThreadBindings( RT.map( LOADER, RT.makeClassLoader(), SOURCE_PATH, sourcePath, SOURCE, sourceName, METHOD, null, LOCAL_ENV, null, LOOP_LOCALS, null, NEXT_LOCAL_NUM, 0, RT.CURRENT_NS, RT.CURRENT_NS.deref(), LINE_BEFORE, lineNumber, LINE_AFTER, lineNumber, RT.UNCHECKED_MATH, RT.UNCHECKED_MATH.deref(), RT.WARN_ON_REFLECTION, RT.WARN_ON_REFLECTION.deref(), RT.DATA_READERS, RT.DATA_READERS.deref())); UPTRLispReader reader = new UPTRLispReader(vf, errors); try { if (TreeAdapter.isAmb(file)) { System.err.println("Amb"); } // File is start[File], so IConstructor file2 = (IConstructor) TreeAdapter.getArgs(file).get(1); IList args = TreeAdapter.getArgs(file2); // Probably only this is the list of forms; don't forget to fix // below. IList forms = TreeAdapter.getArgs((IConstructor) args.get(0)); IListWriter newArgs = vf.listWriter(); for (int i = 0; i < forms.length(); i++) { IConstructor form = (IConstructor) forms.get(i); // only forms, no literals at this level. UPTRLispReader.Pair p = reader.read(form); newArgs.append(p.tree); LINE_AFTER.set(TreeAdapter.getLocation(form).getEndLine()); ret = eval(p.obj, false); LINE_BEFORE.set(TreeAdapter.getLocation(form).getBeginLine()); if (i < forms.length() - 2) { i++; newArgs.append(forms.get(i)); // layout } } // Fix tree file2 = file2.set("args", newArgs.done()); file = file.set( "args", vf.list(TreeAdapter.getArgs(file).get(0), file2, TreeAdapter.getArgs(file).get(2))); } catch (UPTRLispReader.ReaderException e) { throw new CompilerException(sourcePath, e.line, e.getCause()); } finally { Var.popThreadBindings(); } fileRef[0] = file; return ret; }
public static IResource getResourceHandler(ISourceLocation uri) { for (String key : resourceHandlers.keySet()) { if (uri.getURI().getHost().equals(key)) return resourceHandlers.get(key); } return null; // TODO: Throw here! }