Esempio n. 1
0
func hsExpressionInfo(expression string, flags CompileFlag) (*ExprInfo, error) {
	var info *C.hs_expr_info_t
	var err *C.hs_compile_error_t

	expr := C.CString(expression)

	ret := C.hs_expression_info(expr, C.uint(flags), &info, &err)

	C.free(unsafe.Pointer(expr))

	if ret == C.HS_SUCCESS && info != nil {
		defer hsMiscFree(unsafe.Pointer(info))

		return &ExprInfo{
			MinWidth:        uint(info.min_width),
			MaxWidth:        uint(info.max_width),
			ReturnUnordered: info.unordered_matches != 0,
			AtEndOfData:     info.matches_at_eod != 0,
			OnlyAtEndOfData: info.matches_only_at_eod != 0,
		}, nil
	}

	if err != nil {
		defer C.hs_free_compile_error(err)
	}

	if ret == C.HS_COMPILER_ERROR && err != nil {
		return nil, &compileError{C.GoString(err.message), int(err.expression)}
	}

	return nil, fmt.Errorf("compile error, %d", int(ret))
}
Esempio n. 2
0
func hsCompile(expression string, flags CompileFlag, mode ModeFlag, info *hsPlatformInfo) (hsDatabase, error) {
	var db *C.hs_database_t
	var err *C.hs_compile_error_t
	var platform *C.hs_platform_info_t

	if info != nil {
		platform = &info.platform
	}

	expr := C.CString(expression)

	ret := C.hs_compile(expr, C.uint(flags), C.uint(mode), platform, &db, &err)

	C.free(unsafe.Pointer(expr))

	if ret == C.HS_SUCCESS {
		return db, nil
	}

	if err != nil {
		defer C.hs_free_compile_error(err)
	}

	if ret == C.HS_COMPILER_ERROR && err != nil {
		return nil, &compileError{C.GoString(err.message), int(err.expression)}
	}

	return nil, fmt.Errorf("compile error, %d", int(ret))
}
Esempio n. 3
0
func hsCompileMulti(expressions []string, flags []CompileFlag, ids []uint, mode ModeFlag, info *hsPlatformInfo) (hsDatabase, error) {
	var db *C.hs_database_t
	var err *C.hs_compile_error_t
	var platform *C.hs_platform_info_t

	if info != nil {
		platform = &info.platform
	}

	cexprs := make([]*C.char, len(expressions))

	for i, expr := range expressions {
		cexprs[i] = C.CString(expr)
	}

	var cflags, cids *C.uint

	if flags != nil {
		values := make([]C.uint, len(flags))

		for i, flag := range flags {
			values[i] = C.uint(flag)
		}

		cflags = &values[0]
	}

	if ids != nil {
		values := make([]C.uint, len(ids))

		for i, id := range ids {
			values[i] = C.uint(id)
		}

		cids = &values[0]
	}

	ret := C.hs_compile_multi(&cexprs[0], cflags, cids, C.uint(len(cexprs)), C.uint(mode), platform, &db, &err)

	for _, expr := range cexprs {
		C.free(unsafe.Pointer(expr))
	}

	if ret == C.HS_SUCCESS {
		return db, nil
	}

	if err != nil {
		defer C.hs_free_compile_error(err)
	}

	if ret == C.HS_COMPILER_ERROR && err != nil {
		return nil, &compileError{C.GoString(err.message), int(err.expression)}
	}

	return nil, fmt.Errorf("compile error, %d", int(ret))
}