@@ -13,7 +13,9 @@ import (
13
13
)
14
14
15
15
var (
16
- sepRegex = regexp .MustCompile (`^\s*---+\s*$` )
16
+ sepRegex = regexp .MustCompile (`^\s*---+\s*$` )
17
+ strictSepRegex = regexp .MustCompile (`^---\n$` )
18
+ skipRegex = regexp .MustCompile (`^![-\w]+\s*$` )
17
19
)
18
20
19
21
func normalize (key string ) string {
@@ -160,6 +162,8 @@ type context struct {
160
162
tool types.Tool
161
163
instructions []string
162
164
inBody bool
165
+ skipNode bool
166
+ seenParam bool
163
167
}
164
168
165
169
func (c * context ) finish (tools * []types.Tool ) {
@@ -170,17 +174,6 @@ func (c *context) finish(tools *[]types.Tool) {
170
174
* c = context {}
171
175
}
172
176
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
-
184
177
func Parse (input io.Reader ) ([]types.Tool , error ) {
185
178
scan := bufio .NewScanner (input )
186
179
@@ -197,16 +190,21 @@ func Parse(input io.Reader) ([]types.Tool, error) {
197
190
}
198
191
199
192
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
- }
204
193
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 ) {
206
200
context .finish (& tools )
207
201
continue
208
202
}
209
203
204
+ if context .skipNode {
205
+ continue
206
+ }
207
+
210
208
if ! context .inBody {
211
209
// If the very first line is #! just skip because this is a unix interpreter declaration
212
210
if strings .HasPrefix (line , "#!" ) && lineNo == 1 {
@@ -218,6 +216,11 @@ func Parse(input io.Reader) ([]types.Tool, error) {
218
216
continue
219
217
}
220
218
219
+ if ! context .seenParam && skipRegex .MatchString (line ) {
220
+ context .skipNode = true
221
+ continue
222
+ }
223
+
221
224
// Blank line
222
225
if strings .TrimSpace (line ) == "" {
223
226
continue
@@ -227,6 +230,7 @@ func Parse(input io.Reader) ([]types.Tool, error) {
227
230
if isParam , err := isParam (line , & context .tool ); err != nil {
228
231
return nil , NewErrLine ("" , lineNo , err )
229
232
} else if isParam {
233
+ context .seenParam = true
230
234
continue
231
235
}
232
236
}
0 commit comments