packer-cn/vendor/github.com/ChrisTrenkamp/goxpath/internal/lexer/lexer.go

420 lines
7.3 KiB
Go
Raw Normal View History

Remove LGPL dependencies This changeset performs the following: - Updates `masterzen/winrm` vendor to include change from (https://github.com/masterzen/winrm/pull/73). - Removes `masterzen/xmlpath` dependency, as it is licensed under the LGPL license. - Updates `dylanmei/winrmtest` vendor to include change from (https://github.com/dylanmei/winrmtest/pull/4). - Updates `packer-community/winrmcp` vendor to include the removal of the `masterzen/winrm/winrm` sub-class as a result of the `winrm` CLI tool being removed from the `masterzen/winrm` repository. - Updates nested dependencies as a result of the above vendor changes. - Refactors `parallels/driver_9` to use `goxpath` instead of `xmlpath`, as `xmlpath` is licensed under LGPL. - Adds a basic unit test for the refactor work in `parallels/driver_9` to confirm functionality. This should completely remove any LGPL licensed dependencies inside of the Packer project. ``` $ make test /home/jake/src/go/src/github.com/hashicorp/packer/scripts/gofmtcheck.sh $(find . -not -path "./vendor/*" -name "*.go") ==> Checking that code complies with gofmt requirements... Check passed. ok github.com/hashicorp/packer 0.044s ok github.com/hashicorp/packer/builder/alicloud/ecs 0.055s ok github.com/hashicorp/packer/builder/amazon/chroot 0.040s ok github.com/hashicorp/packer/builder/amazon/common 0.021s ok github.com/hashicorp/packer/builder/amazon/ebs 0.016s ok github.com/hashicorp/packer/builder/amazon/ebssurrogate 0.015s ok github.com/hashicorp/packer/builder/amazon/ebsvolume 0.014s ok github.com/hashicorp/packer/builder/amazon/instance 0.058s ok github.com/hashicorp/packer/builder/azure/arm 2.833s ok github.com/hashicorp/packer/builder/azure/common 0.018s ? github.com/hashicorp/packer/builder/azure/common/constants [no test files] ? github.com/hashicorp/packer/builder/azure/common/lin [no test files] ? github.com/hashicorp/packer/builder/azure/common/logutil [no test files] ok github.com/hashicorp/packer/builder/azure/common/template 0.038s ok github.com/hashicorp/packer/builder/azure/pkcs12 0.120s ok github.com/hashicorp/packer/builder/azure/pkcs12/rc2 0.020s ok github.com/hashicorp/packer/builder/cloudstack 0.038s ok github.com/hashicorp/packer/builder/digitalocean 0.015s ok github.com/hashicorp/packer/builder/docker 0.028s ok github.com/hashicorp/packer/builder/file 0.029s ok github.com/hashicorp/packer/builder/googlecompute 3.162s ok github.com/hashicorp/packer/builder/hyperv/common 0.006s ok github.com/hashicorp/packer/builder/hyperv/iso 0.042s ok github.com/hashicorp/packer/builder/null 0.018s ok github.com/hashicorp/packer/builder/oneandone 0.022s ok github.com/hashicorp/packer/builder/openstack 0.052s ok github.com/hashicorp/packer/builder/parallels/common 2.549s ok github.com/hashicorp/packer/builder/parallels/iso 0.050s ok github.com/hashicorp/packer/builder/parallels/pvm 0.033s ok github.com/hashicorp/packer/builder/profitbricks 0.023s ok github.com/hashicorp/packer/builder/qemu 0.039s ok github.com/hashicorp/packer/builder/triton 0.018s ok github.com/hashicorp/packer/builder/virtualbox/common 5.533s ok github.com/hashicorp/packer/builder/virtualbox/iso 0.044s ok github.com/hashicorp/packer/builder/virtualbox/ovf 0.012s ok github.com/hashicorp/packer/builder/vmware/common 5.325s ok github.com/hashicorp/packer/builder/vmware/iso 0.076s ok github.com/hashicorp/packer/builder/vmware/vmx 0.010s ok github.com/hashicorp/packer/command 0.062s ok github.com/hashicorp/packer/common 0.053s ok github.com/hashicorp/packer/common/json 0.003s [no tests to run] ok github.com/hashicorp/packer/common/powershell 0.015s ? github.com/hashicorp/packer/common/powershell/hyperv [no test files] ? github.com/hashicorp/packer/common/ssh [no test files] ok github.com/hashicorp/packer/common/uuid 0.010s ok github.com/hashicorp/packer/communicator/none 0.008s ok github.com/hashicorp/packer/communicator/ssh 0.068s ok github.com/hashicorp/packer/communicator/winrm 0.046s ok github.com/hashicorp/packer/fix 0.005s ok github.com/hashicorp/packer/helper/builder/testing 0.007s ok github.com/hashicorp/packer/helper/communicator 0.013s ok github.com/hashicorp/packer/helper/config 0.004s ? github.com/hashicorp/packer/helper/enumflag [no test files] ok github.com/hashicorp/packer/helper/flag-kv 0.009s ok github.com/hashicorp/packer/helper/flag-slice 0.006s ok github.com/hashicorp/packer/packer 0.219s ok github.com/hashicorp/packer/packer/plugin 0.233s ok github.com/hashicorp/packer/packer/rpc 0.141s ok github.com/hashicorp/packer/plugin/example 0.008s [no tests to run] ? github.com/hashicorp/packer/post-processor/alicloud-import [no test files] ? github.com/hashicorp/packer/post-processor/amazon-import [no test files] ok github.com/hashicorp/packer/post-processor/artifice 0.003s [no tests to run] ok github.com/hashicorp/packer/post-processor/atlas 0.018s ok github.com/hashicorp/packer/post-processor/checksum 0.015s ok github.com/hashicorp/packer/post-processor/compress 0.047s ok github.com/hashicorp/packer/post-processor/docker-import 0.012s ok github.com/hashicorp/packer/post-processor/docker-push 0.012s ok github.com/hashicorp/packer/post-processor/docker-save 0.008s ok github.com/hashicorp/packer/post-processor/docker-tag 0.008s ok github.com/hashicorp/packer/post-processor/googlecompute-export 0.015s [no tests to run] ? github.com/hashicorp/packer/post-processor/manifest [no test files] ok github.com/hashicorp/packer/post-processor/shell-local 0.028s ok github.com/hashicorp/packer/post-processor/vagrant 0.030s ok github.com/hashicorp/packer/post-processor/vagrant-cloud 0.028s ok github.com/hashicorp/packer/post-processor/vsphere 0.014s ok github.com/hashicorp/packer/provisioner 0.011s ok github.com/hashicorp/packer/provisioner/ansible 0.140s ok github.com/hashicorp/packer/provisioner/ansible-local 0.056s ok github.com/hashicorp/packer/provisioner/chef-client 0.050s ok github.com/hashicorp/packer/provisioner/chef-solo 0.061s ok github.com/hashicorp/packer/provisioner/converge 0.018s ok github.com/hashicorp/packer/provisioner/file 0.015s ok github.com/hashicorp/packer/provisioner/powershell 0.168s ok github.com/hashicorp/packer/provisioner/puppet-masterless 0.008s ok github.com/hashicorp/packer/provisioner/puppet-server 0.031s ok github.com/hashicorp/packer/provisioner/salt-masterless 0.010s ok github.com/hashicorp/packer/provisioner/shell 0.010s ok github.com/hashicorp/packer/provisioner/shell-local 0.037s ok github.com/hashicorp/packer/provisioner/windows-restart 0.180s ok github.com/hashicorp/packer/provisioner/windows-shell 0.159s ? github.com/hashicorp/packer/scripts [no test files] ok github.com/hashicorp/packer/template 0.008s ok github.com/hashicorp/packer/template/interpolate 0.004s ? github.com/hashicorp/packer/version [no test files] ```
2017-08-20 14:21:42 -04:00
package lexer
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
const (
//XItemError is an error with the parser input
XItemError XItemType = "Error"
//XItemAbsLocPath is an absolute path
XItemAbsLocPath = "Absolute path"
//XItemAbbrAbsLocPath represents an abbreviated absolute path
XItemAbbrAbsLocPath = "Abbreviated absolute path"
//XItemAbbrRelLocPath marks the start of a path expression
XItemAbbrRelLocPath = "Abbreviated relative path"
//XItemRelLocPath represents a relative location path
XItemRelLocPath = "Relative path"
//XItemEndPath marks the end of a path
XItemEndPath = "End path instruction"
//XItemAxis marks an axis specifier of a path
XItemAxis = "Axis"
//XItemAbbrAxis marks an abbreviated axis specifier (just @ at this point)
XItemAbbrAxis = "Abbreviated attribute axis"
//XItemNCName marks a namespace name in a node test
XItemNCName = "Namespace"
//XItemQName marks the local name in an a node test
XItemQName = "Local name"
//XItemNodeType marks a node type in a node test
XItemNodeType = "Node type"
//XItemProcLit marks a processing-instruction literal
XItemProcLit = "processing-instruction"
//XItemFunction marks a function call
XItemFunction = "function"
//XItemArgument marks a function argument
XItemArgument = "function argument"
//XItemEndFunction marks the end of a function
XItemEndFunction = "end of function"
//XItemPredicate marks a predicate in an axis
XItemPredicate = "predicate"
//XItemEndPredicate marks a predicate in an axis
XItemEndPredicate = "end of predicate"
//XItemStrLit marks a string literal
XItemStrLit = "string literal"
//XItemNumLit marks a numeric literal
XItemNumLit = "numeric literal"
//XItemOperator marks an operator
XItemOperator = "operator"
//XItemVariable marks a variable reference
XItemVariable = "variable"
)
const (
eof = -(iota + 1)
)
//XItemType is the parser token types
type XItemType string
//XItem is the token emitted from the parser
type XItem struct {
Typ XItemType
Val string
}
type stateFn func(*Lexer) stateFn
//Lexer lexes out XPath expressions
type Lexer struct {
input string
start int
pos int
width int
items chan XItem
}
//Lex an XPath expresion on the io.Reader
func Lex(xpath string) chan XItem {
l := &Lexer{
input: xpath,
items: make(chan XItem),
}
go l.run()
return l.items
}
func (l *Lexer) run() {
for state := startState; state != nil; {
state = state(l)
}
if l.peek() != eof {
l.errorf("Malformed XPath expression")
}
close(l.items)
}
func (l *Lexer) emit(t XItemType) {
l.items <- XItem{t, l.input[l.start:l.pos]}
l.start = l.pos
}
func (l *Lexer) emitVal(t XItemType, val string) {
l.items <- XItem{t, val}
l.start = l.pos
}
func (l *Lexer) next() (r rune) {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += l.width
return r
}
func (l *Lexer) ignore() {
l.start = l.pos
}
func (l *Lexer) backup() {
l.pos -= l.width
}
func (l *Lexer) peek() rune {
r := l.next()
l.backup()
return r
}
func (l *Lexer) peekAt(n int) rune {
if n <= 1 {
return l.peek()
}
width := 0
var ret rune
for count := 0; count < n; count++ {
r, s := utf8.DecodeRuneInString(l.input[l.pos+width:])
width += s
if l.pos+width > len(l.input) {
return eof
}
ret = r
}
return ret
}
func (l *Lexer) accept(valid string) bool {
if strings.ContainsRune(valid, l.next()) {
return true
}
l.backup()
return false
}
func (l *Lexer) acceptRun(valid string) {
for strings.ContainsRune(valid, l.next()) {
}
l.backup()
}
func (l *Lexer) skip(num int) {
for i := 0; i < num; i++ {
l.next()
}
l.ignore()
}
func (l *Lexer) skipWS(ig bool) {
for {
n := l.next()
if n == eof || !unicode.IsSpace(n) {
break
}
}
l.backup()
if ig {
l.ignore()
}
}
func (l *Lexer) errorf(format string, args ...interface{}) stateFn {
l.items <- XItem{
XItemError,
fmt.Sprintf(format, args...),
}
return nil
}
func isElemChar(r rune) bool {
return string(r) != ":" && string(r) != "/" &&
(unicode.Is(first, r) || unicode.Is(second, r) || string(r) == "*") &&
r != eof
}
func startState(l *Lexer) stateFn {
l.skipWS(true)
if string(l.peek()) == "/" {
l.next()
l.ignore()
if string(l.next()) == "/" {
l.ignore()
return abbrAbsLocPathState
}
l.backup()
return absLocPathState
} else if string(l.peek()) == `'` || string(l.peek()) == `"` {
if err := getStrLit(l, XItemStrLit); err != nil {
return l.errorf(err.Error())
}
if l.peek() != eof {
return startState
}
} else if getNumLit(l) {
l.skipWS(true)
if l.peek() != eof {
return startState
}
} else if string(l.peek()) == "$" {
l.next()
l.ignore()
r := l.peek()
for unicode.Is(first, r) || unicode.Is(second, r) {
l.next()
r = l.peek()
}
tok := l.input[l.start:l.pos]
if len(tok) == 0 {
return l.errorf("Empty variable name")
}
l.emit(XItemVariable)
l.skipWS(true)
if l.peek() != eof {
return startState
}
} else if st := findOperatorState(l); st != nil {
return st
} else {
if isElemChar(l.peek()) {
colons := 0
for {
if isElemChar(l.peek()) {
l.next()
} else if string(l.peek()) == ":" {
l.next()
colons++
} else {
break
}
}
if string(l.peek()) == "(" && colons <= 1 {
tok := l.input[l.start:l.pos]
err := procFunc(l, tok)
if err != nil {
return l.errorf(err.Error())
}
l.skipWS(true)
if string(l.peek()) == "/" {
l.next()
l.ignore()
if string(l.next()) == "/" {
l.ignore()
return abbrRelLocPathState
}
l.backup()
return relLocPathState
}
return startState
}
l.pos = l.start
return relLocPathState
} else if string(l.peek()) == "@" {
return relLocPathState
}
}
return nil
}
func strPeek(str string, l *Lexer) bool {
for i := 0; i < len(str); i++ {
if string(l.peekAt(i+1)) != string(str[i]) {
return false
}
}
return true
}
func findOperatorState(l *Lexer) stateFn {
l.skipWS(true)
switch string(l.peek()) {
case ">", "<", "!":
l.next()
if string(l.peek()) == "=" {
l.next()
}
l.emit(XItemOperator)
return startState
case "|", "+", "-", "*", "=":
l.next()
l.emit(XItemOperator)
return startState
case "(":
l.next()
l.emit(XItemOperator)
for state := startState; state != nil; {
state = state(l)
}
l.skipWS(true)
if string(l.next()) != ")" {
return l.errorf("Missing end )")
}
l.emit(XItemOperator)
return startState
}
if strPeek("and", l) {
l.next()
l.next()
l.next()
l.emit(XItemOperator)
return startState
}
if strPeek("or", l) {
l.next()
l.next()
l.emit(XItemOperator)
return startState
}
if strPeek("mod", l) {
l.next()
l.next()
l.next()
l.emit(XItemOperator)
return startState
}
if strPeek("div", l) {
l.next()
l.next()
l.next()
l.emit(XItemOperator)
return startState
}
return nil
}
func getStrLit(l *Lexer, tok XItemType) error {
q := l.next()
var r rune
l.ignore()
for r != q {
r = l.next()
if r == eof {
return fmt.Errorf("Unexpected end of string literal.")
}
}
l.backup()
l.emit(tok)
l.next()
l.ignore()
return nil
}
func getNumLit(l *Lexer) bool {
const dig = "0123456789"
l.accept("-")
start := l.pos
l.acceptRun(dig)
if l.pos == start {
return false
}
if l.accept(".") {
l.acceptRun(dig)
}
l.emit(XItemNumLit)
return true
}