Skip to content
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

Parsing comments inside an array in an Option #138

Closed
patdimond opened this issue Oct 25, 2023 · 4 comments
Closed

Parsing comments inside an array in an Option #138

patdimond opened this issue Oct 25, 2023 · 4 comments

Comments

@patdimond
Copy link

I have some proto that looks like this

syntax = "proto3";

package test.service.test.alpha;

service MyTestService {

  rpc MyTestRPC(input) returns (output) {

    option test = {
      scope_rules : [
        // A comment
        {has : [ "test" ]}
      ]
    };
  }
}

Which causes the parser to throw this error: Error: <input>:12:9: found "{" but expected [, or ]]

I got a hacky fix to work and pass tests by adding the following lines in option.go after the check for a closing bracket ]
https://github.com/emicklei/proto/blob/master/option.go#L201-L207

		// if it's a comment, consume it
		if '/' == r {
			p.next()
		}

but then realised that would only work for a single comment.
I'm unsure what an appropriate fix is here so figured I'd raise an issue. Thanks.

Sidenote - It seems to work properly if the comment appears after the {

    option test = {
      scope_rules : [ {
        // A comment
        has : [ "test" ]
      } ]
    };
@emicklei
Copy link
Owner

thank you for reporting this issue. I am aware that comment parsing is not implemented at all possible locations. I will have a look if this case can be supported

@emicklei
Copy link
Owner

emicklei commented Nov 1, 2023

adding test to reproduce

func TestCommentInsideArray(t *testing.T) {
	src := `option test = {
			scope_rules : [
			  // A comment
			  // Another comment
			  {has : [ 
				// comment on test
				"test" 
			  ]}
			]
		  };
		`
	opt, err := newParserOn(src).Parse()
	if err != nil {
		t.Fatal(err)
	}
	opt2 := opt.Elements[0].(*Option)
	scp, _ := opt2.Constant.OrderedMap.Get("scope_rules")
	elem0 := scp.Array[0]
	t.Log("comment:", elem0.InlineComment.Lines)
	has, _ := elem0.OrderedMap.Get("has")
	if got, want := has.Array[0].Source, "test"; got != want {
		t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
	}
	t.Log("comment:", has.Array[0].InlineComment.Lines)
}

@emicklei
Copy link
Owner

emicklei commented Nov 1, 2023

check #139

@patdimond
Copy link
Author

Works for me. Thanks!

@emicklei emicklei closed this as completed Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants