forked from rime/librime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculus.h
95 lines (77 loc) · 1.79 KB
/
calculus.h
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
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2012-01-17 GONG Chen <chen.sst@gmail.com>
//
#ifndef RIME_CALCULUS_H_
#define RIME_CALCULUS_H_
#include <stdint.h>
#include <boost/regex.hpp>
#include <rime_api.h>
#include <rime/common.h>
#include "spelling.h"
namespace rime {
class Calculation {
public:
using Factory = Calculation*(const vector<string>& args);
Calculation() = default;
virtual ~Calculation() = default;
virtual bool Apply(Spelling* spelling) = 0;
virtual bool addition() { return true; }
virtual bool deletion() { return true; }
};
class Calculus {
public:
RIME_API Calculus();
void Register(const string& token, Calculation::Factory* factory);
RIME_API Calculation* Parse(const string& definition);
private:
map<string, Calculation::Factory*> factories_;
};
// xlit/zyx/abc/
class Transliteration : public Calculation {
public:
static Factory Parse;
bool Apply(Spelling* spelling);
protected:
map<uint32_t, uint32_t> char_map_;
};
// xform/x/y/
class Transformation : public Calculation {
public:
static Factory Parse;
bool Apply(Spelling* spelling);
protected:
boost::regex pattern_;
string replacement_;
};
// erase/x/
class Erasion : public Calculation {
public:
static Factory Parse;
bool Apply(Spelling* spelling);
bool addition() { return false; }
protected:
boost::regex pattern_;
};
// derive/x/X/
class Derivation : public Transformation {
public:
static Factory Parse;
bool deletion() { return false; }
};
// fuzz/zyx/zx/
class Fuzzing : public Derivation {
public:
static Factory Parse;
bool Apply(Spelling* spelling);
};
// abbrev/zyx/z/
class Abbreviation : public Derivation {
public:
static Factory Parse;
bool Apply(Spelling* spelling);
};
} // namespace rime
#endif // RIME_CALCULUS_H_