Skip to content

Commit 830f1f1

Browse files
committed
Added initial commit for the tokenizer
1 parent ba547d3 commit 830f1f1

File tree

11 files changed

+1121
-0
lines changed

11 files changed

+1121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# go-tokenizer
2+
23
General Tokenizer and Abstract Syntax Tree Generator

doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
The `tokenizer` package implements a generic expression scanner for tokens
3+
*/
4+
package tokenizer

errors.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package tokenizer
2+
3+
import "fmt"
4+
5+
///////////////////////////////////////////////////////////////////////////////
6+
// TYPES
7+
8+
type Error uint
9+
10+
///////////////////////////////////////////////////////////////////////////////
11+
// GLOBALS
12+
13+
const (
14+
ErrSuccess Error = iota
15+
ErrBadParameter
16+
ErrDuplicateEntry
17+
ErrUnexpectedResponse
18+
ErrNotFound
19+
ErrNotModified
20+
ErrInternalAppError
21+
ErrNotImplemented
22+
)
23+
24+
///////////////////////////////////////////////////////////////////////////////
25+
// STRINGIFY
26+
27+
func (e Error) Error() string {
28+
switch e {
29+
case ErrSuccess:
30+
return "ErrSuccess"
31+
case ErrBadParameter:
32+
return "ErrBadParameter"
33+
case ErrDuplicateEntry:
34+
return "ErrDuplicateEntry"
35+
case ErrUnexpectedResponse:
36+
return "ErrUnexpectedResponse"
37+
case ErrNotFound:
38+
return "ErrNotFound"
39+
case ErrNotModified:
40+
return "ErrNotModified"
41+
case ErrInternalAppError:
42+
return "ErrInternalAppError"
43+
case ErrNotImplemented:
44+
return "ErrNotImplemented"
45+
default:
46+
return "[?? Invalid Error value]"
47+
}
48+
}
49+
50+
func (e Error) With(args ...any) error {
51+
return fmt.Errorf("%w: %s", e, fmt.Sprint(args...))
52+
}
53+
54+
func (e Error) Withf(format string, args ...any) error {
55+
return fmt.Errorf("%w: %s", e, fmt.Sprintf(format, args...))
56+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/mutablelogic/go-tokenizer
2+
3+
go 1.25.4

pos.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package tokenizer
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
///////////////////////////////////////////////////////////////////////////////
8+
// TYPES
9+
10+
// Pos marks the position within a context, like a file path. If path is
11+
// nil then Pos is not used. Note that Line and Col are zero-indexed, so
12+
// 1 needs to be added to them when printing.
13+
type Pos struct {
14+
Path *string
15+
Line uint
16+
Col uint
17+
18+
// Record previous position for unread
19+
x, y uint
20+
}
21+
22+
// Error with a position
23+
type PosError struct {
24+
Err error
25+
Pos Pos
26+
}
27+
28+
///////////////////////////////////////////////////////////////////////////////
29+
// LIFECYCLE
30+
31+
func NewPosError(err error, pos Pos) error {
32+
return &PosError{Err: err, Pos: pos}
33+
}
34+
35+
func NewParseError(t *Token) error {
36+
return &PosError{Err: ErrBadParameter.Withf("Parse error near %q", t.toString()), Pos: t.Pos}
37+
}
38+
39+
///////////////////////////////////////////////////////////////////////////////
40+
// STRINGIFY
41+
42+
func (p *Pos) String() string {
43+
if p.Path == nil || *p.Path == "" {
44+
return fmt.Sprintf("pos<%d:%d>", p.Line+1, p.Col+1)
45+
} else {
46+
return fmt.Sprintf("pos<%s:%d:%d>", *p.Path, p.Line+1, p.Col+1)
47+
}
48+
}
49+
50+
func (e *PosError) Error() string {
51+
if e.Pos.Path == nil || *e.Pos.Path == "" {
52+
return fmt.Errorf("%d:%d: %w", e.Pos.Line+1, e.Pos.Col+1, e.Err).Error()
53+
} else {
54+
return fmt.Errorf("%s:%d:%d: %w", *e.Pos.Path, e.Pos.Line+1, e.Pos.Col+1, e.Err).Error()
55+
}
56+
}

pos_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tokenizer_test
2+
3+
import (
4+
"testing"
5+
6+
// Packages
7+
tokenizer "github.com/mutablelogic/go-tokenizer"
8+
assert "github.com/stretchr/testify/assert"
9+
)
10+
11+
func Test_pos_001(t *testing.T) {
12+
assert := assert.New(t)
13+
pos := tokenizer.Pos{}
14+
assert.Equal("pos<1:1>", pos.String())
15+
}
16+
17+
func Test_pos_002(t *testing.T) {
18+
assert := assert.New(t)
19+
path := "test.hcl"
20+
pos := tokenizer.Pos{Path: &path}
21+
assert.Equal("pos<test.hcl:1:1>", pos.String())
22+
}

0 commit comments

Comments
 (0)