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


Golang Interface.Next方法代码示例

本文整理汇总了Golang中github.com/katydid/katydid/parser.Interface.Next方法的典型用法代码示例。如果您正苦于以下问题:Golang Interface.Next方法的具体用法?Golang Interface.Next怎么用?Golang Interface.Next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/katydid/katydid/parser.Interface的用法示例。


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

示例1: encode

func encode(p parser.Interface) (interface{}, error) {
	var fields = make(map[string]interface{})
	var list []interface{}
	for {
		if err := p.Next(); err != nil {
			if err == io.EOF {
				if len(fields) > 0 {
					return fields, nil
				}
				if list != nil {
					return list, nil
				}
				return nil, nil
			}
			return nil, err
		}
		if p.IsLeaf() {
			if v, err := p.Int(); err == nil {
				return v, nil
			}
			if v, err := p.Uint(); err == nil {
				return v, nil
			}
			if v, err := p.Double(); err == nil {
				return v, nil
			}
			if v, err := p.Bool(); err == nil {
				return v, nil
			}
			if v, err := p.String(); err == nil {
				return v, nil
			}
			if v, err := p.Bytes(); err == nil {
				return base64.StdEncoding.EncodeToString(v), nil
			}
			return nil, nil
		}
		if _, err := p.Int(); err == nil {
			p.Down()
			item, err := encode(p)
			if err != nil {
				return nil, err
			}
			p.Up()
			list = append(list, item)
			continue
		}
		name, err := p.String()
		if err != nil {
			return nil, err
		}
		p.Down()
		value, err := encode(p)
		if err != nil {
			return nil, err
		}
		p.Up()
		fields[name] = value
	}
}
开发者ID:katydid,项目名称:katydid,代码行数:60,代码来源:json.go

示例2: encode

func (this *msg) encode(buf []byte, offset int, p parser.Interface) ([]byte, int, error) {
	for {
		if err := p.Next(); err != nil {
			if err == io.EOF {
				return buf, offset, nil
			} else {
				return nil, 0, err
			}
		}
		name, err := p.String()
		if err != nil {
			return nil, 0, err
		}
		fieldEnc, ok := this.fieldEncoders[name]
		if !ok {
			continue //skip field
		}
		p.Down()
		buf, offset, err = fieldEnc.encode(buf, offset, p)
		if err != nil {
			return nil, 0, err
		}
		p.Up()
	}
}
开发者ID:katydid,项目名称:katydid,代码行数:25,代码来源:proto.go

示例3: deriv

func deriv(mem *Mem, patterns int, tree parser.Interface) (int, error) {
	for {
		if !mem.escapable(patterns) {
			return patterns, nil
		}
		if err := tree.Next(); err != nil {
			if err == io.EOF {
				break
			} else {
				return 0, err
			}
		}
		callTree, err := mem.getCallTree(patterns)
		if err != nil {
			return 0, err
		}
		childPatterns, stackElm, err := callTree.Eval(tree)
		if err != nil {
			return 0, err
		}
		if !tree.IsLeaf() {
			tree.Down()
			childPatterns, err = deriv(mem, childPatterns, tree)
			if err != nil {
				return 0, err
			}
			tree.Up()
		}
		nullIndex := mem.getNullable(childPatterns)
		patterns = mem.getReturn(stackElm, nullIndex)
	}
	return patterns, nil
}
开发者ID:katydid,项目名称:katydid,代码行数:33,代码来源:deriv.go

示例4: encode

func encode(e *xml.Encoder, p parser.Interface) error {
	for {
		if err := p.Next(); err != nil {
			if err == io.EOF {
				break
			} else {
				return err
			}
		}
		if p.IsLeaf() {
			if err := e.EncodeToken(getCharData(p)); err != nil {
				return err
			}
		} else {
			name := getName(p)
			if err := e.EncodeToken(xml.StartElement{Name: name}); err != nil {
				return err
			}
			p.Down()
			if err := encode(e, p); err != nil {
				return err
			}
			p.Up()
			if err := e.EncodeToken(xml.EndElement{Name: name}); err != nil {
				return err
			}
		}
	}
	return nil
}
开发者ID:katydid,项目名称:katydid,代码行数:30,代码来源:xml.go

示例5: newValue

func newValue(p parser.Interface, typ reflect.Type) (reflect.Value, error) {
	if err := p.Next(); err != nil {
		if err == io.EOF {
			return reflect.ValueOf(nil), nil
		} else {
			panic(err)
		}
	}
	if !p.IsLeaf() {
		return reflect.ValueOf(nil), fmt.Errorf("expected leaf")
	}
	if value, err := p.Int(); err == nil {
		return reflect.ValueOf(value), nil
	}
	if value, err := p.Uint(); err == nil {
		return reflect.ValueOf(value), nil
	}
	if value, err := p.Double(); err == nil {
		return reflect.ValueOf(value), nil
	}
	if value, err := p.Bool(); err == nil {
		return reflect.ValueOf(value), nil
	}
	if value, err := p.String(); err == nil {
		return reflect.ValueOf(value), nil
	}
	if value, err := p.Bytes(); err == nil {
		return reflect.ValueOf(value), nil
	}
	return reflect.ValueOf(nil), nil
}
开发者ID:katydid,项目名称:katydid,代码行数:31,代码来源:reflect.go

示例6: deriv

func deriv(auto *Auto, current int, tree parser.Interface) (int, error) {
	for {
		if !auto.escapables[current] {
			return current, nil
		}
		if err := tree.Next(); err != nil {
			if err == io.EOF {
				break
			} else {
				return 0, err
			}
		}
		callTree := auto.calls[current]
		childState, stackElm, err := callTree.Eval(tree)
		if err != nil {
			return 0, err
		}
		if !tree.IsLeaf() {
			tree.Down()
			childState, err = deriv(auto, childState, tree)
			if err != nil {
				return 0, err
			}
			tree.Up()
		}
		nullIndex := auto.stateToNullable[childState]
		current = auto.returns[stackElm].lookup(nullIndex)
	}
	return current, nil
}
开发者ID:katydid,项目名称:katydid,代码行数:30,代码来源:auto.go

示例7: RandomWalk

//RandomWalk does a random walk of the parser, given two probabilities.
//  next is passed to r.Intn and when a zero value is returned the Next method on the parser is called.
//  down is passed to r.Intn and when a non zero value is returned the Down method on the parser is called.
//RandomWalk is great for testing that the implemented parser can handle random skipping of parts of the tree.
func RandomWalk(p parser.Interface, r Rand, next, down int) Nodes {
	a := make(Nodes, 0)
	for {
		if r.Intn(next) == 0 {
			break
		}
		if err := p.Next(); err != nil {
			if err == io.EOF {
				break
			} else {
				panic(err)
			}
		}
		value := getValue(p)
		if p.IsLeaf() {
			a = append(a, Node{fmt.Sprintf("%#v", value), nil})
		} else {
			name := fmt.Sprintf("%#v", value)
			var v Nodes
			if r.Intn(down) != 0 {
				p.Down()
				v = RandomWalk(p, r, next, down)
				p.Up()
			}
			a = append(a, Node{name, v})
		}
	}
	return a
}
开发者ID:katydid,项目名称:katydid,代码行数:33,代码来源:walk.go

示例8: newList

func newList(p parser.Interface, typ reflect.Type) (reflect.Value, error) {
	list := reflect.MakeSlice(typ, 0, 0)
	for {
		if err := p.Next(); err != nil {
			if err == io.EOF {
				return list, nil
			} else {
				panic(err)
			}
		}
		if p.IsLeaf() {
			return reflect.ValueOf(nil), fmt.Errorf("list: did not expect leaf")
		}
		_, err := p.Int()
		if err != nil {
			return reflect.ValueOf(nil), err
		}
		elemType := typ.Elem()
		elemKind := elemType.Kind()
		elemIsPtr := false
		if elemKind == reflect.Ptr {
			elemIsPtr = true
			elemType = typ.Elem().Elem()
			elemKind = elemType.Kind()
		}
		p.Down()
		switch elemKind {
		case reflect.Struct:
			if !p.IsLeaf() {
				elem := reflect.New(elemType).Elem()
				if err := encodeStruct(p, elem); err != nil {
					return reflect.ValueOf(nil), err
				}
				if elemIsPtr {
					elem = elem.Addr()
				}
				list = reflect.Append(list, elem)
			} else {
				list = reflect.Append(list, reflect.Zero(typ.Elem()))
			}
		case reflect.Slice:
			newList, err := newList(p, elemType)
			if err != nil {
				return reflect.ValueOf(nil), err
			}
			list = reflect.Append(list, newList)
		default:
			elem, err := newValue(p, typ.Elem())
			if err != nil {
				return reflect.ValueOf(nil), err
			}
			list = reflect.Append(list, elem)
		}
		p.Up()
	}
}
开发者ID:katydid,项目名称:katydid,代码行数:56,代码来源:reflect.go

示例9: next

func next(t *testing.T, parser parser.Interface) {
	if err := parser.Next(); err != nil {
		if err != io.EOF {
			t.Fatal(err)
		}
	}
}
开发者ID:katydid,项目名称:katydid,代码行数:7,代码来源:proto_test.go

示例10: Walk

//Walk walks through the whole parser in a top down manner and records the values into a Nodes structute.
func Walk(p parser.Interface) Nodes {
	a := make(Nodes, 0)
	for {
		if err := p.Next(); err != nil {
			if err == io.EOF {
				break
			} else {
				panic(err)
			}
		}
		value := getValue(p)
		if p.IsLeaf() {
			a = append(a, Node{fmt.Sprintf("%v", value), nil})
		} else {
			name := fmt.Sprintf("%v", value)
			p.Down()
			v := Walk(p)
			p.Up()
			a = append(a, Node{name, v})
		}
	}
	return a
}
开发者ID:katydid,项目名称:katydid,代码行数:24,代码来源:walk.go

示例11: encodeStruct

func encodeStruct(p parser.Interface, v reflect.Value) error {
	kind := v.Type().Kind()
	isPtr := false
	if kind == reflect.Ptr {
		kind = v.Type().Elem().Kind()
		isPtr = true
	}
	if kind != reflect.Struct {
		return fmt.Errorf("expected struct")
	}
	for {
		if err := p.Next(); err != nil {
			if err == io.EOF {
				return nil
			} else {
				panic(err)
			}
		}
		if p.IsLeaf() {
			return fmt.Errorf("struct: did not expect leaf")
		}
		name, err := p.String()
		if err != nil {
			return err
		}
		strct := v
		if isPtr {
			strct = v.Elem()
		}
		field := strct.FieldByName(name)
		if !field.IsValid() {
			continue //skip field
		}
		fieldKind := field.Kind()
		fieldIsPtr := false
		fieldType := field.Type()
		if fieldKind == reflect.Ptr {
			fieldIsPtr = true
			fieldKind = field.Type().Elem().Kind()
			fieldType = field.Type().Elem()
		}
		p.Down()
		switch fieldKind {
		case reflect.Struct:
			if !p.IsLeaf() {
				if fieldIsPtr {
					field.Set(reflect.New(field.Type().Elem()))
				}
				if err := encodeStruct(p, field); err != nil {
					return err
				}
			}
		case reflect.Slice:
			list, err := newList(p, fieldType)
			if err != nil {
				return err
			}
			field.Set(list)
		default:

			value, err := newValue(p, fieldType)
			if err != nil {
				return err
			}
			field.Set(value)
		}
		p.Up()
	}
}
开发者ID:katydid,项目名称:katydid,代码行数:69,代码来源:reflect.go


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