mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 00:47:56 +01:00
Factor out a parser.parseIterateNode method
This commit is contained in:
1 parent
a73e56712d
commit
a511e927a1
1 file changed
+78
-70
+78
-70
@@ -622,76 +622,7 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
|||||||
return o.Node(), err
|
return o.Node(), err
|
||||||
|
|
||||||
case t.IDIterate:
|
case t.IDIterate:
|
||||||
p.src = p.src[1:]
|
return p.parseIterateNode()
|
||||||
label, err := p.parseLabel()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if x := p.peek1(); x != t.IDOpenBracket {
|
|
||||||
got := p.tm.ByID(x)
|
|
||||||
return nil, fmt.Errorf(`parse: expected "[", got %q at %s:%d`, got, p.filename, p.line())
|
|
||||||
}
|
|
||||||
p.src = p.src[1:]
|
|
||||||
|
|
||||||
unroll := p.peek1()
|
|
||||||
if unroll.SmallPowerOf2Value() == 0 {
|
|
||||||
return nil, fmt.Errorf(`parse: expected power-of-2 unroll count in [1..256], got %q at %s:%d`,
|
|
||||||
p.tm.ByID(unroll), p.filename, p.line())
|
|
||||||
}
|
|
||||||
p.src = p.src[1:]
|
|
||||||
|
|
||||||
if x := p.peek1(); x != t.IDComma {
|
|
||||||
got := p.tm.ByID(x)
|
|
||||||
return nil, fmt.Errorf(`parse: expected ",", got %q at %s:%d`, got, p.filename, p.line())
|
|
||||||
}
|
|
||||||
p.src = p.src[1:]
|
|
||||||
|
|
||||||
step := p.peek1()
|
|
||||||
if step.SmallPowerOf2Value() == 0 {
|
|
||||||
return nil, fmt.Errorf(`parse: expected power-of-2 step count in [1..256], got %q at %s:%d`,
|
|
||||||
p.tm.ByID(step), p.filename, p.line())
|
|
||||||
}
|
|
||||||
p.src = p.src[1:]
|
|
||||||
|
|
||||||
if x := p.peek1(); x != t.IDCloseBracket {
|
|
||||||
got := p.tm.ByID(x)
|
|
||||||
return nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line())
|
|
||||||
}
|
|
||||||
p.src = p.src[1:]
|
|
||||||
|
|
||||||
vars, err := p.parseList(t.IDCloseParen, (*parser).parseIterateVarNode)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
asserts, err := p.parseAsserts()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
body, err := p.parseBlock()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tail := ([]*a.Node)(nil)
|
|
||||||
if step == t.ID1 {
|
|
||||||
if x := p.peek1(); x == t.IDElse {
|
|
||||||
got := p.tm.ByID(x)
|
|
||||||
return nil, fmt.Errorf(`parse: expected non-"else", got %q at %s:%d`, got, p.filename, p.line())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if x := p.peek1(); x != t.IDElse {
|
|
||||||
got := p.tm.ByID(x)
|
|
||||||
return nil, fmt.Errorf(`parse: expected "else", got %q at %s:%d`, got, p.filename, p.line())
|
|
||||||
}
|
|
||||||
p.src = p.src[1:]
|
|
||||||
tail, err = p.parseBlock()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return a.NewIterate(unroll, step, label, vars, asserts, body, tail).Node(), nil
|
|
||||||
|
|
||||||
case t.IDReturn, t.IDYield:
|
case t.IDReturn, t.IDYield:
|
||||||
p.src = p.src[1:]
|
p.src = p.src[1:]
|
||||||
@@ -793,6 +724,83 @@ func (p *parser) parseIf() (*a.If, error) {
|
|||||||
return a.NewIf(condition, elseIf, bodyIfTrue, bodyIfFalse), nil
|
return a.NewIf(condition, elseIf, bodyIfTrue, bodyIfFalse), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *parser) parseIterateNode() (*a.Node, error) {
|
||||||
|
if x := p.peek1(); x != t.IDIterate {
|
||||||
|
got := p.tm.ByID(x)
|
||||||
|
return nil, fmt.Errorf(`parse: expected "if", got %q at %s:%d`, got, p.filename, p.line())
|
||||||
|
}
|
||||||
|
p.src = p.src[1:]
|
||||||
|
label, err := p.parseLabel()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if x := p.peek1(); x != t.IDOpenBracket {
|
||||||
|
got := p.tm.ByID(x)
|
||||||
|
return nil, fmt.Errorf(`parse: expected "[", got %q at %s:%d`, got, p.filename, p.line())
|
||||||
|
}
|
||||||
|
p.src = p.src[1:]
|
||||||
|
|
||||||
|
unroll := p.peek1()
|
||||||
|
if unroll.SmallPowerOf2Value() == 0 {
|
||||||
|
return nil, fmt.Errorf(`parse: expected power-of-2 unroll count in [1..256], got %q at %s:%d`,
|
||||||
|
p.tm.ByID(unroll), p.filename, p.line())
|
||||||
|
}
|
||||||
|
p.src = p.src[1:]
|
||||||
|
|
||||||
|
if x := p.peek1(); x != t.IDComma {
|
||||||
|
got := p.tm.ByID(x)
|
||||||
|
return nil, fmt.Errorf(`parse: expected ",", got %q at %s:%d`, got, p.filename, p.line())
|
||||||
|
}
|
||||||
|
p.src = p.src[1:]
|
||||||
|
|
||||||
|
step := p.peek1()
|
||||||
|
if step.SmallPowerOf2Value() == 0 {
|
||||||
|
return nil, fmt.Errorf(`parse: expected power-of-2 step count in [1..256], got %q at %s:%d`,
|
||||||
|
p.tm.ByID(step), p.filename, p.line())
|
||||||
|
}
|
||||||
|
p.src = p.src[1:]
|
||||||
|
|
||||||
|
if x := p.peek1(); x != t.IDCloseBracket {
|
||||||
|
got := p.tm.ByID(x)
|
||||||
|
return nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line())
|
||||||
|
}
|
||||||
|
p.src = p.src[1:]
|
||||||
|
|
||||||
|
vars, err := p.parseList(t.IDCloseParen, (*parser).parseIterateVarNode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
asserts, err := p.parseAsserts()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body, err := p.parseBlock()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
tail := ([]*a.Node)(nil)
|
||||||
|
if step == t.ID1 {
|
||||||
|
if x := p.peek1(); x == t.IDElse {
|
||||||
|
got := p.tm.ByID(x)
|
||||||
|
return nil, fmt.Errorf(`parse: expected non-"else", got %q at %s:%d`, got, p.filename, p.line())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if x := p.peek1(); x != t.IDElse {
|
||||||
|
got := p.tm.ByID(x)
|
||||||
|
return nil, fmt.Errorf(`parse: expected "else", got %q at %s:%d`, got, p.filename, p.line())
|
||||||
|
}
|
||||||
|
p.src = p.src[1:]
|
||||||
|
tail, err = p.parseBlock()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return a.NewIterate(unroll, step, label, vars, asserts, body, tail).Node(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *parser) parseArgNode() (*a.Node, error) {
|
func (p *parser) parseArgNode() (*a.Node, error) {
|
||||||
name, err := p.parseIdent()
|
name, err := p.parseIdent()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user