From 38cd5e4db6e427c054524c197f4df94bf08e1dab Mon Sep 17 00:00:00 2001 From: Ernest Micklei Date: Mon, 27 Nov 2017 14:09:36 +0100 Subject: [PATCH] 15 tests to go... --- comment.go | 4 +++- extensions_test.go | 2 +- field.go | 2 -- import.go | 7 ++----- message.go | 5 +---- message_test.go | 8 ++++---- option.go | 10 +++++++--- option_test.go | 4 ++-- parser_test.go | 6 ++---- service_test.go | 2 +- syntax.go | 6 ++++-- token.go | 8 ++++++++ 12 files changed, 35 insertions(+), 29 deletions(-) diff --git a/comment.go b/comment.go index ee44a92..4da8a67 100644 --- a/comment.go +++ b/comment.go @@ -152,7 +152,9 @@ func mergeOrReturnComment(elements []Visitee, lit string, pos scanner.Position) // do not merge c-style comments + // last comment line was on previous line if esize := len(elements); esize > 0 { - if last, ok := elements[esize-1].(*Comment); ok && !last.Cstyle && pos.Line <= last.Position.Line+len(last.Lines) { // less than because last line of file could be inline comment + if last, ok := elements[esize-1].(*Comment); ok && + !last.Cstyle && + pos.Line <= last.Position.Line+len(last.Lines) { // less than because last line of file could be inline comment last.Merge(com) // mark as merged com = nil diff --git a/extensions_test.go b/extensions_test.go index 01e0dc0..b5828ee 100644 --- a/extensions_test.go +++ b/extensions_test.go @@ -38,7 +38,7 @@ func TestExtensions(t *testing.T) { t.Fatal(err) } if len(m.Elements) != 1 { - t.Fatal("1 extension expected, got", len(m.Elements)) + t.Fatal("1 elements expected, got", len(m.Elements)) } f := m.Elements[0].(*Extensions) if got, want := len(f.Ranges), 2; got != want { diff --git a/field.go b/field.go index 203bf5e..6031cf9 100644 --- a/field.go +++ b/field.go @@ -25,7 +25,6 @@ package proto import ( "fmt" - "log" "strconv" "text/scanner" ) @@ -123,7 +122,6 @@ done: // fieldName "=" fieldNumber [ "[" fieldOptions "]" ] "; func parseFieldAfterType(f *Field, p *Parser) error { pos, tok, lit := p.next() - log.Println("parseFieldAfterType", f.Type, lit) if tok != tIDENT { if !isKeyword(tok) { return p.unexpected(lit, "field identifier", f) diff --git a/import.go b/import.go index 596565a..a318b12 100644 --- a/import.go +++ b/import.go @@ -46,11 +46,8 @@ func (i *Import) parse(p *Parser) error { case tPUBLIC: i.Kind = lit return i.parse(p) - // TODO - // case tQUOTE: - // i.Filename = p.s.scanUntil('"') - // case tSINGLEQUOTE: - // i.Filename = p.s.scanUntil('\'') + case tIDENT: + i.Filename = unQuote(lit) default: return p.unexpected(lit, "import classifier weak|public|quoted", i) } diff --git a/message.go b/message.go index c7a756d..75e24cb 100644 --- a/message.go +++ b/message.go @@ -25,7 +25,6 @@ package proto import ( "fmt" - "log" "text/scanner" ) @@ -47,14 +46,13 @@ func (m *Message) groupName() string { // parse expects ident { messageBody func (m *Message) parse(p *Parser) error { - pos, tok, lit := p.next() + _, tok, lit := p.next() if tok != tIDENT { if !isKeyword(tok) { return p.unexpected(lit, m.groupName()+" identifier", m) } } m.Name = lit - m.Position = pos _, tok, lit = p.next() if tok != tLEFTCURLY { return p.unexpected(lit, m.groupName()+" opening {", m) @@ -210,7 +208,6 @@ func (m *Message) Accept(v Visitor) { // addElement is part of elementContainer func (m *Message) addElement(v Visitee) { - log.Printf("adding %v to %v", v, m) // TODO m.Elements = append(m.Elements, v) } diff --git a/message_test.go b/message_test.go index 1c85fd1..99491d8 100644 --- a/message_test.go +++ b/message_test.go @@ -53,19 +53,19 @@ func TestMessage(t *testing.T) { if got, want := m.Name, "Out"; got != want { t.Errorf("got [%v] want [%v]", got, want) } - if got, want := m.Position.String(), "2:3"; got != want { + if got, want := m.Position.String(), ":2:3"; got != want { t.Errorf("got [%v] want [%v]", got, want) } if got, want := len(m.Elements), 6; got != want { t.Errorf("got [%v] want [%v]", got, want) } - if got, want := m.Elements[0].(*NormalField).Position.String(), "4:3"; got != want { + if got, want := m.Elements[0].(*NormalField).Position.String(), ":4:3"; got != want { t.Errorf("got [%v] want [%v]", got, want) } - if got, want := m.Elements[0].(*NormalField).Comment.Position.String(), "3:3"; got != want { + if got, want := m.Elements[0].(*NormalField).Comment.Position.String(), ":3:3"; got != want { t.Errorf("got [%v] want [%v]", got, want) } - if got, want := m.Elements[3].(*Message).Position.String(), "12:3"; got != want { + if got, want := m.Elements[3].(*Message).Position.String(), ":12:3"; got != want { t.Errorf("got [%v] want [%v]", got, want) } if got, want := m.Elements[3].(*Message).Elements[0].(*NormalField).Position.Line, 13; got != want { diff --git a/option.go b/option.go index 565364c..d16c1ab 100644 --- a/option.go +++ b/option.go @@ -161,7 +161,12 @@ func (l Literal) String() string { // parse expects to read a literal constant after =. func (l *Literal) parse(p *Parser) error { pos, _, lit := p.next() - l.Position, l.Source, l.IsString = pos, lit, isString(lit) + source := lit + isString := isString(lit) + if isString { + source = unQuote(source) + } + l.Position, l.Source, l.IsString = pos, source, isString return nil } @@ -177,8 +182,7 @@ func (o *Option) parseAggregate(p *Parser) error { for { pos, tok, lit := p.next() if tRIGHTSQUARE == tok { - // TODO - // p.unscan() + p.nextPut(pos, tok, lit) // caller has checked for open square ; will consume rightsquare, rightcurly and semicolon return nil } diff --git a/option_test.go b/option_test.go index 28cb1f6..1c36772 100644 --- a/option_test.go +++ b/option_test.go @@ -176,10 +176,10 @@ message Bar { if got, want := o.Position.Line, 3; got != want { t.Fatalf("got [%v] want [%v]", got, want) } - if got, want := o.Comment.Position.String(), "2:1"; got != want { + if got, want := o.Comment.Position.String(), ":2:1"; got != want { t.Fatalf("got [%v] want [%v]", got, want) } - if got, want := f.Position.String(), "5:3"; got != want { + if got, want := f.Position.String(), ":5:3"; got != want { t.Fatalf("got [%v] want [%v]", got, want) } if got, want := ac[0].Position.Line, 6; got != want { diff --git a/parser_test.go b/parser_test.go index 94f8e75..b81ac1d 100644 --- a/parser_test.go +++ b/parser_test.go @@ -66,8 +66,7 @@ func TestScanIgnoreWhitespace_Digits(t *testing.T) { if got, want := lit, "1"; got != want { t.Errorf("got [%v] want [%v]", got, want) } - // pos right after "4" - if got, want := pos.String(), "1:5"; got != want { + if got, want := pos.String(), ":1:1"; got != want { t.Errorf("got [%v] want [%v]", got, want) } } @@ -78,8 +77,7 @@ func TestScanIgnoreWhitespace_Minus(t *testing.T) { if got, want := lit, "-"; got != want { t.Errorf("got [%v] want [%v]", got, want) } - // pos right after "-" - if got, want := pos.String(), "1:2"; got != want { + if got, want := pos.String(), ":1:1"; got != want { t.Errorf("got [%v] want [%v]", got, want) } } diff --git a/service_test.go b/service_test.go index 3792130..1733bce 100644 --- a/service_test.go +++ b/service_test.go @@ -39,7 +39,7 @@ func TestService(t *testing.T) { if got, want := len(srv.Elements), 2; got != want { t.Fatalf("got [%v] want [%v]", got, want) } - if got, want := srv.Position.String(), "1:1"; got != want { + if got, want := srv.Position.String(), ":1:1"; got != want { t.Fatalf("got [%v] want [%v]", got, want) } rpc1 := srv.Elements[0].(*RPC) diff --git a/syntax.go b/syntax.go index 9d3cdfe..f4c5f74 100644 --- a/syntax.go +++ b/syntax.go @@ -23,7 +23,9 @@ package proto -import "text/scanner" +import ( + "text/scanner" +) // Syntax should have value "proto" type Syntax struct { @@ -41,7 +43,7 @@ func (s *Syntax) parse(p *Parser) error { if !isString(lit) { return p.unexpected(lit, "syntax string constant", s) } - s.Value = lit + s.Value = unQuote(lit) return nil } diff --git a/token.go b/token.go index 94f85d0..a3a8c65 100644 --- a/token.go +++ b/token.go @@ -113,6 +113,10 @@ func isComment(lit string) bool { return strings.HasPrefix(lit, "//") || strings.HasPrefix(lit, "/*") } +func unQuote(lit string) string { + return strings.Trim(lit, "\"'") +} + func asToken(literal string) token { switch literal { // delimiters @@ -192,6 +196,10 @@ func asToken(literal string) token { case "required": return tREQUIRED default: + // special cases + if isComment(literal) { + return tCOMMENT + } return tIDENT } }