From e12e83921b285bb3e2d4a14dcb2fd55697ef1fed Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 6 Apr 2024 14:01:14 -0400 Subject: [PATCH] Parse constant access unit specifiers --- parser/fspl/expression.go | 27 +++++++++++++++++++-------- parser/fspl/parser_test.go | 3 ++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/parser/fspl/expression.go b/parser/fspl/expression.go index 18467af..15ee335 100644 --- a/parser/fspl/expression.go +++ b/parser/fspl/expression.go @@ -97,7 +97,7 @@ func (this *treeParser) parseExpressionRoot () (entity.Expression, error) { switch this.Kind() { case lexer.Ident: return this.parseExpressionRootIdent() - case lexer.TypeIdent: return this.parseConstant() + case lexer.TypeIdent: return this.parseConstantCore(this.Pos(), "") case lexer.LParen: return this.parseExpressionRootLParen() case lexer.LBracket: return this.parseExpressionRootLBracket() case lexer.LBrace: return this.parseBlock() @@ -129,11 +129,19 @@ func (this *treeParser) parseExpressionRootIdent () (entity.Expression, error) { // Colon: declaration return this.parseDeclarationCore(pos, name) case lexer.DoubleColon: - // DoubleColon: call - err := this.ExpectNext(lexer.LBracket) + // DoubleColon: call, constant + err := this.ExpectNext(lexer.LBracket, lexer.TypeIdent) if err != nil { return nil, err } - this.Next() - return this.parseCallCore(pos, name) + + switch this.Kind() { + case lexer.TypeIdent: + // TypeIdent: constant + return this.parseConstantCore(pos, name) + case lexer.LBracket: + // LBracket: call + this.Next() + return this.parseCallCore(pos, name) + } default: // *: variable return &entity.Variable { @@ -142,6 +150,7 @@ func (this *treeParser) parseExpressionRootIdent () (entity.Expression, error) { }, nil } } + panic(this.bug()) } func (this *treeParser) parseExpressionRootLParen () (entity.Expression, error) { @@ -719,12 +728,14 @@ func (this *treeParser) parseFor () (*entity.For, error) { return loop, nil } -func (this *treeParser) parseConstant () (*entity.Constant, error) { +func (this *treeParser) parseConstantCore (pos errors.Position, unitNickname string) (*entity.Constant, error) { + pos = pos.Union(this.Pos()) err := this.Expect(lexer.TypeIdent) if err != nil { return nil, err } constant := &entity.Constant { - Pos: this.Pos(), - TypeName: this.Value(), + Pos: this.Pos(), + UnitNickname: unitNickname, + TypeName: this.Value(), } err = this.ExpectNext(lexer.Dot) diff --git a/parser/fspl/parser_test.go b/parser/fspl/parser_test.go index c5aea05..0d18c0c 100644 --- a/parser/fspl/parser_test.go +++ b/parser/fspl/parser_test.go @@ -84,7 +84,8 @@ testString (test, - [for s:String] = for e:Byte in s [break e] - [matchToInt u:(| Int F64)]:Int = match u | u:Int u | u:F64 [~ Int u] * 0 - [switch x:Int] = switch x | 0 5 | 1 4 | 2 3 * 0 -- [whatFile fd:FD]:String = switch fd | FD.stdin 'in' | FD.stdout 'out' * '?'`, +- [whatFile fd:FD]:String = switch fd | FD.stdin 'in' | FD.stdout 'out' * '?' +- [whatFile2 fd:io::FD]:String = switch fd | io::FD.stdin 'in' | io::FD.stdout 'out' * '?'`, // input ` [var] = sdfdf