Check-in [61937abf4c]
Overview
Comment:Added parse.Digit and a test
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 61937abf4ccfe96754ece2b1a11d628de455e3a7
User & Date: spaskalev on 2015-03-19 20:51:40.908
Other Links: manifest | tags
Context
2015-04-06
19:20
sync tbd check-in: 87e5dc43ff user: spaskalev tags: trunk
2015-03-19
20:51
Added parse.Digit and a test check-in: 61937abf4c user: spaskalev tags: trunk
20:40
Added a test for parse.Defer check-in: b37c997693 user: spaskalev tags: trunk
Changes
86
87
88
89
90
91
92
















93
94
95
96
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




	return func(s string) (N, string) {
		n, r := accept(s)
		if !n.Matched {
			return n, s
		}
		if n.Content == value {
			return n, r
		}
		return N{Matched: false, Content: "", Nodes: []N{n}}, s
	}
}

// Returns a parser that matches any single digit
// It could have been written as Any(String("0"),String("1"),...,String("9")) as well :)
func Digit() P {
	accept := Accept(1)
	return func(s string) (N, string) {
		n, r := accept(s)
		if !n.Matched {
			return n, s
		}
		if n.Content[0] >= 48 && n.Content[0] <= 57 {
			return n, r
		}
		return N{Matched: false, Content: "", Nodes: []N{n}}, s
	}
}
72
73
74
75
76
77
78










72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88







+
+
+
+
+
+
+
+
+
+
	if n, r := s("aa"); !n.Matched || n.Content != "aa" || n.Nodes != nil || r != "" {
		t.Error("Invalid result for String match test", n)
	}
	if n, r := s("ab"); n.Matched || n.Content != "" || n.Nodes == nil || r != "ab" {
		t.Error("Invalid result for String no-match test", n, r, len(r))
	}
}

func TestDigit(t *testing.T) {
	d := Digit()
	if n, r := d("a"); n.Matched || n.Content != "" || n.Nodes == nil || r != "a" {
		t.Error("Invalid result for Digit no-match test", n)
	}
	if n, r := d("1"); !n.Matched || n.Content != "1" || n.Nodes != nil || r != "" {
		t.Error("Invalid result for Digit match test", n)
	}
}