-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcfgrammar_comments.go
70 lines (65 loc) · 1.1 KB
/
cfgrammar_comments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package fmr
import (
"fmt"
)
func (p *parser) comments() error {
defer p.ws()
for {
p.ws()
c, err := p.comment()
if err != nil {
return err
}
if len(c) == 0 {
return nil
}
}
}
func (p *parser) comment() (string, error) {
if p.next() != '/' {
p.backup()
return "", nil
}
switch r := p.peek(); {
case r == '/':
return p.lineComment()
case r == '*':
return p.multiLineComment()
default:
return "", fmt.Errorf("%s : invalid char %s", p.posInfo(), string(r))
}
}
func (p *parser) lineComment() (string, error) {
if err := p.eat('/'); err != nil {
return "", err
}
ret := []rune{'/', '/'}
for {
r := p.next()
if r == '\n' {
break
}
ret = append(ret, r)
}
return string(ret), nil
}
func (p *parser) multiLineComment() (string, error) {
if err := p.eat('*'); err != nil {
return "", err
}
ret := []rune{'/', '*'}
var prev rune
for {
r := p.next()
if r == eof {
return "", fmt.Errorf("%s : unterminated string", p.posInfo())
}
if prev == '*' && r == '/' {
break
}
ret = append(ret, r)
prev = r
}
ret = append(ret, '/')
return string(ret), nil
}