-
Notifications
You must be signed in to change notification settings - Fork 8
/
parser.go
173 lines (142 loc) · 3.79 KB
/
parser.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package messageformat
import (
"bytes"
"fmt"
"github.com/gotnospirit/makeplural/plural"
)
const (
EscapeChar = '\\'
OpenChar = '{'
CloseChar = '}'
PartChar = ','
PoundChar = '#'
)
type (
// parseFunc describes a function used to parse a subset of the input string into an expression.
parseFunc func(string, *Parser, rune, int, int, *[]rune) (Expression, int, error)
// formatFunc describes a function used to format an expression into the output buffer.
formatFunc func(Expression, *bytes.Buffer, *map[string]interface{}, *MessageFormat, string) error
// pluralFunc describes a function used to produce a named key when processing a plural or selectordinal expression.
pluralFunc func(interface{}, bool) string
Parser struct {
parsers map[string]parseFunc
formatters map[string]formatFunc
plural pluralFunc
}
)
func (x *Parser) Parse(input string) (*MessageFormat, error) {
runes := []rune(input)
pos, end := 0, len(runes)
root := node{}
for pos < end {
i, level, err := x.parse(pos, end, &runes, &root)
if err != nil {
return nil, parseError{err.Error(), i}
} else if level != 0 {
return nil, parseError{"UnbalancedBraces", i}
}
pos = i
}
return &MessageFormat{root, x.formatters, x.plural}, nil
}
func (x *Parser) Register(key string, p parseFunc, f formatFunc) error {
if _, ok := x.parsers[key]; ok {
return fmt.Errorf("ParserAlreadyRegistered")
}
x.parsers[key] = p
x.formatters[key] = f
return nil
}
func (x *Parser) parseExpression(start, end int, ptr_input *[]rune) (string, Expression, int, error) {
varname, char, pos, err := readVar(start, end, ptr_input)
if err != nil {
return "", nil, pos, err
} else if varname == "" {
return "", nil, pos, fmt.Errorf("MissingVarName")
} else if char == CloseChar {
return "var", varname, pos, nil
}
ctype, char, pos, err := readVar(pos+1, end, ptr_input)
if err != nil {
return "", nil, pos, err
}
fn, ok := x.parsers[ctype]
if !ok {
return "", nil, pos, fmt.Errorf("UnknownType: `%s`", ctype)
} else if fn == nil {
return "", nil, pos, fmt.Errorf("UndefinedParseFunc: `%s`", ctype)
}
expr, pos, err := fn(varname, x, char, pos, end, ptr_input)
if err != nil {
return "", nil, pos, err
}
if pos >= end || (*ptr_input)[pos] != CloseChar {
return "", nil, pos, fmt.Errorf("UnbalancedBraces")
}
return ctype, expr, pos, nil
}
func (x *Parser) parse(start, end int, ptr_input *[]rune, parent *node) (int, int, error) {
pos := start
level := 0
escaped := false
input := *ptr_input
loop:
for pos < end {
char := input[pos]
switch char {
default:
pos++
escaped = false
case EscapeChar:
pos++
escaped = true
case CloseChar:
if !escaped {
level--
break loop
}
pos++
escaped = false
case OpenChar:
if !escaped {
level++
if pos > start {
parent.add("literal", parseLiteral(start, pos, ptr_input))
}
ctype, child, i, err := x.parseExpression(pos+1, end, ptr_input)
if err != nil {
return i, level, err
}
parent.add(ctype, child)
level--
pos = i
start = pos + 1
}
pos++
escaped = false
}
}
if pos > start {
parent.add("literal", parseLiteral(start, pos, ptr_input))
}
return pos, level, nil
}
func NewWithCulture(name string) (*Parser, error) {
fn, err := plural.GetFunc(name)
if err != nil {
return nil, err
}
result := new(Parser)
result.parsers = make(map[string]parseFunc)
result.formatters = make(map[string]formatFunc)
result.plural = fn
result.Register("literal", nil, formatLiteral)
result.Register("var", nil, formatVar)
result.Register("select", parseSelect, formatSelect)
result.Register("selectordinal", parseSelect, formatOrdinal)
result.Register("plural", parsePlural, formatPlural)
return result, nil
}
func New() (*Parser, error) {
return NewWithCulture("en")
}