Skip to content

Commit 72527ff

Browse files
authored
allow comma separated use traits (#136)
1 parent abdfd2e commit 72527ff

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

evaluator/evaluator_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,36 @@ func TestTraitMethodLookup(t *testing.T) {
297297
`,
298298
expected: 30,
299299
},
300+
{
301+
name: "comma-separated traits",
302+
input: `
303+
trait First {
304+
function first() {
305+
return 1
306+
}
307+
}
308+
309+
trait Second {
310+
function second() {
311+
return 2
312+
}
313+
}
314+
315+
trait Third {
316+
function third() {
317+
return 3
318+
}
319+
}
320+
321+
class Thing {
322+
use First, Second, Third
323+
}
324+
325+
t = Thing.new()
326+
t.first() + t.second() + t.third()
327+
`,
328+
expected: 6,
329+
},
300330
}
301331

302332
for _, tt := range tests {

parser/use.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ func (parser *Parser) useExpression() ast.ExpressionNode {
1313
}
1414

1515
identifier := &ast.Identifier{Token: parser.currentToken, Value: parser.currentToken.Lexeme}
16-
1716
use.Traits = append(use.Traits, identifier)
1817

18+
for parser.nextTokenIs(token.COMMA) {
19+
parser.readToken() // consume comma
20+
21+
if !parser.expectNextTokenIs(token.IDENTIFIER) {
22+
return nil
23+
}
24+
25+
identifier := &ast.Identifier{Token: parser.currentToken, Value: parser.currentToken.Lexeme}
26+
27+
use.Traits = append(use.Traits, identifier)
28+
}
29+
1930
return use
2031
}

0 commit comments

Comments
 (0)