Example #1
0
File: parser.go Project: h12w/gombi
func (p *parser) parseParamDecl(n *parse.Node, scope *ast.Scope) *ast.Field {
	field := ast.Field{}
	if n.Child(0).Is(type_) {
		field.Type = p.parseType(n.Child(0))
	} else {
		field.Names = p.parseIdentList(n.Child(0))
		if n.Child(1).Is(type_) {
			field.Type = p.parseType(n.Child(1))
		} else {
			// TODO ...
			field.Type = p.parseType(n.Child(2))
		}
	}
	p.declare(field, nil, scope, ast.Var, field.Names...)
	if field.Type != nil {
		p.resolve(field.Type)
	}
	return &field
}
Example #2
0
// Struct creates a struct{} expression. The arguments are a series
// of name/type/tag tuples. Name must be of type *ast.Ident, type
// must be of type ast.Expr, and tag must be of type *ast.BasicLit,
// The number of arguments must be a multiple of 3, or a run-time
// panic will occur.
func Struct(args ...ast.Expr) *ast.StructType {
	fields := new(ast.FieldList)
	if len(args)%3 != 0 {
		panic("Number of args to FieldList must be a multiple of 3, got " + strconv.Itoa(len(args)))
	}
	for i := 0; i < len(args); i += 3 {
		var field ast.Field
		name, typ, tag := args[i], args[i+1], args[i+2]
		if name != nil {
			field.Names = []*ast.Ident{name.(*ast.Ident)}
		}
		if typ != nil {
			field.Type = typ
		}
		if tag != nil {
			field.Tag = tag.(*ast.BasicLit)
		}
		fields.List = append(fields.List, &field)
	}
	return &ast.StructType{Fields: fields}
}