generate: Add comments to lexer
This commit is contained in:
@@ -15,6 +15,7 @@ const (
|
||||
TokenRBrace
|
||||
TokenLBracket
|
||||
TokenRBracket
|
||||
TokenComment
|
||||
)
|
||||
|
||||
var tokenNames = map[parse.TokenKind] string {
|
||||
@@ -26,6 +27,7 @@ var tokenNames = map[parse.TokenKind] string {
|
||||
TokenRBrace: "RBrace",
|
||||
TokenLBracket: "LBracket",
|
||||
TokenRBracket: "RBracket",
|
||||
TokenComment: "Comment",
|
||||
}
|
||||
|
||||
func Lex(fileName string, reader io.Reader) (parse.Lexer, error) {
|
||||
@@ -81,6 +83,18 @@ func (this *lexer) nextInternal() (token parse.Token, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
unexpected := func() error {
|
||||
if unicode.IsPrint(this.rune) {
|
||||
return parse.Errorf (
|
||||
this.pos(), "unexpected rune '%c'",
|
||||
this.rune)
|
||||
} else {
|
||||
return parse.Errorf (
|
||||
this.pos(), "unexpected rune %U",
|
||||
this.rune)
|
||||
}
|
||||
}
|
||||
|
||||
defer func () {
|
||||
newPos := this.pos()
|
||||
newPos.End -- // TODO figure out why tf we have to do this
|
||||
@@ -133,14 +147,21 @@ func (this *lexer) nextInternal() (token parse.Token, err error) {
|
||||
token.Kind = TokenRBracket
|
||||
appendRune()
|
||||
if this.eof { err = nil; return }
|
||||
case unicode.IsPrint(this.rune):
|
||||
err = parse.Errorf (
|
||||
this.pos(), "unexpected rune '%c'",
|
||||
this.rune)
|
||||
// Comment
|
||||
case this.rune == '/':
|
||||
token.Kind = TokenComment
|
||||
appendRune()
|
||||
if this.eof { return }
|
||||
if this.rune != '/' {
|
||||
err = unexpected()
|
||||
return
|
||||
}
|
||||
for this.rune != '\n' {
|
||||
appendRune()
|
||||
if this.eof { err = nil; return }
|
||||
}
|
||||
default:
|
||||
err = parse.Errorf (
|
||||
this.pos(), "unexpected rune %U",
|
||||
this.rune)
|
||||
err = unexpected()
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user