}
if (isSort(symbol) || isLex(symbol) || isKeyword(symbol)) {
return getName(symbol);
}
if (isCharClass(symbol)) {
IList ranges = getRanges(symbol);
StringBuilder b = new StringBuilder();
b.append("[");
for (IValue range : ranges) {
IInteger from = getRangeBegin(range);
IInteger to = getRangeEnd(range);
if (from.equals(to)) {
b.append(escapeChar(from.intValue()));
}
else {
b.append(escapeChar(from.intValue()));
b.append("-");
b.append(escapeChar(to.intValue()));
}
}
b.append("]");
return b.toString();
}
if (isIterPlusSeps(symbol)) {
IList seps = getSeparators(symbol);
StringBuilder b = new StringBuilder();
if (!withLayout && seps.length() == 1 && isLayouts((IConstructor) seps.get(0))) {
b.append(toString(getSymbol(symbol), withLayout));
b.append('+');
}
else {
b.append('{');
b.append(toString(getSymbol(symbol), withLayout));
for (IValue sep : seps) {
b.append(" ");
b.append(toString((IConstructor) sep, withLayout));
}
b.append('}');
b.append('+');
}
return b.toString();
}
if (isIterStarSeps(symbol)) {
StringBuilder b = new StringBuilder();
IList seps = getSeparators(symbol);
if (!withLayout && seps.length() == 1 && isLayouts((IConstructor) seps.get(0))) {
b.append(toString(getSymbol(symbol), withLayout));
b.append('*');
}
else {
b.append('{');
b.append(toString(getSymbol(symbol), withLayout));
for (IValue sep : seps) {
if (!isLayouts((IConstructor) sep)) {
b.append(" ");
b.append(toString((IConstructor) sep, withLayout));
}
}
b.append('}');
b.append('*');
}
return b.toString();
}
if (isIterPlus(symbol)) {
return toString(getSymbol(symbol), withLayout) + '+';
}
if (isIterStar(symbol)) {
return toString(getSymbol(symbol), withLayout) + '*';
}
if (isOpt(symbol)) {
return toString(getSymbol(symbol), withLayout) + '?';
}
if (isSeq(symbol)) {
return "(" + toString(getSymbols(symbol), ' ', withLayout) + ")";
}
if (isAlt(symbol)) {
ISet alts = getAlternatives(symbol);
StringBuilder b = new StringBuilder();
b.append("(");
boolean first = true;
for (IValue elem : alts) {
if (!first) {
first = false;
b.append(" | ");
}
b.append(toString((IConstructor) elem, withLayout));
}
b.append(")");
return b.toString();
}
if (isLayouts(symbol)) {
return withLayout ? "layout[" + symbol.get("name") + "]" : "";
}
if (isLiteral(symbol)) {
return '"' + ((IString) symbol.get("string")).getValue() + '"';
}
if (isCILiteral(symbol)) {
return '\'' + ((IString) symbol.get("string")).getValue() + '\'';
}
if (isParameterizedSort(symbol) || isParameterizedLex(symbol)) {
StringBuilder b = new StringBuilder();
b.append(getName(symbol));
IList params = (IList) symbol.get("parameters");
b.append('[');
b.append(toString(params, ',', withLayout));
b.append(']');
return b.toString();
}
if (isStartSort(symbol)) {
return "start[" + toString(getStart(symbol), withLayout) + "]";
}
if (isParameter(symbol)) {
return "&" + getName(symbol);
}
if (isInt(symbol) || isStr(symbol) || isReal(symbol) || isBool(symbol) || isRat(symbol)
|| isNode(symbol) || isValue(symbol) || isVoid(symbol) || isNum(symbol) || isDatetime(symbol) || isLoc(symbol)) {
return symbol.getName();
}
if (isSet(symbol) || isList(symbol) || isBag(symbol) || isReifiedType(symbol)) {
return symbol.getName() + "[" + toString((IConstructor) symbol.get("symbol"), withLayout) + "]";
}
if (isRel(symbol) || isListRel(symbol) || isTuple(symbol)) {
StringBuilder b = new StringBuilder();
b.append(symbol.getName());
IList symbols = (IList) symbol.get("symbols");
b.append('[');
b.append(toString(symbols, ',', withLayout));
b.append(']');
return b.toString();
}
if (isMap(symbol)) {
return symbol.getName() + "[" + toString((IConstructor) symbol.get("from"), withLayout) + "," + toString((IConstructor) symbol.get("to"), withLayout) + "]";
}
if (isConditional(symbol)) {
ISet conditions = getConditions(symbol);
StringBuilder b = new StringBuilder();
// first prefix conditions
for (IValue elem : conditions) {
IConstructor cond = (IConstructor) elem;
switch (cond.getConstructorType().getName()) {
case "precede":
b.append(toString((IConstructor) cond.get("symbol"), withLayout));
b.append(" << ");
break;
case "not-precede":
b.append(toString((IConstructor) cond.get("symbol"), withLayout));
b.append(" !<< ");
break;
case "begin-of-line":
b.append("^ ");
break;
}
}
// then the symbol
b.append(toString(getSymbol(symbol), withLayout));
// then the postfix conditions
for (IValue elem : conditions) {
IConstructor cond = (IConstructor) elem;
switch (cond.getConstructorType().getName()) {
case "follow":
b.append(" >> ");
b.append(toString((IConstructor) cond.get("symbol"), withLayout));
break;
case "not-follow":
b.append(" !>> ");
b.append(toString((IConstructor) cond.get("symbol"), withLayout));
break;
case "delete":
b.append(" \\ ");
b.append(toString((IConstructor) cond.get("symbol"), withLayout));
break;
case "except":
b.append("!");
b.append(((IString) cond.get("label")).getValue());
case "end-of-line":
b.append(" $");
break;
}
}
return b.toString();
}
if (isADT(symbol) || isAlias(symbol)) {
StringBuilder b = new StringBuilder();
b.append(getName(symbol));
IList params = (IList) symbol.get("parameters");
if (!params.isEmpty()) {
b.append('[');
b.append(toString(params, ',', withLayout));
b.append(']');
}
return b.toString();