-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
166 lines (138 loc) · 2.64 KB
/
parse.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
package zhuji
import (
"strings"
"unicode/utf8"
)
type parser struct {
source string
pos int
}
func newParser(source string) *parser {
return &parser{source, 0}
}
// Parser primaries.
func (p *parser) eof() bool {
return p.pos == len(p.source)
}
func (p *parser) rest() string {
return p.source[p.pos:]
}
func (p *parser) next() rune {
r, size := utf8.DecodeRuneInString(p.rest())
p.pos += size
return r
}
func (p *parser) peek() rune {
r, _ := utf8.DecodeRuneInString(p.rest())
return r
}
func (p *parser) from(i int) string {
return p.source[i:p.pos]
}
func (p *parser) to(i int) string {
rest := p.rest()
if i == -1 {
return rest
}
return rest[:i]
}
func (p *parser) uptoFunc(f func(rune) bool) string {
return p.to(strings.IndexFunc(p.rest(), f))
}
func (p *parser) uptoAny(chars string) string {
return p.to(strings.IndexAny(p.rest(), chars))
}
func in(r rune, s string) bool {
return strings.ContainsRune(s, r)
}
// Parsing functions.
var (
keywords = "者自若则非毕"
wordSep = "、,:;"
sentenceSep = "也。\n"
wordTerm = wordSep + sentenceSep
)
type Word struct {
Name string
IsKeyword bool
}
func (w *Word) isNumeral() bool {
r, _ := utf8.DecodeRuneInString(w.Name)
return in(r, numerals)
}
func (w *Word) String() string {
return w.Name
}
func (p *parser) word() (w *Word) {
begin := p.pos
w = &Word{}
defer func() {
w.Name = p.from(begin)
}()
if r := p.next(); in(r, keywords) {
w.IsKeyword = true
return
} else if in(r, numerals) {
for in(p.peek(), numerals) {
p.next()
}
return
}
for !p.eof() {
r := p.peek()
if in(r, numerals) || in(r, keywords) || in(r, wordTerm) {
return
}
p.next()
}
return
}
type Sentence struct {
Words []*Word
}
func (p *parser) sentence() Sentence {
s := Sentence{}
jux := false
for !p.eof() {
word := p.word()
s.Words = append(s.Words, word)
if jux && word.isNumeral() {
// Numeral juxtaposes another word: swap unless the previous word
// is a keyword. This enables infix notation.
n := len(s.Words)
if !s.Words[n-2].IsKeyword {
s.Words[n-1], s.Words[n-2] = s.Words[n-2], s.Words[n-1]
}
}
jux = true
for !p.eof() {
r := p.peek()
if in(r, wordSep) {
p.next()
jux = false
} else if in(r, sentenceSep) {
p.next()
for in(p.peek(), sentenceSep) {
p.next()
}
return s
} else {
break
}
}
}
return s
}
type Article struct {
Sentences []Sentence
}
func (p *parser) article() Article {
a := Article{}
for !p.eof() {
a.Sentences = append(a.Sentences, p.sentence())
}
return a
}
func Parse(source string) Article {
return newParser(source).article()
}