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

Allowed Chinese character #502

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions parse/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"strconv"
"strings"
"unicode"

"github.com/yuin/gopher-lua/ast"
)
Expand All @@ -31,14 +32,17 @@ func (e *Error) Error() string {
}
}

func writeChar(buf *bytes.Buffer, c int) { buf.WriteByte(byte(c)) }
func writeChar(buf *bytes.Buffer, c int) { buf.WriteRune(rune(c)) }

func isDecimal(ch int) bool { return '0' <= ch && ch <= '9' }

func isIdent(ch int, pos int) bool {
return ch == '_' || 'A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z' || isDecimal(ch) && pos > 0
return isChinese(rune(ch)) || ch == '_' || 'A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z' || isDecimal(ch) && pos > 0
}

// isChinese
func isChinese(ch rune) bool { return unicode.Is(unicode.Han, ch) }

func isDigit(ch int) bool {
return '0' <= ch && ch <= '9' || 'a' <= ch && ch <= 'f' || 'A' <= ch && ch <= 'F'
}
Expand All @@ -64,7 +68,7 @@ func (sc *Scanner) Error(tok string, msg string) *Error { return &Error{sc.Pos,
func (sc *Scanner) TokenError(tok ast.Token, msg string) *Error { return &Error{tok.Pos, msg, tok.Str} }

func (sc *Scanner) readNext() int {
ch, err := sc.reader.ReadByte()
ch, _, err := sc.reader.ReadRune()
if err == io.EOF {
return EOF
}
Expand Down Expand Up @@ -101,7 +105,7 @@ func (sc *Scanner) Next() int {
func (sc *Scanner) Peek() int {
ch := sc.readNext()
if ch != EOF {
sc.reader.UnreadByte()
sc.reader.UnreadRune()
}
return ch
}
Expand Down