shorthand
shorthand copied to clipboard
Parsing fails when URL is used
Hi! :-)
I recently found a bug when using a URL within the values.
Example: go run cmd/j/main.go --verbose scheme: http, url: http://test.de:4242
This fails with:
Input: scheme: http, url: http://test.de:4242
Setting key scheme
Detected object, wrapping in { and }
Setting key
Parsing sub-object
Setting key scheme
Parse value: http
Setting key url
Parse value: http:
Sub-object done
Expected '}' but found { at line 1 col 41
{scheme: http, url: http://test.de:4242}
........................................
exit status 1
The problem seems to be the removement of comments. In my case // is no comment.
I tried to understand your code and created a patch, that currently works for me, but I am sure you would create a better patch.
I am also no GO expert. :-)
diff --git a/parse.go b/parse.go
index 1cd2c71..6d6ee7b 100644
--- a/parse.go
+++ b/parse.go
@@ -110,6 +110,16 @@ func (d *Document) back() {
d.pos -= d.lastWidth
}
+// before returns the previous rune without moving the position backward.
+func (d *Document) before() rune {
+ if d.pos < 2 {
+ return -1
+ }
+
+ return rune(d.expression[d.pos-2])
+}
+
+
// peek returns the next rune without moving the position forward.
func (d *Document) peek() rune {
if d.pos >= uint(len(d.expression)) {
@@ -156,6 +166,10 @@ func (d *Document) skipWhitespace() {
func (d *Document) skipComments(r rune) bool {
if r == '/' && d.peek() == '/' {
+ if d.before() == ':'{
+ d.next()
+ return false
+ }
for {
r = d.next()
if r == -1 || r == '\n' {
The original problem comes with restish, but I thought it belongs here at this place.
Thank you very much.