Exemple #1
0
func (p *parser) parseForStmt(n *parse.Node) (r ast.Stmt) {
	p.openScope()
	forPos := token.Pos(n.Child(0).Pos())
	n = n.Child(1)
	if n.Is(block) {
		return &ast.ForStmt{
			For:  forPos,
			Body: p.parseBlock(n, p.topScope),
		}
	}
	option := n.Child(0).Child(0)
	body := p.parseBlock(n.Child(1), p.topScope)
	switch option.Rule() {
	case condition:
		fmt.Println(option)
	case forClause:
		fmt.Println(option)
	case rangeClause:
		forStmt := p.parseRangeStmt(option)
		forStmt.For, forStmt.Body = forPos, body
		r = forStmt
	}
	p.closeScope()
	return
}
Exemple #2
0
func (p *parser) parseElement(n *parse.Node) ast.Expr {
	n = n.Child(0)
	if n.Is(value) {
		return p.parseValue(n)
	}
	return p.parseKeyValue(n)
}
Exemple #3
0
func (p *parser) parseArgs(n *parse.Node) ([]ast.Expr, token.Pos) {
	n = n.Child(0)
	if n.Is(exprList) {
		return p.parseExprList(n), 0
	}
	return p.parseExprList(n.Child(0)), token.Pos(n.Child(1).Pos())
}
Exemple #4
0
func (p *parser) parseInitCond(n *parse.Node) (ast.Stmt, ast.Expr) {
	n = n.Child(0)
	if n.Is(expr) {
		return nil, p.parseCond(n)
	} else {
		return p.parseInit(n.Child(0)), p.parseCond(n.Child(2))
	}
}
Exemple #5
0
func (p *parser) parseVarSpec(n *parse.Node) *ast.ValueSpec {
	if n.Is(valueSpec) {
		return p.parseValueSpec(n)
	}
	return &ast.ValueSpec{
		Names: p.parseIdentList(n.Child(0)),
		Type:  p.parseType(n.Child(1)),
	}
}
Exemple #6
0
func (p *parser) parsePost(n *parse.Node) ast.Stmt {
	if n == nil {
		return nil
	}
	n = n.Child(0)
	if !n.Is(forClause) {
		return nil
	}
	fmt.Println(n)
	return nil
}
Exemple #7
0
func (p *parser) parseValueSpec(n *parse.Node) *ast.ValueSpec {
	n = n.Child(0)
	spec := ast.ValueSpec{}
	if n.Is(identifierList) {
		spec.Names = p.parseIdentList(n)
	} else {
		spec.Names = p.parseIdentList(n.Child(0))
		if n.Child(1).Is(type_) {
			spec.Type = p.parseType(n.Child(1))
		}
		spec.Values = p.parseExprList(n.LastChild())
	}
	return &spec
}
Exemple #8
0
func (p *parser) parseImportDecl(n *parse.Node) (decl *ast.GenDecl, specs []*ast.ImportSpec) {
	decl = &ast.GenDecl{
		TokPos: token.Pos(n.Child(0).Pos()),
		Tok:    token.IMPORT,
	}
	n = n.Child(1)
	if n.Is(importSpec) {
		spec := p.parseImportSpec(n)
		specs = append(specs, spec)
		decl.Specs = append(decl.Specs, spec)
		return
	}
	decl.Lparen = token.Pos(n.Child(0).Pos())
	decl.Rparen = token.Pos(n.LastChild().Pos())
	if n.ChildCount() == 3 {
		eachListItem(importSpec, n.Child(1), func(item *parse.Node) {
			spec := p.parseImportSpec(item)
			specs = append(specs, spec)
			decl.Specs = append(decl.Specs, spec)
		})
	}
	return
}