@@ -26,13 +26,15 @@ func (l *lexable) next() (rune, bool) {
2626 * l = (* l )[width :]
2727 return curchar , true
2828 }
29+
2930 return ' ' , false
3031}
3132func (l * lexable ) peek () (rune , bool ) {
3233 if ! l .empty () {
3334 c , _ := utf8 .DecodeRuneInString (string (* l ))
3435 return c , true
3536 }
37+
3638 return ' ' , false
3739}
3840
@@ -50,13 +52,16 @@ func (l *lexable) lexDecimalNumber() (int64, error) {
5052 r , _ := l .next ()
5153 number += fmt .Sprintf ("%c" , r )
5254 }
55+
5356 if len (number ) == 0 {
5457 return 0 , fmt .Errorf ("number not found in string: %s" , * l )
5558 }
59+
5660 i , err := strconv .ParseInt (number , 10 , 64 )
5761 if err != nil {
5862 return 0 , err
5963 }
64+
6065 return i , nil
6166}
6267
@@ -76,6 +81,7 @@ func (l *lexable) lexWord() (string, error) {
7681 word = append (word , buf [0 :x ]... )
7782 }
7883 }
84+
7985 return string (word ), nil
8086}
8187
@@ -85,9 +91,11 @@ func (l *lexable) lexGloss() (string, error) {
8591 if ! ok {
8692 return "" , fmt .Errorf ("definition expected" )
8793 }
94+
8895 if r != '|' {
8996 return "" , fmt .Errorf ("definition expected (want '|' got '%c') [%q]" , r , string (* l ))
9097 }
98+
9199 return strings .TrimSpace (string (* l )), nil
92100}
93101
@@ -101,13 +109,16 @@ func (l *lexable) lexHexNumber() (int64, error) {
101109 r , _ := l .next ()
102110 number += fmt .Sprintf ("%c" , r )
103111 }
112+
104113 if len (number ) == 0 {
105114 return 0 , fmt .Errorf ("number not found in string: %s" , * l )
106115 }
116+
107117 i , err := strconv .ParseInt (number , 16 , 64 )
108118 if err != nil {
109119 return 0 , err
110120 }
121+
111122 return i , nil
112123}
113124
@@ -116,14 +127,17 @@ func (l *lexable) lexOffset() (string, error) {
116127 if len (* l ) < 8 {
117128 return "" , fmt .Errorf ("invalid offset" )
118129 }
130+
119131 for i := 0 ; i < 8 ; i ++ {
120132 if ! unicode .IsDigit (rune ((* l )[i ])) {
121133 return "" , fmt .Errorf ("invalid chars in offset: %s" , string ((* l )[0 :8 ]))
122134 }
123135 }
136+
124137 cpy := make ([]byte , 8 )
125138 copy (cpy , (* l )[0 :8 ])
126139 * l = (* l )[8 :]
140+
127141 return string (cpy ), nil
128142}
129143
@@ -133,6 +147,7 @@ func (l *lexable) lexPOS() (PartOfSpeech, error) {
133147 if ! ok {
134148 return 0 , fmt .Errorf ("unexpected end of input" )
135149 }
150+
136151 switch curchar {
137152 case 'n' :
138153 return Noun , nil
@@ -149,6 +164,7 @@ func (l *lexable) lexPOS() (PartOfSpeech, error) {
149164 case 'r' :
150165 return Adverb , nil
151166 }
167+
152168 return 0 , fmt .Errorf ("invalid part of speech: %c" , curchar )
153169}
154170
@@ -158,6 +174,7 @@ func (l *lexable) lexRelationType() (Relation, error) {
158174 if err != nil {
159175 return 0 , fmt .Errorf ("can't read relation type: %s" , err )
160176 }
177+
161178 switch word {
162179 case "!" :
163180 return Antonym , nil
@@ -213,6 +230,7 @@ func (l *lexable) lexRelationType() (Relation, error) {
213230 case "~i" :
214231 return InstanceHyponym , nil
215232 }
233+
216234 return 0 , fmt .Errorf ("unrecognized pointer type: %q" , word )
217235}
218236
@@ -247,25 +265,30 @@ func parseLine(data []byte, line int64) (*parsed, error) {
247265 }
248266 return nil , fmt .Errorf ("can't parse line, expected comment or Offset" )
249267 }
268+
250269 // file number
251270 filenum , err := l .lexDecimalNumber ()
252271 if err != nil {
253272 return nil , fmt .Errorf ("filenumber expected: %s" , err )
254273 }
274+
255275 pos , err := l .lexPOS ()
256276 if err != nil {
257277 return nil , fmt .Errorf ("part of speech expected: %s" , err )
258278 }
279+
259280 // lexicographer file containing the word
260281 wordcount , err := l .lexHexNumber ()
261282 if err != nil {
262283 return nil , fmt .Errorf ("wordcount expected: %s" , err )
263284 }
285+
264286 p := parsed {
265287 byteOffset : byteOffset ,
266288 pos : pos ,
267289 fileNum : filenum ,
268290 }
291+
269292 for ; wordcount > 0 ; wordcount -- {
270293 value , err := l .lexWord ()
271294 if err != nil {
@@ -281,10 +304,12 @@ func parseLine(data []byte, line int64) (*parsed, error) {
281304 sense : uint8 (sense ),
282305 })
283306 }
307+
284308 pcount , err := l .lexDecimalNumber ()
285309 if err != nil {
286310 return nil , fmt .Errorf ("pointer count expected: %s" , err )
287311 }
312+
288313 for ; pcount > 0 ; pcount -- {
289314 if rt , err := l .lexRelationType (); err != nil {
290315 return nil , err
@@ -310,6 +335,7 @@ func parseLine(data []byte, line int64) (*parsed, error) {
310335 p .rels = append (p .rels , r )
311336 }
312337 }
338+
313339 // parse optional frame count
314340 frameCount , err := l .lexDecimalNumber ()
315341 if err == nil {
@@ -324,10 +350,12 @@ func parseLine(data []byte, line int64) (*parsed, error) {
324350 }
325351 }
326352 }
353+
327354 gloss , err := l .lexGloss ()
328355 if err != nil {
329356 return nil , err
330357 }
358+
331359 p .gloss = gloss
332360
333361 return & p , nil
0 commit comments