diff --git a/examples/nested-alt/partialtest1.hlog b/examples/nested-alt/partialtest1.hlog index a86e508..9180371 100644 --- a/examples/nested-alt/partialtest1.hlog +++ b/examples/nested-alt/partialtest1.hlog @@ -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. \ No newline at end of file diff --git a/hank/Alt.hx b/hank/Alt.hx index fd9b60f..d3bc6c6 100644 --- a/hank/Alt.hx +++ b/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)); } } diff --git a/hank/Output.hx b/hank/Output.hx index 7884e26..2c95b0a 100644 --- a/hank/Output.hx +++ b/hank/Output.hx @@ -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) { + private static function updateLastPart(parts: Array, 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 { + public static function parseLastText(text: String, isPartOfAlt: Bool): Array { 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; diff --git a/tests/ParserTest.hx b/tests/ParserTest.hx index 4e3c335..8360860 100644 --- a/tests/ParserTest.hx +++ b/tests/ParserTest.hx @@ -68,7 +68,7 @@ class ParserTest extends utest.Test { Sequence, [ new Output([Text('This is a sequence, too')]), - new Output([OutputType.Text('')]) + new Output([]) ] )) ]))