Exemple #1
0
func (s *Scanner) scanIdentifier() (string, token.Token, token.Pos) {
	start := s.offset
	var str string

	for unicode.IsLetter(s.ch) || unicode.IsDigit(s.ch) {
		str += string(s.ch)
		s.next()
	}
	return str, token.Lookup(str), s.file.Pos(start)
}
Exemple #2
0
func (s *Scanner) scanIdentifier() (string, token.Token, token.Pos) {
	start := s.offset

	for unicode.IsLetter(s.ch) || unicode.IsDigit(s.ch) {
		s.next()
	}
	offset := s.offset
	if s.ch == rune(0) {
		offset++
	}
	lit := s.src[start:offset]
	return lit, token.Lookup(lit), s.file.Pos(start)
}
Exemple #3
0
func TestLookup(t *testing.T) {
	var tests = []struct {
		str string
		tok token.Token
	}{
		{"+", token.ADD},
		{"%", token.REM},
		{"EOF", token.EOF},
		{"Integer", token.INTEGER},
		{"", token.IDENT},
	}

	for i, v := range tests {
		if res := token.Lookup(v.str); res != v.tok {
			t.Fatal(i, "- Expected:", v.tok, "Got:", res)
		}
	}
}