Skip to content
ydahPublic

About

Ruby-native lexer generator for parser authors. Write ordinary Ruby specs, run them at runtime, or generate deterministic Ruby for deployment.

Topics

Resources

Contributing

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

flexr

A lexer generator for Ruby
Write lexer rules once and run them directly or generate a Ruby scanner.

Gem version Gem downloads Ruby 3.4 or newer Runtime CI MIT license

Quick start · Playground · Documentation · Product site


Quick start

Install flexr on Ruby 3.4 or newer.

gem install flexr

Save the rules as lexer.flexr.rb.

require "flexr"

class Lexer < Flexr::Lexer
  emits :INTEGER, :PLUS

  rule(/[ \t\r\n]+/, skip: true)
  rule(/[0-9]+/) { emit :INTEGER, text.to_i }
  rule(/\+/) { emit :PLUS }
end

Run the lexer on a short input.

ruby -r ./lexer.flexr.rb -e 'p Lexer.new("12 + 3").tokens'
[[:INTEGER, 12], [:PLUS, "+"], [:INTEGER, 3]]

Generate a scanner

Generate a Ruby file from the same rules.

flexr lexer.flexr.rb -o lexer.rb

Load lexer.rb in your application with the flexr gem installed. Use --standalone to embed the runtime when you need to distribute a single file that runs without the gem.

Generation reads the specification statically by default. The --eval option executes the Ruby source at build time. Use it only with trusted specifications.

Matching rules

By default, flexr chooses the longest match at each input position. For example, == wins over = even if the = rule comes first. For matches of equal length, higher priority: values win. Definition order breaks ties between rules with the same priority.

Rules support a subset of Ruby regexp syntax designed for DFA matching.

Playground

Run a lexer in the browser playground to inspect its tokens, diagnostics, and match decisions.

Playground scans two separate comments with an absence group, then shows the input that shadows an if rule.

Open the specifications from the demo.

Documentation

  • Calculator tutorial walks through building, checking, and generating a lexer.
  • Documentation map links to how-to guides, including parser integration and migration.
  • Reference covers the DSL, CLI, diagnostics, and API stability.
  • Examples contains runnable lexers and parser integrations.
  • Changelog lists release highlights and upgrade notes.

Contributing

See CONTRIBUTING.md for local checks and RELEASING.md for the release process.

License

MIT. See LICENSE.txt.

About

Ruby-native lexer generator for parser authors. Write ordinary Ruby specs, run them at runtime, or generate deterministic Ruby for deployment.

Topics

Resources

Contributing

Stars

1 star

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages