예제 #1
0
func parseConstant(s string) *ast.Constant {
	prog, err := idl.Parse([]byte(s))
	if err != nil {
		panic(fmt.Sprintf("failure to parse: %v: %s", err, s))
	}

	if len(prog.Definitions) != 1 {
		panic("parseConstant may be used to parse single constants only")
	}

	return prog.Definitions[0].(*ast.Constant)
}
예제 #2
0
func parseService(s string) *ast.Service {
	prog, err := idl.Parse([]byte(s))
	if err != nil {
		panic(fmt.Sprintf("failure to parse: %v: %s", err, s))
	}

	if len(prog.Definitions) != 1 {
		panic("parseService may be used to parse single services only")
	}

	return prog.Definitions[0].(*ast.Service)
}
예제 #3
0
func parseTypedef(s string) *ast.Typedef {
	prog, err := idl.Parse([]byte(s))
	if err != nil {
		panic(fmt.Sprintf("failure to parse: %v: %s", err, s))
	}

	if len(prog.Definitions) != 1 {
		panic("parseTypedef may be used to parse a single typedef only")
	}

	return prog.Definitions[0].(*ast.Typedef)
}
예제 #4
0
// load populates the compiler with information from the given Thrift file.
//
// The types aren't actually compiled in this step.
func (c compiler) load(p string) (*Module, error) {
	p, err := c.fs.Abs(p)
	if err != nil {
		return nil, err
	}

	if m, ok := c.Modules[p]; ok {
		// Already loaded.
		return m, nil
	}

	s, err := c.fs.Read(p)
	if err != nil {
		return nil, fileReadError{Path: p, Reason: err}
	}

	prog, err := idl.Parse(s)
	if err != nil {
		return nil, parseError{Path: p, Reason: err}
	}

	m := &Module{
		Name:       fileBaseName(p),
		ThriftPath: p,
		Includes:   make(map[string]*IncludedModule),
		Constants:  make(map[string]*Constant),
		Types:      make(map[string]TypeSpec),
		Services:   make(map[string]*ServiceSpec),
	}
	c.Modules[p] = m
	// the module is added to the map before processing includes to break
	// cyclic includes.

	if err := c.gather(m, prog); err != nil {
		return nil, fileCompileError{Path: p, Reason: err}
	}
	return m, nil
}