Class: Parslet::Expression

### Home Classes Methods

### In Files

- lib/parslet/expression.rb

### Parent

Object

### Namespace

- CLASS Parslet::Expression::Treetop

### Methods

- ::new
- #parse
- #to_parslet
- #transform

### Included Modules

- Parslet

### Files

- README

### Class/Module Index

Quicksearch

- Parslet
- Parslet::Atoms
- Parslet::Atoms::Alternative
- Parslet::Atoms::Base
- Parslet::Atoms::CanFlatten
- Parslet::Atoms::Capture
- Parslet::Atoms::Context
- Parslet::Atoms::DSL
- Parslet::Atoms::Dynamic
- Parslet::Atoms::Entity
- Parslet::Atoms::Lookahead
- Parslet::Atoms::Named
- Parslet::Atoms::Precedence
- Parslet::Atoms::Re
- Parslet::Atoms::Repetition
- Parslet::Atoms::Scope
- Parslet::Atoms::Sequence
- Parslet::Atoms::Str
- Parslet::Cause
- Parslet::ClassMethods
- Parslet::DelayedMatchConstructor
- Parslet::ErrorReporter
- Parslet::ErrorReporter::Deepest
- Parslet::ErrorReporter::Tree
- Parslet::Expression
- Parslet::Expression::Treetop
- Parslet::Expression::Treetop::Parser
- Parslet::Expression::Treetop::Transform
- Parslet::ParseFailed
- Parslet::Parser
- Parslet::Parser::PrettyPrinter
- Parslet::Parser::Visitors
- Parslet::Parser::Visitors::Citrus
- Parslet::Parser::Visitors::Treetop
- Parslet::Pattern
- Parslet::Pattern::SequenceBind
- Parslet::Pattern::SimpleBind
- Parslet::Pattern::SubtreeBind
- Parslet::Scope
- Parslet::Scope::Binding
- Parslet::Scope::NotFound
- Parslet::Slice
- Parslet::Source
- Parslet::Source::LineCache
- Parslet::Source::RangeSearch
- Parslet::Transform
- Parslet::Transform::Context

No matching classes.

# Parslet::Expression

Allows specifying rules as strings using the exact same grammar that treetop does, minus the actions. This is on one hand a good example of a fully fledged parser and on the other hand might even turn out really useful.

This can be viewed as an extension to parslet and might even be hosted in its own gem one fine day.

### Public Class Methods

new(str, opts={}, context=self) click to toggle source

Creates a parslet from a foreign language expression.

Example:

    Parslet::Expression.new("'a' 'b'")

    # File lib/parslet/expression.rb, line 20
    def initialize(str, opts={}, context=self)
      @type = opts[:type] || :treetop
      @exp = str
      @parslet = transform(
        parse(str))
    end

### Public Instance Methods

parse(str) click to toggle source

Parses the string and returns a parse tree.

    # File lib/parslet/expression.rb, line 41
    def parse(str)
      parser = Treetop::Parser.new
      parser.parse(str)
    end

to_parslet() click to toggle source

Turns this expression into a parslet.

    # File lib/parslet/expression.rb, line 48
    def to_parslet
      @parslet
    end

transform(tree) click to toggle source

Transforms the parse tree into a parslet expression.

    # File lib/parslet/expression.rb, line 29
    def transform(tree)
      transform = Treetop::Transform.new

      # pp tree
      transform.apply(tree)
    rescue
      warn "Could not transform: " + tree.inspect
      raise
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.