Skip to content

Commit bf58593

Browse files
committed
Source => []rune
1 parent 19f69a8 commit bf58593

File tree

6 files changed

+16
-51
lines changed

6 files changed

+16
-51
lines changed

file/error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (e *Error) Error() string {
1717
return e.format()
1818
}
1919

20-
func (e *Error) Bind(source *Source) *Error {
20+
func (e *Error) Bind(source Source) *Error {
2121
if snippet, found := source.Snippet(e.Location.Line); found {
2222
snippet := strings.Replace(snippet, "\t", " ", -1)
2323
srcLine := "\n | " + snippet

file/source.go

+10-30
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,40 @@
11
package file
22

33
import (
4-
"encoding/json"
54
"strings"
65
"unicode/utf8"
76
)
87

9-
type Source struct {
10-
contents []rune
11-
}
12-
13-
func NewSource(contents string) *Source {
14-
return &Source{
15-
contents: []rune(contents),
16-
}
17-
}
18-
19-
func (s *Source) MarshalJSON() ([]byte, error) {
20-
return json.Marshal(s.contents)
21-
}
22-
23-
func (s *Source) UnmarshalJSON(b []byte) error {
24-
contents := make([]rune, 0)
25-
err := json.Unmarshal(b, &contents)
26-
if err != nil {
27-
return err
28-
}
8+
type Source []rune
299

30-
s.contents = contents
31-
return nil
10+
func NewSource(contents string) Source {
11+
return []rune(contents)
3212
}
3313

34-
func (s *Source) String() string {
35-
return string(s.contents)
14+
func (s Source) String() string {
15+
return string(s)
3616
}
3717

38-
func (s *Source) Snippet(line int) (string, bool) {
18+
func (s Source) Snippet(line int) (string, bool) {
3919
if s == nil {
4020
return "", false
4121
}
42-
lines := strings.Split(string(s.contents), "\n")
22+
lines := strings.Split(string(s), "\n")
4323
lineOffsets := make([]int, len(lines))
4424
var offset int
4525
for i, line := range lines {
4626
offset = offset + utf8.RuneCountInString(line) + 1
4727
lineOffsets[i] = offset
4828
}
4929
charStart, found := getLineOffset(lineOffsets, line)
50-
if !found || len(s.contents) == 0 {
30+
if !found || len(s) == 0 {
5131
return "", false
5232
}
5333
charEnd, found := getLineOffset(lineOffsets, line+1)
5434
if found {
55-
return string(s.contents[charStart : charEnd-1]), true
35+
return string(s[charStart : charEnd-1]), true
5636
}
57-
return string(s.contents[charStart:]), true
37+
return string(s[charStart:]), true
5838
}
5939

6040
func getLineOffset(lineOffsets []int, line int) (int, bool) {

file/source_test.go

-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package file
22

33
import (
4-
"encoding/json"
54
"testing"
6-
7-
"github.com/expr-lang/expr/internal/testify/assert"
85
)
96

107
const (
@@ -55,15 +52,3 @@ func TestStringSource_SnippetSingleLine(t *testing.T) {
5552
t.Errorf(unexpectedSnippet, t.Name(), str2, "")
5653
}
5754
}
58-
59-
func TestStringSource_MarshalJSON(t *testing.T) {
60-
source := NewSource("hello, world")
61-
encoded, err := json.Marshal(source)
62-
assert.NoError(t, err)
63-
assert.Equal(t, `[104,101,108,108,111,44,32,119,111,114,108,100]`, string(encoded))
64-
65-
decoded := &Source{}
66-
err = json.Unmarshal(encoded, decoded)
67-
assert.NoError(t, err)
68-
assert.Equal(t, source.String(), decoded.String())
69-
}

parser/lexer/lexer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/expr-lang/expr/file"
99
)
1010

11-
func Lex(source *file.Source) ([]Token, error) {
11+
func Lex(source file.Source) ([]Token, error) {
1212
l := &lexer{
1313
input: source.String(),
1414
tokens: make([]Token, 0),

parser/parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type parser struct {
5555

5656
type Tree struct {
5757
Node Node
58-
Source *file.Source
58+
Source file.Source
5959
}
6060

6161
func Parse(input string) (*Tree, error) {

vm/program.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Program struct {
2121
Arguments []int
2222
Constants []any
2323

24-
source *file.Source
24+
source file.Source
2525
node ast.Node
2626
locations []file.Location
2727
variables int
@@ -32,7 +32,7 @@ type Program struct {
3232

3333
// NewProgram returns a new Program. It's used by the compiler.
3434
func NewProgram(
35-
source *file.Source,
35+
source file.Source,
3636
node ast.Node,
3737
locations []file.Location,
3838
variables int,
@@ -58,7 +58,7 @@ func NewProgram(
5858
}
5959

6060
// Source returns origin file.Source.
61-
func (program *Program) Source() *file.Source {
61+
func (program *Program) Source() file.Source {
6262
return program.source
6363
}
6464

0 commit comments

Comments
 (0)