mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-08 20:38:47 +00:00
New Grammars Compiler (#3915)
* grammars: Update several grammars with compat issues * [WIP] Add new grammar conversion tools * Wrap in a Docker script * Proper Dockerfile support * Add Javadoc grammar * Remove NPM package.json * Remove superfluous test This is now always checked by the grammars compiler * Update JSyntax grammar to new submodule * Approve Javadoc license * grammars: Remove checked-in dependencies * grammars: Add regex checks to the compiler * grammars: Point Oz to its actual submodule * grammars: Refactor compiler to group errors by repo * grammars: Cleanups to error reporting
This commit is contained in:
53
tools/grammars/pcre/pcre.go
Normal file
53
tools/grammars/pcre/pcre.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package pcre
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lpcre
|
||||
#include <pcre.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func RegexPP(re string) string {
|
||||
if len(re) > 32 {
|
||||
re = fmt.Sprintf("\"`%s`...\"", re[:32])
|
||||
} else {
|
||||
re = fmt.Sprintf("\"`%s`\"", re)
|
||||
}
|
||||
return strings.Replace(re, "\n", "", -1)
|
||||
}
|
||||
|
||||
type CompileError struct {
|
||||
Pattern string
|
||||
Message string
|
||||
Offset int
|
||||
}
|
||||
|
||||
func (e *CompileError) Error() string {
|
||||
return fmt.Sprintf("regex %s: %s (at offset %d)",
|
||||
RegexPP(e.Pattern), e.Message, e.Offset)
|
||||
}
|
||||
|
||||
const DefaultFlags = int(C.PCRE_DUPNAMES | C.PCRE_UTF8 | C.PCRE_NEWLINE_ANYCRLF)
|
||||
|
||||
func CheckRegexp(pattern string, flags int) error {
|
||||
pattern1 := C.CString(pattern)
|
||||
defer C.free(unsafe.Pointer(pattern1))
|
||||
|
||||
var errptr *C.char
|
||||
var erroffset C.int
|
||||
ptr := C.pcre_compile(pattern1, C.int(flags), &errptr, &erroffset, nil)
|
||||
if ptr == nil {
|
||||
return &CompileError{
|
||||
Pattern: pattern,
|
||||
Message: C.GoString(errptr),
|
||||
Offset: int(erroffset),
|
||||
}
|
||||
}
|
||||
C.free(unsafe.Pointer(ptr))
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user