mirror of
https://github.com/adnanh/webhook.git
synced 2025-07-05 02:38:31 +00:00
wip
This commit is contained in:
parent
e329b6d9ff
commit
568c711625
138 changed files with 22876 additions and 90497 deletions
58
vendor/github.com/antonmedv/expr/file/error.go
generated
vendored
Normal file
58
vendor/github.com/antonmedv/expr/file/error.go
generated
vendored
Normal file
|
@ -0,0 +1,58 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Location
|
||||
Message string
|
||||
Snippet string
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
return e.format()
|
||||
}
|
||||
|
||||
func (e *Error) Bind(source *Source) *Error {
|
||||
if snippet, found := source.Snippet(e.Location.Line); found {
|
||||
snippet := strings.Replace(snippet, "\t", " ", -1)
|
||||
srcLine := "\n | " + snippet
|
||||
var bytes = []byte(snippet)
|
||||
var indLine = "\n | "
|
||||
for i := 0; i < e.Location.Column && len(bytes) > 0; i++ {
|
||||
_, sz := utf8.DecodeRune(bytes)
|
||||
bytes = bytes[sz:]
|
||||
if sz > 1 {
|
||||
goto noind
|
||||
} else {
|
||||
indLine += "."
|
||||
}
|
||||
}
|
||||
if _, sz := utf8.DecodeRune(bytes); sz > 1 {
|
||||
goto noind
|
||||
} else {
|
||||
indLine += "^"
|
||||
}
|
||||
srcLine += indLine
|
||||
|
||||
noind:
|
||||
e.Snippet = srcLine
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Error) format() string {
|
||||
if e.Location.Empty() {
|
||||
return e.Message
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"%s (%d:%d)%s",
|
||||
e.Message,
|
||||
e.Line,
|
||||
e.Column+1, // add one to the 0-based column for display
|
||||
e.Snippet,
|
||||
)
|
||||
}
|
10
vendor/github.com/antonmedv/expr/file/location.go
generated
vendored
Normal file
10
vendor/github.com/antonmedv/expr/file/location.go
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
package file
|
||||
|
||||
type Location struct {
|
||||
Line int // The 1-based line of the location.
|
||||
Column int // The 0-based column number of the location.
|
||||
}
|
||||
|
||||
func (l Location) Empty() bool {
|
||||
return l.Column == 0 && l.Line == 0
|
||||
}
|
95
vendor/github.com/antonmedv/expr/file/source.go
generated
vendored
Normal file
95
vendor/github.com/antonmedv/expr/file/source.go
generated
vendored
Normal file
|
@ -0,0 +1,95 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Source struct {
|
||||
contents []rune
|
||||
lineOffsets []int32
|
||||
}
|
||||
|
||||
func NewSource(contents string) *Source {
|
||||
s := &Source{
|
||||
contents: []rune(contents),
|
||||
}
|
||||
s.updateOffsets()
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Source) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s.contents)
|
||||
}
|
||||
|
||||
func (s *Source) UnmarshalJSON(b []byte) error {
|
||||
contents := make([]rune, 0)
|
||||
err := json.Unmarshal(b, &contents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.contents = contents
|
||||
s.updateOffsets()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Source) Content() string {
|
||||
return string(s.contents)
|
||||
}
|
||||
|
||||
func (s *Source) Snippet(line int) (string, bool) {
|
||||
charStart, found := s.findLineOffset(line)
|
||||
if !found || len(s.contents) == 0 {
|
||||
return "", false
|
||||
}
|
||||
charEnd, found := s.findLineOffset(line + 1)
|
||||
if found {
|
||||
return string(s.contents[charStart : charEnd-1]), true
|
||||
}
|
||||
return string(s.contents[charStart:]), true
|
||||
}
|
||||
|
||||
// updateOffsets compute line offsets up front as they are referred to frequently.
|
||||
func (s *Source) updateOffsets() {
|
||||
lines := strings.Split(string(s.contents), "\n")
|
||||
offsets := make([]int32, len(lines))
|
||||
var offset int32
|
||||
for i, line := range lines {
|
||||
offset = offset + int32(utf8.RuneCountInString(line)) + 1
|
||||
offsets[int32(i)] = offset
|
||||
}
|
||||
s.lineOffsets = offsets
|
||||
}
|
||||
|
||||
// findLineOffset returns the offset where the (1-indexed) line begins,
|
||||
// or false if line doesn't exist.
|
||||
func (s *Source) findLineOffset(line int) (int32, bool) {
|
||||
if line == 1 {
|
||||
return 0, true
|
||||
} else if line > 1 && line <= len(s.lineOffsets) {
|
||||
offset := s.lineOffsets[line-2]
|
||||
return offset, true
|
||||
}
|
||||
return -1, false
|
||||
}
|
||||
|
||||
// findLine finds the line that contains the given character offset and
|
||||
// returns the line number and offset of the beginning of that line.
|
||||
// Note that the last line is treated as if it contains all offsets
|
||||
// beyond the end of the actual source.
|
||||
func (s *Source) findLine(characterOffset int32) (int32, int32) {
|
||||
var line int32 = 1
|
||||
for _, lineOffset := range s.lineOffsets {
|
||||
if lineOffset > characterOffset {
|
||||
break
|
||||
} else {
|
||||
line++
|
||||
}
|
||||
}
|
||||
if line == 1 {
|
||||
return line, 0
|
||||
}
|
||||
return line, s.lineOffsets[line-2]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue