Exemplo n.º 1
0
func decodeArray(s *lex.Scanner) (Element, error) {
	var a Array
	for s.Next() {
		switch s.Token {
		case lex.DataToken:
			a = append(a, DataString(s.Data))
		case lex.ArrayOpen:
			el, err := decodeArray(s)
			if err != nil {
				return nil, err
			}
			a = append(a, el)

		case lex.ArrayClose:
			return a, nil
		}
	}
	return nil, ErrMissingClose
}
Exemplo n.º 2
0
func decodeDict(s *lex.Scanner) (Element, error) {
	d := make(Dict)
	for s.Next() {
		switch s.Token {
		case lex.DataToken:
			key := DataString(s.Data)
			el, err := decodeElement(s)
			if err != nil {
				return nil, err
			}
			d[key] = el
		case lex.DictClose:
			return d, nil
		default:
			return nil, ErrBadKeyType
		}
	}
	return nil, ErrMissingClose
}
Exemplo n.º 3
0
func decodeElement(s *lex.Scanner) (Element, error) {
	if !s.Next() {
		return nil, ErrUnexpectedClose
	}

	switch s.Token {
	case lex.DataToken:
		return DataString(s.Data), nil
	case lex.ArrayOpen:
		return decodeArray(s)
	case lex.ArrayClose:
		return nil, ErrUnexpectedClose
	case lex.DictOpen:
		return decodeDict(s)
	case lex.DictClose:
		return nil, ErrUnexpectedClose
	}
	panic("Unknown lexer token type")
}