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

parser/lexer: correct ID_Start & ID_Continue checks #524

Merged
merged 2 commits into from
Jun 12, 2024
Merged
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
51 changes: 49 additions & 2 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,68 @@ func digitValue(chr rune) int {
return 16 // Larger than any legal digit value
}

// See https://www.unicode.org/reports/tr31/ for reference on ID_Start and ID_Continue.
var includeIDStart = []*unicode.RangeTable{
unicode.Lu,
unicode.Ll,
unicode.Lt,
unicode.Lm,
unicode.Lo,
unicode.Nl,
unicode.Other_ID_Start,
}

var includeIDContinue = []*unicode.RangeTable{
unicode.Lu,
unicode.Ll,
unicode.Lt,
unicode.Lm,
unicode.Lo,
unicode.Nl,
unicode.Other_ID_Start,
unicode.Mn,
unicode.Mc,
unicode.Nd,
unicode.Pc,
unicode.Other_ID_Continue,
}

var exclude = []*unicode.RangeTable{
unicode.Pattern_Syntax,
unicode.Pattern_White_Space,
}

func unicodeIDStart(r rune) bool {
if unicode.In(r, exclude...) {
return false
}

return unicode.In(r, includeIDStart...)
}

func unicodeIDContinue(r rune) bool {
if unicode.In(r, exclude...) {
return false
}

return unicode.In(r, includeIDContinue...)
}

func isDigit(chr rune, base int) bool {
return digitValue(chr) < base
}

func isIdentifierStart(chr rune) bool {
return chr == '$' || chr == '_' || chr == '\\' ||
'a' <= chr && chr <= 'z' || 'A' <= chr && chr <= 'Z' ||
chr >= utf8.RuneSelf && unicode.IsLetter(chr)
chr >= utf8.RuneSelf && unicodeIDStart(chr)
}

func isIdentifierPart(chr rune) bool {
return chr == '$' || chr == '_' || chr == '\\' ||
'a' <= chr && chr <= 'z' || 'A' <= chr && chr <= 'Z' ||
'0' <= chr && chr <= '9' ||
chr >= utf8.RuneSelf && (unicode.IsLetter(chr) || unicode.IsDigit(chr))
chr >= utf8.RuneSelf && unicodeIDContinue(chr)
}

func (p *parser) scanIdentifier() (string, error) {
Expand Down
25 changes: 25 additions & 0 deletions parser/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ Second line \
token.RIGHT_BRACKET, "", 6,
)

// Identifier from Unicode Nl
test("\u16ee",
token.IDENTIFIER, "ᛮ", 1,
)

// Identifier from Unicode Other_ID_Start
test("\u212e",
token.IDENTIFIER, "℮", 1,
)

// Using char from ID_Continue after valid start char
test("a\u0300",
token.IDENTIFIER, "à", 1,
)

// ILLEGAL

test(`3ea`,
Expand Down Expand Up @@ -383,5 +398,15 @@ Second line \
token.STRING, "\"\\x0G\"", 1,
token.EOF, "", 7,
)

// Starting identifier with ID_Continue char from Nm
test("\u0300",
token.ILLEGAL,
)

// Starting identifier with Pattern_Syntax
test("'",
token.ILLEGAL,
)
})
}