示例#1
0
文件: for.go 项目: xet7/gerb
func RangedForFactory(p *core.Parser) (core.Code, error) {
	code := &RangedForCode{NormalContainer: new(core.NormalContainer)}
	tokens, err := p.ReadTokenList()
	if err != nil {
		return nil, err
	}
	if len(tokens) != 2 {
		return nil, p.Error("Invalid for loop, ranged loop should have two variables")
	}
	code.tokens = tokens
	c := p.SkipSpaces()
	if c == ':' {
		c = p.Next()
	}
	if c != '=' {
		return nil, p.Error("Invalid for loop, ranged loop expecting assignment operator")
	}
	p.Next()

	if p.SkipSpaces() != 'r' || p.ConsumeIf([]byte("range")) == false {
		return nil, p.Error("invalid for loop, expected 'range' keyword")
	}

	value, err := p.ReadValue()
	if err != nil {
		return nil, err
	}
	code.value = value
	if p.SkipSpaces() != '{' {
		return nil, p.Error("Missing openening brace for if statement")
	}
	p.Next()

	return code, nil

}