Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing begin of symbol. #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,40 @@ func scanDiv(s *Scanner) (token.Token, []byte) {
return token.Div, nil
}

var symbolPrefixMap = [...]bool{
'!': true,
'"': true,
'$': true,
'%': true,
'&': true,
'\'': true,
'*': true,
'+': true,
'-': true,
'/': true,
'<': true,
'=': true,
'>': true,
'@': true,
'[': true,
'^': true,
'_': true,
'`': true,
'|': true,
'~': true,
}

func isSymbolPrefix(c byte) bool {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gometalinter]

  • [gocyclo] cyclomatic complexity 13 of function isSymbolPrefix() is high (> 12)

return token.IsAlnum(c) || symbolPrefixMap[c]
}

func scanColon(s *Scanner) (token.Token, []byte) {
if s.char == ':' {
switch {
case s.char == ':':
s.next()
return token.Colon2, nil
case isSymbolPrefix(s.char):
return token.SymbolBegin, nil
}
return token.Colon, nil
}
Expand Down
38 changes: 38 additions & 0 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,44 @@ var rules = map[string][]struct {
{3, token.IdentLocalVar, []byte("b")},
},

// symbol
":a": {
{0, token.SymbolBegin, nil},
{1, token.IdentLocalVar, []byte("a")},
},
":@a": {
{0, token.SymbolBegin, nil},
{1, token.IdentInstanceVar, []byte("@a")},
},
":@@a": {
{0, token.SymbolBegin, nil},
{1, token.IdentClassVar, []byte("@@a")},
},
":$a": {
{0, token.SymbolBegin, nil},
{1, token.IdentGlobalVar, []byte("$a")},
},
":<<": {
{0, token.SymbolBegin, nil},
{1, token.LShift, nil},
},
":'a'": {
{0, token.SymbolBegin, nil},
{1, token.String, []byte("a")},
},
":\"a\"": {
{0, token.SymbolBegin, nil},
{1, token.String, []byte("a")},
},
":\"#{a}\"": {
{0, token.SymbolBegin, nil},
{1, token.StringPart, nil},
{2, token.InsertBegin, nil},
{4, token.IdentLocalVar, []byte("a")},
{5, token.InsertEnd, nil},
{6, token.String, nil},
},

// keywords
"__LINE__": {{0, token.KeywordLINE, nil}},
"__ENCODING__": {{0, token.KeywordENCODING, nil}},
Expand Down
1 change: 1 addition & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
HeredocEnd
InsertBegin
InsertEnd
SymbolBegin

// brackets:
LParen // (
Expand Down