c_lexer 2.5.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3b6fdc822346ef189eb54aee2418b66806d4a1716483326a7f33be3953ef18b6
4
+ data.tar.gz: 74921a8413c03e7c5b388f8f7b1ea54e1e7d7579c613a608fe2b00f9f4355107
5
+ SHA512:
6
+ metadata.gz: c4f17e98515d2f684b1dbde7427f96c7a52020a562b0d4c5d630066f1e0dc7c76bac279cdd8efc3d20da9b9593e0ec75aae8c40e7ae3f31f54dbb4177e6d3234
7
+ data.tar.gz: 8629e89c200a663e916b127338b8aeac648fc61b883e03be7a5f80b7622bdd5fb60429365cff579d837879d5944c2816d91d5a7a589b07a318b69b3a66716fb0
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ ext/lexer/lexer.c
13
+ lib/lexer.bundle
14
+ lib/lexer.so
15
+
16
+ test.rb
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "parser"]
2
+ path = vendor/parser
3
+ url = git://github.com/whitequark/parser.git
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ - 2.4.4
6
+ - 2.3.7
7
+ before_install: gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| 'https://github.com/#{repo_name}' }
4
+
5
+ # Specify your gem's dependencies in c_lexer.gemspec
6
+ gemspec
7
+
8
+ gem 'pry'
9
+ gem 'parser', path: 'vendor/parser'
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ c_lexer (2.5.1.0.pre1)
5
+ ast (~> 2.4.0)
6
+ parser (= 2.5.1.0)
7
+
8
+ PATH
9
+ remote: vendor/parser
10
+ specs:
11
+ parser (2.5.1.0)
12
+ ast (~> 2.4.0)
13
+
14
+ GEM
15
+ remote: https://rubygems.org/
16
+ specs:
17
+ ast (2.4.0)
18
+ cliver (0.3.2)
19
+ coderay (1.1.2)
20
+ docile (1.1.5)
21
+ json (2.1.0)
22
+ method_source (0.9.0)
23
+ minitest (5.11.3)
24
+ pry (0.11.3)
25
+ coderay (~> 1.1.0)
26
+ method_source (~> 0.9.0)
27
+ racc (1.4.14)
28
+ rake (10.5.0)
29
+ rake-compiler (0.9.9)
30
+ rake
31
+ simplecov (0.15.1)
32
+ docile (~> 1.1.0)
33
+ json (>= 1.8, < 3)
34
+ simplecov-html (~> 0.10.0)
35
+ simplecov-html (0.10.2)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (~> 1.16)
42
+ c_lexer!
43
+ cliver (~> 0.3.2)
44
+ minitest (~> 5.10)
45
+ parser!
46
+ pry
47
+ racc (= 1.4.14)
48
+ rake (~> 10.0)
49
+ rake-compiler (~> 0.9)
50
+ simplecov (~> 0.15.1)
51
+
52
+ BUNDLED WITH
53
+ 1.16.1
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # CLexer
2
+
3
+ [![Build Status](https://travis-ci.org/opal/c_lexer.svg?branch=master)](https://travis-ci.org/opal/c_lexer)
4
+
5
+ A C port of the whitequark/parser's lexer.
6
+
7
+ Initially based on [whitequark/parser#248](https://github.com/whitequark/parser/pull/248) (thanks [@alexdowad](https://github.com/alexdowad)).
8
+
9
+ This project is still under development.
10
+
11
+ ## Installation
12
+
13
+ ```
14
+ $ git submodule update --init
15
+ $ bundle install
16
+ $ rake
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Simply require `c_lexer` after `parser` in your code.
22
+ It should print `Using C lexer`.
23
+
24
+ ## Contributing
25
+
26
+ Bug reports and pull requests are welcome on GitHub at https://github.com/opal/c_lexer.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rake/extensiontask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.ruby_opts = ["-rpatch_helper"]
7
+ t.libs = %w(lib/ test/ vendor/parser/test/ vendor/parser/lib/)
8
+ t.test_files = %w(vendor/parser/test/test_lexer.rb vendor/parser/test/test_parser.rb)
9
+ t.warning = false
10
+ end
11
+
12
+ Rake::ExtensionTask.new('lexer')
13
+
14
+ namespace :ruby_parser do
15
+ desc "'rake generate' in the Ruby Parser"
16
+ task :generate do
17
+ sh 'cd vendor/parser && rake generate'
18
+ end
19
+
20
+ desc "'rake clean' in the Ruby Parser"
21
+ task :clean do
22
+ sh 'cd vendor/parser && rake clean'
23
+ end
24
+ end
25
+
26
+ namespace :c_lexer do
27
+ desc 'Generate lexer.c from lexer.rl'
28
+ task :generate do
29
+ source = 'ext/lexer/lexer.rl'
30
+ target = 'ext/lexer/lexer.c'
31
+
32
+ sh "ragel -F1 #{source} -o #{target}"
33
+
34
+ # Ragel likes to use int variables where a #define would do
35
+ src = File.read(target)
36
+ src.gsub!(/^static const int (\w+) = (\d+);/, '#define \1 \2')
37
+ File.open(target, 'w') { |f| f.write(src) }
38
+ end
39
+ end
40
+
41
+ task :clean do
42
+ sh 'rm -rf tmp'
43
+ sh 'rm -f lib/lexer.*'
44
+ sh 'rm -f ext/lexer/lexer.c'
45
+ sh 'cd vendor/parser && rake clean'
46
+ end
47
+
48
+ task generate: ['ruby_parser:generate', 'c_lexer:generate']
49
+ task test: [:generate, :compile]
50
+ task build: :generate
51
+ task default: :test
data/bin/c-ruby-parse ADDED
@@ -0,0 +1,10 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift(File.expand_path('../../vendor/parser/lib', __FILE__))
5
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
6
+ require 'c_lexer'
7
+ require 'parser/runner/ruby_parse'
8
+
9
+ Parser::Runner::RubyParse.go(ARGV)
10
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'c_lexer'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/c_lexer.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'c_lexer/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'c_lexer'
7
+ spec.version = CLexer::VERSION
8
+ spec.authors = ['Ilya Bylich']
9
+ spec.email = ['ibylich@gmail.com']
10
+
11
+ spec.description = %q{A Ruby parser written in C}
12
+ spec.summary = spec.description
13
+ spec.homepage = 'https://github.com/iliabylich/c_lexer'
14
+
15
+ spec.files = `git ls-files`.split.reject do |f|
16
+ f.match(%r{^(test|spec|features|vendor)/})
17
+ end + ['ext/lexer/lexer.c']
18
+
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+ spec.extensions = ['ext/lexer/extconf.rb']
23
+
24
+ spec.add_dependency 'ast', '~> 2.4.0'
25
+ spec.add_dependency 'parser', '= 2.5.1.0'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.16'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rake-compiler', '~> 0.9'
30
+
31
+ # Parser dev dependencies
32
+ spec.add_development_dependency 'minitest', '~> 5.10'
33
+ spec.add_development_dependency 'simplecov', '~> 0.15.1'
34
+ spec.add_development_dependency 'racc', '= 1.4.14'
35
+ spec.add_development_dependency 'cliver', '~> 0.3.2'
36
+ end
@@ -0,0 +1,47 @@
1
+ static VALUE lexer_push_cmdarg_state(VALUE self, VALUE bit)
2
+ {
3
+ INIT_LEXER_STATE(self, state);
4
+ int bitval = RTEST(bit) ? 1 : 0;
5
+ stack_state_push(&state->cmdarg, bitval);
6
+ return Qnil;
7
+ }
8
+
9
+ static VALUE lexer_pop_cmdarg_state(VALUE self)
10
+ {
11
+ INIT_LEXER_STATE(self, state);
12
+ stack_state_pop(&state->cmdarg);
13
+ return Qnil;
14
+ }
15
+
16
+ static VALUE lexer_lexpop_cmdarg_state(VALUE self)
17
+ {
18
+ INIT_LEXER_STATE(self, state);
19
+ stack_state_lexpop(&state->cmdarg);
20
+ return Qnil;
21
+ }
22
+
23
+ static VALUE lexer_clear_cmdarg_state(VALUE self)
24
+ {
25
+ INIT_LEXER_STATE(self, state);
26
+ stack_state_clear(&state->cmdarg);
27
+ return Qnil;
28
+ }
29
+
30
+ static VALUE lexer_cmdarg_state_empty_p(VALUE self)
31
+ {
32
+ INIT_LEXER_STATE(self, state);
33
+ return stack_state_empty_p(&state->cmdarg);
34
+ }
35
+
36
+ static VALUE lexer_cmdarg_state_value(VALUE self)
37
+ {
38
+ INIT_LEXER_STATE(self, state);
39
+ return INT2NUM(stack_state_value(&state->cmdarg));
40
+ }
41
+
42
+ static VALUE lexer_set_cmdarg_state(VALUE self, VALUE value)
43
+ {
44
+ INIT_LEXER_STATE(self, state);
45
+ stack_set_value(&state->cmdarg, NUM2INT(value));
46
+ return Qtrue;
47
+ }
data/ext/lexer/cond.h ADDED
@@ -0,0 +1,47 @@
1
+ static VALUE lexer_push_cond_state(VALUE self, VALUE bit)
2
+ {
3
+ INIT_LEXER_STATE(self, state);
4
+ int bitval = RTEST(bit) ? 1 : 0;
5
+ stack_state_push(&state->cond, bitval);
6
+ return Qnil;
7
+ }
8
+
9
+ static VALUE lexer_pop_cond_state(VALUE self)
10
+ {
11
+ INIT_LEXER_STATE(self, state);
12
+ stack_state_pop(&state->cond);
13
+ return Qnil;
14
+ }
15
+
16
+ static VALUE lexer_lexpop_cond_state(VALUE self)
17
+ {
18
+ INIT_LEXER_STATE(self, state);
19
+ stack_state_lexpop(&state->cond);
20
+ return Qnil;
21
+ }
22
+
23
+ static VALUE lexer_clear_cond_state(VALUE self)
24
+ {
25
+ INIT_LEXER_STATE(self, state);
26
+ stack_state_clear(&state->cond);
27
+ return Qnil;
28
+ }
29
+
30
+ static VALUE lexer_cond_state_empty_p(VALUE self)
31
+ {
32
+ INIT_LEXER_STATE(self, state);
33
+ return stack_state_empty_p(&state->cond);
34
+ }
35
+
36
+ static VALUE lexer_cond_state_value(VALUE self)
37
+ {
38
+ INIT_LEXER_STATE(self, state);
39
+ return INT2NUM(stack_state_value(&state->cond));
40
+ }
41
+
42
+ static VALUE lexer_set_cond_state(VALUE self, VALUE value)
43
+ {
44
+ INIT_LEXER_STATE(self, state);
45
+ stack_set_value(&state->cond, NUM2INT(value));
46
+ return Qtrue;
47
+ }
@@ -0,0 +1,177 @@
1
+ #define try_mapping(str, token) if (strcmp(value_str, str) == 0) { emit_token(state, token, value, start, end); return; }
2
+ #define invalid_mapping rb_raise(rb_eArgError, "Invalid punctuation token: %s", value_str);
3
+
4
+ static void emit_table_PUNCTUATION(lexer_state *state, VALUE value, long start, long end)
5
+ {
6
+ const char *value_str = RSTRING_PTR(value);
7
+
8
+ try_mapping("=", tEQL);
9
+ try_mapping("&", tAMPER2);
10
+ try_mapping("|", tPIPE);
11
+ try_mapping("!", tBANG);
12
+ try_mapping("^", tCARET);
13
+ try_mapping("+", tPLUS);
14
+ try_mapping("-", tMINUS);
15
+ try_mapping("*", tSTAR2);
16
+ try_mapping("/", tDIVIDE);
17
+ try_mapping("%", tPERCENT);
18
+ try_mapping("~", tTILDE);
19
+ try_mapping(",", tCOMMA);
20
+ try_mapping(";", tSEMI);
21
+ try_mapping(".", tDOT);
22
+ try_mapping("..", tDOT2);
23
+ try_mapping("...", tDOT3);
24
+ try_mapping("[", tLBRACK2);
25
+ try_mapping("]", tRBRACK);
26
+ try_mapping("(", tLPAREN2);
27
+ try_mapping(")", tRPAREN);
28
+ try_mapping("?", tEH);
29
+ try_mapping(":", tCOLON);
30
+ try_mapping("&&", tANDOP);
31
+ try_mapping("||", tOROP);
32
+ try_mapping("-@", tUMINUS);
33
+ try_mapping("+@", tUPLUS);
34
+ try_mapping("~@", tTILDE);
35
+ try_mapping("**", tPOW);
36
+ try_mapping("->", tLAMBDA);
37
+ try_mapping("=~", tMATCH);
38
+ try_mapping("!~", tNMATCH);
39
+ try_mapping("==", tEQ);
40
+ try_mapping("!=", tNEQ);
41
+ try_mapping(">", tGT);
42
+ try_mapping(">>", tRSHFT);
43
+ try_mapping(">=", tGEQ);
44
+ try_mapping("<", tLT);
45
+ try_mapping("<<", tLSHFT);
46
+ try_mapping("<=", tLEQ);
47
+ try_mapping("=>", tASSOC);
48
+ try_mapping("::", tCOLON2);
49
+ try_mapping("===", tEQQ);
50
+ try_mapping("<=>", tCMP);
51
+ try_mapping("[]", tAREF);
52
+ try_mapping("[]=", tASET);
53
+ try_mapping("{", tLCURLY);
54
+ try_mapping("}", tRCURLY);
55
+ try_mapping("`", tBACK_REF2);
56
+ try_mapping("!@", tBANG);
57
+ try_mapping("&.", tANDDOT);
58
+
59
+ invalid_mapping;
60
+ }
61
+
62
+ static void emit_table_PUNCTUATION_BEGIN(lexer_state *state, VALUE value, long start, long end)
63
+ {
64
+ const char *value_str = RSTRING_PTR(value);
65
+
66
+ try_mapping("&", tAMPER);
67
+ try_mapping("*", tSTAR);
68
+ try_mapping("**", tDSTAR);
69
+ try_mapping("+", tUPLUS);
70
+ try_mapping("-", tUMINUS);
71
+ try_mapping("::", tCOLON3);
72
+ try_mapping("(", tLPAREN);
73
+ try_mapping("{", tLBRACE);
74
+ try_mapping("[", tLBRACK);
75
+
76
+ invalid_mapping;
77
+ }
78
+
79
+ static void emit_table_KEYWORDS(lexer_state *state, VALUE value, long start, long end)
80
+ {
81
+ const char *value_str = RSTRING_PTR(value);
82
+
83
+ try_mapping("if", kIF_MOD);
84
+ try_mapping("unless", kUNLESS_MOD);
85
+ try_mapping("while", kWHILE_MOD);
86
+ try_mapping("until", kUNTIL_MOD);
87
+ try_mapping("rescue", kRESCUE_MOD);
88
+ try_mapping("defined?", kDEFINED);
89
+ try_mapping("BEGIN", klBEGIN);
90
+ try_mapping("END", klEND);
91
+
92
+ try_mapping("class", kCLASS);
93
+ try_mapping("module", kMODULE);
94
+ try_mapping("def", kDEF);
95
+ try_mapping("undef", kUNDEF);
96
+ try_mapping("begin", kBEGIN);
97
+ try_mapping("end", kEND);
98
+ try_mapping("then", kTHEN);
99
+ try_mapping("elsif", kELSIF);
100
+ try_mapping("else", kELSE);
101
+ try_mapping("ensure", kENSURE);
102
+ try_mapping("case", kCASE);
103
+ try_mapping("when", kWHEN);
104
+ try_mapping("for", kFOR);
105
+ try_mapping("break", kBREAK);
106
+ try_mapping("next", kNEXT);
107
+ try_mapping("redo", kREDO);
108
+ try_mapping("retry", kRETRY);
109
+ try_mapping("in", kIN);
110
+ try_mapping("do", kDO);
111
+ try_mapping("return", kRETURN);
112
+ try_mapping("yield", kYIELD);
113
+ try_mapping("super", kSUPER);
114
+ try_mapping("self", kSELF);
115
+ try_mapping("nil", kNIL);
116
+ try_mapping("true", kTRUE);
117
+ try_mapping("false", kFALSE);
118
+ try_mapping("and", kAND);
119
+ try_mapping("or", kOR);
120
+ try_mapping("not", kNOT);
121
+ try_mapping("alias", kALIAS);
122
+ try_mapping("__FILE__", k__FILE__);
123
+ try_mapping("__LINE__", k__LINE__);
124
+ try_mapping("__ENCODING__", k__ENCODING__);
125
+
126
+ invalid_mapping;
127
+ }
128
+
129
+ static void emit_table_KEYWORDS_BEGIN(lexer_state *state, VALUE value, long start, long end)
130
+ {
131
+ const char *value_str = RSTRING_PTR(value);
132
+
133
+ try_mapping("if", kIF);
134
+ try_mapping("unless", kUNLESS);
135
+ try_mapping("while", kWHILE);
136
+ try_mapping("until", kUNTIL);
137
+ try_mapping("rescue", kRESCUE);
138
+ try_mapping("defined?", kDEFINED);
139
+ try_mapping("BEGIN", klBEGIN);
140
+ try_mapping("END", klEND);
141
+
142
+ try_mapping("class", kCLASS);
143
+ try_mapping("module", kMODULE);
144
+ try_mapping("def", kDEF);
145
+ try_mapping("undef", kUNDEF);
146
+ try_mapping("begin", kBEGIN);
147
+ try_mapping("end", kEND);
148
+ try_mapping("then", kTHEN);
149
+ try_mapping("elsif", kELSIF);
150
+ try_mapping("else", kELSE);
151
+ try_mapping("ensure", kENSURE);
152
+ try_mapping("case", kCASE);
153
+ try_mapping("when", kWHEN);
154
+ try_mapping("for", kFOR);
155
+ try_mapping("break", kBREAK);
156
+ try_mapping("next", kNEXT);
157
+ try_mapping("redo", kREDO);
158
+ try_mapping("retry", kRETRY);
159
+ try_mapping("in", kIN);
160
+ try_mapping("do", kDO);
161
+ try_mapping("return", kRETURN);
162
+ try_mapping("yield", kYIELD);
163
+ try_mapping("super", kSUPER);
164
+ try_mapping("self", kSELF);
165
+ try_mapping("nil", kNIL);
166
+ try_mapping("true", kTRUE);
167
+ try_mapping("false", kFALSE);
168
+ try_mapping("and", kAND);
169
+ try_mapping("or", kOR);
170
+ try_mapping("not", kNOT);
171
+ try_mapping("alias", kALIAS);
172
+ try_mapping("__FILE__", k__FILE__);
173
+ try_mapping("__LINE__", k__LINE__);
174
+ try_mapping("__ENCODING__", k__ENCODING__);
175
+
176
+ invalid_mapping;
177
+ }