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     86   	return func(s string) (N, string) {
    87     87   		n, r := accept(s)
    88     88   		if !n.Matched {
    89     89   			return n, s
    90     90   		}
    91     91   		if n.Content == value {
    92     92   			return n, r
           93  +		}
           94  +		return N{Matched: false, Content: "", Nodes: []N{n}}, s
           95  +	}
           96  +}
           97  +
           98  +// Returns a parser that matches any single digit
           99  +// It could have been written as Any(String("0"),String("1"),...,String("9")) as well :)
          100  +func Digit() P {
          101  +	accept := Accept(1)
          102  +	return func(s string) (N, string) {
          103  +		n, r := accept(s)
          104  +		if !n.Matched {
          105  +			return n, s
          106  +		}
          107  +		if n.Content[0] >= 48 && n.Content[0] <= 57 {
          108  +			return n, r
    93    109   		}
    94    110   		return N{Matched: false, Content: "", Nodes: []N{n}}, s
    95    111   	}
    96    112   }

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

    72     72   	if n, r := s("aa"); !n.Matched || n.Content != "aa" || n.Nodes != nil || r != "" {
    73     73   		t.Error("Invalid result for String match test", n)
    74     74   	}
    75     75   	if n, r := s("ab"); n.Matched || n.Content != "" || n.Nodes == nil || r != "ab" {
    76     76   		t.Error("Invalid result for String no-match test", n, r, len(r))
    77     77   	}
    78     78   }
           79  +
           80  +func TestDigit(t *testing.T) {
           81  +	d := Digit()
           82  +	if n, r := d("a"); n.Matched || n.Content != "" || n.Nodes == nil || r != "a" {
           83  +		t.Error("Invalid result for Digit no-match test", n)
           84  +	}
           85  +	if n, r := d("1"); !n.Matched || n.Content != "1" || n.Nodes != nil || r != "" {
           86  +		t.Error("Invalid result for Digit match test", n)
           87  +	}
           88  +}