token

package
v7.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2020 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package token 解析 xml 内容

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(h *core.MessageHandler, p *Parser, v interface{}, namespace string)

Decode 将 p 中的 XML 内容解码至 v 对象中

Decode 中所有返回的错误对象,都可以转换成 *core.SyntaxError

func Encode

func Encode(indent string, v interface{}, namespace, prefix string) ([]byte, error)

Encode 将 v 转换成 XML 内容

namespace 指定 XML 的命名空间; prefix 命名空间的前缀,如果为空表示使用默认命名空间;

Types

type AttrDecoder

type AttrDecoder interface {
	// 解析属性值
	//
	// 接口应该只返回 *core.SyntaxError 作为错误对象。
	DecodeXMLAttr(p *Parser, attr *Attribute) error
}

AttrDecoder 实现从 attr 中解码内容到当前对象的值

type AttrEncoder

type AttrEncoder interface {
	// 仅需要返回属性的 XML 表示,不需要包含属性值的引号字符。
	EncodeXMLAttr() (string, error)
}

AttrEncoder 将属性值编码成符合 XML 规范的值

type Attribute

type Attribute struct {
	core.Range
	Name  Name
	Value String
}

Attribute 表示 XML 属性

type Base

type Base struct {
	core.Range
	UsageKey message.Reference `apidoc:"-"` // 表示对当前元素的一个说明内容的翻译 ID
}

Base 所有 XML 节点的基本元素

type BaseAttribute

type BaseAttribute struct {
	Base
	AttributeName Name `apidoc:"-"`
}

BaseAttribute 所有 XML 属性节点的基本元素

type BaseTag

type BaseTag struct {
	Base
	StartTag Name `apidoc:"-"` // 表示起始标签名
	EndTag   Name `apidoc:"-"` // 表示标签的结束名称,如果是自闭合的标签,此值为空。
}

BaseTag 所有 XML 标签的基本元素

type CData

type CData struct {
	BaseTag
	Value String
}

CData 表示 XML 的 CDATA 数据

type Comment

type Comment struct {
	core.Range
	Value String
}

Comment 表示 XML 的注释

type Decoder

type Decoder interface {
	// 从 p 中读取内容并实例化到当前对象中
	//
	// 必须要同时从 p 中读取相应的 EndElement 才能返回。
	// end 表示 EndElement.End 的值。
	//
	// NOTE: 如果是自闭合标签,则不会调用该接口。
	//
	// 接口应该只返回 *core.SyntaxError 作为错误对象。
	DecodeXML(p *Parser, start *StartElement) (end *EndElement, err error)
}

Decoder 实现从 p 中解码内容到当前对象的值

type Encoder

type Encoder interface {
	// 仅需要返回元素内容的 XML 编码,不需要包含本身的标签和属性。
	EncodeXML() (string, error)
}

Encoder 将元素内容编码成 XML 内容

type EndElement

type EndElement struct {
	core.Range
	Name Name
}

EndElement XML 的结束元素

type Instruction

type Instruction struct {
	core.Range
	Name       String
	Attributes []*Attribute
}

Instruction 表示 XML 的指令

type Name

type Name struct {
	core.Range
	Prefix String
	Local  String
}

Name 表示 XML 中的名称

func (Name) Equal

func (n Name) Equal(v Name) bool

Equal 两个 name 是否相等

func (Name) String

func (n Name) String() string

String fmt.Stringer

type Parser

type Parser struct {
	*lexer.Lexer
}

Parser 代码块的解析器

func NewParser

func NewParser(b core.Block) (*Parser, error)

NewParser 声明新的 Parser 实例

func (*Parser) NewError

func (p *Parser) NewError(start, end core.Position, field string, key message.Reference, v ...interface{}) error

NewError 生成 *core.SyntaxError 对象

func (*Parser) Token

func (p *Parser) Token() (interface{}, core.Range, error)

Token 返回下一个 token 对象

token 可能的类型为 *StartElement、*EndElement、*Instruction、*Attribute、*CData、*Comment 和 *String。 其中 *String 用于表示 XML 元素的内容。

当返回 nil, io.EOF 时,表示已经结束

func (*Parser) WithError

func (p *Parser) WithError(start, end core.Position, field string, err error) error

WithError 将 err 包装成 *core.SyntaxError 类型

如果 err 本身就是 *core.SyntaxError 类型,则只取 err.Err 作为返回对象有的 Err 字段,其它字段弃用。

type Sanitizer

type Sanitizer interface {
	// 验证数据是否正确
	//
	// 可以通过 p.NewError 和 p.WithError 返回 *core.SyntaxError 类型的错误
	Sanitize(p *Parser) error
}

Sanitizer 用于验证和修改对象中的数据

type StartElement

type StartElement struct {
	core.Range
	Name       Name
	Attributes []*Attribute
	Close      bool // 是否自闭合
}

StartElement 表示 XML 的元素

func (*StartElement) Match

func (s *StartElement) Match(end *EndElement) bool

Match 是否与 end 相匹配

type String

type String struct {
	core.Range
	Value string
}

String 表示 XML 的字符串数据

type Tip

type Tip struct {
	core.Range
	Usage string
}

Tip 定义了 LSP 查找功能返回的提示内容

func SearchUsage

func SearchUsage(v reflect.Value, pos core.Position, exclude ...string) (tip *Tip)

SearchUsage 根据 r 从 v 中查找相应的 usage 字段内容

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL