Beispiel #1
0
func (r *resolver) resolveMethod(s *scope, mth *ast.Method) error {
	o := r.resolveName(s, mth.InTypeName)
	if o == nil {
		return fmt.Errorf("failed to resolve name %q", mth.InTypeName)
	}
	mth.InType = o.last()

	o = r.resolveName(s, mth.OutTypeName)
	if o == nil {
		return fmt.Errorf("failed to resolve name %q", mth.OutTypeName)
	}
	mth.OutType = o.last()

	return nil
}
Beispiel #2
0
func (p *parser) readMethodOptions(mth *ast.Method) *parseError {
	if err := p.readToken("{"); err != nil {
		return err
	}
	for !p.done {
		tok := p.next()
		switch tok.value {
		case "}":
			// End of Options
			return nil
		case "option":
			//Handled below
		default:
			return p.errorf(`got %q, want "option" or "}"`, tok.value)
		}
		if err := p.readToken("("); err != nil {
			return err
		}
		tok = p.next()
		if tok.err != nil {
			return tok.err
		}
		key := tok.value
		if err := p.readToken(")"); err != nil {
			return err
		}
		if err := p.readToken("="); err != nil {
			return err
		}
		tok = p.next()
		if tok.err != nil {
			return tok.err
		}
		val := tok.value
		if err := p.readToken(";"); err != nil {
			return err
		}
		mth.Options = append(mth.Options, [2]string{key, val})
	}
	return nil
}
Beispiel #3
0
func (p *parser) readService(srv *ast.Service) *parseError {
	if err := p.readToken("service"); err != nil {
		return err
	}
	srv.Position = p.cur.astPosition()

	tok := p.next()
	if tok.err != nil {
		return tok.err
	}
	srv.Name = tok.value // TODO: validate

	if err := p.readToken("{"); err != nil {
		return err
	}

	// Parse methods
	for !p.done {
		tok := p.next()
		if tok.err != nil {
			return tok.err
		}
		switch tok.value {
		case "}":
			// end of service
			return nil
		case "rpc":
			// handled below
		default:
			return p.errorf(`got %q, want "rpc" or "}"`, tok.value)
		}

		tok = p.next()
		if tok.err != nil {
			return tok.err
		}
		mth := new(ast.Method)
		srv.Methods = append(srv.Methods, mth)
		mth.Position = tok.astPosition()
		mth.Name = tok.value // TODO: validate
		mth.Up = srv

		if err := p.readToken("("); err != nil {
			return err
		}

		tok = p.next()
		if tok.err != nil {
			return tok.err
		}
		mth.InTypeName = tok.value // TODO: validate
		if err := p.readToken(")"); err != nil {
			return err
		}
		if err := p.readToken("returns"); err != nil {
			return err
		}
		if err := p.readToken("("); err != nil {
			return err
		}
		tok = p.next()
		if tok.err != nil {
			return tok.err
		}
		mth.OutTypeName = tok.value // TODO: validate

		if err := p.readToken(")"); err != nil {
			return err
		}
		tok = p.next()
		if tok.err != nil {
			return tok.err
		}
		if tok.value == "{" {
			p.back()
			if err := p.readMethodOptions(mth); err != nil {
				return err
			}
		} else if tok.value != ";" {
			return p.errorf("unexpected %v while parsing Method", tok.value)
		}
	}

	return p.errorf("unexpected EOF while parsing service")
}