mini

package
v0.0.0-...-88345fd Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2015 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

mini 包提供 minified Go 支持, 以紧凑格式输出 Go 源码. 该格式为:

单行 Go 源码, 并且无法再缩减掉任何一个间隔符.

此单行(尾部无换行符) Go 源码不包含除权威导入路径之外的注释.

Index

Constants

View Source
const ErrILLEGAL = errorString("ILLEGAL") // 扫描错误 ILLEGAL.

Variables

This section is empty.

Functions

func Compare

func Compare(a, b ast.Node) bool

Compare 遍历比较 a, b 是否相同(除注释之外). 事实上只是简单调用了 Diff:

a, b = Diff(a, b)
return a == b

func Diff

func Diff(a, b ast.Node) (na, nb ast.Node)

Diff 遍历两个 AST 节点 a, b, 比较权威导入路径 (Custom Import Path) 和其它非注释节点, 相同返回 nil, nil, 否则返回一对儿不同的子节点.

NOTE(yhc): 算法缺陷:

如果两个参数都不是 minified Go 节点,
比较权威导入路径的算法可能会误判. 例如下列极端情况:
	package name
	// import "name"
如果该情况出现, 那么上段代码因为有换行, 所以未定义权威导入路径,
分析换行需要引入 token.File 或者 token.FileSet. Diff 未引入它们,
所以会误判为定义了权威导入路径. 经格式化的代码不会出现此情况,
Diff 精简了参数, 忽略了此情况.

func Examine

func Examine(src []byte) error

func Write

func Write(w io.Writer, src []byte) (int, error)

Write 等同于 New(w, 0).Write(src); 不负责语法检查.

func WriteString

func WriteString(w io.Writer, src string) (int, error)

WriteString 等同于 New(w, 0).Write([]byte(src)); 不负责语法检查.

Types

type Mini

type Mini struct {
	// contains filtered or unexported fields
}

Mini 提供 minified Go 支持, 必须使用 New 创建.

func New

func New(w io.Writer, mode Mode) *Mini

New 新建并返回一个 Mini. 参数 w 为 minified Go 输出目标. 如果 w 为 nil, 使用 os.Stdout 替代. 参数 mode 参见 Mode 常量.

func (*Mini) FileSet

func (m *Mini) FileSet() *token.FileSet

FileSet 返回最后一次 Write 新建的 token.FileSet. 它可能为 nil, 或只包含一个 token.File 的 token.FileSet.

func (*Mini) Write

func (m *Mini) Write(src []byte) (int, error)

Write 方法扫描 src, 并向输出目标写入 minified Go; 不负责语法检查. src 为已经格式化的 Go 源码(片段). Write 总是新建 token.FileSet 和命名为 "minifiedGo" 的 token.File, 调用者可以用 FileSet 方法得到 token.FileSet.

func (*Mini) WriteString

func (m *Mini) WriteString(src string) (int, error)

WriteString 等同于 m.Write([]byte(src)); 不负责语法检查.

func (*Mini) WriteToken

func (m *Mini) WriteToken(pos token.Pos, tok token.Token, lit string) bool

WriteToken 接收扫描结果并以 minified Go 输出到目标; 不负责语法检查. 返回 false 表示调用者应停止调用该方法. 该方法应作为 ScanLooker 使用.

type Mode

type Mode int
const (
	OmitImportPath Mode = 1 << iota // 忽略权威导入路径; 默认包含.
)

Jump to

Keyboard shortcuts

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