lib-ruby-parser (C++ bindings)  4.0.1+ruby-3.1.1
C++ bindings for lib-ruby-parser library
C++ bindings for lib-ruby-parser

Documentation

All classes/methods are defined under lib_ruby_parser namespace. API mostly mirrors Rust version.

Pre-compiled libraries and header files are available on Releases, supported platforms:

  • x86_64-apple-darwin
  • x86_64-unknown-linux-gnu
  • x86_64-pc-windows-msvc

Basic API

// Configure parsing options
/* 1. filename */
/* 2. decoder */
/* 3. token_rewriter */
/* 4. record_tokens */
true);
// Setup input to parse
std::move(input),
std::move(options));
assert_eq(result.tokens.len, 4); // tINT tPLUS tINT EOF
assert_eq(result.comments.len, 0);
assert_eq(result.magic_comments.len, 0);
assert_byte_list(result.input.bytes, "2 + 3");

ParserResult contains the following fields:

  1. Node* ast - potentually nullable AST, tagged enum
  2. TokenList tokens - list of tokens
  3. DiagnosticList diagnostics - list of diagnostics
  4. CommentList comments - list of comments
  5. MagicCommentList magic_comments - list of magic comments
  6. DecodedInput input - decoded input

All node classes fully match node structs of the original Rust implementation. You can check full documentation (nodes module)

Development

  • Clone the repo
  • Set environment variables:
    • TARGET (e.g. export TARGET=x86_64-apple-darwin, no default value)
    • CXX (e.g. g++)
    • BUILD_ENV (debug or release, debug is the default value)
  • run make tests/run to run tests
  • run make libruby_parser_cpp.a (or make libruby_parser_cpp.lib for MSVC) to get a static library
  • run make lib-ruby-parser.hpp to get a header file

Structure

  • Each directory has its own build.mk file that is included my the main Makefile
  • Rust parser with basic C++ bindings is located under ruby-parser-cpp.
  • Actual C++ bindings are located in *.{hpp, cpp} files in the root directory
  • scripts directory contains per-triplet additional configurations for make
  • codegen directory is a Rust micro-library that does code generation
  • benchmark directory contains a set of scripts to compare performance of Rust vs C++ vs Ripper
lib_ruby_parser::ParserResult::comments
CommentList comments
List of comments.
Definition: parser_result.hpp:27
lib_ruby_parser::ByteList::Copied
static ByteList Copied(const char *s, size_t len)
Constructs a ByteList by copying given pointer.
lib_ruby_parser::TokenRewriter
Definition: token_rewriter.hpp:50
lib_ruby_parser::Node::tag
Tag tag
Definition: nodes.hpp:4499
lib_ruby_parser::MaybeDecoder
Definition: decoder.hpp:101
lib_ruby_parser::ParserResult::magic_comments
MagicCommentList magic_comments
List of magic comments.
Definition: parser_result.hpp:30
lib_ruby_parser::ParserResult
Equivalent of lib_ruby_parser::ParserResult
Definition: parser_result.hpp:14
lib_ruby_parser::ParserOptions
Equivalent of lib_ruby_parser::ParserOptions
Definition: parser_options.hpp:13
lib_ruby_parser::TokenList::len
size_t len
Definition: token.hpp:45
lib_ruby_parser::Decoder
Definition: decoder.hpp:92
lib_ruby_parser::ByteList
Definition: bytes.hpp:11
lib_ruby_parser::CommentList::len
size_t len
Definition: comment.hpp:43
lib_ruby_parser::ParserResult::input
DecodedInput input
Definition: parser_result.hpp:42
lib_ruby_parser::DecodedInput::bytes
ByteList bytes
Definition: decoded_input.hpp:16
lib_ruby_parser::parse
ParserResult parse(ByteList input, ParserOptions options)
lib_ruby_parser::MagicCommentList::len
size_t len
Definition: magic_comment.hpp:42
lib_ruby_parser::ParserResult::ast
Node * ast
Final AST, nullable.
Definition: parser_result.hpp:18
lib_ruby_parser::String::Copied
static String Copied(const char *s)
lib_ruby_parser::Node::Tag::SEND
@ SEND
lib_ruby_parser::ParserResult::tokens
TokenList tokens
List of tokens, empty if parser_options.record_tokens was set to false.
Definition: parser_result.hpp:21
lib_ruby_parser::MaybeTokenRewriter
Definition: token_rewriter.hpp:58