Skip to content

Commit a70f7db

Browse files
committed
Fix comments not working, fixes #2
1 parent 1a0a878 commit a70f7db

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/TokenStream.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function readNext() {
7575
return null;
7676
}
7777
$char = $this->input->peek();
78-
if ($char == "--") {
78+
if ($this->isComment()) {
7979
$this->skipComment();
8080
return $this->readNext();
8181
}
@@ -267,6 +267,13 @@ protected function isDoubleBracketString() {
267267
return $this->input->peek() == '[' && !$this->input->eof(1) && $this->input->peek(1) == '[';
268268
}
269269

270+
/**
271+
* @return bool
272+
*/
273+
protected function isComment() {
274+
return $this->input->peek() == '-' && !$this->input->eof(1) && $this->input->peek(1) == '-';
275+
}
276+
270277
/**
271278
* @param string $char
272279
*

tests/LuaParserTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,25 @@ public function testInvalid() {
185185

186186
$parser->parse();
187187
}
188+
189+
public function testComments() {
190+
$parser = new Parser(new TokenStream(new InputStream('{
191+
-- comment
192+
foo = {
193+
test = 123
194+
}
195+
}')));
196+
197+
$parser->parse();
198+
}
199+
200+
public function testInlineComments() {
201+
$parser = new Parser(new TokenStream(new InputStream('{
202+
foo = {
203+
test = 123 -- comment
204+
}
205+
}')));
206+
207+
$parser->parse();
208+
}
188209
}

tests/LuaToPhpConverterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,21 @@ public function testEmptyTable() {
8181

8282
$this->assertEquals([], $result);
8383
}
84+
85+
public function testSimpleTableWithComments() {
86+
$parser = new Parser(new TokenStream(new InputStream('{
87+
foo = "bar" -- comment
88+
}')));
89+
90+
$node = $parser->parse();
91+
92+
$result = LuaToPhpConverter::convertToPhpValue($node);
93+
94+
$this->assertEquals(
95+
[
96+
'foo' => 'bar',
97+
],
98+
$result
99+
);
100+
}
84101
}

0 commit comments

Comments
 (0)