Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
orangecat
ollama
Commits
ed89da92
"vscode:/vscode.git/clone" did not exist on "93df388449ce61fb9b9721fc73b90802b8b2f379"
Commit
ed89da92
authored
Jul 24, 2023
by
Mohit Gaur
Browse files
Improve command parsing and multiline string handling
parent
a3297fed
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
11 deletions
+12
-11
parser/parser.go
parser/parser.go
+12
-11
No files found.
parser/parser.go
View file @
ed89da92
...
@@ -2,12 +2,14 @@ package parser
...
@@ -2,12 +2,14 @@ package parser
import
(
import
(
"bufio"
"bufio"
"bytes"
"errors"
"errors"
"fmt"
"fmt"
"io"
"io"
"strings"
)
)
const
multilineString
=
`"""`
type
Command
struct
{
type
Command
struct
{
Name
string
Name
string
Args
string
Args
string
...
@@ -20,7 +22,6 @@ func (c *Command) Reset() {
...
@@ -20,7 +22,6 @@ func (c *Command) Reset() {
func
Parse
(
reader
io
.
Reader
)
([]
Command
,
error
)
{
func
Parse
(
reader
io
.
Reader
)
([]
Command
,
error
)
{
var
commands
[]
Command
var
commands
[]
Command
var
command
,
modelCommand
Command
var
command
,
modelCommand
Command
scanner
:=
bufio
.
NewScanner
(
reader
)
scanner
:=
bufio
.
NewScanner
(
reader
)
...
@@ -33,21 +34,21 @@ func Parse(reader io.Reader) ([]Command, error) {
...
@@ -33,21 +34,21 @@ func Parse(reader io.Reader) ([]Command, error) {
continue
continue
}
}
switch
string
(
byte
s
.
ToUpper
(
fields
[
0
]))
{
switch
strings
.
ToUpper
(
string
(
fields
[
0
]))
{
case
"FROM"
:
case
"FROM"
:
command
.
Name
=
"model"
command
.
Name
=
"model"
command
.
Args
=
string
(
fields
[
1
])
command
.
Args
=
string
(
fields
[
1
])
// copy command for validation
// copy command for validation
modelCommand
=
command
modelCommand
=
command
case
"LICENSE"
,
"TEMPLATE"
,
"SYSTEM"
,
"PROMPT"
:
case
"LICENSE"
,
"TEMPLATE"
,
"SYSTEM"
,
"PROMPT"
:
command
.
Name
=
string
(
byte
s
.
ToLower
(
fields
[
0
]))
command
.
Name
=
strings
.
ToLower
(
string
(
fields
[
0
]))
command
.
Args
=
string
(
fields
[
1
])
command
.
Args
=
string
(
fields
[
1
])
case
"PARAMETER"
:
case
"PARAMETER"
:
fields
=
bytes
.
SplitN
(
fields
[
1
],
[]
byte
(
" "
),
2
)
fields
=
bytes
.
SplitN
(
fields
[
1
],
[]
byte
(
" "
),
2
)
command
.
Name
=
string
(
fields
[
0
])
command
.
Name
=
string
(
fields
[
0
])
command
.
Args
=
string
(
fields
[
1
])
command
.
Args
=
string
(
fields
[
1
])
default
:
default
:
continue
return
nil
,
fmt
.
Errorf
(
"unknown command: %s"
,
fields
[
0
])
}
}
commands
=
append
(
commands
,
command
)
commands
=
append
(
commands
,
command
)
...
@@ -55,7 +56,7 @@ func Parse(reader io.Reader) ([]Command, error) {
...
@@ -55,7 +56,7 @@ func Parse(reader io.Reader) ([]Command, error) {
}
}
if
modelCommand
.
Args
==
""
{
if
modelCommand
.
Args
==
""
{
return
nil
,
fmt
.
Errorf
(
"no FROM line for the model was specified"
)
return
nil
,
errors
.
New
(
"no FROM line for the model was specified"
)
}
}
return
commands
,
scanner
.
Err
()
return
commands
,
scanner
.
Err
()
...
@@ -64,18 +65,18 @@ func Parse(reader io.Reader) ([]Command, error) {
...
@@ -64,18 +65,18 @@ func Parse(reader io.Reader) ([]Command, error) {
func
scanModelfile
(
data
[]
byte
,
atEOF
bool
)
(
advance
int
,
token
[]
byte
,
err
error
)
{
func
scanModelfile
(
data
[]
byte
,
atEOF
bool
)
(
advance
int
,
token
[]
byte
,
err
error
)
{
newline
:=
bytes
.
IndexByte
(
data
,
'\n'
)
newline
:=
bytes
.
IndexByte
(
data
,
'\n'
)
if
start
:=
bytes
.
Index
(
data
,
[]
byte
(
`"""`
));
start
>=
0
&&
start
<
newline
{
if
start
:=
bytes
.
Index
(
data
,
[]
byte
(
multilineString
));
start
>=
0
&&
start
<
newline
{
end
:=
bytes
.
Index
(
data
[
start
+
3
:
],
[]
byte
(
`"""`
))
end
:=
bytes
.
Index
(
data
[
start
+
len
(
multilineString
)
:
],
[]
byte
(
multilineString
))
if
end
<
0
{
if
end
<
0
{
if
atEOF
{
if
atEOF
{
return
0
,
nil
,
errors
.
New
(
`
unterminated multiline string: "
""`
)
return
0
,
nil
,
errors
.
New
(
"
unterminated multiline string: "
+
multilineString
)
}
else
{
}
else
{
return
0
,
nil
,
nil
return
0
,
nil
,
nil
}
}
}
}
n
:=
start
+
3
+
end
+
3
n
:=
start
+
len
(
multilineString
)
+
end
+
len
(
multilineString
)
return
n
,
bytes
.
Replace
(
data
[
:
n
],
[]
byte
(
`"""`
),
[]
byte
(
""
),
2
)
,
nil
return
n
,
data
[
:
n
]
,
nil
}
}
return
bufio
.
ScanLines
(
data
,
atEOF
)
return
bufio
.
ScanLines
(
data
,
atEOF
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment