Check-in [61937abf4c]
Overview
SHA1:61937abf4ccfe96754ece2b1a11d628de455e3a7
Date: 2015-03-19 20:51:40
User: spaskalev
Comment:Added parse.Digit and a test
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2015-04-06
19:20
[87e5dc43ff] sync tbd (user: spaskalev, tags: trunk)
2015-03-19
20:51
[61937abf4c] Added parse.Digit and a test (user: spaskalev, tags: trunk)
20:40
[b37c997693] Added a test for parse.Defer (user: spaskalev, tags: trunk)
Changes

Modified src/0dev.org/parse/parse.go from [6601f36901] to [bccb31a2c9].

86
87
88
89
90
91
92
















93
94
95
96
	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
	}
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




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
	}
}

Modified src/0dev.org/parse/parse_test.go from [eebf67dde7] to [496af272f1].

72
73
74
75
76
77
78










	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))
	}
}

















>
>
>
>
>
>
>
>
>
>
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)
	}
}