Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
15 tests to go...
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest Micklei committed Nov 27, 2017
1 parent a4740ea commit 38cd5e4
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 29 deletions.
4 changes: 3 additions & 1 deletion comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package proto

import (
"fmt"
"log"
"strconv"
"text/scanner"
)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 1 addition & 4 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package proto

import (
"fmt"
"log"
"text/scanner"
)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "<input>: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(), "<input>: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(), "<input>: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(), "<input>: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 {
Expand Down
10 changes: 7 additions & 3 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "<input>: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(), "<input>:5:3"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := ac[0].Position.Line, 6; got != want {
Expand Down
6 changes: 2 additions & 4 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "<input>:1:1"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
Expand All @@ -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(), "<input>:1:1"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
Expand Down
2 changes: 1 addition & 1 deletion service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "<input>:1:1"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
rpc1 := srv.Elements[0].(*RPC)
Expand Down
6 changes: 4 additions & 2 deletions syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

package proto

import "text/scanner"
import (
"text/scanner"
)

// Syntax should have value "proto"
type Syntax struct {
Expand All @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -192,6 +196,10 @@ func asToken(literal string) token {
case "required":
return tREQUIRED
default:
// special cases
if isComment(literal) {
return tCOMMENT
}
return tIDENT
}
}

0 comments on commit 38cd5e4

Please sign in to comment.