// Complex receiver: def (a Int, b Int) sum()
// Indexer: def (this String)[index Int]
// Index setter: def (this String)[index Int] = (c Char)
// Parse the left argument, if any.
Pattern leftArg;
if (lookAhead(TokenType.LEFT_PAREN)) {
leftArg = parsePattern();
} else {
leftArg = Pattern.nothing();
}
// Parse the message.
String name;
Pattern rightArg;
if (matchAny(TokenType.NAME,
TokenType.ASTERISK,
TokenType.SLASH,
TokenType.PERCENT,
TokenType.PLUS,
TokenType.MINUS,
TokenType.LT,
TokenType.GT,
TokenType.LTE,
TokenType.GTE,
TokenType.EQEQ,
TokenType.NOTEQ)) {
// Regular named message.
name = last(1).getString();
// Parse the right argument, if any.
if (lookAhead(TokenType.LEFT_PAREN)) {
rightArg = parsePattern();
} else {
rightArg = Pattern.nothing();
}
} else {
// No name, so it must be an indexer.
name = "[]";
consume(TokenType.LEFT_BRACKET);
if (!match(TokenType.RIGHT_BRACKET)) {
rightArg = PatternParser.parse(this);
consume(TokenType.RIGHT_BRACKET);
} else {
rightArg = Pattern.nothing();
}
}
// Parse the setter's rvalue type, if any.
Pattern setValue = null;
if (match(TokenType.EQ)) {
setValue = parsePattern();
}
// Combine into a single multimethod pattern.
Pattern pattern = Pattern.record(leftArg, rightArg);
if (setValue != null) {
name = Name.makeAssigner(name);
pattern = Pattern.record(pattern, setValue);
}