当前位置: 首页>>代码示例>>Golang>>正文


Golang ast.Extension类代码示例

本文整理汇总了Golang中github.com/myitcv/protobuf/ast.Extension的典型用法代码示例。如果您正苦于以下问题:Golang Extension类的具体用法?Golang Extension怎么用?Golang Extension使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Extension类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: readExtension

func (p *parser) readExtension(ext *ast.Extension) *parseError {
	if err := p.readToken("extend"); err != nil {
		return err
	}
	ext.Position = p.cur.astPosition()

	tok := p.next()
	if tok.err != nil {
		return tok.err
	}
	ext.Extendee = tok.value // checked during resolution

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

	for !p.done {
		tok := p.next()
		if tok.err != nil {
			return tok.err
		}
		if tok.value == "}" {
			// end of extension
			return nil
		}
		p.back()
		field := new(ast.Field)
		ext.Fields = append(ext.Fields, field)
		field.Up = ext // p.readFile uses this
		if err := p.readField(field); err != nil {
			return err
		}
	}
	return p.errorf("unexpected EOF while parsing extension")
}
开发者ID:myitcv,项目名称:old_protobuf,代码行数:35,代码来源:parser.go

示例2: resolveExtension

func (r *resolver) resolveExtension(s *scope, ext *ast.Extension) error {
	o := r.resolveName(s, ext.Extendee)
	if o == nil {
		return fmt.Errorf("failed to resolve name %q", ext.Extendee)
	}
	m, ok := o.last().(*ast.Message)
	if !ok {
		return fmt.Errorf("extendee %q resolved to non-message %T", ext.Extendee, o.last())
	}
	ext.ExtendeeType = m
	// Resolve fields.
	for _, field := range ext.Fields {
		ft, ok := r.resolveFieldTypeName(s, field.TypeName)
		if !ok {
			return fmt.Errorf("failed to resolve name %q", field.TypeName)
		}
		field.Type = ft

		// TODO: Map fields should be forbidden?
	}
	return nil
}
开发者ID:myitcv,项目名称:old_protobuf,代码行数:22,代码来源:resolver.go

示例3: readMessageContents

func (p *parser) readMessageContents(msg *ast.Message) *parseError {
	// Parse message fields and other things inside a message.
	var oneof *ast.Oneof // set while inside a oneof
	for !p.done {
		tok := p.next()
		if tok.err != nil {
			return tok.err
		}
		switch tok.value {
		case "extend":
			// extension
			p.back()
			ext := new(ast.Extension)
			msg.Extensions = append(msg.Extensions, ext)
			if err := p.readExtension(ext); err != nil {
				return err
			}
			ext.Up = msg
		case "oneof":
			// oneof
			if oneof != nil {
				return p.errorf("nested oneof not permitted")
			}
			oneof = new(ast.Oneof)
			msg.Oneofs = append(msg.Oneofs, oneof)
			oneof.Position = p.cur.astPosition()

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

			if err := p.readToken("{"); err != nil {
				return err
			}
		case "message":
			// nested message
			p.back()
			nmsg := new(ast.Message)
			msg.Messages = append(msg.Messages, nmsg)
			if err := p.readMessage(nmsg); err != nil {
				return err
			}
			nmsg.Up = msg
		case "enum":
			// nested enum
			p.back()
			ne := new(ast.Enum)
			msg.Enums = append(msg.Enums, ne)
			if err := p.readEnum(ne); err != nil {
				return err
			}
			ne.Up = msg
		case "extensions":
			// extension range
			p.back()
			r, err := p.readExtensionRange()
			if err != nil {
				return err
			}
			msg.ExtensionRanges = append(msg.ExtensionRanges, r...)
		default:
			// field; this token is required/optional/repeated,
			// a primitive type, or a named type.
			p.back()
			field := new(ast.Field)
			msg.Fields = append(msg.Fields, field)
			field.Oneof = oneof
			field.Up = msg // p.readField uses this
			if err := p.readField(field); err != nil {
				return err
			}
		case "}":
			if oneof != nil {
				// end of oneof
				oneof = nil
				continue
			}
			// end of message
			p.back()
			return nil
		}
	}
	return p.errorf("unexpected EOF while parsing message")
}
开发者ID:myitcv,项目名称:old_protobuf,代码行数:87,代码来源:parser.go

示例4: readFile


//.........这里部分代码省略.........
			tok, err := p.readString()
			if err != nil {
				return err
			}
			f.Imports = append(f.Imports, tok.unquoted)
			if err := p.readToken(";"); err != nil {
				return err
			}
		case "message":
			p.back()
			msg := new(ast.Message)
			f.Messages = append(f.Messages, msg)
			if err := p.readMessage(msg); err != nil {
				return err
			}
			msg.Up = f
		case "enum":
			p.back()
			enum := new(ast.Enum)
			f.Enums = append(f.Enums, enum)
			if err := p.readEnum(enum); err != nil {
				return err
			}
			enum.Up = f
		case "service":
			p.back()
			srv := new(ast.Service)
			f.Services = append(f.Services, srv)
			if err := p.readService(srv); err != nil {
				return err
			}
			srv.Up = f
		case "extend":
			p.back()
			ext := new(ast.Extension)
			f.Extensions = append(f.Extensions, ext)
			if err := p.readExtension(ext); err != nil {
				return err
			}
			ext.Up = f
		default:
			return p.errorf("unknown top-level thing %q", tok.value)
		}
	}

	// Handle comments.
	for len(p.comments) > 0 {
		n := 1
		for ; n < len(p.comments); n++ {
			if p.comments[n].line != p.comments[n-1].line+1 {
				break
			}
		}
		c := &ast.Comment{
			Start: ast.Position{
				Line:   p.comments[0].line,
				Offset: p.comments[0].offset,
			},
			End: ast.Position{
				Line:   p.comments[n-1].line,
				Offset: p.comments[n-1].offset,
			},
		}
		for _, comm := range p.comments[:n] {
			c.Text = append(c.Text, comm.text)
		}
		p.comments = p.comments[n:]

		// Strip common whitespace prefix and any whitespace suffix.
		// TODO: this is a bodgy implementation of Longest Common Prefix,
		// and also doesn't do tabs vs. spaces well.
		var prefix string
		for i, line := range c.Text {
			line = strings.TrimRightFunc(line, unicode.IsSpace)
			c.Text[i] = line
			trim := len(line) - len(strings.TrimLeftFunc(line, unicode.IsSpace))
			if i == 0 {
				prefix = line[:trim]
			} else {
				// Check how much of prefix is in common.
				for !strings.HasPrefix(line, prefix) {
					prefix = prefix[:len(prefix)-1]
				}
			}
			if prefix == "" {
				break
			}
		}
		if prefix != "" {
			for i, line := range c.Text {
				c.Text[i] = strings.TrimPrefix(line, prefix)
			}
		}

		f.Comments = append(f.Comments, c)
	}
	// No need to sort comments; they are already in source order.

	return nil
}
开发者ID:myitcv,项目名称:old_protobuf,代码行数:101,代码来源:parser.go


注:本文中的github.com/myitcv/protobuf/ast.Extension类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。