Source File
resyntax.go
Belonging Package
github.com/google/licensecheck/internal/match
package match
import (
)
type SyntaxError struct {
File string
Offset int
Context string
Err string
}
func ( *SyntaxError) () string {
var string
if .File != "" {
= fmt.Sprintf("%s:#%d: syntax error", .File, .Offset)
} else {
= fmt.Sprintf("syntax error at offset %d", .Offset)
}
if .Context != "" {
+= " near `" + .Context + "`"
}
+= ": " + .Err
return
}
func ( *bytes.Buffer, *reSyntax, *Dict) {
switch .op {
case opEmpty:
.WriteString("(( ))")
case opConcat:
if len(.sub) == 0 {
.WriteString("«empty concat»")
}
for , := range .sub {
if > 0 && .Len() > 0 && .Bytes()[.Len()-1] != '\n' {
.WriteString(" ")
}
(, , )
}
case opAlternate:
nl()
.WriteString("((")
for , := range .sub {
if > 0 {
.WriteString(" || ")
}
(, , )
}
.WriteString("))\n")
case opWild:
fmt.Fprintf(, "__%d__", .n)
case opQuest:
:= .sub[0]
nl()
if .op == opAlternate {
(, , )
.Truncate(.Len() - 1) // strip \n
} else {
.WriteString("((")
(, , )
.WriteString("))")
}
.WriteString("??\n")
case opWords:
if len(.w) == 0 {
.WriteString("«empty opWords»")
}
for , := range .w {
if > 0 && .Len() > 0 && .Bytes()[.Len()-1] != '\n' {
.WriteString(" ")
}
:= .Words()[]
if == "" {
.WriteString("''")
} else {
.WriteString(.Words()[])
}
}
}
}
func ( *Dict, string, bool) (*reSyntax, error) {
var reParser
.dict =
:= 0
:= 0
:= 0
for < len() {
switch {
case strings.HasPrefix([:], "(("):
if && !atBOL(, ) {
return nil, reSyntaxError(, , fmt.Errorf("(( not at beginning of line"))
}
.words([:], "((")
.push(&reSyntax{op: opLeftParen})
+= 2
=
++
case strings.HasPrefix([:], "||"):
if && == 0 {
return nil, reSyntaxError(, , fmt.Errorf("|| outside (( ))"))
}
.words([:], "||")
if := .verticalBar(); != nil {
return nil, reSyntaxError(, , )
}
+= 2
=
if {
:= + 2
for < len() && ([] == ' ' || [] == '\t') {
++
}
if < len() && [] != '\n' && (+1 >= len() || [] != '?' || [+1] != '?') {
return nil, reSyntaxError(, , fmt.Errorf(")) not at end of line"))
}
}
.words([:], "))")
if := .rightParen(); != nil {
return nil, reSyntaxError(, , )
}
+= 2
=
--
if {
:=
for > 0 && ([-1] == ' ' || [-1] == '\t') {
--
}
if < 2 || [-1] != ')' || [-2] != ')' {
return nil, reSyntaxError(, , fmt.Errorf("?? not preceded by ))"))
}
}
if && !atEOL(, +2) {
return nil, reSyntaxError(, , fmt.Errorf("?? not at end of line"))
}
.words([:], "??")
if := .quest(); != nil {
return nil, reSyntaxError(, , )
}
+= 2
=
case strings.HasPrefix([:], "__"):
:= + 2
for < len() && '0' <= [] && [] <= '9' {
++
}
if == +2 {
++
continue
}
if !strings.HasPrefix([:], "__") {
++
continue
}
, := strconv.Atoi([+2 : ])
if != nil {
return nil, reSyntaxError(, , errors.New("invalid wildcard count "+[:+2]))
}
.words([:], "__")
.push(&reSyntax{op: opWild, n: int32()})
= + 2
=
case strings.HasPrefix([:], "//**"):
:= strings.Index([+4:], "**//")
if < 0 {
return nil, reSyntaxError(, , errors.New("opening //** without closing **//"))
}
.words([:], "//** **//")
+= 4 + + 4
=
default:
++
}
}
.words([:], "")
.concat()
func ( *reParser) (, string) {
:= .dict.InsertSplit()
if len() == 0 {
return
}
if !.swapVerticalBar() {
.push(&reSyntax{op: opVerticalBar})
}
return nil
}
func ( *reSyntax) () []phrase {
switch .op {
default:
panic("bad op in phrases")
case opWild:
return []phrase{{BadWord, BadWord}, {AnyWord, BadWord}, {AnyWord, AnyWord}}
case opEmpty:
return []phrase{{BadWord, BadWord}}
case opWords:
:= .w
var phrase
if len() == 0 {
= phrase{BadWord, BadWord}
} else if len() == 1 {
= phrase{[0], BadWord}
} else {
= phrase{[0], [1]}
}
return []phrase{}
case opQuest:
:= .sub[0].()
for , := range {
if [0] == BadWord {
return
}
}
= append(, phrase{BadWord, BadWord})
return
case opAlternate:
var []phrase
:= make(map[phrase]bool)
for , := range .sub {
for , := range .() {
if ![] {
[] = true
= append(, )
}
}
}
return
case opConcat:
:= []phrase{{BadWord, BadWord}}
for , := range .sub {
:= true
for , := range {
if [1] == BadWord {
= false
}
}
if {
break
}
:= .()
:= make(map[phrase]bool)
var []phrase
for , := range {
if [1] != BadWord {
if ![] {
[] = true
= append(, )
}
continue
}
for , := range {
var phrase
if [0] == BadWord {
=
} else {
= phrase{[0], [0]}
}
if ![] {
[] = true
= append(, )
}
}
}
=
}
return
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |