takeLine() bug fix

This commit is contained in:
2019-03-16 19:50:20 -06:00
parent 901f6852ea
commit e446256c97
5 changed files with 29 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
This file contains test cases for output expression parsing.
A line won't /*--absolutely will not!--*/be interrupted /*by inline comments*/ or anything.
A line won't /*--absolutely will not!--*/be interrupted /*by inline comments*/ or anything.
Multiline comments /*will not
split*/ an output expression. This should parse as one line of output.
Comments at the end of lines won't parse as part of the Output. // I'm my own expression!

View File

@@ -230,10 +230,13 @@ class HankBuffer {
}
/** DRY Helper for peekLine() and takeLine() **/
function getLine(trimmed: String, retriever: Array<String> -> Bool -> Bool -> Option<BufferOutput>): Option<String> {
var nextLine = retriever(['\n'], true, true);
function getLine(trimmed: String, retriever: Array<String> -> Bool -> Bool -> Option<BufferOutput>, dropNewline: Bool): Option<String> {
var nextLine = retriever(['\n'], true, false);
return switch (nextLine) {
case Some({output: nextLine, terminator: _}):
if (dropNewline && !isEmpty()) {
drop('\n');
}
if (trimmed.indexOf('r') != -1) {
nextLine = StringTools.rtrim(nextLine);
}
@@ -250,14 +253,14 @@ class HankBuffer {
@param trimmed Which sides of the line to trim ('r' 'l', 'lr', or 'rl')
**/
public function peekLine(trimmed = ''): Option<String> {
return getLine(trimmed, peekUntil);
return getLine(trimmed, peekUntil, false);
}
/** Take the next line of data from the file.
@param trimmed Which sides of the line to trim ('r' 'l', 'lr', or 'rl')
**/
public function takeLine(trimmed = ''): Option<String> {
return getLine(trimmed, takeUntil);
return getLine(trimmed, takeUntil, true);
}
public function skipWhitespace() {

View File

@@ -43,11 +43,16 @@ class Output {
}
if (endSegment == buffer.length() || endSegment != 0) {
var peekLine = buffer.peekLine().unwrap();
trace('peek: $peekLine');
if (peekLine.length < endSegment) {
parts.push(Text(buffer.takeLine().unwrap()));
var text = buffer.takeLine().unwrap();
trace(text);
parts.push(Text(text));
break;
} else {
parts.push(Text(buffer.take(endSegment)));
var text = buffer.take(endSegment);
trace(text);
parts.push(Text(text));
}
} else {
if (buffer.indexOf('{{') == 0) {
@@ -58,15 +63,22 @@ class Output {
}
}
// Parse out optional text parts
// If the last output is Text, it should be trimmed at the end.
if (parts.length > 0) {
var lastPart = parts[parts.length - 1];
switch(lastPart) {
case Text(t):
parts[parts.length -1] = Text(StringTools.rtrim(t));
default:
}
}
// Find the first level of nested brace, expressions, and parse them out
// TODO parse out optional text parts
return new Output(parts);
}
public static function parseHaxeExpression(buffer: HankBuffer) {
trace('parseHaxeExpresion');
var rawExpression = buffer.findNestedExpression('{', '}').unwrap().checkValue();
// Strip out the enclosing braces
var hExpression = rawExpression.substr(1, rawExpression.length - 2);
@@ -78,7 +90,6 @@ class Output {
}
public static function parseAltExpression(buffer: HankBuffer) {
trace('parseAltExpression');
return AltExpression({behavior: Cycle, outputs:[]});
}
}

View File

@@ -70,6 +70,8 @@ class HankBufferTest extends utest.Test {
assertPosition();
HankAssert.equals(Some('Line of text.'), file.peekLine());
assertPosition();
file.takeLine();
HankAssert.equals(Some('Line of text without a comment.'), file.peekLine());
}
function testTakeLine() {

View File

@@ -23,6 +23,8 @@ class ParserTest extends utest.Test {
ast = parser.parseFile('examples/parsing/output.hank');
assertNextExpr(EOutput(new Output([Text("This file contains test cases for output expression parsing.")])));
assertNextExpr(EOutput(new Output([Text("A line won't be interrupted or anything.")])));
assertNextExpr(EOutput(new Output([Text("Multiline comments an output expression. This should parse as one line of output.")])));
assertNextExpr(EOutput(new Output([Text("Comments at the end of lines won't parse as part of the Output.")])));
}