lexer

package
v7.2.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package lexer 提供基本的分词功能

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lexer

type Lexer struct {
	core.Block
	// contains filtered or unexported fields
}

Lexer 是对一个文本内容的包装,方便 blocker 等接口操作。

func New

func New(b core.Block) (*Lexer, error)

New 声明 Lexer 实例

func (*Lexer) All

func (l *Lexer) All() []byte

All 获取当前定位之后的所有内容

NOTE: 可回滚该操作

func (*Lexer) AtEOF

func (l *Lexer) AtEOF() bool

func (*Lexer) Bytes

func (l *Lexer) Bytes(start, end int) []byte

Bytes 返回指定范围的内容

NOTE: 并不会改变定位信息

func (*Lexer) Current added in v7.1.0

func (l *Lexer) Current() Position

Current 返回当前在 data 中的偏移量

func (*Lexer) Delim

func (l *Lexer) Delim(delim rune, contain bool) ([]byte, bool)

Delim 查找 delim 并返回到此字符的所有内容

NOTE: 可回滚此操作

func (*Lexer) DelimFunc

func (l *Lexer) DelimFunc(f func(r rune) bool, contain bool) ([]byte, bool)

DelimFunc 查找并返回当前位置到 f 确定位置的所有内容

contain 表示是否包含字符本身,如果为 false,则返回内容不包含,且该字符会退回至输入流中,等待下次被读取。

NOTE: 可回滚此操作

func (*Lexer) DelimString

func (l *Lexer) DelimString(delim string, contain bool) ([]byte, bool)

DelimString 查找 delim 并返回到此字符的所有内容

contain 表示是否包含 delim 本身,如果为 false,则返回内容不包含,且该字符串会退回至输入流中,等待下次被读取。

NOTE: 可回滚此操作

func (*Lexer) Match

func (l *Lexer) Match(word string) bool

Match 接下来的 n 个字符是否匹配指定的字符串, 若匹配,则将指定移向该字符串这后,否则不作任何操作。

NOTE: 可回滚该操作

func (*Lexer) Move

func (l *Lexer) Move(p Position)

Move 移动当前的分析器的位置

执行此操作之后,Rollback 将失效

不会限制 p 的值,如果将 p.Offset 的值设置为大于整个数据的长度, 在下次调用 AtEOF 时会返回 true;如果将 p.Offset 设置为负值,则 panic。

func (*Lexer) Next

func (l *Lexer) Next(n int) []byte

Next 返回之后的 n 个字符,或是直到内容结束

NOTE: 可回滚该操作

func (*Lexer) Rollback

func (l *Lexer) Rollback()

Rollback 回滚上一次的操作

func (*Lexer) Spaces

func (l *Lexer) Spaces(exclude rune) []byte

Spaces 获取之后的所有空格,不包含换行符

NOTE: 可回滚该操作

type Position

type Position struct {
	core.Position

	// 表示的是字节的偏移量,
	// 而 Position.Character 表示的是当前行`字符`的偏移量
	Offset int
}

Position 描述 Lexer 中的定位信息

func BlockEndPosition

func BlockEndPosition(b core.Block) (Position, error)

BlockEndPosition 计算 b 的尾部位置

func (Position) AddRune

func (p Position) AddRune(r rune) Position

AddRune 向后移动一个字符

func (Position) Equal

func (p Position) Equal(v Position) bool

Equal 判断与 v 是否相等

func (Position) SubRune

func (p Position) SubRune(r rune) Position

SubRune 向前移动一个字符

Jump to

Keyboard shortcuts

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