Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add templating mechanism to automatically generate .c/.h files #2098

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ gem 'tempfile'
gem "rdoc"
gem "fileutils"
gem "raap"
gem "activesupport"

group :libs do
# Libraries required for stdlib test
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ PLATFORMS

DEPENDENCIES
abbrev
activesupport
base64
benchmark-ips
bigdecimal
Expand Down
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ task :confirm_annotation do
sh "git diff --exit-code core stdlib"
end

task :templates do
sh "bundle exec ruby templates/template.rb ext/rbs_extension/constants.h"
sh "bundle exec ruby templates/template.rb ext/rbs_extension/ruby_objs.h"
sh "bundle exec ruby templates/template.rb ext/rbs_extension/constants.c"
sh "bundle exec ruby templates/template.rb ext/rbs_extension/ruby_objs.c"
end

task :compile => "ext/rbs_extension/lexer.c"
Rake::Task[:compile].prereqs.prepend :templates

task :test_doc do
files = Dir.chdir(File.expand_path('..', __FILE__)) do
Expand Down
323 changes: 323 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
nodes:
- name: RBS::AST::Annotation
fields:
- name: string
- name: location
- name: RBS::AST::Comment
fields:
- name: string
- name: location
- name: RBS::AST::Declarations::Class
fields:
- name: name
- name: type_params
- name: super_class
- name: members
- name: annotations
- name: location
- name: comment
- name: RBS::AST::Declarations::Class::Super
fields:
- name: name
- name: args
- name: location
- name: RBS::AST::Declarations::ClassAlias
fields:
- name: new_name
- name: old_name
- name: location
- name: comment
- name: RBS::AST::Declarations::Constant
fields:
- name: name
- name: type
- name: location
- name: comment
- name: RBS::AST::Declarations::Global
fields:
- name: name
- name: type
- name: location
- name: comment
- name: RBS::AST::Declarations::Interface
fields:
- name: name
- name: type_params
- name: members
- name: annotations
- name: location
- name: comment
- name: RBS::AST::Declarations::Module
fields:
- name: name
- name: type_params
- name: self_types
- name: members
- name: annotations
- name: location
- name: comment
- name: RBS::AST::Declarations::Module::Self
fields:
- name: name
- name: args
- name: location
- name: RBS::AST::Declarations::ModuleAlias
fields:
- name: new_name
- name: old_name
- name: location
- name: comment
- name: RBS::AST::Declarations::TypeAlias
fields:
- name: name
- name: type_params
- name: type
- name: annotations
- name: location
- name: comment
- name: RBS::AST::Directives::Use
fields:
- name: clauses
- name: location
- name: RBS::AST::Directives::Use::SingleClause
fields:
- name: type_name
- name: new_name
- name: location
- name: RBS::AST::Directives::Use::WildcardClause
fields:
- name: namespace
- name: location
- name: RBS::AST::Members::Alias
fields:
- name: new_name
- name: old_name
- name: kind
- name: annotations
- name: location
- name: comment
- name: RBS::AST::Members::AttrAccessor
fields:
- name: name
- name: type
- name: ivar_name
- name: kind
- name: annotations
- name: location
- name: comment
- name: visibility
- name: RBS::AST::Members::AttrReader
fields:
- name: name
- name: type
- name: ivar_name
- name: kind
- name: annotations
- name: location
- name: comment
- name: visibility
- name: RBS::AST::Members::AttrWriter
fields:
- name: name
- name: type
- name: ivar_name
- name: kind
- name: annotations
- name: location
- name: comment
- name: visibility
- name: RBS::AST::Members::ClassInstanceVariable
fields:
- name: name
- name: type
- name: location
- name: comment
- name: RBS::AST::Members::ClassVariable
fields:
- name: name
- name: type
- name: location
- name: comment
- name: RBS::AST::Members::Extend
fields:
- name: name
- name: args
- name: annotations
- name: location
- name: comment
- name: RBS::AST::Members::Include
fields:
- name: name
- name: args
- name: annotations
- name: location
- name: comment
- name: RBS::AST::Members::InstanceVariable
fields:
- name: name
- name: type
- name: location
- name: comment
- name: RBS::AST::Members::MethodDefinition
fields:
- name: name
- name: kind
- name: overloads
- name: annotations
- name: location
- name: comment
- name: overloading
- name: visibility
- name: RBS::AST::Members::MethodDefinition::Overload
fields:
- name: annotations
- name: method_type
- name: RBS::AST::Members::Prepend
fields:
- name: name
- name: args
- name: annotations
- name: location
- name: comment
- name: RBS::AST::Members::Private
fields:
- name: location
- name: RBS::AST::Members::Public
fields:
- name: location
- name: RBS::AST::TypeParam
fields:
- name: name
- name: variance
- name: upper_bound
- name: default_type
- name: location
- name: RBS::MethodType
fields:
- name: type_params
- name: type
- name: block
- name: location
- name: RBS::Namespace
fields:
- name: path
- name: absolute
- name: RBS::TypeName
fields:
- name: namespace
- name: name
- name: RBS::Types::Alias
fields:
- name: name
c_name: typename # Temporary name to match existing C code. Will be removed in next PR.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The identifiers used in C don't always match those used in the Ruby API. Where necessary, we used this c_name: field to preserve the C code as it was.

- name: args
- name: location
- name: RBS::Types::Bases::Any
fields:
- name: location
- name: RBS::Types::Bases::Bool
fields:
- name: location
- name: RBS::Types::Bases::Bottom
fields:
- name: location
- name: RBS::Types::Bases::Class
fields:
- name: location
- name: RBS::Types::Bases::Instance
fields:
- name: location
- name: RBS::Types::Bases::Nil
fields:
- name: location
- name: RBS::Types::Bases::Self
fields:
- name: location
- name: RBS::Types::Bases::Top
fields:
- name: location
- name: RBS::Types::Bases::Void
fields:
- name: location
- name: RBS::Types::Block
fields:
- name: type
- name: required
- name: self_type
- name: RBS::Types::ClassInstance
fields:
- name: name
c_name: typename # Temporary name to match existing C code. Will be removed in next PR.
- name: args
c_name: type_args # Temporary name to match existing C code. Will be removed in next PR.
- name: location
- name: RBS::Types::ClassSingleton
fields:
- name: name
c_name: typename # Temporary name to match existing C code. Will be removed in next PR.
- name: location
- name: RBS::Types::Function
fields:
- name: required_positionals
c_name: required_positional_params # Temporary name to match existing C code. Will be removed in next PR.
- name: optional_positionals
c_name: optional_positional_params # Temporary name to match existing C code. Will be removed in next PR.
- name: rest_positionals
c_name: rest_positional_params # Temporary name to match existing C code. Will be removed in next PR.
- name: trailing_positionals
c_name: trailing_positional_params # Temporary name to match existing C code. Will be removed in next PR.
- name: required_keywords
- name: optional_keywords
- name: rest_keywords
- name: return_type
- name: RBS::Types::Function::Param
fields:
- name: type
- name: name
- name: location
- name: RBS::Types::Interface
fields:
- name: name
c_name: typename # Temporary name to match existing C code. Will be removed in next PR.
- name: args
c_name: type_args # Temporary name to match existing C code. Will be removed in next PR.
- name: location
- name: RBS::Types::Intersection
fields:
- name: types
- name: location
- name: RBS::Types::Literal
fields:
- name: literal
- name: location
- name: RBS::Types::Optional
fields:
- name: type
- name: location
- name: RBS::Types::Proc
fields:
- name: type
c_name: function # Temporary name to match existing C code. Will be removed in next PR.
- name: block
- name: location
- name: self_type
- name: RBS::Types::Record
fields:
- name: all_fields
c_name: fields # Temporary name to match existing C code. Will be removed in next PR.
- name: location
- name: RBS::Types::Tuple
fields:
- name: types
- name: location
- name: RBS::Types::Union
fields:
- name: types
- name: location
- name: RBS::Types::UntypedFunction
fields:
- name: return_type
- name: RBS::Types::Variable
fields:
- name: name
- name: location
Loading