Skip to content

Commit

Permalink
Merge pull request #1 from yui-knk/import_ruby_3_3
Browse files Browse the repository at this point in the history
Import ruby
  • Loading branch information
yui-knk authored Dec 30, 2023
2 parents a073bc0 + 6686577 commit bb4e693
Show file tree
Hide file tree
Showing 32 changed files with 24,085 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: test

on:
push:
pull_request:

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
ruby: ['3.3']
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle install
- run: bundle exec rake build:parser
- run: bundle exec rake compile
- run: nm --undefined-only --extern-only lib/ruby-parser.so
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/ext/ruby-parser/ruby/parse.c
/ext/ruby-parser/ruby/parse.h
/lib/*.bundle
/tmp
/Gemfile.lock
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

gem "lrama", "0.6.0"
gem "rake-compiler", "~> 1.0"
8 changes: 8 additions & 0 deletions LEGAL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# LEGAL NOTICE INFORMATION

All the files in this distribution are covered under the MIT License except some files
mentioned below.

## Same with Ruby

Files under "ext/ruby-parser/ruby/" are licensed same with Ruby. See https://github.com/ruby/ruby/blob/master/COPYING for more information.
21 changes: 21 additions & 0 deletions MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Yuichiro Kaneko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Set up

```shell
$ bundle install
$ bundle exec rake build:parser
$ bundle exec rake compile
```

## Copy files from ruby/ruby

```shell
$ bundle exec rake 'setup:copy_files[path_to_ruby_dir]'
```

## License

See LEGAL.md file.
82 changes: 82 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require "fileutils"
require "rake/extensiontask"

COPY_TARGETS = %w[
internal/array.h
internal/compilers.h
internal/imemo.h
internal/parse.h
internal/ruby_parser.h
internal/serial.h
internal/static_assert.h
internal/vm.h
internal.h
node.c
node.h
parser_bits.h
parser_node.h
parser_st.c
parser_st.h
parser_value.h
rubyparser.h
st.c
universal_parser.c
]

namespace "build" do
desc "build parse.c and parse.h from parse.y"
task :parser do
sh "bundle exec lrama -oext/ruby-parser/ruby/parse.c -Hext/ruby-parser/ruby/parse.h ext/ruby-parser/ruby/parse.tmp.y"
end
end

namespace "setup" do
desc "copy files from ruby/ruby"
task :copy_files, [:ruby_dir] do |task, args|
unless (ruby_dir = args[:ruby_dir])
raise "ruby_dir is not specified"
end

dist = File.expand_path("../ext/ruby-parser/ruby", __FILE__)

COPY_TARGETS.each do |target|
FileUtils.cp File.join(ruby_dir, target), File.join(dist, target)
end

# "parse.tmp.y"
id2token_path = File.join(ruby_dir, "tool/id2token.rb")
parse_y_path = File.join(ruby_dir, "parse.y")
parse_tmp_y_path = File.join(dist, "parse.tmp.y")
sh "ruby #{id2token_path} #{parse_y_path} > #{parse_tmp_y_path}"

# "id.h"
generic_erb_path = File.join(ruby_dir, "tool/generic_erb.rb")
id_h_tmpl_path = File.join(ruby_dir, "template/id.h.tmpl")
id_h_path = File.join(dist, "id.h")
sh "ruby #{generic_erb_path} --output=#{id_h_path} #{id_h_tmpl_path}"

# "probes.h"
probes_h_path = File.join(dist, "probes.h")
File.open(probes_h_path, "w+") do |f|
f << <<~SRC
#define RUBY_DTRACE_PARSE_BEGIN_ENABLED() (0)
#define RUBY_DTRACE_PARSE_BEGIN(arg0, arg1) (void)(arg0), (void)(arg1);
#define RUBY_DTRACE_PARSE_END_ENABLED() (0)
#define RUBY_DTRACE_PARSE_END(arg0, arg1) (void)(arg0), (void)(arg1);
SRC
end

# "node_name.inc"
node_name_path = File.join(ruby_dir, "tool/node_name.rb")
rubyparser_h_path = File.join(ruby_dir, "rubyparser.h")
node_name_inc_path = File.join(dist, "node_name.inc")
sh "ruby -n #{node_name_path} < #{rubyparser_h_path} > #{node_name_inc_path}"

# TODO: Generate "lex.c"
# At the moment, copy on local
end
end

Rake::ExtensionTask.new("ruby-parser") do |ext|
ext.ext_dir = "ext/ruby-parser/ruby"
end
17 changes: 17 additions & 0 deletions ext/ruby-parser/ruby/extconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'mkmf'

# lex.c is included from parse.c then not include it to $objs
$objs = %w[
node
parse
parser_st
].map do |o|
o + ".#{$OBJEXT}"
end

append_cppflags("-DUNIVERSAL_PARSER=1")

$INCFLAGS << " -I" << File.expand_path("../../../../", __FILE__)
$INCFLAGS << " -I" << File.expand_path("../ruby", __FILE__)

create_makefile('ruby-parser')
Loading

0 comments on commit bb4e693

Please sign in to comment.