Skip to content

feat: Add parser skip feature #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2024
Merged
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
38 changes: 21 additions & 17 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

var (
sepRegex = regexp.MustCompile(`^\s*---+\s*$`)
sepRegex = regexp.MustCompile(`^\s*---+\s*$`)
strictSepRegex = regexp.MustCompile(`^---\n$`)
skipRegex = regexp.MustCompile(`^![-\w]+\s*$`)
)

func normalize(key string) string {
Expand Down Expand Up @@ -160,6 +162,8 @@ type context struct {
tool types.Tool
instructions []string
inBody bool
skipNode bool
seenParam bool
}

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

func commentEmbedded(line string) (string, bool) {
for _, i := range []string{"#", "# ", "//", "// "} {
prefix := i + "gptscript:"
cut, ok := strings.CutPrefix(line, prefix)
if ok {
return strings.TrimSpace(cut) + "\n", ok
}
}
return line, false
}

func Parse(input io.Reader) ([]types.Tool, error) {
scan := bufio.NewScanner(input)

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

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

if sepRegex.MatchString(line) {
if context.skipNode {
if strictSepRegex.MatchString(line) {
context.finish(&tools)
continue
}
} else if sepRegex.MatchString(line) {
context.finish(&tools)
continue
}

if context.skipNode {
continue
}

if !context.inBody {
// If the very first line is #! just skip because this is a unix interpreter declaration
if strings.HasPrefix(line, "#!") && lineNo == 1 {
Expand All @@ -218,6 +216,11 @@ func Parse(input io.Reader) ([]types.Tool, error) {
continue
}

if !context.seenParam && skipRegex.MatchString(line) {
context.skipNode = true
continue
}

// Blank line
if strings.TrimSpace(line) == "" {
continue
Expand All @@ -227,6 +230,7 @@ func Parse(input io.Reader) ([]types.Tool, error) {
if isParam, err := isParam(line, &context.tool); err != nil {
return nil, NewErrLine("", lineNo, err)
} else if isParam {
context.seenParam = true
continue
}
}
Expand Down
70 changes: 70 additions & 0 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package parser

import (
"strings"
"testing"

"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/hexops/autogold/v2"
"github.com/stretchr/testify/require"
)

func TestParse(t *testing.T) {
var input = `
first
---
name: second
---

!third

name: third
---
name: fourth
!forth dont skip
---
name: fifth

#!ignore
---
!skip
name: six

----
name: bad
---
name: bad
--
name: bad
---
name: bad
---
name: seven
`
out, err := Parse(strings.NewReader(input))
require.NoError(t, err)
autogold.Expect([]types.Tool{
{
Instructions: "first",
Source: types.ToolSource{LineNo: 1},
},
{
Parameters: types.Parameters{Name: "second"},
Source: types.ToolSource{LineNo: 4},
},
{
Parameters: types.Parameters{Name: "fourth"},
Instructions: "!forth dont skip",
Source: types.ToolSource{LineNo: 11},
},
{
Parameters: types.Parameters{Name: "fifth"},
Instructions: "#!ignore",
Source: types.ToolSource{LineNo: 14},
},
{
Parameters: types.Parameters{Name: "seven"},
Source: types.ToolSource{LineNo: 30},
},
}).Equal(t, out)
}