parsing record access and update
This commit is contained in:
parent
6e8b5afd3b
commit
b6f7e63e49
3 changed files with 48 additions and 3 deletions
|
|
@ -148,16 +148,54 @@ export class Parser {
|
|||
}
|
||||
|
||||
private parseApplication(): AST {
|
||||
let func = this.parsePrimary();
|
||||
let func = this.parsePostfix();
|
||||
|
||||
while (this.canStartPrimary()) {
|
||||
const arg = this.parsePrimary();
|
||||
const arg = this.parsePostfix();
|
||||
func = { kind: 'apply', func, args: [arg] };
|
||||
}
|
||||
|
||||
return func;
|
||||
}
|
||||
|
||||
private parsePostfix(): AST {
|
||||
let expr = this.parsePrimary();
|
||||
|
||||
while (true) {
|
||||
if (this.current().kind === 'dot') {
|
||||
// Record access
|
||||
this.advance();
|
||||
const fieldToken = this.expect('ident');
|
||||
const field = (fieldToken as { value: string }).value;
|
||||
expr = { kind: 'record-access', record: expr, field };
|
||||
} else if (this.current().kind === 'open-brace') {
|
||||
// Record update
|
||||
this.advance();
|
||||
const updates: { [key: string]: AST } = {};
|
||||
let first = true;
|
||||
|
||||
while (this.current().kind !== 'close-brace') {
|
||||
if (!first) {
|
||||
this.expect('comma');
|
||||
}
|
||||
first = false;
|
||||
|
||||
const keyToken = this.expect('ident');
|
||||
const key = (keyToken as { value: string }).value;
|
||||
this.expect('equals');
|
||||
updates[key] = this.parseExpression();
|
||||
}
|
||||
|
||||
this.expect('close-brace');
|
||||
expr = { kind: 'record-update', record: expr, updates }
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
private parsePrimary(): AST {
|
||||
const token = this.current();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue