iv-phonic 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +24 -0
- data/Manifest.txt +49 -0
- data/README.rdoc +32 -0
- data/Rakefile +54 -0
- data/ext/include/iv/algorithm.h +23 -0
- data/ext/include/iv/alloc.h +200 -0
- data/ext/include/iv/any.h +71 -0
- data/ext/include/iv/ast-factory.h +277 -0
- data/ext/include/iv/ast-fwd.h +92 -0
- data/ext/include/iv/ast-serializer.h +579 -0
- data/ext/include/iv/ast-visitor.h +121 -0
- data/ext/include/iv/ast.h +1127 -0
- data/ext/include/iv/chars.h +83 -0
- data/ext/include/iv/cmdline.h +830 -0
- data/ext/include/iv/conversions.h +308 -0
- data/ext/include/iv/dtoa.h +20 -0
- data/ext/include/iv/enable_if.h +18 -0
- data/ext/include/iv/errors.h +15 -0
- data/ext/include/iv/fixedcontainer.h +42 -0
- data/ext/include/iv/functor.h +29 -0
- data/ext/include/iv/lexer.h +1281 -0
- data/ext/include/iv/location.h +23 -0
- data/ext/include/iv/mt19937.h +175 -0
- data/ext/include/iv/noncopyable.h +30 -0
- data/ext/include/iv/none.h +10 -0
- data/ext/include/iv/parser.h +2150 -0
- data/ext/include/iv/source.h +27 -0
- data/ext/include/iv/space.h +178 -0
- data/ext/include/iv/static_assert.h +30 -0
- data/ext/include/iv/stringpiece.h +385 -0
- data/ext/include/iv/token.h +311 -0
- data/ext/include/iv/ucdata.h +58 -0
- data/ext/include/iv/uchar.h +8 -0
- data/ext/include/iv/ustring.h +28 -0
- data/ext/include/iv/ustringpiece.h +9 -0
- data/ext/include/iv/utils.h +83 -0
- data/ext/include/iv/xorshift.h +74 -0
- data/ext/iv/phonic/ast-fwd.h +21 -0
- data/ext/iv/phonic/ast.h +10 -0
- data/ext/iv/phonic/creator.h +530 -0
- data/ext/iv/phonic/encoding.h +110 -0
- data/ext/iv/phonic/extconf.rb +5 -0
- data/ext/iv/phonic/factory.h +247 -0
- data/ext/iv/phonic/parser.h +12 -0
- data/ext/iv/phonic/phonic.cc +69 -0
- data/ext/iv/phonic/rnode.h +15 -0
- data/ext/iv/phonic/rparser.h +48 -0
- data/ext/iv/phonic/source.h +146 -0
- data/test/test_iv_phonic.rb +32 -0
- metadata +159 -0
@@ -0,0 +1,247 @@
|
|
1
|
+
#ifndef _IV_PHONIC_FACTORY_H_
|
2
|
+
#define _IV_PHONIC_FACTORY_H_
|
3
|
+
#include <tr1/array>
|
4
|
+
#include <ruby.h>
|
5
|
+
#include <ruby/oniguruma.h>
|
6
|
+
#include <iv/alloc.h>
|
7
|
+
#include <iv/ustringpiece.h>
|
8
|
+
#include "encoding.h"
|
9
|
+
#include "ast.h"
|
10
|
+
namespace iv {
|
11
|
+
namespace phonic {
|
12
|
+
|
13
|
+
class AstFactory : public core::Space<2> {
|
14
|
+
public:
|
15
|
+
AstFactory()
|
16
|
+
: core::Space<2>(),
|
17
|
+
undefined_instance_(new(this)Undefined()),
|
18
|
+
empty_statement_instance_(new(this)EmptyStatement()),
|
19
|
+
debugger_statement_instance_(new(this)DebuggerStatement()),
|
20
|
+
this_instance_(new(this)ThisLiteral()),
|
21
|
+
null_instance_(new(this)NullLiteral()),
|
22
|
+
true_instance_(new(this)TrueLiteral()),
|
23
|
+
false_instance_(new(this)FalseLiteral()) {
|
24
|
+
}
|
25
|
+
|
26
|
+
Identifier* NewIdentifier(const core::UStringPiece& buffer) {
|
27
|
+
Identifier* ident = new (this) Identifier(buffer, this);
|
28
|
+
return ident;
|
29
|
+
}
|
30
|
+
|
31
|
+
Identifier* NewIdentifier(const std::vector<uc16>& buffer) {
|
32
|
+
Identifier* ident = new (this) Identifier(buffer, this);
|
33
|
+
return ident;
|
34
|
+
}
|
35
|
+
|
36
|
+
Identifier* NewIdentifier(const std::vector<char>& buffer) {
|
37
|
+
Identifier* ident = new (this) Identifier(buffer, this);
|
38
|
+
return ident;
|
39
|
+
}
|
40
|
+
|
41
|
+
NumberLiteral* NewNumberLiteral(const double& val) {
|
42
|
+
return new (this) NumberLiteral(val);
|
43
|
+
}
|
44
|
+
|
45
|
+
StringLiteral* NewStringLiteral(const std::vector<uc16>& buffer) {
|
46
|
+
return new (this) StringLiteral(buffer, this);
|
47
|
+
}
|
48
|
+
|
49
|
+
Directivable* NewDirectivable(const std::vector<uc16>& buffer) {
|
50
|
+
return new (this) Directivable(buffer, this);
|
51
|
+
}
|
52
|
+
|
53
|
+
RegExpLiteral* NewRegExpLiteral(const std::vector<uc16>& content,
|
54
|
+
const std::vector<uc16>& flags) {
|
55
|
+
// OnigErrorInfo einfo;
|
56
|
+
// regex_t* reg;
|
57
|
+
// // TODO(Constellation) Little Endian?
|
58
|
+
// int r = onig_new(®,
|
59
|
+
// reinterpret_cast<const OnigUChar*>(content.data()),
|
60
|
+
// reinterpret_cast<const OnigUChar*>(content.data()+content.size()),
|
61
|
+
// ONIG_OPTION_DEFAULT, rb_enc_get(Encoding::UTF16LEEncoding()),
|
62
|
+
// ONIG_SYNTAX_DEFAULT, &einfo);
|
63
|
+
// if (r != ONIG_NORMAL) {
|
64
|
+
// return NULL;
|
65
|
+
// }
|
66
|
+
// onig_free(reg);
|
67
|
+
return new (this) RegExpLiteral(content, flags, this);
|
68
|
+
}
|
69
|
+
|
70
|
+
FunctionLiteral* NewFunctionLiteral(FunctionLiteral::DeclType type) {
|
71
|
+
return new (this) FunctionLiteral(type, this);
|
72
|
+
}
|
73
|
+
|
74
|
+
ArrayLiteral* NewArrayLiteral() {
|
75
|
+
return new (this) ArrayLiteral(this);
|
76
|
+
}
|
77
|
+
|
78
|
+
ObjectLiteral* NewObjectLiteral() {
|
79
|
+
return new (this) ObjectLiteral(this);
|
80
|
+
}
|
81
|
+
|
82
|
+
Identifiers* NewLabels() {
|
83
|
+
return new (New(sizeof(Identifiers)))
|
84
|
+
Identifiers(Identifiers::allocator_type(this));
|
85
|
+
}
|
86
|
+
|
87
|
+
NullLiteral* NewNullLiteral() {
|
88
|
+
return null_instance_;
|
89
|
+
}
|
90
|
+
|
91
|
+
EmptyStatement* NewEmptyStatement() {
|
92
|
+
return empty_statement_instance_;
|
93
|
+
}
|
94
|
+
|
95
|
+
DebuggerStatement* NewDebuggerStatement() {
|
96
|
+
return debugger_statement_instance_;
|
97
|
+
}
|
98
|
+
|
99
|
+
ThisLiteral* NewThisLiteral() {
|
100
|
+
return this_instance_;
|
101
|
+
}
|
102
|
+
|
103
|
+
Undefined* NewUndefined() {
|
104
|
+
return undefined_instance_;
|
105
|
+
}
|
106
|
+
|
107
|
+
TrueLiteral* NewTrueLiteral() {
|
108
|
+
return true_instance_;
|
109
|
+
}
|
110
|
+
|
111
|
+
FalseLiteral* NewFalseLiteral() {
|
112
|
+
return false_instance_;
|
113
|
+
}
|
114
|
+
|
115
|
+
FunctionStatement* NewFunctionStatement(FunctionLiteral* func) {
|
116
|
+
return new (this) FunctionStatement(func);
|
117
|
+
}
|
118
|
+
|
119
|
+
Block* NewBlock() {
|
120
|
+
return new (this) Block(this);
|
121
|
+
}
|
122
|
+
|
123
|
+
VariableStatement* NewVariableStatement(core::Token::Type token) {
|
124
|
+
return new (this) VariableStatement(token, this);
|
125
|
+
}
|
126
|
+
|
127
|
+
Declaration* NewDeclaration(Identifier* name, Expression* expr) {
|
128
|
+
return new (this) Declaration(name, expr);
|
129
|
+
}
|
130
|
+
|
131
|
+
IfStatement* NewIfStatement(Expression* cond, Statement* then) {
|
132
|
+
return new (this) IfStatement(cond, then);
|
133
|
+
}
|
134
|
+
|
135
|
+
DoWhileStatement* NewDoWhileStatement() {
|
136
|
+
return new (this) DoWhileStatement();
|
137
|
+
}
|
138
|
+
|
139
|
+
WhileStatement* NewWhileStatement(Expression* expr) {
|
140
|
+
return new (this) WhileStatement(expr);
|
141
|
+
}
|
142
|
+
|
143
|
+
ForInStatement* NewForInStatement(Statement* each, Expression* enumerable) {
|
144
|
+
return new (this) ForInStatement(each, enumerable);
|
145
|
+
}
|
146
|
+
|
147
|
+
ExpressionStatement* NewExpressionStatement(Expression* expr) {
|
148
|
+
return new (this) ExpressionStatement(expr);
|
149
|
+
}
|
150
|
+
|
151
|
+
ForStatement* NewForStatement() {
|
152
|
+
return new (this) ForStatement();
|
153
|
+
}
|
154
|
+
|
155
|
+
ContinueStatement* NewContinueStatement() {
|
156
|
+
return new (this) ContinueStatement();
|
157
|
+
}
|
158
|
+
|
159
|
+
BreakStatement* NewBreakStatement() {
|
160
|
+
return new (this) BreakStatement();
|
161
|
+
}
|
162
|
+
|
163
|
+
ReturnStatement* NewReturnStatement(Expression* expr) {
|
164
|
+
return new (this) ReturnStatement(expr);
|
165
|
+
}
|
166
|
+
|
167
|
+
WithStatement* NewWithStatement(Expression* expr, Statement* stmt) {
|
168
|
+
return new (this) WithStatement(expr, stmt);
|
169
|
+
}
|
170
|
+
|
171
|
+
SwitchStatement* NewSwitchStatement(Expression* expr) {
|
172
|
+
return new (this) SwitchStatement(expr, this);
|
173
|
+
}
|
174
|
+
|
175
|
+
CaseClause* NewCaseClause() {
|
176
|
+
return new (this) CaseClause(this);
|
177
|
+
}
|
178
|
+
|
179
|
+
ThrowStatement* NewThrowStatement(Expression* expr) {
|
180
|
+
return new (this) ThrowStatement(expr);
|
181
|
+
}
|
182
|
+
|
183
|
+
TryStatement* NewTryStatement(Block* block) {
|
184
|
+
return new (this) TryStatement(block);
|
185
|
+
}
|
186
|
+
|
187
|
+
LabelledStatement* NewLabelledStatement(Expression* expr, Statement* stmt) {
|
188
|
+
return new (this) LabelledStatement(expr, stmt);
|
189
|
+
}
|
190
|
+
|
191
|
+
BinaryOperation* NewBinaryOperation(core::Token::Type op,
|
192
|
+
Expression* result,
|
193
|
+
Expression* right) {
|
194
|
+
return new (this) BinaryOperation(op, result, right);
|
195
|
+
}
|
196
|
+
|
197
|
+
Assignment* NewAssignment(core::Token::Type op,
|
198
|
+
Expression* left,
|
199
|
+
Expression* right) {
|
200
|
+
return new (this) Assignment(op, left, right);
|
201
|
+
}
|
202
|
+
|
203
|
+
ConditionalExpression* NewConditionalExpression(Expression* cond,
|
204
|
+
Expression* left,
|
205
|
+
Expression* right) {
|
206
|
+
return new (this) ConditionalExpression(cond, left, right);
|
207
|
+
}
|
208
|
+
|
209
|
+
UnaryOperation* NewUnaryOperation(core::Token::Type op,
|
210
|
+
Expression* expr) {
|
211
|
+
return new (this) UnaryOperation(op, expr);
|
212
|
+
}
|
213
|
+
|
214
|
+
PostfixExpression* NewPostfixExpression(core::Token::Type op,
|
215
|
+
Expression* expr) {
|
216
|
+
return new (this) PostfixExpression(op, expr);
|
217
|
+
}
|
218
|
+
|
219
|
+
FunctionCall* NewFunctionCall(Expression* expr) {
|
220
|
+
return new (this) FunctionCall(expr, this);
|
221
|
+
}
|
222
|
+
|
223
|
+
ConstructorCall* NewConstructorCall(Expression* target) {
|
224
|
+
return new (this) ConstructorCall(target, this);
|
225
|
+
}
|
226
|
+
|
227
|
+
IndexAccess* NewIndexAccess(Expression* expr, Expression* index) {
|
228
|
+
return new (this) IndexAccess(expr, index);
|
229
|
+
}
|
230
|
+
|
231
|
+
IdentifierAccess* NewIdentifierAccess(Expression* expr, Identifier* ident) {
|
232
|
+
return new (this) IdentifierAccess(expr, ident);
|
233
|
+
}
|
234
|
+
|
235
|
+
private:
|
236
|
+
Undefined* undefined_instance_;
|
237
|
+
EmptyStatement* empty_statement_instance_;
|
238
|
+
DebuggerStatement* debugger_statement_instance_;
|
239
|
+
ThisLiteral* this_instance_;
|
240
|
+
NullLiteral* null_instance_;
|
241
|
+
TrueLiteral* true_instance_;
|
242
|
+
FalseLiteral* false_instance_;
|
243
|
+
};
|
244
|
+
|
245
|
+
|
246
|
+
} } // namespace iv::phonic
|
247
|
+
#endif // _IV_PHONIC_FACTORY_H_
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#ifndef _IV_PHONIC_PARSER_H_
|
2
|
+
#define _IV_PHONIC_PARSER_H_
|
3
|
+
#include <ruby.h>
|
4
|
+
#include <iv/parser.h>
|
5
|
+
#include "factory.h"
|
6
|
+
namespace iv {
|
7
|
+
namespace phonic {
|
8
|
+
|
9
|
+
typedef core::Parser<AstFactory> Parser;
|
10
|
+
|
11
|
+
} } // namespace iv::phonic
|
12
|
+
#endif // _IV_PHONIC_PARSER_H_
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include "rparser.h"
|
3
|
+
|
4
|
+
#define RBFUNC(func) (reinterpret_cast<VALUE(*)(...)>(func))
|
5
|
+
|
6
|
+
extern "C" {
|
7
|
+
|
8
|
+
void Init_phonic() {
|
9
|
+
iv::phonic::Encoding::Init();
|
10
|
+
// IV
|
11
|
+
VALUE mIV = rb_define_module("IV");
|
12
|
+
|
13
|
+
// IV::Phonic
|
14
|
+
VALUE mIVPhonic = rb_define_module_under(mIV, "Phonic");
|
15
|
+
rb_define_module_function(mIVPhonic, "parse",
|
16
|
+
RBFUNC(&iv::phonic::RParser::Parse), 1);
|
17
|
+
|
18
|
+
iv::phonic::RParser::Init(mIVPhonic);
|
19
|
+
|
20
|
+
VALUE cNode = rb_define_class_under(mIVPhonic, "Node", rb_cObject);
|
21
|
+
VALUE cExpr = rb_define_class_under(mIVPhonic, "Expression", cNode);
|
22
|
+
VALUE cStmt = rb_define_class_under(mIVPhonic, "Statement", cNode);
|
23
|
+
|
24
|
+
// Expressions
|
25
|
+
rb_define_class_under(mIVPhonic, "Identifier", cExpr);
|
26
|
+
rb_define_class_under(mIVPhonic, "StringLiteral", cExpr);
|
27
|
+
rb_define_class_under(mIVPhonic, "NumberLiteral", cExpr);
|
28
|
+
rb_define_class_under(mIVPhonic, "RegExpLiteral", cExpr);
|
29
|
+
rb_define_class_under(mIVPhonic, "FunctionLiteral", cExpr);
|
30
|
+
rb_define_class_under(mIVPhonic, "ArrayLiteral", cExpr);
|
31
|
+
rb_define_class_under(mIVPhonic, "ObjectLiteral", cExpr);
|
32
|
+
rb_define_class_under(mIVPhonic, "NullLiteral", cExpr);
|
33
|
+
rb_define_class_under(mIVPhonic, "ThisLiteral", cExpr);
|
34
|
+
rb_define_class_under(mIVPhonic, "Undefined", cExpr);
|
35
|
+
rb_define_class_under(mIVPhonic, "TrueLiteral", cExpr);
|
36
|
+
rb_define_class_under(mIVPhonic, "FalseLiteral", cExpr);
|
37
|
+
rb_define_class_under(mIVPhonic, "ConditionalExpression", cExpr);
|
38
|
+
rb_define_class_under(mIVPhonic, "PostfixExpression", cExpr);
|
39
|
+
rb_define_class_under(mIVPhonic, "BinaryOperation", cExpr);
|
40
|
+
rb_define_class_under(mIVPhonic, "UnaryOperation", cExpr);
|
41
|
+
rb_define_class_under(mIVPhonic, "Assignment", cExpr);
|
42
|
+
rb_define_class_under(mIVPhonic, "FunctionCall", cExpr);
|
43
|
+
rb_define_class_under(mIVPhonic, "ConstructorCall", cExpr);
|
44
|
+
rb_define_class_under(mIVPhonic, "IndexAccess", cExpr);
|
45
|
+
rb_define_class_under(mIVPhonic, "IdentifierAccess", cExpr);
|
46
|
+
|
47
|
+
// Statements
|
48
|
+
rb_define_class_under(mIVPhonic, "Block", cStmt);
|
49
|
+
rb_define_class_under(mIVPhonic, "VariableStatement", cStmt);
|
50
|
+
rb_define_class_under(mIVPhonic, "IfStatement", cStmt);
|
51
|
+
rb_define_class_under(mIVPhonic, "DoWhileStatement", cStmt);
|
52
|
+
rb_define_class_under(mIVPhonic, "WhileStatement", cStmt);
|
53
|
+
rb_define_class_under(mIVPhonic, "ForInStatement", cStmt);
|
54
|
+
rb_define_class_under(mIVPhonic, "ForStatement", cStmt);
|
55
|
+
rb_define_class_under(mIVPhonic, "ExpressionStatement", cStmt);
|
56
|
+
rb_define_class_under(mIVPhonic, "ContinueStatement", cStmt);
|
57
|
+
rb_define_class_under(mIVPhonic, "ReturnStatement", cStmt);
|
58
|
+
rb_define_class_under(mIVPhonic, "BreakStatement", cStmt);
|
59
|
+
rb_define_class_under(mIVPhonic, "WithStatement", cStmt);
|
60
|
+
rb_define_class_under(mIVPhonic, "SwitchStatement", cStmt);
|
61
|
+
rb_define_class_under(mIVPhonic, "ThrowStatement", cStmt);
|
62
|
+
rb_define_class_under(mIVPhonic, "TryStatement", cStmt);
|
63
|
+
rb_define_class_under(mIVPhonic, "LabelledStatement", cStmt);
|
64
|
+
rb_define_class_under(mIVPhonic, "DebuggerStatement", cStmt);
|
65
|
+
}
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
#undef RBFUNC
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef _IV_PHONIC_RNODE_H_
|
2
|
+
#define _IV_PHONIC_RNODE_H_
|
3
|
+
#include <cstdlib>
|
4
|
+
#include <cstring>
|
5
|
+
#include <tr1/type_traits>
|
6
|
+
#include <ruby.h>
|
7
|
+
#include <ruby/encoding.h>
|
8
|
+
#include <ruby/intern.h>
|
9
|
+
#include <iv/ast.h>
|
10
|
+
#include <iv/static_assert.h>
|
11
|
+
namespace iv {
|
12
|
+
namespace phonic {
|
13
|
+
|
14
|
+
} } // namespace iv::phonic
|
15
|
+
#endif // _IV_PHONIC_RNODE_H_
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#ifndef _IV_PHONIC_RPARSER_H_
|
2
|
+
#define _IV_PHONIC_RPARSER_H_
|
3
|
+
#include <cstdlib>
|
4
|
+
#include <cstring>
|
5
|
+
#include <tr1/type_traits>
|
6
|
+
#include <ruby.h>
|
7
|
+
#include <ruby/encoding.h>
|
8
|
+
#include <ruby/intern.h>
|
9
|
+
#include <iv/parser.h>
|
10
|
+
#include "encoding.h"
|
11
|
+
#include "factory.h"
|
12
|
+
#include "parser.h"
|
13
|
+
#include "creator.h"
|
14
|
+
#include "source.h"
|
15
|
+
namespace iv {
|
16
|
+
namespace phonic {
|
17
|
+
|
18
|
+
static VALUE cParseError;
|
19
|
+
|
20
|
+
class RParser {
|
21
|
+
public:
|
22
|
+
static VALUE Parse(VALUE self, VALUE rb_str) {
|
23
|
+
AstFactory factory;
|
24
|
+
Check_Type(rb_str, T_STRING);
|
25
|
+
VALUE encoded_rb_str = rb_str_encode(rb_str,
|
26
|
+
Encoding::UTF16Encoding(),
|
27
|
+
0,
|
28
|
+
Qnil);
|
29
|
+
const char* str = StringValuePtr(encoded_rb_str);
|
30
|
+
std::size_t len = RSTRING_LEN(encoded_rb_str);
|
31
|
+
UTF16Source source(str, len);
|
32
|
+
Parser parser(&source, &factory);
|
33
|
+
FunctionLiteral* global = parser.ParseProgram();
|
34
|
+
if (!global) {
|
35
|
+
rb_raise(cParseError, "%s", parser.error().c_str());
|
36
|
+
} else {
|
37
|
+
Creator creator;
|
38
|
+
return creator.Result(global);
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
static void Init(VALUE mIVPhonic) {
|
43
|
+
cParseError = rb_define_class_under(mIVPhonic, "ParseError", rb_eStandardError);
|
44
|
+
}
|
45
|
+
};
|
46
|
+
|
47
|
+
} } // namespace iv::phonic
|
48
|
+
#endif // _IV_PHONIC_RPARSER_H_
|
@@ -0,0 +1,146 @@
|
|
1
|
+
#ifndef _IV_PHONIC_SOURCE_H_
|
2
|
+
#define _IV_PHONIC_SOURCE_H_
|
3
|
+
#include <cstddef>
|
4
|
+
#include <cassert>
|
5
|
+
#include <string>
|
6
|
+
#include <tr1/cstdint>
|
7
|
+
#include <ruby.h>
|
8
|
+
#include <iv/source.h>
|
9
|
+
#include <iv/stringpiece.h>
|
10
|
+
#include <iv/ustring.h>
|
11
|
+
#include <iv/none.h>
|
12
|
+
namespace iv {
|
13
|
+
namespace phonic {
|
14
|
+
namespace detail {
|
15
|
+
template<typename T>
|
16
|
+
class FilenameData {
|
17
|
+
public:
|
18
|
+
static const std::string kFilename;
|
19
|
+
};
|
20
|
+
|
21
|
+
template<typename T>
|
22
|
+
const std::string FilenameData<T>::kFilename = "<anonymous>";
|
23
|
+
|
24
|
+
} // namespace iv::phonic::detail
|
25
|
+
|
26
|
+
typedef detail::FilenameData<core::None> FilenameData;
|
27
|
+
|
28
|
+
class AsciiSource : public core::BasicSource {
|
29
|
+
public:
|
30
|
+
static const int kEOS = -1;
|
31
|
+
explicit AsciiSource(const char* str)
|
32
|
+
: source_(str) {
|
33
|
+
}
|
34
|
+
~AsciiSource() { }
|
35
|
+
|
36
|
+
inline uc16 Get(std::size_t pos) const {
|
37
|
+
assert(pos < size());
|
38
|
+
return source_[pos];
|
39
|
+
}
|
40
|
+
inline std::size_t size() const {
|
41
|
+
return source_.size();
|
42
|
+
}
|
43
|
+
inline const std::string& filename() const {
|
44
|
+
return FilenameData::kFilename;
|
45
|
+
}
|
46
|
+
inline core::UStringPiece SubString(
|
47
|
+
std::size_t n, std::size_t len = std::string::npos) const {
|
48
|
+
return core::UStringPiece();
|
49
|
+
}
|
50
|
+
|
51
|
+
private:
|
52
|
+
std::string source_;
|
53
|
+
};
|
54
|
+
|
55
|
+
class UTF16Source : public core::BasicSource {
|
56
|
+
public:
|
57
|
+
static const int kEOS = -1;
|
58
|
+
explicit UTF16Source(const char* str, std::size_t len)
|
59
|
+
: buf_(reinterpret_cast<const uint16_t*>(str)),
|
60
|
+
size_(len / 2) {
|
61
|
+
assert(len % 2 == 0);
|
62
|
+
}
|
63
|
+
~UTF16Source() { }
|
64
|
+
|
65
|
+
inline uc16 Get(std::size_t pos) const {
|
66
|
+
assert(pos < size());
|
67
|
+
return *(buf_ + pos);
|
68
|
+
}
|
69
|
+
inline std::size_t size() const {
|
70
|
+
return size_;
|
71
|
+
}
|
72
|
+
inline const std::string& filename() const {
|
73
|
+
return FilenameData::kFilename;
|
74
|
+
}
|
75
|
+
inline core::UStringPiece SubString(
|
76
|
+
std::size_t n, std::size_t len = std::string::npos) const {
|
77
|
+
return core::UStringPiece();
|
78
|
+
}
|
79
|
+
|
80
|
+
private:
|
81
|
+
const uint16_t* buf_;
|
82
|
+
std::size_t size_;
|
83
|
+
};
|
84
|
+
|
85
|
+
class UTF16LESource : public core::BasicSource {
|
86
|
+
public:
|
87
|
+
static const int kEOS = -1;
|
88
|
+
explicit UTF16LESource(const char* str, std::size_t len)
|
89
|
+
: buf_(str),
|
90
|
+
size_(len / 2) {
|
91
|
+
assert(len % 2 == 0);
|
92
|
+
}
|
93
|
+
~UTF16LESource() { }
|
94
|
+
|
95
|
+
inline uc16 Get(std::size_t pos) const {
|
96
|
+
assert(pos < size());
|
97
|
+
return (*(buf_ + pos*2)) | (*(buf_ + pos*2 + 1) << 8);
|
98
|
+
}
|
99
|
+
inline std::size_t size() const {
|
100
|
+
return size_;
|
101
|
+
}
|
102
|
+
inline const std::string& filename() const {
|
103
|
+
return FilenameData::kFilename;
|
104
|
+
}
|
105
|
+
inline core::UStringPiece SubString(
|
106
|
+
std::size_t n, std::size_t len = std::string::npos) const {
|
107
|
+
return core::UStringPiece();
|
108
|
+
}
|
109
|
+
|
110
|
+
private:
|
111
|
+
const char* buf_;
|
112
|
+
std::size_t size_;
|
113
|
+
};
|
114
|
+
|
115
|
+
class UTF16BESource : public core::BasicSource {
|
116
|
+
public:
|
117
|
+
static const int kEOS = -1;
|
118
|
+
explicit UTF16BESource(const char* str, std::size_t len)
|
119
|
+
: buf_(str),
|
120
|
+
size_(len / 2) {
|
121
|
+
assert(len % 2 == 0);
|
122
|
+
}
|
123
|
+
~UTF16BESource() { }
|
124
|
+
|
125
|
+
inline uc16 Get(std::size_t pos) const {
|
126
|
+
assert(pos < size());
|
127
|
+
return (*(buf_ + pos*2) << 8) | (*(buf_ + pos*2 + 1));
|
128
|
+
}
|
129
|
+
inline std::size_t size() const {
|
130
|
+
return size_;
|
131
|
+
}
|
132
|
+
inline const std::string& filename() const {
|
133
|
+
return FilenameData::kFilename;
|
134
|
+
}
|
135
|
+
inline core::UStringPiece SubString(
|
136
|
+
std::size_t n, std::size_t len = std::string::npos) const {
|
137
|
+
return core::UStringPiece();
|
138
|
+
}
|
139
|
+
|
140
|
+
private:
|
141
|
+
const char* buf_;
|
142
|
+
std::size_t size_;
|
143
|
+
};
|
144
|
+
|
145
|
+
} } // namespace iv::phonic
|
146
|
+
#endif // _IV_PHONIC_SOURCE_H_
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "test/unit"
|
3
|
+
require "iv/phonic"
|
4
|
+
require "pp"
|
5
|
+
|
6
|
+
class TestPhonic < Test::Unit::TestCase
|
7
|
+
def test_phonic
|
8
|
+
assert(Object.const_defined? :IV)
|
9
|
+
assert(IV.const_defined? :Phonic)
|
10
|
+
end
|
11
|
+
def test_parse
|
12
|
+
assert_respond_to(IV::Phonic, :parse)
|
13
|
+
assert_raise(TypeError) {
|
14
|
+
IV::Phonic::parse(100);
|
15
|
+
}
|
16
|
+
assert_raise(TypeError) {
|
17
|
+
IV::Phonic::parse(/TEST/);
|
18
|
+
}
|
19
|
+
assert_raise(TypeError) {
|
20
|
+
IV::Phonic::parse(IV);
|
21
|
+
}
|
22
|
+
assert(IV::Phonic::parse("FILE"))
|
23
|
+
assert(IV::Phonic::parse("T"))
|
24
|
+
assert(IV::Phonic::parse("var test = \"おはようございます\";"))
|
25
|
+
assert_raise(IV::Phonic::ParseError) {
|
26
|
+
IV::Phonic::parse("var test = var;")
|
27
|
+
}
|
28
|
+
assert_nothing_raised {
|
29
|
+
IV::Phonic::parse("var test = /test/;")
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|