-
Notifications
You must be signed in to change notification settings - Fork 15
/
cfgrammar_terminal.go
64 lines (61 loc) · 1.11 KB
/
cfgrammar_terminal.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
package fmr
import "fmt"
func (p *parser) terminal() (flags, text string, err error) {
if err = p.eat('"'); err != nil {
return
}
p.ws()
if p.peek() == '(' {
p.eat('(')
p.ws()
if err = p.eat('?'); err != nil {
return
}
p.ws()
if flags, err = p.text(); err != nil {
return
}
if err = p.eat(')'); err != nil {
return
}
}
if text, err = p.terminalText(); err != nil {
return
}
err = p.eat('"')
return
}
func (p *parser) terminalText() (string, error) {
var ret []rune
var prev rune
for {
switch r := p.next(); {
case r == '"' && prev != '\\':
p.backup()
return string(ret), nil
case r == eof:
return "", fmt.Errorf("%s : unterminated string", p.posInfo())
case prev == '\\':
switch r {
case '\\':
ret = append(ret, '\\')
case 'n':
ret = append(ret, '\n')
case 't':
ret = append(ret, '\t')
case '"':
ret = append(ret, '"')
case '(':
ret = append(ret, '(')
default:
return "", fmt.Errorf("%s : unexpected escape string", p.posInfo())
}
prev = 0
case r == '\\':
prev = r
default:
ret = append(ret, r)
prev = r
}
}
}