scanner: # style line comment scanning implemented

This commit is contained in:
Fatih Arslan 2015-10-05 13:24:38 +03:00
parent 9096153a34
commit 2216cd81e9
2 changed files with 41 additions and 2 deletions

View File

@ -120,6 +120,9 @@ func (s *Scanner) Scan() (tok token.Token) {
case '"':
tok = token.STRING
s.scanString()
case '#':
tok = token.COMMENT
s.scanComment(ch)
case '.':
ch = s.peek()
if isDecimal(ch) {
@ -152,6 +155,17 @@ func (s *Scanner) Scan() (tok token.Token) {
return tok
}
func (s *Scanner) scanComment(ch rune) {
if ch == '#' {
// line comment
ch = s.next()
for ch != '\n' && ch >= 0 {
ch = s.next()
}
s.unread()
}
}
// scanNumber scans a HCL number definition starting with the given rune
func (s *Scanner) scanNumber(ch rune) token.Token {
if ch == '0' {

View File

@ -30,16 +30,41 @@ func testTokenList(t *testing.T, tokenList []tokenPair) {
for _, ident := range tokenList {
tok := s.Scan()
if tok != ident.tok {
t.Errorf("tok = %s want %s for %s\n", tok, ident.tok, ident.text)
t.Errorf("tok = %q want %q for %q\n", tok, ident.tok, ident.text)
}
if s.TokenText() != ident.text {
t.Errorf("text = %s want %s", s.TokenText(), ident.text)
t.Errorf("text = %q want %q", s.TokenText(), ident.text)
}
}
}
func TestComment(t *testing.T) {
var tokenList = []tokenPair{
// {token.COMMENT, "//"},
// {token.COMMENT, "////"},
// {token.COMMENT, "// comment"},
// {token.COMMENT, "// /* comment */"},
// {token.COMMENT, "// // comment //"},
{token.COMMENT, "#"},
{token.COMMENT, "##"},
{token.COMMENT, "# comment"},
{token.COMMENT, "# /* comment */"},
{token.COMMENT, "# # comment #"},
{token.COMMENT, "#" + f100},
// {token.COMMENT, "/**/"},
// {token.COMMENT, "/***/"},
// {token.COMMENT, "/* comment */"},
// {token.COMMENT, "/* // comment */"},
// {token.COMMENT, "/* /* comment */"},
// {token.COMMENT, "/*\n comment\n*/"},
// {token.COMMENT, "/*" + f100 + "*/"},
}
testTokenList(t, tokenList)
}
func TestOperator(t *testing.T) {
var tokenList = []tokenPair{
{token.LBRACK, "["},