Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ type StringNode struct {
Value string // Value of the string.
}

// BytesNode represents a byte slice.
type BytesNode struct {
base
Value []byte // Value of the byte slice.
}

// ConstantNode represents a constant.
// Constants are predefined values like nil, true, false, array, map, etc.
// The parser.Parse will never generate ConstantNode, it is only generated
Expand Down
4 changes: 4 additions & 0 deletions ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (n *StringNode) String() string {
return fmt.Sprintf("%q", n.Value)
}

func (n *BytesNode) String() string {
return fmt.Sprintf("b%q", n.Value)
}

func (n *ConstantNode) String() string {
if n.Value == nil {
return "nil"
Expand Down
1 change: 1 addition & 0 deletions ast/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Walk(node *Node, v Visitor) {
case *FloatNode:
case *BoolNode:
case *StringNode:
case *BytesNode:
case *ConstantNode:
case *UnaryNode:
Walk(&n.Node, v)
Expand Down
3 changes: 3 additions & 0 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
mapType = reflect.TypeOf(map[string]any{})
timeType = reflect.TypeOf(time.Time{})
durationType = reflect.TypeOf(time.Duration(0))
byteSliceType = reflect.TypeOf([]byte(nil))

anyTypeSlice = []reflect.Type{anyType}
)
Expand Down Expand Up @@ -194,6 +195,8 @@ func (v *Checker) visit(node ast.Node) Nature {
nt = v.config.NtCache.FromType(boolType)
case *ast.StringNode:
nt = v.config.NtCache.FromType(stringType)
case *ast.BytesNode:
nt = v.config.NtCache.FromType(byteSliceType)
case *ast.ConstantNode:
nt = v.config.NtCache.FromType(reflect.TypeOf(n.Value))
case *ast.UnaryNode:
Expand Down
6 changes: 6 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ func (c *compiler) compile(node ast.Node) {
c.BoolNode(n)
case *ast.StringNode:
c.StringNode(n)
case *ast.BytesNode:
c.BytesNode(n)
case *ast.ConstantNode:
c.ConstantNode(n)
case *ast.UnaryNode:
Expand Down Expand Up @@ -410,6 +412,10 @@ func (c *compiler) StringNode(node *ast.StringNode) {
c.emitPush(node.Value)
}

func (c *compiler) BytesNode(node *ast.BytesNode) {
c.emitPush(node.Value)
}

func (c *compiler) ConstantNode(node *ast.ConstantNode) {
if node.Value == nil {
c.emit(OpNil)
Expand Down
38 changes: 38 additions & 0 deletions docs/language-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
<code>nil</code>
</td>
</tr>
<tr>
<td><strong>Bytes</strong></td>
<td>
<code>b"hello"</code>, <code>b'\xff\x00'</code>
</td>
</tr>
</table>

### Strings
Expand All @@ -73,6 +79,38 @@ World`

Backticks strings are raw strings, they do not support escape sequences.

### Bytes

Bytes literals are represented by string literals preceded by a `b` or `B` character.
The bytes literal returns a `[]byte` value.

```expr
b"abc" // []byte{97, 98, 99}
```

Non-ASCII characters are UTF-8 encoded:

```expr
b"ÿ" // []byte{195, 191} - UTF-8 encoding of ÿ
```

Bytes literals support escape sequences for specifying arbitrary byte values:

- `\xNN` - hexadecimal escape (2 hex digits, value 0-255)
- `\NNN` - octal escape (3 octal digits, value 0-377)
- `\n`, `\t`, `\r`, etc. - standard escape sequences

```expr
b"\xff" // []byte{255}
b"\x00\x01" // []byte{0, 1}
b"\101" // []byte{65} - octal for 'A'
```

:::note
Unlike string literals, bytes literals do not support `\u` or `\U` Unicode escapes.
Use `\x` escapes for arbitrary byte values.
:::

## Operators

<table>
Expand Down
70 changes: 70 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func ExampleCompile() {
// Output: true
}

func ExampleEval_bytes_literal() {
// Bytes literal returns []byte.
output, err := expr.Eval(`b"abc"`, nil)
if err != nil {
fmt.Printf("%v", err)
return
}

fmt.Printf("%v", output)

// Output: [97 98 99]
}

func TestDisableIfOperator_AllowsIfFunction(t *testing.T) {
env := map[string]any{
"if": func(x int) int { return x + 1 },
Expand Down Expand Up @@ -2929,3 +2942,60 @@ func TestDisableShortCircuit(t *testing.T) {
assert.Equal(t, 3, count)
assert.True(t, got.(bool))
}

func TestBytesLiteral(t *testing.T) {
tests := []struct {
code string
want []byte
}{
{`b"hello"`, []byte("hello")},
{`b'world'`, []byte("world")},
{`b""`, []byte{}},
{`b'\x00\xff'`, []byte{0, 255}},
{`b"\x41\x42\x43"`, []byte("ABC")},
{`b'\101\102\103'`, []byte("ABC")},
{`b'\n\t\r'`, []byte{'\n', '\t', '\r'}},
{`b'hello\x00world'`, []byte("hello\x00world")},
{`b"ÿ"`, []byte{0xc3, 0xbf}}, // UTF-8 encoding of ÿ
}

for _, tt := range tests {
t.Run(tt.code, func(t *testing.T) {
program, err := expr.Compile(tt.code)
require.NoError(t, err)

output, err := expr.Run(program, nil)
require.NoError(t, err)
assert.Equal(t, tt.want, output)
})
}
}

func TestBytesLiteral_type(t *testing.T) {
env := map[string]any{
"data": []byte("test"),
}

// Verify bytes literal has []byte type and can be compared with []byte
program, err := expr.Compile(`data == b"test"`, expr.Env(env))
require.NoError(t, err)

output, err := expr.Run(program, env)
require.NoError(t, err)
assert.Equal(t, true, output)
}

func TestBytesLiteral_errors(t *testing.T) {
// \u and \U escapes should not be allowed in bytes literals
errorCases := []string{
`b'\u0041'`,
`b"\U00000041"`,
}

for _, code := range errorCases {
t.Run(code, func(t *testing.T) {
_, err := expr.Compile(code)
require.Error(t, err)
})
}
}
56 changes: 56 additions & 0 deletions parser/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,52 @@ func TestLex(t *testing.T) {
{Kind: EOF},
},
},
{
`b"hello" b'world'`,
[]Token{
{Kind: Bytes, Value: "hello"},
{Kind: Bytes, Value: "world"},
{Kind: EOF},
},
},
{
`b"\x00\xff" b'\x41\x42\x43'`,
[]Token{
{Kind: Bytes, Value: "\x00\xff"},
{Kind: Bytes, Value: "ABC"},
{Kind: EOF},
},
},
{
`b"\101\102\103" b'\n\t\r'`,
[]Token{
{Kind: Bytes, Value: "ABC"},
{Kind: Bytes, Value: "\n\t\r"},
{Kind: EOF},
},
},
{
`b""`,
[]Token{
{Kind: Bytes, Value: ""},
{Kind: EOF},
},
},
{
`B"hello" B'world'`,
[]Token{
{Kind: Bytes, Value: "hello"},
{Kind: Bytes, Value: "world"},
{Kind: EOF},
},
},
{
`b"ÿ"`,
[]Token{
{Kind: Bytes, Value: "\xc3\xbf"},
{Kind: EOF},
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -380,6 +426,16 @@ früh ♥︎
unrecognized character: U+2665 '♥' (1:6)
| früh ♥︎
| .....^

b"\u0041"
unable to unescape string (1:9)
| b"\u0041"
| ........^

b'\U00000041'
unable to unescape string (1:13)
| b'\U00000041'
| ............^
`

func TestLex_error(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions parser/lexer/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func root(l *Lexer) stateFn {
l.emitValue(String, str)
case r == '`':
l.scanRawString(r)
case (r == 'b' || r == 'B') && (l.peek() == '\'' || l.peek() == '"'):
quote := l.next()
l.scanString(quote)
str, err := unescapeBytes(l.word()[1:]) // skip 'b'
if err != nil {
l.error("%v", err)
}
l.emitValue(Bytes, str)
case '0' <= r && r <= '9':
l.backup()
return number
Expand Down
1 change: 1 addition & 0 deletions parser/lexer/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
Identifier Kind = "Identifier"
Number Kind = "Number"
String Kind = "String"
Bytes Kind = "Bytes"
Operator Kind = "Operator"
Bracket Kind = "Bracket"
EOF Kind = "EOF"
Expand Down
Loading
Loading