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

All structs, enums and functions are prefixed with LIB_RUBY_PARSER_. API mostly mirrors Rust version.

Documentation

Pre-compiled library and header file are available on Releases, supported platforms:

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

API

char *input = "2 + 2";
input,
strlen(input));
.decoder = {.decoder = {.f = NULL}},
.record_tokens = false,
.token_rewriter = {.token_rewriter = {.f = NULL}}};
LIB_RUBY_PARSER_ParserResult LIB_RUBY_PARSER_parse(LIB_RUBY_PARSER_ByteList input, LIB_RUBY_PARSER_ParserOptions options)
LIB_RUBY_PARSER_ByteList LIB_RUBY_PARSER_new_bytes_from_cstr(const char *s, size_t len)
LIB_RUBY_PARSER_String LIB_RUBY_PARSER_new_string_from_cstr(const char *s)
Definition: bytes.h:17
Equivalent of lib_ruby_parser::ParserOptions
Definition: parser_options.h:18
LIB_RUBY_PARSER_String buffer_name
Definition: parser_options.h:19
Parser output.
Definition: parser_result.h:18

Parser input:

  • LIB_RUBY_PARSER_ByteList input - source code you want to parse
  • LIB_RUBY_PARSER_ParserOptions options - options of parsing:
    • buffer_name - name of your source code
    • decoder - decoder that is used if there's a magic comment with non-UTF-8 encoding
    • record_token specifies whether tokens should be recorded during parsing
    • token_rewriter - token rewriter, optional, called for each token after lexing but before shifting.

Parser output:

Development

  • Clone the repo
  • Set environment variables:
    • TARGET (e.g. export TARGET=x86_64-apple-darwin, no default value)
    • CC (e.g. gcc)
    • BUILD_ENV (debug or release, debug is the default value)
  • run make tests/run to run tests
  • run make libruby_parser_c.a (or make libruby_parser_c.lib for MSVC) to get a static library
  • run make lib-ruby-parser.h 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-c.
  • Actual C bindings are located in *.{h, c} 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