ddfreyne/d-parse

View on GitHub
samples/parse-readme

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'd-parse'

module Grammar
  extend DParse::DSL

  DIGIT = char_in('0'..'9')

  ROOT =
    seq(
      intersperse(
        repeat(DIGIT).capture.map { |d| d.to_i(10) },
        char(',').ignore,
      ).compact,
      eof,
    ).first
end

res = Grammar::ROOT.apply('1,2,100,582048,07,09')
case res
when DParse::Success
  p res.data
when DParse::Failure
  warn res.pretty_message
  exit 1
end