Skip to content

Commit

Permalink
fixes #916
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Jun 1, 2023
1 parent e10bf78 commit 9c858c0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
33 changes: 30 additions & 3 deletions src/org/rascalmpl/semantics/dynamic/PathPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
*******************************************************************************/
package org.rascalmpl.semantics.dynamic;

import java.net.URI;
import java.net.URISyntaxException;

import org.rascalmpl.ast.Expression;
import org.rascalmpl.ast.PathChars;
import org.rascalmpl.ast.PathTail;
import org.rascalmpl.ast.PrePathChars;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.interpreter.IEvaluator;
import org.rascalmpl.interpreter.result.Result;
import org.rascalmpl.interpreter.result.ResultFactory;
import org.rascalmpl.uri.URIUtil;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;

public abstract class PathPart extends org.rascalmpl.ast.PathPart {
Expand All @@ -37,9 +44,29 @@ public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {

Result<IValue> pre = this.getPre().interpret(__eval);
Result<IValue> expr = this.getExpression().interpret(__eval);
Result<IValue> tail = this.getTail().interpret(__eval);

return pre.add(expr).add(tail);
// here we have to encode the string for us in the path part of a uri
// the trick is to use new File which does the encoding for us, in
// a way that conforms to the current OS where we are running. So if someone
// is splicing a windows path here, with the slashes the other way around,
// they are first interpreted as path separators and not encoded as %slash
// However, first we need to map the expression result to string by using
// the `add` semantics of strings:
IString path = (IString) ResultFactory.makeResult(TF.stringType(), VF.string(""), __eval)
.add(expr).getValue();

try {
// reuse our URI encoders here on the unencoded expression part
URI tmp = URIUtil.create("x", "", "/" + path.getValue());
// but get the path out directly anyway, unencoded!
path = VF.string(tmp.getRawPath());

// connect the pre, middle and end pieces
Result<IValue> tail = this.getTail().interpret(__eval);
return pre.add(ResultFactory.makeResult(TF.stringType(), path, __eval)).add(tail);
}
catch (URISyntaxException e) {
throw RuntimeExceptionFactory.malformedURI(path.getValue(), getExpression(), __eval.getStackTrace());
}
}

}
Expand Down
26 changes: 24 additions & 2 deletions src/org/rascalmpl/semantics/dynamic/PathTail.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
*******************************************************************************/
package org.rascalmpl.semantics.dynamic;

import java.net.URI;
import java.net.URISyntaxException;

import org.rascalmpl.ast.Expression;
import org.rascalmpl.ast.MidPathChars;
import org.rascalmpl.ast.PostPathChars;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.interpreter.IEvaluator;
import org.rascalmpl.interpreter.result.Result;
import org.rascalmpl.interpreter.result.ResultFactory;
import org.rascalmpl.uri.URIUtil;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;

public abstract class PathTail extends org.rascalmpl.ast.PathTail {
Expand All @@ -34,9 +42,23 @@ public Mid(ISourceLocation __param1, IConstructor tree, MidPathChars __param2, E
public Result<IValue> interpret(IEvaluator<Result<IValue>> __eval) {
Result<IValue> mid = this.getMid().interpret(__eval);
Result<IValue> expr = this.getExpression().interpret(__eval);
Result<IValue> tail = this.getTail().interpret(__eval);

return mid.add(expr).add(tail);
IString path = (IString) ResultFactory.makeResult(TF.stringType(), VF.string(""), __eval)
.add(expr).getValue();

try {
// reuse our URI encoders here on the unencoded expression part
URI tmp = URIUtil.create("x", "", "/" + path.getValue());
// but get the path out directly anyway, unencoded!
path = VF.string(tmp.getRawPath());

// connect the pre, middle and end pieces
Result<IValue> tail = this.getTail().interpret(__eval);
return mid.add(ResultFactory.makeResult(TF.stringType(), path, __eval)).add(tail);
}
catch (URISyntaxException e) {
throw RuntimeExceptionFactory.malformedURI(path.getValue(), getExpression(), __eval.getStackTrace());
}
}

}
Expand Down

0 comments on commit 9c858c0

Please sign in to comment.