iv-phonic 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. data/.autotest +24 -0
  2. data/Manifest.txt +49 -0
  3. data/README.rdoc +32 -0
  4. data/Rakefile +54 -0
  5. data/ext/include/iv/algorithm.h +23 -0
  6. data/ext/include/iv/alloc.h +200 -0
  7. data/ext/include/iv/any.h +71 -0
  8. data/ext/include/iv/ast-factory.h +277 -0
  9. data/ext/include/iv/ast-fwd.h +92 -0
  10. data/ext/include/iv/ast-serializer.h +579 -0
  11. data/ext/include/iv/ast-visitor.h +121 -0
  12. data/ext/include/iv/ast.h +1127 -0
  13. data/ext/include/iv/chars.h +83 -0
  14. data/ext/include/iv/cmdline.h +830 -0
  15. data/ext/include/iv/conversions.h +308 -0
  16. data/ext/include/iv/dtoa.h +20 -0
  17. data/ext/include/iv/enable_if.h +18 -0
  18. data/ext/include/iv/errors.h +15 -0
  19. data/ext/include/iv/fixedcontainer.h +42 -0
  20. data/ext/include/iv/functor.h +29 -0
  21. data/ext/include/iv/lexer.h +1281 -0
  22. data/ext/include/iv/location.h +23 -0
  23. data/ext/include/iv/mt19937.h +175 -0
  24. data/ext/include/iv/noncopyable.h +30 -0
  25. data/ext/include/iv/none.h +10 -0
  26. data/ext/include/iv/parser.h +2150 -0
  27. data/ext/include/iv/source.h +27 -0
  28. data/ext/include/iv/space.h +178 -0
  29. data/ext/include/iv/static_assert.h +30 -0
  30. data/ext/include/iv/stringpiece.h +385 -0
  31. data/ext/include/iv/token.h +311 -0
  32. data/ext/include/iv/ucdata.h +58 -0
  33. data/ext/include/iv/uchar.h +8 -0
  34. data/ext/include/iv/ustring.h +28 -0
  35. data/ext/include/iv/ustringpiece.h +9 -0
  36. data/ext/include/iv/utils.h +83 -0
  37. data/ext/include/iv/xorshift.h +74 -0
  38. data/ext/iv/phonic/ast-fwd.h +21 -0
  39. data/ext/iv/phonic/ast.h +10 -0
  40. data/ext/iv/phonic/creator.h +530 -0
  41. data/ext/iv/phonic/encoding.h +110 -0
  42. data/ext/iv/phonic/extconf.rb +5 -0
  43. data/ext/iv/phonic/factory.h +247 -0
  44. data/ext/iv/phonic/parser.h +12 -0
  45. data/ext/iv/phonic/phonic.cc +69 -0
  46. data/ext/iv/phonic/rnode.h +15 -0
  47. data/ext/iv/phonic/rparser.h +48 -0
  48. data/ext/iv/phonic/source.h +146 -0
  49. data/test/test_iv_phonic.rb +32 -0
  50. metadata +159 -0
data/.autotest ADDED
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ at.add_mapping(/.*\.(?:cc|c)/) do |f, _|
13
+ at.files_matching(/test_.*rb$/)
14
+ end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ end
20
+
21
+ Autotest.add_hook :run_command do |at|
22
+ system "rake clean compile"
23
+ end
24
+ # -*- ruby -*-
data/Manifest.txt ADDED
@@ -0,0 +1,49 @@
1
+ .autotest
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ test/test_iv_phonic.rb
6
+ ext/include/iv/uchar.h
7
+ ext/include/iv/space.h
8
+ ext/include/iv/lexer.h
9
+ ext/include/iv/conversions.h
10
+ ext/include/iv/dtoa.h
11
+ ext/include/iv/mt19937.h
12
+ ext/include/iv/ast.h
13
+ ext/include/iv/noncopyable.h
14
+ ext/include/iv/ast-factory.h
15
+ ext/include/iv/stringpiece.h
16
+ ext/include/iv/static_assert.h
17
+ ext/include/iv/functor.h
18
+ ext/include/iv/parser.h
19
+ ext/include/iv/errors.h
20
+ ext/include/iv/ast-visitor.h
21
+ ext/include/iv/token.h
22
+ ext/include/iv/enable_if.h
23
+ ext/include/iv/source.h
24
+ ext/include/iv/any.h
25
+ ext/include/iv/cmdline.h
26
+ ext/include/iv/xorshift.h
27
+ ext/include/iv/location.h
28
+ ext/include/iv/none.h
29
+ ext/include/iv/algorithm.h
30
+ ext/include/iv/ustring.h
31
+ ext/include/iv/ast-fwd.h
32
+ ext/include/iv/utils.h
33
+ ext/include/iv/alloc.h
34
+ ext/include/iv/chars.h
35
+ ext/include/iv/fixedcontainer.h
36
+ ext/include/iv/ustringpiece.h
37
+ ext/include/iv/ast-serializer.h
38
+ ext/include/iv/ucdata.h
39
+ ext/iv/phonic/rnode.h
40
+ ext/iv/phonic/ast.h
41
+ ext/iv/phonic/factory.h
42
+ ext/iv/phonic/parser.h
43
+ ext/iv/phonic/encoding.h
44
+ ext/iv/phonic/source.h
45
+ ext/iv/phonic/phonic.cc
46
+ ext/iv/phonic/rparser.h
47
+ ext/iv/phonic/creator.h
48
+ ext/iv/phonic/ast-fwd.h
49
+ ext/iv/phonic/extconf.rb
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ == iv / phonic
2
+
3
+ https://github.com/Constellation/iv/tree/master/src/phonic/
4
+
5
+ == DESCRIPTION
6
+
7
+ phonic is ruby binding for iv AST API
8
+
9
+ == MILESTONE
10
+
11
+ + parser - done
12
+ + specialized AST - working
13
+
14
+ == TARGET
15
+
16
+ Aims at flexible Ast Access API binding of ECMAScript 5th
17
+
18
+ == INSTALL
19
+
20
+ gem install iv-phonic
21
+
22
+ == USAGE
23
+
24
+ require 'iv/phonic'
25
+ require 'pp'
26
+ pp IV::Phonic::parse(<<-EOS)
27
+ var i = "OK";
28
+ function test() {
29
+ alert("OK");
30
+ }
31
+ EOS
32
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'rake/extensiontask'
5
+ require 'hoe'
6
+ require 'pp'
7
+ $root = File.dirname(__FILE__)
8
+
9
+ directory "ext/include/iv"
10
+
11
+ HOE = Hoe.spec 'iv-phonic' do
12
+ developer('Constellation', 'utatane.tea@gmail.com')
13
+ self.version = '0.0.1'
14
+ self.readme_file = 'README.rdoc'
15
+ self.history_file = 'ChangeLog'
16
+ self.extra_rdoc_files = FileList['*.rdoc']
17
+ self.extra_dev_deps << ['rake-compiler', '>= 0']
18
+ self.spec_extras = {
19
+ :extensions => ['ext/iv/phonic/extconf.rb']
20
+ }
21
+ self.summary = "iv / phonic : ECMAScript AST"
22
+ self.require_ruby_version '>= 1.9.1'
23
+ end
24
+
25
+ Rake::ExtensionTask.new('phonic', HOE.spec) do |ext|
26
+ ext.lib_dir = File.join('lib', 'iv', 'phonic')
27
+ ext.ext_dir = File.join('ext', 'iv', 'phonic')
28
+ ext.config_options << "--with-iv-include=#{File.join($root, 'ext', 'include')}"
29
+ end
30
+
31
+ Rake::Task[:test].prerequisites << :compile
32
+
33
+ task :checkout => ["ext/include/iv"] do |t|
34
+ list = [
35
+ ".autotest",
36
+ "Manifest.txt",
37
+ "README.rdoc",
38
+ "Rakefile",
39
+ "test/test_iv_phonic.rb"
40
+ ]
41
+ Dir.glob("../*.h") do |f|
42
+ path = File.join("ext", "include", "iv", File.basename(f))
43
+ list << path
44
+ cp f, File.expand_path(path)
45
+ end
46
+ Dir.glob("ext/iv/phonic/*.*") do |f|
47
+ list << f
48
+ end
49
+ File.open("Manifest.txt", "w") do |f|
50
+ f.write(list.join("\n"))
51
+ end
52
+ end
53
+
54
+ # vim: syntax=ruby
@@ -0,0 +1,23 @@
1
+ #ifndef _IV_ALGORITHM_H
2
+ #define _IV_ALGORITHM_H_
3
+ namespace iv {
4
+ namespace algorithm {
5
+ template<class InputIterator,
6
+ class OutputIterator,
7
+ class UnaryFunction,
8
+ class Predicate>
9
+ OutputIterator transform_if(InputIterator first,
10
+ InputIterator last,
11
+ OutputIterator result,
12
+ UnaryFunction f,
13
+ Predicate pred) {
14
+ while (first != last) {
15
+ if (pred(*first)) {
16
+ *result++ = f(*first);
17
+ }
18
+ ++first;
19
+ }
20
+ return result;
21
+ }
22
+ } } // namespace iv::algorithm
23
+ #endif // _IV_ALGORITHM_H_
@@ -0,0 +1,200 @@
1
+ #ifndef _IV_ALLOC_H_
2
+ #define _IV_ALLOC_H_
3
+ #include <cstdlib>
4
+ #include <new>
5
+ #include <vector>
6
+ #include <algorithm>
7
+ #include <string>
8
+ #include <limits>
9
+ #include "uchar.h"
10
+ #include "utils.h"
11
+
12
+ namespace iv {
13
+ namespace core {
14
+
15
+ // Malloc
16
+ // memory allocated from std::malloc
17
+ class Malloced {
18
+ public:
19
+ inline void* operator new(std::size_t size) { return New(size); }
20
+ void operator delete(void* p) { Delete(p); }
21
+ static inline void* New(std::size_t size) {
22
+ void* result = std::malloc(size);
23
+ if (result == NULL) {
24
+ OutOfMemory();
25
+ }
26
+ return result;
27
+ }
28
+ static void OutOfMemory() {
29
+ std::exit(EXIT_FAILURE);
30
+ }
31
+ static inline void Delete(void* p) {
32
+ std::free(p);
33
+ }
34
+ };
35
+
36
+ class Pool {
37
+ public:
38
+ static const unsigned int kPoolSize = Size::KB * 4;
39
+ inline void Initialize(uintptr_t start, Pool* next) {
40
+ start_ = start;
41
+ position_ = start;
42
+ limit_ = start + Pool::kPoolSize + 1;
43
+ next_ = next;
44
+ }
45
+ Pool* Next() const { return next_; }
46
+ inline void* New(uintptr_t size) {
47
+ if ((position_ + size) < limit_) {
48
+ // in this pool
49
+ uintptr_t result = position_;
50
+ position_ += size;
51
+ return reinterpret_cast<void*>(result);
52
+ }
53
+ return NULL;
54
+ }
55
+ private:
56
+ uintptr_t start_;
57
+ uintptr_t position_;
58
+ uintptr_t limit_;
59
+ Pool* next_;
60
+ };
61
+
62
+ class Arena {
63
+ public:
64
+ static const unsigned int kPoolNum = 64;
65
+ static const uintptr_t kAlignment = 8;
66
+ static const unsigned int kArenaSize = (Pool::kPoolSize * kPoolNum) +
67
+ ((Pool::kPoolSize <= kAlignment) ?
68
+ 0 : (Pool::kPoolSize - kAlignment));
69
+ Arena()
70
+ : pools_(),
71
+ result_(),
72
+ now_(&pools_[0]),
73
+ start_(now_),
74
+ next_(NULL) {
75
+ uintptr_t address = reinterpret_cast<uintptr_t>(result_);
76
+
77
+ uintptr_t pool_address = AlignOffset(address, Pool::kPoolSize);
78
+ unsigned int c = 0;
79
+ for (; c < kPoolNum-1; ++c) {
80
+ pools_[c].Initialize(pool_address+c*Pool::kPoolSize, &pools_[c+1]);
81
+ }
82
+ pools_[kPoolNum-1].Initialize(
83
+ pool_address+(kPoolNum-1)*Pool::kPoolSize, NULL);
84
+ }
85
+
86
+ inline void* New(std::size_t raw_size) {
87
+ uintptr_t size = AlignOffset(raw_size, kAlignment);
88
+ void* result = now_->New(size);
89
+ if (result == NULL) {
90
+ now_ = now_->Next();
91
+ while (now_) {
92
+ result = now_->New(size);
93
+ if (result == NULL) {
94
+ now_ = now_->Next();
95
+ } else {
96
+ return result;
97
+ }
98
+ }
99
+ return NULL; // full up
100
+ }
101
+ return result;
102
+ }
103
+ inline void SetNext(Arena* next) { next_ = next; }
104
+ inline Arena* Next() const { return next_; }
105
+ private:
106
+ Pool pools_[kPoolNum];
107
+ char result_[kArenaSize];
108
+ Pool* now_;
109
+ const Pool* start_;
110
+ Arena* next_;
111
+ };
112
+
113
+ template<std::size_t N>
114
+ class Space {
115
+ public:
116
+ Space()
117
+ : arena_(&init_arenas_[0]),
118
+ start_malloced_(NULL),
119
+ malloced_() {
120
+ unsigned int c = 0;
121
+ for (; c < kInitArenas-1; ++c) {
122
+ init_arenas_[c].SetNext(&init_arenas_[c+1]);
123
+ }
124
+ }
125
+ virtual ~Space() {
126
+ if (start_malloced_) {
127
+ Arena *now = start_malloced_, *next = start_malloced_;
128
+ while (now) {
129
+ next = now->Next();
130
+ delete now;
131
+ now = next;
132
+ }
133
+ }
134
+ std::for_each(malloced_.begin(), malloced_.end(), &Malloced::Delete);
135
+ }
136
+ inline void* New(std::size_t size) {
137
+ if ((size - 1) < kThreshold) {
138
+ // small memory allocator
139
+ void* result = arena_->New(size);
140
+ if (result == NULL) {
141
+ Arena *arena = arena_->Next();
142
+ if (arena) {
143
+ arena_ = arena;
144
+ } else {
145
+ NewArena();
146
+ if (!start_malloced_) {
147
+ start_malloced_ = arena_;
148
+ }
149
+ }
150
+ result = arena_->New(size);
151
+ if (result == NULL) {
152
+ Malloced::OutOfMemory();
153
+ }
154
+ }
155
+ return result;
156
+ } else {
157
+ // malloc as memory allocator
158
+ void* result = Malloced::New(size);
159
+ malloced_.push_back(result);
160
+ return result;
161
+ }
162
+ }
163
+
164
+ virtual inline void Clear() {
165
+ arena_ = init_arenas_;
166
+ std::for_each(malloced_.begin(), malloced_.end(), &Malloced::Delete);
167
+ malloced_.clear();
168
+ }
169
+
170
+ private:
171
+ static const std::size_t kThreshold = 256;
172
+ static const std::size_t kInitArenas = N;
173
+
174
+ inline Arena* NewArena() {
175
+ Arena* arena = NULL;
176
+ try {
177
+ arena = new Arena();
178
+ if (arena == NULL) {
179
+ Malloced::OutOfMemory();
180
+ return arena;
181
+ }
182
+ } catch(const std::bad_alloc&) {
183
+ Malloced::OutOfMemory();
184
+ return arena;
185
+ }
186
+ arena_->SetNext(arena);
187
+ arena_ = arena;
188
+ return arena;
189
+ }
190
+ inline const std::vector<void*>& malloced() const {
191
+ return malloced_;
192
+ }
193
+ Arena init_arenas_[kInitArenas];
194
+ Arena* arena_;
195
+ Arena* start_malloced_;
196
+ std::vector<void*>malloced_;
197
+ };
198
+
199
+ } } // namespace iv::core
200
+ #endif // _IV_ALLOC_H_
@@ -0,0 +1,71 @@
1
+ #ifndef _IV_ANY_H_
2
+ #define _IV_ANY_H_
3
+ #include <algorithm>
4
+ namespace iv {
5
+ namespace core {
6
+
7
+ class Any {
8
+ public:
9
+ Any() : ptr_(NULL) { }
10
+
11
+ // This is Wrapper Class,
12
+ // so we cannot set constructor as explicit.
13
+ template<typename T>
14
+ Any(const T & val) : ptr_(new Holder<T>(val)) { } // NOLINT
15
+
16
+ template<typename T>
17
+ Any(T* val) : ptr_(new Holder<T*>(val)) { } // NOLINT
18
+
19
+ Any(const Any & other)
20
+ : ptr_(other.ptr_ ? other.ptr_->Clone() : NULL) { }
21
+
22
+ ~Any() {
23
+ delete ptr_;
24
+ }
25
+
26
+ template<typename T>
27
+ T* As() {
28
+ return &(static_cast<Holder<T> *>(ptr_)->val_);
29
+ }
30
+
31
+ void Swap(Any& rhs) throw() {
32
+ using std::swap;
33
+ swap(ptr_, rhs.ptr_);
34
+ }
35
+
36
+ friend void swap(Any& rhs, Any& lhs) {
37
+ return rhs.Swap(lhs);
38
+ }
39
+
40
+ template<typename T>
41
+ Any& operator=(const T & rhs) {
42
+ Any(rhs).Swap(*this); // NOLINT
43
+ return *this;
44
+ }
45
+
46
+ Any& operator=(Any & rhs) {
47
+ rhs.Swap(*this);
48
+ return *this;
49
+ }
50
+
51
+ private:
52
+ class Placeholder {
53
+ public:
54
+ virtual ~Placeholder() { }
55
+ virtual Placeholder* Clone() const = 0;
56
+ };
57
+
58
+ template<typename T>
59
+ class Holder : public Placeholder {
60
+ public:
61
+ explicit Holder(const T& val) : val_(val) { }
62
+ Placeholder* Clone() const {
63
+ return new Holder<T>(val_);
64
+ }
65
+ T val_;
66
+ };
67
+ Placeholder* ptr_;
68
+ };
69
+
70
+ } } // namespace iv::core
71
+ #endif // _IV_ANY_H_