From cf525658322829dd2cc5c3d4c5ba7279c067c327 Mon Sep 17 00:00:00 2001 From: zhouzhihong Date: Mon, 29 Aug 2022 16:02:15 +0800 Subject: [PATCH] Add line and column calculation. --- README.md | 2 +- lexer.go | 11 ++++------- lexer_test.go | 1 + 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a16d966..df603ef 100644 --- a/README.md +++ b/README.md @@ -163,4 +163,4 @@ for tok, err, eof := s.Next(); !eof; tok, err, eof = s.Next() { } ``` -// TODO 优化位置标记计算 \ No newline at end of file +//TODO 错误信息的行列计算 \ No newline at end of file diff --git a/lexer.go b/lexer.go index 1d9686b..c8d39e7 100644 --- a/lexer.go +++ b/lexer.go @@ -3,6 +3,7 @@ package lexmachine import ( "bytes" "fmt" + "reflect" "unicode/utf8" dfapkg "gitea.xintech.co/zhouzhihong/lexmachine/dfa" @@ -54,7 +55,7 @@ func (t *Token) Equals(other *Token) bool { // String formats the token in a human readable form. func (t *Token) String() string { return fmt.Sprintf( - "%d %q %d (%d, %d)-(%d, %d) (%d, %d)-(%d, %d)", + "%d %q %d (%d, %d)-(%d, %d)", t.Type, t.Value, t.TC, @@ -62,10 +63,6 @@ func (t *Token) String() string { t.StartColumn, t.EndLine, t.EndColumn, - t.TSLine, - t.TSColumn, - t.TELine, - t.TEColumn, ) } @@ -180,14 +177,14 @@ func (s *Scanner) Next() (tok interface{}, err error, eos bool) { col++ } - if (i == match.TC) && token != nil { + if (i == match.TC) && reflect.TypeOf(token) == reflect.TypeOf(&Token{}) { token.(*Token).StartLine = line token.(*Token).StartColumn = col } i += sz } - if token != nil { + if reflect.TypeOf(token) == reflect.TypeOf(&Token{}) { token.(*Token).EndLine = line token.(*Token).EndColumn = col } diff --git a/lexer_test.go b/lexer_test.go index 26c09c2..73c33af 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -291,6 +291,7 @@ func TestRegression(t *testing.T) { fmt.Printf("Token: %v\n", tok) found++ } + if found != test.tokens { t.Errorf("Expected exactly %v tokens got %v, ===\nErr: %v\nEOS: %v\nTC: %d\n", test.tokens, found, err, eos, scanner.TC) }