Skip to content

Commit

Permalink
experimental re-implementation of createHole.
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Sep 19, 2023
1 parent 360a0c9 commit dc2e985
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/org/rascalmpl/values/RascalFunctionValueFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,14 @@ public IFunction bootstrapParsers() {
}

public IString createHole(ITree part, IInteger index) {
ASTBuilder builder = new ASTBuilder();
ITree hole = TreeAdapter.getArg(part, "hole");
ITree sym = TreeAdapter.getArg(hole, "symbol");
IConstructor symbol = SymbolFactory.typeToSymbol((Sym) builder.buildValue(sym) , false, "*default*");
IConstructor symbol = SymbolFactory.typeToSymbol(sym , false, null);

return ctx.getValueFactory().string("\u0000" + symbol.toString() + index + ":\u0000");
IString result = ctx.getValueFactory().string("\u0000" + symbol.toString() + ":" + index + "\u0000");

System.err.println("New hole: " + result);
return result;
// we replaced this:
// return getParserGenerator().createHole(part, index);
}
Expand Down
27 changes: 17 additions & 10 deletions src/org/rascalmpl/values/parsetrees/SymbolFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.rascalmpl.ast.Type;
import org.rascalmpl.interpreter.asserts.NotYetImplemented;
import org.rascalmpl.interpreter.utils.Names;
import org.rascalmpl.parser.ASTBuilder;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IInteger;
Expand All @@ -39,10 +40,15 @@

public class SymbolFactory {
private static IValueFactory factory = ValueFactoryFactory.getValueFactory();
private static ASTBuilder builder = new ASTBuilder();

public static IConstructor typeToSymbol(ITree parseTree, boolean lex, String layout) {
return SymbolFactory.typeToSymbol((Sym) builder.buildValue(parseTree), lex, layout);
}

public static IConstructor typeToSymbol(Sym type, boolean lex, String layout) {
return (IConstructor) symbolAST2SymbolConstructor(type, lex, layout);
}
}

public static IConstructor typeToSymbol(Type type, boolean lex, String layout) {
if (type.isUser()) {
Expand All @@ -63,7 +69,8 @@ public static IConstructor typeToSymbol(Type type, boolean lex, String layout) {

// TODO: distribute this code over the dynamic.Sym classes in typeOf method
private static IValue symbolAST2SymbolConstructor(Sym symbol, boolean lex, String layout) {

boolean noExpand = lex || layout == null;

if (symbol.isCaseInsensitiveLiteral()) {
return factory.constructor(RascalValueFactory.Symbol_Cilit, ciliteral2Symbol(symbol.getCistring()));
}
Expand All @@ -72,7 +79,7 @@ private static IValue symbolAST2SymbolConstructor(Sym symbol, boolean lex, Strin
return charclass2Symbol(cc);
}
if (symbol.isIter()) {
if (lex) {
if (noExpand) {
return factory.constructor(RascalValueFactory.Symbol_Iter, symbolAST2SymbolConstructor(symbol.getSymbol(), lex, layout));
}
else {
Expand All @@ -83,7 +90,7 @@ private static IValue symbolAST2SymbolConstructor(Sym symbol, boolean lex, Strin
}
}
if (symbol.isIterStar()) {
if (lex) {
if (noExpand) {
return factory.constructor(RascalValueFactory.Symbol_IterStar, symbolAST2SymbolConstructor(symbol.getSymbol(), lex, layout));
}
else {
Expand All @@ -99,7 +106,7 @@ private static IValue symbolAST2SymbolConstructor(Sym symbol, boolean lex, Strin
IValue sepSym = symbolAST2SymbolConstructor(symbol.getSep(), lex, layout);
IValue seps;

if (lex) {
if (noExpand) {
seps = factory.list(sepSym);
}
else {
Expand All @@ -114,7 +121,7 @@ private static IValue symbolAST2SymbolConstructor(Sym symbol, boolean lex, Strin
IValue elementSym = symbolAST2SymbolConstructor(symbol.getSymbol(), lex, layout);
IValue sepSym = symbolAST2SymbolConstructor(symbol.getSep(), lex, layout);
IValue seps;
if (lex) {
if (noExpand) {
seps = factory.list(sepSym);
}
else {
Expand Down Expand Up @@ -148,10 +155,10 @@ private static IValue symbolAST2SymbolConstructor(Sym symbol, boolean lex, Strin
if(symbol.isSequence()){
List<Sym> symbols = symbol.getSequence();
IValue layoutSymbol = factory.constructor(RascalValueFactory.Symbol_Layouts, factory.string(layout));
IValue[] symValues = new IValue[lex ? symbols.size() : symbols.size() * 2 - 1];
for(int i = symbols.size() - 1; i >= 0; i -= lex ? 1 : 2) {
symValues[lex ? i : i * 2] = symbolAST2SymbolConstructor(symbols.get(i), lex, layout);
if (lex && i > 0) {
IValue[] symValues = new IValue[noExpand ? symbols.size() : symbols.size() * 2 - 1];
for(int i = symbols.size() - 1; i >= 0; i -= noExpand ? 1 : 2) {
symValues[noExpand ? i : i * 2] = symbolAST2SymbolConstructor(symbols.get(i), lex, layout);
if (!noExpand && i > 0) {
symValues[i - 1] = layoutSymbol;
}
}
Expand Down

0 comments on commit dc2e985

Please sign in to comment.