Skip to content

Commit 390a05b

Browse files
authored
fix problem with subsequent comments (#210)
1 parent 16bc1fe commit 390a05b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

classes/local/lexer.php

+6
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ private function consume_comment(): void {
357357
}
358358
// Eat up all white space following the comment.
359359
$this->consume_whitespace();
360+
361+
// If the next char is a # again, the comment seems to continue on the next line,
362+
// so we go for another round.
363+
if ($this->inputstream->peek() === '#') {
364+
$this->consume_comment();
365+
}
360366
}
361367

362368
/**

tests/lexer_test.php

+42
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,48 @@ public function test_get_token_list_7(): void {
424424
}
425425
}
426426

427+
public function test_get_token_list_with_subsequent_comments(): void {
428+
$input = <<<EOF
429+
# First comment
430+
# Second comment
431+
a = 1;
432+
433+
# Comment
434+
435+
# Comment
436+
437+
b = 2;
438+
439+
# Comment
440+
# comment with indentation
441+
#### comment
442+
443+
c = 3;
444+
EOF;
445+
446+
// We are not testing the positions anymore.
447+
$output = [
448+
new token(token::IDENTIFIER, 'a'),
449+
new token(token::OPERATOR, '='),
450+
new token(token::NUMBER, 1),
451+
new token(token::END_OF_STATEMENT, ';'),
452+
new token(token::IDENTIFIER, 'b'),
453+
new token(token::OPERATOR, '='),
454+
new token(token::NUMBER, 2),
455+
new token(token::END_OF_STATEMENT, ';'),
456+
new token(token::IDENTIFIER, 'c'),
457+
new token(token::OPERATOR, '='),
458+
new token(token::NUMBER, 3),
459+
new token(token::END_OF_STATEMENT, ';'),
460+
];
461+
462+
$tokens = (new lexer($input))->get_tokens();
463+
foreach ($tokens as $i => $token) {
464+
self::assertEquals($output[$i]->type, $token->type);
465+
self::assertEquals($output[$i]->value, $token->value);
466+
}
467+
}
468+
427469
public function test_pi(): void {
428470
$input = 'π + π(1 + 2) + pi + pi() + pi(2) + pi(1 + 2)';
429471

0 commit comments

Comments
 (0)