Skip to content

Commit 4f52ef3

Browse files
Add a Builtin AST node
1 parent 7dce663 commit 4f52ef3

File tree

5 files changed

+73
-15
lines changed

5 files changed

+73
-15
lines changed

internal/syntax/ast/ast_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ func TestNode(t *testing.T) {
5656
end: token.Token{Kind: token.Ident, Start: 0, End: 4},
5757
kind: ast.KindIdent,
5858
},
59+
{
60+
name: "builtin",
61+
node: ast.Ident{
62+
Name: "test",
63+
Token: token.Token{Kind: token.Ident, Start: 0, End: 4},
64+
Type: ast.KindBuiltin,
65+
},
66+
start: token.Token{Kind: token.Ident, Start: 0, End: 4},
67+
end: token.Token{Kind: token.Ident, Start: 0, End: 4},
68+
kind: ast.KindBuiltin,
69+
},
5970
{
6071
name: "var statement",
6172
// @variable = sometext

internal/syntax/ast/expression.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,44 @@ func (i Ident) Kind() Kind {
4040
// statementNode marks an [Ident] as an [ast.Expression].
4141
func (i Ident) expressionNode() {}
4242

43+
// Builtin is a built in identifier, prefixed with a '$'.
44+
//
45+
// E.g. 'uuid' is a perfectly fine identifier for any user to use for a variable
46+
// but '$uuid' is a builtin that returns a random UUIDv4.
47+
//
48+
// The Builtin AST node is functionality identical to an [Ident], but must
49+
// be separate to allow differentiating builtins from regular idents.
50+
type Builtin struct {
51+
// Name is the ident's name.
52+
Name string `yaml:"name"`
53+
54+
// The [token.Ident] token.
55+
Token token.Token `yaml:"token"`
56+
57+
// Type is the kind of ast node, in this case [KindBuiltin].
58+
Type Kind `yaml:"type"`
59+
}
60+
61+
// Start returns the first meaningful token in the Builtin, which is
62+
// the [token.Ident].
63+
func (b Builtin) Start() token.Token {
64+
return b.Token
65+
}
66+
67+
// End returns the last token in the Builtin, which is also
68+
// the [token.Ident].
69+
func (b Builtin) End() token.Token {
70+
return b.Token
71+
}
72+
73+
// Kind returns [KindBuiltin].
74+
func (b Builtin) Kind() Kind {
75+
return b.Type
76+
}
77+
78+
// statementNode marks a [Builtin] as an [ast.Expression].
79+
func (b Builtin) expressionNode() {}
80+
4381
// TextLiteral is a literal text expression.
4482
type TextLiteral struct {
4583
// The text value (unquoted)

internal/syntax/ast/kind.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const (
1111
KindFile // File
1212
KindVarStatement // VarStatement
1313
KindIdent // Ident
14+
KindBuiltin // Builtin
1415
KindTextLiteral // TextLiteral
1516
KindInterp // Interp
1617
KindPrompt // Prompt

internal/syntax/ast/kind_string.go

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/zap/zap_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
"go.followtheprocess.codes/zap/internal/zap"
1616
)
1717

18+
// TODO(@FollowTheProcess): This is getting unwieldy and is a bit beyond what testscript is designed for
19+
//
20+
// Once I have builtins and selector expressions working, the http files will be able to
21+
// do e.g. '@base = {{ $env.TEST_SERVER_URL }}' and then I can host a local httpbin in
22+
// a testcontainer or even just create a simple inline server for the tests and it'll
23+
// be much nicer! :)
24+
1825
var update = flag.Bool("update", false, "Update testscript snapshots")
1926

2027
func TestMain(m *testing.M) {

0 commit comments

Comments
 (0)