Fix nested-alt example
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
The Ratbear wastes no time and swipes at you.
|
||||
The Ratbear scratches into your leg.
|
||||
The Ratbear wastes no time and swipes at you.
|
||||
The Ratbear swipes at you.
|
||||
The Ratbear scratches into your arm.
|
||||
The Ratbear wastes no time and swipes at you.
|
||||
The Ratbear swipes at you.
|
||||
The Ratbear scratches into your cheek.
|
||||
The Ratbear wastes no time and swipes at you.
|
||||
The Ratbear swipes at you.
|
||||
The Ratbear scratches into your leg.
|
||||
The Ratbear wastes no time and swipes at you.
|
||||
The Ratbear swipes at you.
|
||||
The Ratbear scratches into your arm.
|
||||
The Ratbear wastes no time and swipes at you.
|
||||
The Ratbear swipes at you.
|
||||
The Ratbear scratches into your cheek.
|
||||
23
hank/Alt.hx
23
hank/Alt.hx
@@ -38,22 +38,23 @@ class Alt {
|
||||
var rawExpr = buffer.findNestedExpression('{', '}').unwrap().checkValue();
|
||||
var expr = rawExpr.substr(1, rawExpr.length-2);
|
||||
|
||||
// Sequences are the default behavior
|
||||
var behavior = Sequence;
|
||||
for (prefix in behaviorMap.keys()) {
|
||||
if (expr.startsWith(prefix)) {
|
||||
var outputExprs = expr.substr(prefix.length).trim();
|
||||
var outputsBuffer = HankBuffer.Dummy(outputExprs);
|
||||
|
||||
var eachOutputExpr = outputsBuffer.rootSplit('|');
|
||||
var outputs = [for(outputExpr in eachOutputExpr) Output.parse(HankBuffer.Dummy(outputExpr))];
|
||||
|
||||
buffer.take(rawExpr.length);
|
||||
return Some(new Alt(behaviorMap[prefix], outputs));
|
||||
expr = expr.substr(prefix.length).trim();
|
||||
behavior = behaviorMap[prefix];
|
||||
}
|
||||
}
|
||||
var outputsBuffer = HankBuffer.Dummy(expr);
|
||||
var eachOutputExpr = outputsBuffer.rootSplit('|');
|
||||
if (eachOutputExpr.length == 1) {
|
||||
return None; // If no pipe is present, it's not an alt
|
||||
}
|
||||
var outputs = [for(outputExpr in eachOutputExpr) Output.parse(HankBuffer.Dummy(outputExpr), true)];
|
||||
|
||||
// Sequences can also occur without the prefix
|
||||
|
||||
return None;
|
||||
buffer.take(rawExpr.length);
|
||||
return Some(new Alt(behavior, outputs));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class Output {
|
||||
return parts.length == 0;
|
||||
}
|
||||
|
||||
public static function parse(buffer: HankBuffer): Output {
|
||||
public static function parse(buffer: HankBuffer, isPartOfAlt: Bool = false): Output {
|
||||
var parts = [];
|
||||
|
||||
// If brackets appear on this line, the first step is to break it up into ToggleOutput segments because ToggleOutputs need to be outermost in the hierarchy.
|
||||
@@ -81,18 +81,18 @@ class Output {
|
||||
}
|
||||
}
|
||||
|
||||
parts = updateLastPart(parts);
|
||||
parts = updateLastPart(parts, isPartOfAlt);
|
||||
return new Output(parts);
|
||||
}
|
||||
|
||||
private static function updateLastPart(parts: Array<OutputType>) {
|
||||
private static function updateLastPart(parts: Array<OutputType>, isPartOfAlt: Bool) {
|
||||
// If the last output is Text, it could contain optional text or an inline divert. Or just need rtrimming.
|
||||
if (parts.length > 0) {
|
||||
var lastPart = parts[parts.length - 1];
|
||||
switch(lastPart) {
|
||||
case Text(t):
|
||||
parts.remove(lastPart);
|
||||
parts = parts.concat(parseLastText(t));
|
||||
parts = parts.concat(parseLastText(t, isPartOfAlt));
|
||||
default:
|
||||
}
|
||||
}
|
||||
@@ -101,7 +101,7 @@ class Output {
|
||||
|
||||
|
||||
/** The last part of an output expression outside of braces can include an inline divert -> like_so **/
|
||||
public static function parseLastText(text: String): Array<OutputType> {
|
||||
public static function parseLastText(text: String, isPartOfAlt: Bool): Array<OutputType> {
|
||||
var parts = [];
|
||||
|
||||
var divertIndex = text.lastIndexOf('->');
|
||||
@@ -112,7 +112,10 @@ class Output {
|
||||
var target = text.substr(divertIndex+2).trim();
|
||||
parts.push(InlineDivert(target));
|
||||
} else {
|
||||
parts.push(Text(text.rtrim()));
|
||||
if (!isPartOfAlt) {
|
||||
text = text.rtrim();
|
||||
}
|
||||
parts.push(Text(text));
|
||||
}
|
||||
|
||||
return parts;
|
||||
@@ -143,7 +146,7 @@ class Output {
|
||||
switch (lastPart) {
|
||||
case InlineDivert(target):
|
||||
parts.remove(lastPart);
|
||||
parts = updateLastPart(parts);
|
||||
parts = updateLastPart(parts, false);
|
||||
return Some(target);
|
||||
default:
|
||||
return None;
|
||||
|
||||
@@ -68,7 +68,7 @@ class ParserTest extends utest.Test {
|
||||
Sequence,
|
||||
[
|
||||
new Output([Text('This is a sequence, too')]),
|
||||
new Output([OutputType.Text('')])
|
||||
new Output([])
|
||||
]
|
||||
))
|
||||
]))
|
||||
|
||||
Reference in New Issue
Block a user