parser

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WhitespaceChars = " \f\n\r\t\v\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"
	Re2Dot          = "[^\r\n\u2028\u2029]"
)

Variables

This section is empty.

Functions

func IsIdentifier

func IsIdentifier(s string) bool

func ParseFile

func ParseFile(fileSet *file.FileSet, filename string, src any, mode Mode, options ...Option) (*ast.Program, error)

ParseFile 解析单个 JavaScript 源文件的代码并返回相应的 ast.Program 节点 若文件集合为空,则解析时不包含文件集合(没有上下文),若文件集合非空,则先将要解析的文件加入其中 文件名参数可选,用于在出现错误时,标记错误发生的文件名 src 可以是 string/[]byte/bytes.Buffer/io.Reader, 但是不论如何,它的内容编码必须是UTF8 解析 JavaScript 将最终产生一个 *ast.Program 和一个 ErrorList

func ParseFunction

func ParseFunction(parameterList, body string, options ...Option) (*ast.FunctionLiteral, error)

ParseFunction 将一个给定的参数列表和主体解析为一个函数,并返回相应的 ast.FunctionLiteral 节点 若有参数,则参数列表必须是一个用逗号分隔的标识符列表

func ReadSource

func ReadSource(filename string, src any) ([]byte, error)

func TransformRegExp

func TransformRegExp(pattern string) (transformed string, err error)

TransformRegExp 将一个 JavaScript 的正则表达式转换为 Go 的正则 re2 (Go) 不能进行回溯,因此遇到回看,如 (?=) (?!) 或 (\1, \2, ...) 将报错 re2 (Go) 对 \s 有不同的定义: [\t\n\f\r ] 另外,JavaScript 还包含了额外的 \v, Unicode 的分割符和空格等

func WithDisableSourceMaps

func WithDisableSourceMaps(opts *options)

WithDisableSourceMaps 禁用源码 map 的选项,在不使用源码 map 时,将会节省解析 map 的时间

Types

type Error

type Error struct {
	Position file.Position
	Message  string
}

Error JavaScript 解析错误,它包含了发生错误的位置和具体的描述

func (Error) Error

func (e Error) Error() string

type ErrorList

type ErrorList []*Error

ErrorList 错误列表

func (*ErrorList) Add

func (l *ErrorList) Add(position file.Position, msg string)

func (*ErrorList) Err

func (l *ErrorList) Err() error

func (*ErrorList) Error

func (l *ErrorList) Error() string

func (*ErrorList) Len

func (l *ErrorList) Len() int

func (*ErrorList) Less

func (l *ErrorList) Less(i, j int) bool

func (*ErrorList) Reset

func (l *ErrorList) Reset()

func (*ErrorList) Sort

func (l *ErrorList) Sort()

func (*ErrorList) Swap

func (l *ErrorList) Swap(i, j int)

type Mode

type Mode uint

Mode 保存一组标志,该标志用于控制 parser 的可选功能

const IgnoreRegExpErrors Mode = 1 << iota // 忽略正则兼容性错误 (允许回溯)

type Option

type Option func(*options)

Option 表示在 parser 内使用的选项之一,目前支持的是 WithDisableSourceMaps 和 WithSourceMapLoader

func WithSourceMapLoader

func WithSourceMapLoader(loader func(path string) ([]byte, error)) Option

WithSourceMapLoader 启用源码 map 的选项,可以设置一个自定义的源码 map 加载器 该加载器将被赋予一个路径或一个 sourceMappingURL 中的 URL,sourceMappingURL 可以是绝对路径或相对路径 任何由加载器返回的错误都会导致解析失败

type RegexpErrorIncompatible

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

func (RegexpErrorIncompatible) Error

func (s RegexpErrorIncompatible) Error() string

type RegexpSyntaxError

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

func (RegexpSyntaxError) Error

func (s RegexpSyntaxError) Error() string

Jump to

Keyboard shortcuts

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