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

Commit

Permalink
9 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 38cd5e4 commit f15df7c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 38 deletions.
35 changes: 17 additions & 18 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,23 @@ type commentInliner interface {
}

// maybeScanInlineComment tries to scan comment on the current line ; if present then set it for the last element added.
func maybeScanInlineComment(p *Parser, c elementContainer) {}

// currentPos := p.s.pos
// // see if there is an inline Comment
// pos, lit := p.Next()
// esize := len(c.elements())
// // seen comment and on same line and elements have been added
// if tCOMMENT == tok && p.s.pos.Line <= currentPos.Line+1 && esize > 0 {
// // if the last added element can have an inline comment then set it
// last := c.elements()[esize-1]
// if inliner, ok := last.(commentInliner); ok {
// // TODO skip multiline?
// inliner.inlineComment(newComment(pos, lit))
// }
// } else {
// p.unscan()
// }
// }
func maybeScanInlineComment(p *Parser, c elementContainer) {
currentPos := p.scanner.Position
// see if there is an inline Comment
pos, tok, lit := p.next()
esize := len(c.elements())
// seen comment and on same line and elements have been added
if tCOMMENT == tok && pos.Line == currentPos.Line && esize > 0 {
// if the last added element can have an inline comment then set it
last := c.elements()[esize-1]
if inliner, ok := last.(commentInliner); ok {
// TODO skip multiline?
inliner.inlineComment(newComment(pos, lit))
}
} else {
p.nextPut(pos, tok, lit)
}
}

// takeLastComment removes and returns the last element of the list if it is a Comment.
func takeLastComment(list []Visitee) (*Comment, []Visitee) {
Expand Down
7 changes: 6 additions & 1 deletion extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

package proto

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

// Extensions declare that a range of field numbers in a message are available for third-party extensions.
// proto2 only
Expand Down Expand Up @@ -53,3 +56,5 @@ func (e *Extensions) parse(p *Parser) error {
e.Ranges = list
return nil
}

func (e *Extensions) String() string { return fmt.Sprintf("<extensions %v>", e.Ranges) }
4 changes: 2 additions & 2 deletions extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func TestExtensions(t *testing.T) {
extensions 4, 20 to max; // max
}`
p := newParserOn(proto)
p.next() // consume extensions
p.next() // consume message
m := new(Message)
err := m.parse(p)
if err != nil {
t.Fatal(err)
}
if len(m.Elements) != 1 {
t.Fatal("1 elements expected, got", len(m.Elements))
t.Fatal("1 elements expected, got", len(m.Elements), m.Elements)
}
f := m.Elements[0].(*Extensions)
if got, want := len(f.Ranges), 2; got != want {
Expand Down
5 changes: 2 additions & 3 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r Range) String() string {
func parseRanges(p *Parser, n Visitee) (list []Range, err error) {
seenTo := false
for {
_, _, lit := p.next()
pos, tok, lit := p.next()
if isString(lit) {
return list, p.unexpected(lit, "integer, <to> <max>", n)
}
Expand All @@ -58,8 +58,7 @@ func parseRanges(p *Parser, n Visitee) (list []Range, err error) {
case "to":
seenTo = true
case ";":
// TODO
// p.s.unread(';') // allow for inline comment parsing
p.nextPut(pos, tok, lit) // allow for inline comment parsing
goto done
case "max":
if !seenTo {
Expand Down
18 changes: 5 additions & 13 deletions reserved.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,28 @@ func (r *Reserved) Accept(v Visitor) {

func (r *Reserved) parse(p *Parser) error {
for {
_, tok, lit := p.next()
pos, tok, lit := p.next()
if len(lit) == 0 {
return p.unexpected(lit, "reserved string or integer", r)
}
// first char that determined tok
ch := []rune(lit)[0]
if isDigit(ch) {
// use unread here because it could be start of ranges
// TODO
// p.s.unread(ch)
p.nextPut(pos, tok, lit)
list, err := parseRanges(p, r)
if err != nil {
return err
}
r.Ranges = list
continue
}
if tQUOTE == tok || tSINGLEQUOTE == tok {
// use unread here because scanLiteral does not look at buf
// TODO p.s.unread(ch)
//field, isString := p.s.scanLiteral()
_, _, lit := p.next()
if !isString(lit) {
return p.unexpected(lit, "reserved string", r)
}
r.FieldNames = append(r.FieldNames, lit)
if isString(lit) {
r.FieldNames = append(r.FieldNames, unQuote(lit))
continue
}
if tSEMICOLON == tok {
// TODO p.unscan()
p.nextPut(pos, tok, lit)
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion reserved_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestReservedRanges(t *testing.T) {
t.Fatal(err)
}
if got, want := r.Ranges[0].String(), "2"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := r.Ranges[2].String(), "9 to 11"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
Expand Down

0 comments on commit f15df7c

Please sign in to comment.