示例#1
0
文件: scan.go 项目: aylusltd/calc
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)
}
示例#2
0
文件: scan.go 项目: unreal/calc
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)
}
示例#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)
		}
	}
}