Skip to content

Commit 70c31ac

Browse files
committed
Lexer first commit
1 parent 3c6ea97 commit 70c31ac

File tree

18 files changed

+487
-144
lines changed

18 files changed

+487
-144
lines changed

ast/location.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ast
22

33
import (
4-
"github.com/antonmedv/expr/internal/file"
4+
"github.com/antonmedv/expr/file"
55
)
66

77
func (n *NilNode) SetLocation(l file.Location) {

ast/node.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package ast
22

33
import (
4+
"github.com/antonmedv/expr/file"
45
"reflect"
56
"regexp"
6-
7-
"github.com/antonmedv/expr/internal/file"
87
)
98

109
// Node represents items of abstract syntax tree.

checker/checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"reflect"
66

77
"github.com/antonmedv/expr/ast"
8+
"github.com/antonmedv/expr/file"
89
"github.com/antonmedv/expr/internal/conf"
9-
"github.com/antonmedv/expr/internal/file"
1010
"github.com/antonmedv/expr/parser"
1111
)
1212

compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package compiler
33
import (
44
"encoding/binary"
55
"fmt"
6+
"github.com/antonmedv/expr/file"
67
"math"
78
"reflect"
89

910
"github.com/antonmedv/expr/ast"
1011
"github.com/antonmedv/expr/internal/conf"
11-
"github.com/antonmedv/expr/internal/file"
1212
"github.com/antonmedv/expr/parser"
1313
. "github.com/antonmedv/expr/vm"
1414
)

internal/file/error.go renamed to file/error.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import (
88
"golang.org/x/text/width"
99
)
1010

11-
// Error type which references a location within source and a message.
1211
type Error struct {
13-
Location Location
14-
Message string
12+
Location
13+
Message string
1514
}
1615

1716
const (
@@ -24,9 +23,13 @@ var (
2423
wideInd = width.Widen.String(ind)
2524
)
2625

27-
func (e *Error) Format(source *Source) string {
26+
func (e *Error) Error() string {
27+
return e.Message
28+
}
29+
30+
func (e *Error) Format(source *Source) error {
2831
if e.Location.Empty() {
29-
return e.Message
32+
return fmt.Errorf(e.Message)
3033
}
3134
var result = fmt.Sprintf(
3235
"%s (%d:%d)",
@@ -55,5 +58,5 @@ func (e *Error) Format(source *Source) string {
5558
}
5659
result += srcLine + indLine
5760
}
58-
return result
61+
return fmt.Errorf(result)
5962
}
File renamed without changes.

internal/file/source.go renamed to file/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (s *Source) updateOffsets() {
6868
func (s *Source) findLineOffset(line int) (int32, bool) {
6969
if line == 1 {
7070
return 0, true
71-
} else if line > 1 && line <= int(len(s.lineOffsets)) {
71+
} else if line > 1 && line <= len(s.lineOffsets) {
7272
offset := s.lineOffsets[line-2]
7373
return offset, true
7474
}

internal/file/source_test.go renamed to file/source_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import (
55
)
66

77
const (
8-
unexpectedValue = "%s got snippet '%v', want '%v'"
98
unexpectedSnippet = "%s got snippet '%s', want '%v'"
109
snippetNotFound = "%s snippet not found, wanted '%v'"
1110
snippetFound = "%s snippet found at Line %d, wanted none"
1211
)
1312

14-
// TestStringSource_SnippetMultiline snippets of text from a multiline source.
15-
func TestStringSource_SnippetMultiline(t *testing.T) {
13+
func TestStringSource_SnippetMultiLine(t *testing.T) {
1614
source := NewSource("hello\nworld\nmy\nbub\n")
1715
if str, found := source.Snippet(1); !found {
1816
t.Errorf(snippetNotFound, t.Name(), 1)
@@ -41,8 +39,7 @@ func TestStringSource_SnippetMultiline(t *testing.T) {
4139
}
4240
}
4341

44-
// TestStringSource_SnippetSingleline snippets from a single line source.
45-
func TestStringSource_SnippetSingleline(t *testing.T) {
42+
func TestStringSource_SnippetSingleLine(t *testing.T) {
4643
source := NewSource("hello, world")
4744
if str, found := source.Snippet(1); !found {
4845
t.Errorf(snippetNotFound, t.Name(), 1)

internal/file/errors.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

internal/file/errors_test.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)