Fix nested-alt example

This commit is contained in:
2019-04-29 19:59:13 -06:00
parent 78a0a75ebe
commit d7f3e302b4
4 changed files with 28 additions and 24 deletions

View File

@@ -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.

View File

@@ -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));
}
}

View File

@@ -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;

View File

@@ -68,7 +68,7 @@ class ParserTest extends utest.Test {
Sequence,
[
new Output([Text('This is a sequence, too')]),
new Output([OutputType.Text('')])
new Output([])
]
))
]))