Add line and column calculation.

This commit is contained in:
zhouzhihong 2022-08-29 16:02:15 +08:00
parent 62b949dd83
commit cf52565832
3 changed files with 6 additions and 8 deletions

View File

@ -163,4 +163,4 @@ for tok, err, eof := s.Next(); !eof; tok, err, eof = s.Next() {
} }
``` ```
// TODO 优化位置标记计算 //TODO 错误信息的行列计算

View File

@ -3,6 +3,7 @@ package lexmachine
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"reflect"
"unicode/utf8" "unicode/utf8"
dfapkg "gitea.xintech.co/zhouzhihong/lexmachine/dfa" 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. // String formats the token in a human readable form.
func (t *Token) String() string { func (t *Token) String() string {
return fmt.Sprintf( return fmt.Sprintf(
"%d %q %d (%d, %d)-(%d, %d) (%d, %d)-(%d, %d)", "%d %q %d (%d, %d)-(%d, %d)",
t.Type, t.Type,
t.Value, t.Value,
t.TC, t.TC,
@ -62,10 +63,6 @@ func (t *Token) String() string {
t.StartColumn, t.StartColumn,
t.EndLine, t.EndLine,
t.EndColumn, 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++ col++
} }
if (i == match.TC) && token != nil { if (i == match.TC) && reflect.TypeOf(token) == reflect.TypeOf(&Token{}) {
token.(*Token).StartLine = line token.(*Token).StartLine = line
token.(*Token).StartColumn = col token.(*Token).StartColumn = col
} }
i += sz i += sz
} }
if token != nil { if reflect.TypeOf(token) == reflect.TypeOf(&Token{}) {
token.(*Token).EndLine = line token.(*Token).EndLine = line
token.(*Token).EndColumn = col token.(*Token).EndColumn = col
} }

View File

@ -291,6 +291,7 @@ func TestRegression(t *testing.T) {
fmt.Printf("Token: %v\n", tok) fmt.Printf("Token: %v\n", tok)
found++ found++
} }
if found != test.tokens { 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) t.Errorf("Expected exactly %v tokens got %v, ===\nErr: %v\nEOS: %v\nTC: %d\n", test.tokens, found, err, eos, scanner.TC)
} }