Sha256: 9b736e6b347e2d5f162ce0abc41dd1d1c8770a03285e449c42f0a8b6c209e464

Contents?: true

Size: 938 Bytes

Versions: 19

Compression:

Stored size: 938 Bytes

Contents

# A small example on how to parse common types of comments. The example
# started out with parser code from Stephen Waits. 

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

require 'pp'
require 'parslet'
require 'parslet/convenience'

class ALanguage < Parslet::Parser
  root(:lines)
  
  rule(:lines) { line.repeat }
  rule(:line) { spaces >> expression.repeat >> newline }
  rule(:newline) { str("\n") >> str("\r").maybe }
  
  rule(:expression) { (str('a').as(:a) >> spaces).as(:exp) }
  
  rule(:spaces) { space.repeat }
  rule(:space) { multiline_comment | line_comment | str(' ') }
  
  rule(:line_comment) { (str('//') >> (newline.absent? >> any).repeat).as(:line) }
  rule(:multiline_comment) { (str('/*') >> (str('*/').absent? >> any).repeat >> str('*/')).as(:multi) }
end

code = %q(
  a
  // line comment
  a a a // line comment
  a /* inline comment */ a 
  /* multiline
  comment */
)

pp ALanguage.new.parse_with_debug(code)

Version data entries

19 entries across 19 versions & 3 rubygems

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