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 (
"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
}

View File

@ -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)
}