Sha256: e31c10315cf407bc8703d306f23505d8b21f4878ddc110e75c655cab1184ebb4

Contents?: true

Size: 1.11 KB

Versions: 15

Compression:

Stored size: 1.11 KB

Contents

$:.unshift File.dirname(__FILE__) + "/../lib"

require 'pp'
require "parslet"

# Demonstrates modular parsers, split out over many classes. Please look at
# ip_address.rb as well.

module ALanguage
  include Parslet
    
  # Parslet rules are really a special kind of method. Mix them into your
  # classes!
  rule(:a_language) { str('aaa') }
end

# Parslet parsers are parslet atoms as well. Create an instance and chain them
# to your other rules. 
#
class BLanguage < Parslet::Parser
  root :blang
  
  rule(:blang) { str('bbb') }
end

# Parslet atoms are really Ruby values, pass them around. 
c_language = Parslet.str('ccc')
  
class Language < Parslet::Parser
  def initialize(c_language)
    @c_language = c_language
    super()
  end
  
  root :root

  include ALanguage
  
  rule(:root) { str('a(') >> a_language >> str(')') >> space |
                str('b(') >> BLanguage.new >> str(')') >> space | 
                str('c(') >> @c_language >> str(')') >> space }
  rule(:space) { str(' ').maybe }
end

Language.new(c_language).parse('a(aaa)')
Language.new(c_language).parse('b(bbb)')
Language.new(c_language).parse('c(ccc)')

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
parslet-2.0.0 example/modularity.rb
parslet-1.8.2 example/modularity.rb
parslet-1.8.1 example/modularity.rb
parslet-1.8.0 example/modularity.rb
swift-pyrite-0.1.1 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/example/modularity.rb
swift-pyrite-0.1.0 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/example/modularity.rb
parslet-1.7.1 example/modularity.rb
parslet-1.7.0 example/modularity.rb
parslet-1.6.2 example/modularity.rb
parslet-1.6.1 example/modularity.rb
parslet-1.6.0 example/modularity.rb
parslet-1.5.0 example/modularity.rb
ghazel-parslet-1.4.0.2 example/modularity.rb
ghazel-parslet-1.4.0.1 example/modularity.rb
parslet-1.4.0 example/modularity.rb