errcode

package
v0.0.0-...-a214d60 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package errcode提供在语义检查完成前的错误处理

标准处理流程

错误处理上下文 ErrCtx 保存错误处理需要的信息

错误处理围绕这个错误处理上下文进行,处理没有关联的错误推荐使用不同错误处理上下文

报错通过 ErrCtx.Panic 进行

在一个单独的goroutin调用 ErrCtx.Handle 处理一个报错

代码如下:
var errctx = NewErrCtx() //创建错误处理上下文
ctx,cancel:=context.WithCancel(context.Background())
defer cancel() //在执行完毕时结束错误处理
go errctx.Handle(ctx,errfn func(ErrInfo){fmt.Println(err.String())},func(){}) //错误处理方式是将报错信息打印
//...词法分析,构建抽象语法树,语义检查,其中包含可能调用 [ErrCtx.Panic]的代码

默认错误上下文

默认错误上下文 DefaultErrCtx 是由于 ErrCtx 出现晚于 Panic Handle Errbol 而诞生的产物

使用 默认错误上下文 的错误意味着报错和处理报错的状态是全局,不推荐在测试以外的情况使用

Index

Constants

This section is empty.

Variables

View Source
var (
	Aoldad = "出现在%s的第%d行 和 %s的第%d行" //Appears on line% d of% s and line% d of% s

)
View Source
var Debug = false
View Source
var (
	DefaultErrCtx = NewErrCtx()
)
View Source
var (
	ErrCodeStrMap = [...]string{}/* 121 elements not displayed */

)

Functions

func ErrEnumStr

func ErrEnumStr(code ...ErrCode) string

ErrEnumStr 实现将错误码 ErrCode 转换为提示信息的算法

func Errbol

func Errbol() bool

Errbol 调用 DefaultErrCtx.Errbol

func Handle

func Handle(ctx context.Context, errfn func(ErrInfo), retfn func())

Handle 调用 DefaultErrCtx.Handle

func Panic

func Panic(file string, line int, addInfo Msg, err ...ErrCode)

Panic 调用 DefaultErrCtx.Panic

Types

type ErrCode

type ErrCode int

错误码,指定某一个错误

const (
	NoErr ErrCode = iota
	VARErr
	FUNCErr
	OneNoNumber
	EmptyLPAREN
	EmptyRPAREN
	OPbug
	NameRetain
	TypeIsNotEqual
	ASSIGNErr
	EmptyRbrace
	UnknownSymbol
	EmptyLBRACE
	NoName
	SymbolUseBeforeDeclaration
	IfErr
	NdSotf // Non-declaration statement outside the function
	ExprNoBool
	ElseErr
	AFIOE //Assignment found in operation expression
	StringNoEnd
	ForErr
	MLCNoEnd             //multiline comment no end
	ForInitNoDeclVarStmt //Is not a declaration variable statement
	ForEndStmt
	SymbolRepeat
	UnexpectedComma
	CallErr
	InsuParame           //Insufficient parameters
	CallNoFunc           //Calling non functions
	FuncNoRetValue       //Function does not return a value
	BoolOpOnlyCmpOrLogic //Boolean type operations can only be comparative equality, inequality, or logical operations
	DeclFuncNoInFunc     //Cannot declare a function in a function at this time
	ReturnErr
	GotoErr
	NoLabel
	LabelErr
	BreakErr
	ContinueErr
	MainNoRetValue         //The main function does not return a value
	BreakStmtInForOrSwitch //The break statement should be in the for code block
	ContinueStmtInFor      //The continue statement should be in the for code block
	ConstErr
	TManyParame //too many parameters
	StructErr
	FieldErr
	NoIdent //Not an identifier
	UnknownType
	NoOSelectorL         //Not an optional selector lvalue
	SelectorLNoSelectorR //The selector left value does not have a selector right value
	UnexpectedRPAREN
	MallocReqParame     //Malloc requires a parameter
	ParameOfMallocAType //One parameter of malloc should be a type symbol
	StringNoSelfOpStmt  //String cannot use self operating statements
	SelfOpStmtErr
	FirstParameOfPrinfASting //The first parameter of printf should be a string
	PtrOpOnlyCmp             //Pointer type operations can only be comparative equal or unequal
	UnexpectedNil
	LeftOrRightValueSelectorEmpty       //The left or right value of the selector is empty
	SelectorLeftAndRightValueMustSymbol //The left and right value of the selector must be a symbol
	ProhibitCallInitFunc                //Prohibit calling init function
	ProhibitCallMainFunc                //Prohibit calling the main function
	UnexpectedLPAREN
	PackageDeclMustName         //The package declaration statement must include the package name
	PackageDeclOnlyName         //The package declaration should only contain the package name
	PackageDeclMustFirstLine    //The package declaration must appear on the first line
	DiffFileHavaDiffPackageDecl //Different file declarations have different package declarations
	SwitchErr
	CaseErr
	ExpectColonAtEnd //Expected a colon at the end
	CaseStmtInSwitch //The case statement should be in the switch
	DefaultErr
	ParameDeclHasNoType //Parameter declaration has no type
	DeclMethodInFunc    //Method cannot be declared in a function at this time
	MethodErr
	LbraceEndOfLine                     //Left brace should be at the end of the same line
	FirstParameTypeOfMethodACustomType  //The first parameter type of the method should be a custom type
	SecondParameOfUnsafeAddAnInt        //The second parameter of unsafeAdd should be an integer
	FirstParameOfUnsafeAddAPtr          //The first parameter of unsafeAdd should be a pointer
	FirstParameOfUnsafeConvertAPtr      //The first parameter of unsafeConvert should be a pointer
	SecondParameOfUnsafeConvertAPtrType //The second parameter of unsafeConvert should be a pointer type
	FirstParameOfUnsafeSizeAType        //The first parameter of unsafeSize should be a type
	AutoFreeErr
	ExprForAutoFreeShouldExprInt   //The expression for autofree should express integers
	CannotUseTypeAsRetValue        //Cannot use type as return value
	RecursiveTypeErr               //Recursive type error
	ImportShouldBeOutSideOfTheFunc //Import should be outside of the function
	ImportPathMustString           //The import path must be a string
	ImportErr
	CircularImportErr //Circular import error
	NotAnExportSymbol //Not an export symbol
	EnumErr
	EnumValueShouldHaveOneOnEachLine         //Enumeration values should have one on each line
	EnumValueShouldBeASymbol                 //The enumeration value should be a symbol
	PackageNameNoEqualDirName                //Package name does not equal directory name
	ParameForMallocSizeMustInt               //The parameter for mallocSize must be an integer
	EmptyEnum                                // empty enumeration
	CanOnlyConvertPointerTypeToUnsafePointer //Can only convert pointer type to unsafe__Pointer
	CanOnlyConvertFloatToInt                 //Can only convert float to int
	CanOnlyConvertIntToFloat                 //Can only convert int to float
	FieldDupName                             //Field duplicate name
	TheNumOfLeftRightBracketsNoMatch         //The number of left and right brackets does not match
	EnumValueDup                             //Enumeration value duplication
	IndexExprElemTypeErr                     //Index expression element type error
	TypeParameSyntaxErr                      //Type parameter Syntax error
	TypeParamrConstraintErr                  //Type parameter constraint error
	GenericInstantiationEmpty                //The generic instantiation list is empty
	UnequlaNumOfGen                          //Unequal number of generics
	OnlyTypePtrCanDeref                      //Only type pointers can be dereferenced
	UnrefNoVarAndConst                       //Unreferencing non variables and constants
	AdjacentOp                               //Adjacent Operators
	PackageNameCannotBeEqualUnsafe           //Package name cannot be equal to unsafe
	MainPackageMustHaveMainFunc              //The main package must have a main function
	NoMainPackageMustHaveNoMainFunc          //Non main packages must have no main function
	NameCannotStartWithGenerate              //The name cannot start with 'generate'
	EndOfNameCannot_mempool                  //The end of the name cannot be _mempool
	RetWithValueInFuncWithoutValue           //Returns with a return value in functions without a return value
	NoType
	NoConstInit
	NoBoolExpr
	NoExpr
)

func (ErrCode) String

func (err ErrCode) String() string

type ErrCtx

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

ErrCtx 错误处理的上下文 保存了错误处理需要的信息

ErrCtx 不能被重用

func NewErrCtx

func NewErrCtx() *ErrCtx

NewErrCtx 创建错误处理的上下文

func (*ErrCtx) Errbol

func (e *ErrCtx) Errbol() bool

Errbol 返回是否保存过错误

func (*ErrCtx) Handle

func (e *ErrCtx) Handle(ctx context.Context, errfn func(ErrInfo), retfn func())

Handle 进行错误处理,是阻塞的

  • Handle循环读取到错误后,调用errfn处理,直到ctx取消并处理完所有错误,如果有错误被保存过,调用retfn
  • 取消一个没有取消的ctx是唯一从外部结束Handle的方法
  • ctx传输取消信号
  • errfn实际处理错误
  • retfn定义读取到错误且收到取消信号后操作

func (*ErrCtx) Panic

func (e *ErrCtx) Panic(file string, line int, addInfo Msg, err ...ErrCode)

Panic 进行报错

  • File是文件名称
  • line是行数
  • addInfo是附加错误信息,可以为nil
  • err是错误码,可以有多个

type ErrInfo

type ErrInfo struct {
	//附加信息
	AddInfo Msg
	//文件名
	File string
	//错误提示
	Message string

	//行数
	Line int
	// contains filtered or unexported fields
}

错误信息

func NewErrInfo

func NewErrInfo(File string, Line int, AddInfo Msg, Message string) ErrInfo

NewErrInfo 创建错误信息

  • File是文件名
  • Line是行号
  • AddInfo是附加错误信息,可以为nil
  • Message是错误的提示信息

func (ErrInfo) IsErr

func (e ErrInfo) IsErr(File string, Line int, AddInfo Msg, code ...ErrCode) bool

IsErr 判断方法参数给出的可以产生错误信息的内容与接收者是否相等

func (*ErrInfo) String

func (e *ErrInfo) String() string

type Msg

type Msg interface {
	//返回提示信息
	Msg() string
}

代码错误提示

type MsgAssignToConst

type MsgAssignToConst struct {
	Name string
}

缩写: Assign values to constants 缩写成 AssignToConst MsgAssignToConst 表示一个对常量赋值错误

func NewMsgAssignToConst

func NewMsgAssignToConst(name string) MsgAssignToConst

创建MsgAssignToConst

func (MsgAssignToConst) Msg

func (msg MsgAssignToConst) Msg() string

Msg 返回提示信息

type MsgAssignTypeIsNotEqual

type MsgAssignTypeIsNotEqual struct {
	//目的操作数的类型
	DestType string
	//源操作数的类型
	SrcType string
	// contains filtered or unexported fields
}

MsgAssignTypeIsNotEqual 表示一个赋值类型不相等错误

func NewMsgAssignTypeIsNotEqual

func NewMsgAssignTypeIsNotEqual(DestType string, SrcType string) MsgAssignTypeIsNotEqual

创建MsgAssignTypeIsNotEqual

func (MsgAssignTypeIsNotEqual) Is

func (msg MsgAssignTypeIsNotEqual) Is(b Msg) bool

func (MsgAssignTypeIsNotEqual) Msg

func (msg MsgAssignTypeIsNotEqual) Msg() string

Msg 返回提示信息

type MsgBracesNoEqual

type MsgBracesNoEqual struct {
	Left  int
	Right int
}

MsgBracesNoEqual 表示左大括号和右大括号数量不匹配

func NewMsgBracesNoEqual

func NewMsgBracesNoEqual(Left int, Right int) MsgBracesNoEqual

func (MsgBracesNoEqual) Msg

func (msg MsgBracesNoEqual) Msg() string

Msg 返回提示信息

type MsgBracketsNoEqual

type MsgBracketsNoEqual struct {
	Left  int
	Right int
}

MsgBracketsNoEqual 表示左中括号和右中括号数量不匹配

func NewMsgBracketsNoEqual

func NewMsgBracketsNoEqual(Left int, Right int) MsgBracketsNoEqual

func (MsgBracketsNoEqual) Msg

func (msg MsgBracketsNoEqual) Msg() string

Msg 返回提示信息

type MsgCallTypeIsNotEqual

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

MsgCallTypeIsNotEqual 表示一个函数调用类型不相等错误

func NewMsgCallTypeIsNotEqual

func NewMsgCallTypeIsNotEqual(src1Type string, src2Type string, num int) MsgCallTypeIsNotEqual

创建MsgCallTypeIsNotEqual

func (MsgCallTypeIsNotEqual) Msg

func (msg MsgCallTypeIsNotEqual) Msg() string

Msg 返回提示信息

type MsgDiffFileHavaDiffPackageDecl

type MsgDiffFileHavaDiffPackageDecl struct {
	Decl1, Decl2 string
}

缩写:Different file declarations have different package declarations 缩写成 DiffFileHavaDiffPackageDecl MsgDiffFileHavaDiffPackageDecl 表示一个不同文件声明的包声明不同附加错误

func NewMsgDiffFileHavaDiffPackageDecl

func NewMsgDiffFileHavaDiffPackageDecl(Decl1, Decl2 string) MsgDiffFileHavaDiffPackageDecl

创建MsgDiffFileHavaDiffPackageDecl

func (MsgDiffFileHavaDiffPackageDecl) Msg

Msg 返回提示信息

type MsgExpectedAndFound

type MsgExpectedAndFound struct {
	Expected, Found string
}

MsgExpectedAndFound 表示一个 需要 x 找到 y 的错误

func NewMsgExpectedAndFound

func NewMsgExpectedAndFound(Expected, Found string) MsgExpectedAndFound

func (MsgExpectedAndFound) Msg

func (msg MsgExpectedAndFound) Msg() string

Msg 返回提示信息

type MsgLabelNoExist

type MsgLabelNoExist struct {
	//不存在的标签名
	Label string
}

MsgLabelNoExist 表示一个不存在的标签错误 缩写:Label that does not exist 缩写成 LabelNoExist

func NewMsgLabelNoExist

func NewMsgLabelNoExist(Label string) MsgLabelNoExist

创建MsgLabelNoExist

func (MsgLabelNoExist) Msg

func (msg MsgLabelNoExist) Msg() string

Msg 返回提示信息

type MsgNumberOfParameNoMatch

type MsgNumberOfParameNoMatch struct {
	FuncName string
	//调用(实际传递的参数)数量
	CallerNum int
	//被调者(函数声明的参数)数量
	CalleeNum int
}

缩写:Number of parameters does not match 缩写成 NumberOfParameNoMatch MsgNumberOfParameNoMatch 表示一个参数数量不匹配错误

func NewMsgNumberOfParameNoMatch

func NewMsgNumberOfParameNoMatch(CallerNum int, CalleeNum int, FuncName string) MsgNumberOfParameNoMatch

创建MsgNumberOfParameNoMatch

func (MsgNumberOfParameNoMatch) Msg

func (msg MsgNumberOfParameNoMatch) Msg() string

Msg 返回提示信息

type MsgNumberOfSemicolons

type MsgNumberOfSemicolons struct {
	Num int
}

MsgNumberOfSemicolons 表示分号数量错误,目前用于for语句

func NewMsgNumberOfSemicolons

func NewMsgNumberOfSemicolons(Num int) MsgNumberOfSemicolons

func (MsgNumberOfSemicolons) Msg

func (msg MsgNumberOfSemicolons) Msg() string

Msg 返回提示信息

type MsgOpexprTypeIsNotEqual

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

MsgOpexprTypeIsNotEqual 表示一个运算表达式类型不相等错误

func NewMsgOpexprTypeIsNotEqual

func NewMsgOpexprTypeIsNotEqual(src1Type string, src2Type string) MsgOpexprTypeIsNotEqual

创建MsgOpexprTypeIsNotEqual

func (MsgOpexprTypeIsNotEqual) Msg

func (msg MsgOpexprTypeIsNotEqual) Msg() string

Msg 返回提示信息

type MsgPackageNameNoEqualDirName

type MsgPackageNameNoEqualDirName struct {
	PackageName string
	DirName     string
}

MsgPackageNameNoEqualDirName 表示一个包名不等于目录名附加错误

func NewMsgPackageNameNoEqualDirName

func NewMsgPackageNameNoEqualDirName(PackageName string, DirName string) MsgPackageNameNoEqualDirName

创建 MsgPackageNameNoEqualDirName

func (MsgPackageNameNoEqualDirName) Msg

Msg 返回提示信息

type MsgRecursiveTypeErr

type MsgRecursiveTypeErr struct {
	Dep []string
}

MsgRecursiveTypeErr 表示一个递归类型附加错误

func NewMsgRecursiveTypeErr

func NewMsgRecursiveTypeErr(finaldep string) MsgRecursiveTypeErr

创建MsgRecursiveTypeErr

func (MsgRecursiveTypeErr) Msg

func (msg MsgRecursiveTypeErr) Msg() string

Msg 返回提示信息

type MsgReturnTypeIsNotEqual

type MsgReturnTypeIsNotEqual struct {
	//声明的类型
	DeclType string
	//返回的类型
	RetType string
}

MsgreturnTypeIsNotEqual 表示一个返回语句类型不相等错误

func NewMsgReturnTypeIsNotEqual

func NewMsgReturnTypeIsNotEqual(DeclType string, RetType string) MsgReturnTypeIsNotEqual

创建MsgReturnTypeIsNotEqual

func (MsgReturnTypeIsNotEqual) Msg

func (msg MsgReturnTypeIsNotEqual) Msg() string

Msg 返回提示信息

type MsgSelectorLNoSelectorR

type MsgSelectorLNoSelectorR struct {
	LeftValue  string
	RightValue string
	// contains filtered or unexported fields
}

缩写: The selector left value does not have a selector right value 缩写成SelectorLNoSelectorR MsgSelectorLNoSelectorR 代表一个选择器左值没有选择器右值的错误

func NewMsgSelectorLNoSelectorR

func NewMsgSelectorLNoSelectorR(LeftValue string, RightValue string) MsgSelectorLNoSelectorR

func (MsgSelectorLNoSelectorR) Is

func (msg MsgSelectorLNoSelectorR) Is(eq Msg) bool

func (MsgSelectorLNoSelectorR) Msg

func (msg MsgSelectorLNoSelectorR) Msg() string

Msg 返回提示信息

type MsgSwitchAndCaseTypNoEqual

type MsgSwitchAndCaseTypNoEqual struct {
	SwitchTyp, CaseTyp string
}

缩写:Switch and case types are not equal 缩写成 SwitchAndCaseTypNoEqual MsgSwitchAndCaseTypNoEqual 表示一个switch和case类型不相等

func NewMsgSwitchAndCaseTypNoEqual

func NewMsgSwitchAndCaseTypNoEqual(SwitchTyp, CaseTyp string) MsgSwitchAndCaseTypNoEqual

创建MsgSwitchAndCaseTypNoEqual

func (MsgSwitchAndCaseTypNoEqual) Msg

Msg 返回提示信息

type MsgSymbol

type MsgSymbol struct {
	//变量名
	Name string
}

MsgSymbol 表示一个报错信息中的符号名

func NewMsgSymbol

func NewMsgSymbol(Name string) MsgSymbol

创建MsgSymbol

func (MsgSymbol) Msg

func (msg MsgSymbol) Msg() string

Msg 返回提示信息

type MsgSymbolRepeat

type MsgSymbolRepeat struct {
	//未知的符号名
	Name                 string
	FileName1, FileName2 string
	LineNum1, LineNum2   int
}

MsgSymbolRepeat 表示一个重复的符号名

func NewMsgSymbolRepeat

func NewMsgSymbolRepeat(Name string, FileName1, FileName2 string, LineNum1, LineNum2 int) MsgSymbolRepeat

创建MsgSymbolRepeat

func (MsgSymbolRepeat) Msg

func (msg MsgSymbolRepeat) Msg() string

Msg 返回提示信息

type MsgUnexpected

type MsgUnexpected struct {
	Str string
}

MsgUnexpected 表示一个意外的字符串

func NewMsgUnexpected

func NewMsgUnexpected(str string) MsgUnexpected

func (MsgUnexpected) Msg

func (msg MsgUnexpected) Msg() string

Msg 返回提示信息

type MsgUnexpected2

type MsgUnexpected2 struct {
	Str1 string
	Str2 string
}

MsgUnexpected2 表示一个意外的两个字符串

func NewMsgUnexpected2

func NewMsgUnexpected2(str1, str2 string) MsgUnexpected2

func (MsgUnexpected2) Msg

func (msg MsgUnexpected2) Msg() string

Msg 返回提示信息

type MsgUnknownSymbol

type MsgUnknownSymbol struct {
	//未知的符号名
	Name string
	// contains filtered or unexported fields
}

MsgUnknownSymbol 表示一个未知的符号错误

func NewMsgUnknownSymbol

func NewMsgUnknownSymbol(Name string) MsgUnknownSymbol

创建MsgUnknownSymbol

func (MsgUnknownSymbol) Is

func (msg MsgUnknownSymbol) Is(eq Msg) bool

func (MsgUnknownSymbol) Msg

func (msg MsgUnknownSymbol) Msg() string

Msg 返回提示信息

Jump to

Keyboard shortcuts

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