Skip to content

Commit 2558f65

Browse files
Merge pull request #244 from ibuildthecloud/skipnode
feat: Add parser skip feature
2 parents 9e8d144 + c0f2ffb commit 2558f65

File tree

2 files changed

+91
-17
lines changed

2 files changed

+91
-17
lines changed

pkg/parser/parser.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
)
1414

1515
var (
16-
sepRegex = regexp.MustCompile(`^\s*---+\s*$`)
16+
sepRegex = regexp.MustCompile(`^\s*---+\s*$`)
17+
strictSepRegex = regexp.MustCompile(`^---\n$`)
18+
skipRegex = regexp.MustCompile(`^![-\w]+\s*$`)
1719
)
1820

1921
func normalize(key string) string {
@@ -160,6 +162,8 @@ type context struct {
160162
tool types.Tool
161163
instructions []string
162164
inBody bool
165+
skipNode bool
166+
seenParam bool
163167
}
164168

165169
func (c *context) finish(tools *[]types.Tool) {
@@ -170,17 +174,6 @@ func (c *context) finish(tools *[]types.Tool) {
170174
*c = context{}
171175
}
172176

173-
func commentEmbedded(line string) (string, bool) {
174-
for _, i := range []string{"#", "# ", "//", "// "} {
175-
prefix := i + "gptscript:"
176-
cut, ok := strings.CutPrefix(line, prefix)
177-
if ok {
178-
return strings.TrimSpace(cut) + "\n", ok
179-
}
180-
}
181-
return line, false
182-
}
183-
184177
func Parse(input io.Reader) ([]types.Tool, error) {
185178
scan := bufio.NewScanner(input)
186179

@@ -197,16 +190,21 @@ func Parse(input io.Reader) ([]types.Tool, error) {
197190
}
198191

199192
line := scan.Text() + "\n"
200-
if embeddedLine, ok := commentEmbedded(line); ok {
201-
// Strip special comments to allow embedding the preamble in python or other interpreted languages
202-
line = embeddedLine
203-
}
204193

205-
if sepRegex.MatchString(line) {
194+
if context.skipNode {
195+
if strictSepRegex.MatchString(line) {
196+
context.finish(&tools)
197+
continue
198+
}
199+
} else if sepRegex.MatchString(line) {
206200
context.finish(&tools)
207201
continue
208202
}
209203

204+
if context.skipNode {
205+
continue
206+
}
207+
210208
if !context.inBody {
211209
// If the very first line is #! just skip because this is a unix interpreter declaration
212210
if strings.HasPrefix(line, "#!") && lineNo == 1 {
@@ -218,6 +216,11 @@ func Parse(input io.Reader) ([]types.Tool, error) {
218216
continue
219217
}
220218

219+
if !context.seenParam && skipRegex.MatchString(line) {
220+
context.skipNode = true
221+
continue
222+
}
223+
221224
// Blank line
222225
if strings.TrimSpace(line) == "" {
223226
continue
@@ -227,6 +230,7 @@ func Parse(input io.Reader) ([]types.Tool, error) {
227230
if isParam, err := isParam(line, &context.tool); err != nil {
228231
return nil, NewErrLine("", lineNo, err)
229232
} else if isParam {
233+
context.seenParam = true
230234
continue
231235
}
232236
}

pkg/parser/parser_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package parser
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/gptscript-ai/gptscript/pkg/types"
8+
"github.com/hexops/autogold/v2"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestParse(t *testing.T) {
13+
var input = `
14+
first
15+
---
16+
name: second
17+
---
18+
19+
!third
20+
21+
name: third
22+
---
23+
name: fourth
24+
!forth dont skip
25+
---
26+
name: fifth
27+
28+
#!ignore
29+
---
30+
!skip
31+
name: six
32+
33+
----
34+
name: bad
35+
---
36+
name: bad
37+
--
38+
name: bad
39+
---
40+
name: bad
41+
---
42+
name: seven
43+
`
44+
out, err := Parse(strings.NewReader(input))
45+
require.NoError(t, err)
46+
autogold.Expect([]types.Tool{
47+
{
48+
Instructions: "first",
49+
Source: types.ToolSource{LineNo: 1},
50+
},
51+
{
52+
Parameters: types.Parameters{Name: "second"},
53+
Source: types.ToolSource{LineNo: 4},
54+
},
55+
{
56+
Parameters: types.Parameters{Name: "fourth"},
57+
Instructions: "!forth dont skip",
58+
Source: types.ToolSource{LineNo: 11},
59+
},
60+
{
61+
Parameters: types.Parameters{Name: "fifth"},
62+
Instructions: "#!ignore",
63+
Source: types.ToolSource{LineNo: 14},
64+
},
65+
{
66+
Parameters: types.Parameters{Name: "seven"},
67+
Source: types.ToolSource{LineNo: 30},
68+
},
69+
}).Equal(t, out)
70+
}

0 commit comments

Comments
 (0)