示例#1
0
func jsonObject(str, value *tomato.Automaton) *tomato.Automaton {
	q0 := tomato.NewState()
	q1 := tomato.NewState()
	q2 := tomato.NewState()
	q3 := tomato.NewState()
	q4 := tomato.NewState()
	q5 := tomato.NewState()
	q6 := tomato.NewState()
	q7 := tomato.NewState()

	q0.Add(transition.NewRune('{', q1))

	q1.Add(transition.NewEpsilon(q2))
	q1.Add(transition.NewEpsilon(q6))

	q2.Add(transition.NewAutomaton(str, []*tomato.State{q3}))

	q3.Add(transition.NewRune(':', q4))

	q4.Add(transition.NewAutomaton(value, []*tomato.State{q5}))

	q5.Add(transition.NewRune(',', q2))
	q5.Add(transition.NewEpsilon(q6))

	q6.Add(transition.NewRune('}', q7))

	return tomato.NewAutomaton(q0, []*tomato.State{q7})
}
示例#2
0
func jsonString() *tomato.Automaton {
	q0 := tomato.NewState()
	q1 := tomato.NewState()
	q2 := tomato.NewState()
	q2_1 := tomato.NewState()
	q2_1_1 := tomato.NewState()
	q3 := tomato.NewState()
	q4 := tomato.NewState()

	q0.Add(transition.NewRune('"', q1))

	q1.Add(transition.NewEpsilon(q2))
	q1.Add(transition.NewEpsilon(q3))

	q2.Add(transition.NewRegexp("[^\\\"]+", q3))
	q2.Add(transition.NewRune('\\', q2_1))

	q2_1.Add(transition.NewRegexp("[\"\\/bfnrt]", q3))
	q2_1.Add(transition.NewRune('u', q2_1_1))

	q2_1_1.Add(transition.NewRegexp("[0-9a-f]{4}", q3))

	q3.Add(transition.NewEpsilon(q2))
	q3.Add(transition.NewRune('"', q4))

	return tomato.NewAutomaton(q0, []*tomato.State{q4})
}
示例#3
0
func jsonNumber() *tomato.Automaton {
	q0 := tomato.NewState()
	q1 := tomato.NewState()
	q1_1 := tomato.NewState()
	q2 := tomato.NewState()
	q3 := tomato.NewState()
	q4 := tomato.NewState()
	q5 := tomato.NewState()
	q5_1 := tomato.NewState()
	q5_2 := tomato.NewState()
	q5_3 := tomato.NewState()
	q6 := tomato.NewState()

	q0.Add(transition.NewRune('-', q1))
	q0.Add(transition.NewEpsilon(q1))

	q1.Add(transition.NewRune('0', q2))
	q1.Add(transition.NewRegexp("[1-9]", q1_1))

	q1_1.Add(transition.NewRegexp("[0-9]", q1_1))
	q1_1.Add(transition.NewEpsilon(q2))

	q2.Add(transition.NewEpsilon(q5))
	q2.Add(transition.NewRune('.', q3))

	q3.Add(transition.NewRegexp("[0-9]", q4))

	q4.Add(transition.NewRegexp("[0-9]", q4))
	q4.Add(transition.NewEpsilon(q5))

	q5.Add(transition.NewRegexp("[eE]", q5_1))
	q5.Add(transition.NewEpsilon(q6))

	q5_1.Add(transition.NewRegexp("[+-]?", q5_2))

	q5_2.Add(transition.NewRegexp("[0-9]", q5_3))

	q5_3.Add(transition.NewRegexp("[0-9]", q5_3))
	q5_3.Add(transition.NewEpsilon(q6))

	return tomato.NewAutomaton(q0, []*tomato.State{q6})
}
示例#4
0
func jsonArray(value *tomato.Automaton) *tomato.Automaton {
	q0 := tomato.NewState()
	q1 := tomato.NewState()
	q2 := tomato.NewState()
	q3 := tomato.NewState()
	q4 := tomato.NewState()
	q5 := tomato.NewState()

	q0.Add(transition.NewRune('[', q1))

	q1.Add(transition.NewEpsilon(q2))
	q1.Add(transition.NewEpsilon(q4))

	q2.Add(transition.NewAutomaton(value, []*tomato.State{q3}))

	q3.Add(transition.NewRune(',', q2))
	q3.Add(transition.NewEpsilon(q4))

	q4.Add(transition.NewRune(']', q5))

	return tomato.NewAutomaton(q0, []*tomato.State{q5})
}
示例#5
0
// A simple automaton that recognizes words containing "ab".
func TestContains(t *testing.T) {
	q0 := tomato.NewState()
	q1 := tomato.NewState()
	q2 := tomato.NewState()

	q0.Add(transition.NewRune('a', q1))
	q0.Add(transition.NewRune('b', q0))
	q0.Add(transition.NewRune('c', q0))

	q1.Add(transition.NewRune('a', q1))
	q1.Add(transition.NewRune('b', q2))
	q1.Add(transition.NewRune('c', q0))

	q2.Add(transition.NewRune('a', q2))
	q2.Add(transition.NewRune('b', q2))
	q2.Add(transition.NewRune('c', q2))

	a := tomato.NewAutomaton(q0, []*tomato.State{q2})

	items := []testCase{
		{"ab", true},
		{"abc", true},
		{"cab", true},
		{"cabc", true},
		{"aaaab", true},
		{"aba", true},
		{"abab", true},
		{"acab", true},
		{"a", false},
		{"b", false},
		{"aa", false},
		{"ba", false},
		{"bbbaaa", false},
		{"aca", false},
	}

	testAll(t, a, items)
}