forked from External/ergo
fix #1003
This commit is contained in:
parent
21958768d8
commit
be0dedf260
12 changed files with 199 additions and 437 deletions
19
vendor/github.com/goshuirc/e-nfa/.travis.yml
generated
vendored
19
vendor/github.com/goshuirc/e-nfa/.travis.yml
generated
vendored
|
|
@ -1,19 +0,0 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- 1.4
|
||||
- tip
|
||||
|
||||
before_install:
|
||||
- go get golang.org/x/tools/cmd/cover
|
||||
- go get golang.org/x/tools/cmd/vet
|
||||
- go get golang.org/x/tools/cmd/goimports
|
||||
- go get github.com/golang/lint/golint
|
||||
- go get github.com/mattn/goveralls
|
||||
|
||||
script:
|
||||
- go vet ./...
|
||||
# - $HOME/gopath/bin/goveralls -coverprofile=coverage.cov -service=travis-ci
|
||||
# - bash <(curl -s https://codecov.io/bash)
|
||||
- go test -bench=. -benchmem ./...
|
||||
#- sh ./install_all_cmd.sh
|
||||
122
vendor/github.com/goshuirc/e-nfa/README.md
generated
vendored
122
vendor/github.com/goshuirc/e-nfa/README.md
generated
vendored
|
|
@ -1,122 +0,0 @@
|
|||
ε-NFA: Epsilon-Nondeterministic finite automaton
|
||||
==============
|
||||
|
||||
[](https://raw.githubusercontent.com/kkdai/e-nfa/master/LICENSE) [](https://godoc.org/github.com/kkdai/e-nfa) [](https://travis-ci.org/kkdai/e-nfa)
|
||||
|
||||
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
What is Epsilon-Nondeterministic finite automaton
|
||||
=============
|
||||
|
||||
`ε-NFA`: Epsilon-Nondeterministic finite automaton (so call:Nondeterministic finite automaton with ε-moves)
|
||||
|
||||
In the automata theory, a nondeterministic finite automaton with ε-moves (NFA-ε)(also known as NFA-λ) is an extension of nondeterministic finite automaton(NFA), which allows a transformation to a new state without consuming any input symbols. The transitions without consuming an input symbol are called ε-transitions or λ-transitions. In the state diagrams, they are usually labeled with the Greek letter ε or λ.
|
||||
|
||||
(sited from [here](https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton))
|
||||
|
||||
|
||||
Looking for DFA implement?
|
||||
=============
|
||||
|
||||
I also write a DFA implenent in Go here. [https://github.com/kkdai/dfa](https://github.com/kkdai/dfa)
|
||||
|
||||
Looking for NFA implement?
|
||||
=============
|
||||
|
||||
I also write a NFA implenent in Go here. [https://github.com/kkdai/nfa](https://github.com/kkdai/nfa)
|
||||
|
||||
|
||||
Installation and Usage
|
||||
=============
|
||||
|
||||
|
||||
Install
|
||||
---------------
|
||||
|
||||
go get github.com/kkdai/e-nfa
|
||||
|
||||
|
||||
|
||||
Usage
|
||||
---------------
|
||||
|
||||
Following is sample code to implement a epsilon-NFA automata diagram as follow:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
```go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kkdai/enfa"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
nfa := NewENFA(0, false)
|
||||
nfa.AddState(1, false)
|
||||
nfa.AddState(2, false)
|
||||
nfa.AddState(3, true)
|
||||
nfa.AddState(4, false)
|
||||
nfa.AddState(5, false)
|
||||
|
||||
nfa.AddTransition(0, "1", 1)
|
||||
nfa.AddTransition(0, "0", 4)
|
||||
|
||||
nfa.AddTransition(1, "1", 2)
|
||||
nfa.AddTransition(1, "", 3) //epsilon
|
||||
nfa.AddTransition(2, "1", 3)
|
||||
nfa.AddTransition(4, "0", 5)
|
||||
nfa.AddTransition(4, "", 1, 2) //E -> epsilon B C
|
||||
nfa.AddTransition(5, "0", 3)
|
||||
|
||||
nfa.PrintTransitionTable()
|
||||
|
||||
if !nfa.VerifyInputs([]string{"1"}) {
|
||||
fmt.Printf("Verify inputs is failed")
|
||||
}
|
||||
|
||||
nfa.Reset()
|
||||
|
||||
if !nfa.VerifyInputs([]string{"1", "1", "1"}) {
|
||||
fmt.Printf("Verify inputs is failed")
|
||||
}
|
||||
|
||||
nfa.Reset()
|
||||
|
||||
if !nfa.VerifyInputs([]string{"0", "1"}) {
|
||||
fmt.Printf"Verify inputs is failed")
|
||||
}
|
||||
|
||||
nfa.Reset()
|
||||
if !nfa.VerifyInputs([]string{"0", "0", "0"}) {
|
||||
fmt.Printf("Verify inputs is failed")
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Inspired By
|
||||
=============
|
||||
|
||||
- [ε-NFA: Wiki](https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton_with_%CE%B5-moves)
|
||||
- [Coursera: Automata](https://class.coursera.org/automata-004/)
|
||||
|
||||
Project52
|
||||
---------------
|
||||
|
||||
It is one of my [project 52](https://github.com/kkdai/project52).
|
||||
|
||||
|
||||
License
|
||||
---------------
|
||||
|
||||
This package is licensed under MIT license. See LICENSE for details.
|
||||
185
vendor/github.com/goshuirc/e-nfa/enfa.go
generated
vendored
185
vendor/github.com/goshuirc/e-nfa/enfa.go
generated
vendored
|
|
@ -1,185 +0,0 @@
|
|||
package enfa
|
||||
|
||||
import "fmt"
|
||||
|
||||
type transitionInput struct {
|
||||
srcState int
|
||||
input string
|
||||
}
|
||||
|
||||
type destState map[int]bool
|
||||
|
||||
type ENFA struct {
|
||||
initState int
|
||||
currentState map[int]bool
|
||||
totalStates []int
|
||||
finalStates []int
|
||||
transition map[transitionInput]destState
|
||||
inputMap map[string]bool
|
||||
}
|
||||
|
||||
//New a new NFA
|
||||
func NewENFA(initState int, isFinal bool) *ENFA {
|
||||
|
||||
retNFA := &ENFA{
|
||||
transition: make(map[transitionInput]destState),
|
||||
inputMap: make(map[string]bool),
|
||||
initState: initState}
|
||||
|
||||
retNFA.currentState = make(map[int]bool)
|
||||
retNFA.currentState[initState] = true
|
||||
retNFA.AddState(initState, isFinal)
|
||||
return retNFA
|
||||
}
|
||||
|
||||
//Add new state in this NFA
|
||||
func (d *ENFA) AddState(state int, isFinal bool) {
|
||||
if state == -1 {
|
||||
fmt.Println("Cannot add state as -1, it is dead state")
|
||||
return
|
||||
}
|
||||
|
||||
d.totalStates = append(d.totalStates, state)
|
||||
if isFinal {
|
||||
d.finalStates = append(d.finalStates, state)
|
||||
}
|
||||
}
|
||||
|
||||
//Add new transition function into NFA
|
||||
func (d *ENFA) AddTransition(srcState int, input string, dstStateList ...int) {
|
||||
find := false
|
||||
|
||||
//find input if exist in NFA input List
|
||||
if _, ok := d.inputMap[input]; !ok {
|
||||
//not exist, new input in this NFA
|
||||
d.inputMap[input] = true
|
||||
}
|
||||
|
||||
for _, v := range d.totalStates {
|
||||
if v == srcState {
|
||||
find = true
|
||||
}
|
||||
}
|
||||
|
||||
if !find {
|
||||
fmt.Println("No such state:", srcState, " in current NFA")
|
||||
return
|
||||
}
|
||||
|
||||
dstMap := make(map[int]bool)
|
||||
for _, destState := range dstStateList {
|
||||
dstMap[destState] = true
|
||||
}
|
||||
|
||||
targetTrans := transitionInput{srcState: srcState, input: input}
|
||||
d.transition[targetTrans] = dstMap
|
||||
}
|
||||
|
||||
func (d *ENFA) CheckPathExist(src int, input string, dst int) bool {
|
||||
retMap, _ := d.transition[transitionInput{srcState: src, input: input}]
|
||||
if _, ok := retMap[dst]; ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (d *ENFA) Input(testInput string) []int {
|
||||
updateCurrentState := make(map[int]bool)
|
||||
for current, _ := range d.currentState {
|
||||
for _, realTestInput := range []string{testInput, "*", "?"} {
|
||||
intputTrans := transitionInput{srcState: current, input: realTestInput}
|
||||
valMap, ok := d.transition[intputTrans]
|
||||
if ok {
|
||||
for dst, _ := range valMap {
|
||||
updateCurrentState[dst] = true
|
||||
|
||||
//Update epsilon input way... if exist
|
||||
epsilonTrans := transitionInput{srcState: dst}
|
||||
if eMap, ok := d.transition[epsilonTrans]; ok {
|
||||
for eDst, _ := range eMap {
|
||||
updateCurrentState[eDst] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//dead state, remove in current state
|
||||
//do nothing.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//update curret state
|
||||
d.currentState = updateCurrentState
|
||||
|
||||
//return result
|
||||
var ret []int
|
||||
for state, _ := range updateCurrentState {
|
||||
ret = append(ret, state)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
//To verify current state if it is final state
|
||||
func (d *ENFA) Verify() bool {
|
||||
for _, val := range d.finalStates {
|
||||
for cState, _ := range d.currentState {
|
||||
if val == cState {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//Reset NFA state to initilize state, but all state and transition function will remain
|
||||
func (d *ENFA) Reset() {
|
||||
initState := make(map[int]bool)
|
||||
initState[d.initState] = true
|
||||
d.currentState = initState
|
||||
}
|
||||
|
||||
//Verify if list of input could be accept by NFA or not
|
||||
func (d *ENFA) VerifyInputs(inputs []string) bool {
|
||||
for _, v := range inputs {
|
||||
d.Input(v)
|
||||
}
|
||||
return d.Verify()
|
||||
}
|
||||
|
||||
//To print detail transition table contain of current NFA
|
||||
func (d *ENFA) PrintTransitionTable() {
|
||||
fmt.Println("===================================================")
|
||||
//list all inputs
|
||||
var inputList []string
|
||||
for key, _ := range d.inputMap {
|
||||
if key == "" {
|
||||
fmt.Printf("\tε|")
|
||||
} else {
|
||||
fmt.Printf("\t%s|", key)
|
||||
}
|
||||
inputList = append(inputList, key)
|
||||
}
|
||||
|
||||
fmt.Printf("\n")
|
||||
fmt.Println("---------------------------------------------------")
|
||||
|
||||
for _, state := range d.totalStates {
|
||||
fmt.Printf("%d |", state)
|
||||
for _, key := range inputList {
|
||||
checkInput := transitionInput{srcState: state, input: key}
|
||||
if dstState, ok := d.transition[checkInput]; ok {
|
||||
fmt.Printf("\t")
|
||||
for val, _ := range dstState {
|
||||
fmt.Printf("%d,", val)
|
||||
}
|
||||
fmt.Printf("|")
|
||||
} else {
|
||||
fmt.Printf("\tNA|")
|
||||
}
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
fmt.Println("---------------------------------------------------")
|
||||
fmt.Println("===================================================")
|
||||
}
|
||||
7
vendor/github.com/goshuirc/irc-go/ircmatch/doc.go
generated
vendored
7
vendor/github.com/goshuirc/irc-go/ircmatch/doc.go
generated
vendored
|
|
@ -1,7 +0,0 @@
|
|||
// written by Daniel Oaks <daniel@danieloaks.net>
|
||||
// released under the ISC license
|
||||
|
||||
/*
|
||||
Package ircmatch handles matching IRC strings with the traditional glob-like syntax.
|
||||
*/
|
||||
package ircmatch
|
||||
57
vendor/github.com/goshuirc/irc-go/ircmatch/ircmatch.go
generated
vendored
57
vendor/github.com/goshuirc/irc-go/ircmatch/ircmatch.go
generated
vendored
|
|
@ -1,57 +0,0 @@
|
|||
package ircmatch
|
||||
|
||||
import enfa "github.com/goshuirc/e-nfa"
|
||||
|
||||
// Matcher represents an object that can match IRC strings.
|
||||
type Matcher struct {
|
||||
internalENFA *enfa.ENFA
|
||||
}
|
||||
|
||||
// MakeMatch creates a Matcher.
|
||||
func MakeMatch(globTemplate string) Matcher {
|
||||
var newmatch Matcher
|
||||
|
||||
// assemble internal enfa
|
||||
newmatch.internalENFA = enfa.NewENFA(0, false)
|
||||
|
||||
var currentState int
|
||||
var lastWasStar bool
|
||||
for _, char := range globTemplate {
|
||||
if char == '*' {
|
||||
if lastWasStar {
|
||||
continue
|
||||
}
|
||||
newmatch.internalENFA.AddTransition(currentState, "*", currentState)
|
||||
lastWasStar = true
|
||||
continue
|
||||
} else if char == '?' {
|
||||
newmatch.internalENFA.AddState(currentState+1, false)
|
||||
newmatch.internalENFA.AddTransition(currentState, "?", currentState+1)
|
||||
currentState++
|
||||
} else {
|
||||
newmatch.internalENFA.AddState(currentState+1, false)
|
||||
newmatch.internalENFA.AddTransition(currentState, string(char), currentState+1)
|
||||
currentState++
|
||||
}
|
||||
|
||||
lastWasStar = false
|
||||
}
|
||||
|
||||
// create end state
|
||||
newmatch.internalENFA.AddState(currentState+1, true)
|
||||
newmatch.internalENFA.AddTransition(currentState, "", currentState+1)
|
||||
|
||||
return newmatch
|
||||
}
|
||||
|
||||
// Match returns true if the given string matches this glob.
|
||||
func (menfa *Matcher) Match(search string) bool {
|
||||
var searchChars []string
|
||||
for _, char := range search {
|
||||
searchChars = append(searchChars, string(char))
|
||||
}
|
||||
|
||||
isMatch := menfa.internalENFA.VerifyInputs(searchChars)
|
||||
menfa.internalENFA.Reset()
|
||||
return isMatch
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue