Skip to content

Commit 9536ed0

Browse files
committed
extract declaration parameter and property helpers
1 parent db6de67 commit 9536ed0

3 files changed

Lines changed: 59 additions & 57 deletions

File tree

docs/architecture.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ Key files:
7474
- `vibes/parser_block_literals.go` (block literals, block params, and typed union param parsing)
7575
- `vibes/parser_statements.go` (statement dispatch + return/raise/block parsing)
7676
- `vibes/parser_expression_statements.go` (expression/assert/assignment statement parsing)
77-
- `vibes/parser_declarations.go` (function/class/property declaration parsing)
77+
- `vibes/parser_declarations.go` (function/class declaration parsing)
78+
- `vibes/parser_declaration_helpers.go` (parameter list and property declaration parsing)
7879
- `vibes/parser_control.go` (if/loop/begin-rescue-ensure parsing)
7980
- `vibes/parser_precedence.go` (precedence table + assignable-expression helpers)
8081
- `vibes/parser_types.go` (type-expression parsing)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package vibes
2+
3+
import "strings"
4+
5+
func (p *parser) parseParams() []Param {
6+
params := []Param{}
7+
for {
8+
if p.curToken.Type != tokenIdent && p.curToken.Type != tokenIvar {
9+
p.errorExpected(p.curToken, "parameter name")
10+
return params
11+
}
12+
param := Param{Name: p.curToken.Literal}
13+
if p.curToken.Type == tokenIvar {
14+
param.IsIvar = true
15+
param.Name = strings.TrimPrefix(param.Name, "@")
16+
}
17+
if p.peekToken.Type == tokenColon {
18+
p.nextToken()
19+
p.nextToken()
20+
param.Type = p.parseTypeExpr()
21+
if param.Type == nil {
22+
return params
23+
}
24+
}
25+
if p.peekToken.Type == tokenAssign {
26+
p.nextToken()
27+
p.nextToken()
28+
param.DefaultVal = p.parseExpression(lowestPrec)
29+
}
30+
params = append(params, param)
31+
if p.peekToken.Type != tokenComma {
32+
break
33+
}
34+
p.nextToken()
35+
p.nextToken()
36+
}
37+
return params
38+
}
39+
40+
func (p *parser) parsePropertyDecl(kind TokenType) PropertyDecl {
41+
pos := p.curToken.Pos
42+
names := []string{}
43+
p.nextToken()
44+
if p.curToken.Type == tokenIdent {
45+
names = append(names, p.curToken.Literal)
46+
for p.peekToken.Type == tokenComma {
47+
p.nextToken()
48+
p.nextToken()
49+
if p.curToken.Type != tokenIdent {
50+
p.errorExpected(p.curToken, "property name")
51+
break
52+
}
53+
names = append(names, p.curToken.Literal)
54+
}
55+
}
56+
return PropertyDecl{Names: names, Kind: strings.ToLower(string(kind)), position: pos}
57+
}

vibes/parser_declarations.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package vibes
22

3-
import "strings"
4-
53
func (p *parser) parseFunctionStatement() Statement {
64
pos := p.curToken.Pos
75
p.nextToken()
@@ -132,41 +130,6 @@ func (p *parser) parsePrivateStatement() Statement {
132130
return fn
133131
}
134132

135-
func (p *parser) parseParams() []Param {
136-
params := []Param{}
137-
for {
138-
if p.curToken.Type != tokenIdent && p.curToken.Type != tokenIvar {
139-
p.errorExpected(p.curToken, "parameter name")
140-
return params
141-
}
142-
param := Param{Name: p.curToken.Literal}
143-
if p.curToken.Type == tokenIvar {
144-
param.IsIvar = true
145-
param.Name = strings.TrimPrefix(param.Name, "@")
146-
}
147-
if p.peekToken.Type == tokenColon {
148-
p.nextToken()
149-
p.nextToken()
150-
param.Type = p.parseTypeExpr()
151-
if param.Type == nil {
152-
return params
153-
}
154-
}
155-
if p.peekToken.Type == tokenAssign {
156-
p.nextToken()
157-
p.nextToken()
158-
param.DefaultVal = p.parseExpression(lowestPrec)
159-
}
160-
params = append(params, param)
161-
if p.peekToken.Type != tokenComma {
162-
break
163-
}
164-
p.nextToken()
165-
p.nextToken()
166-
}
167-
return params
168-
}
169-
170133
func (p *parser) parseClassStatement() Statement {
171134
pos := p.curToken.Pos
172135
if !p.expectPeek(tokenIdent) {
@@ -230,22 +193,3 @@ func (p *parser) parseClassStatement() Statement {
230193

231194
return stmt
232195
}
233-
234-
func (p *parser) parsePropertyDecl(kind TokenType) PropertyDecl {
235-
pos := p.curToken.Pos
236-
names := []string{}
237-
p.nextToken()
238-
if p.curToken.Type == tokenIdent {
239-
names = append(names, p.curToken.Literal)
240-
for p.peekToken.Type == tokenComma {
241-
p.nextToken()
242-
p.nextToken()
243-
if p.curToken.Type != tokenIdent {
244-
p.errorExpected(p.curToken, "property name")
245-
break
246-
}
247-
names = append(names, p.curToken.Literal)
248-
}
249-
}
250-
return PropertyDecl{Names: names, Kind: strings.ToLower(string(kind)), position: pos}
251-
}

0 commit comments

Comments
 (0)