Class: Dhaka::Lexer

### Home Classes Methods

### In Files

- lib/dhaka/lexer/lexer.rb

### Parent

LexerSupport::StateMachine

### Methods

- ::new
- #action_for_pattern
- #compile_to_ruby_source_as
- #lex

### Class/Module Index

Quicksearch

- Dhaka
- Dhaka::CompiledLexer
- Dhaka::CompiledParser
- Dhaka::Evaluator
- Dhaka::Grammar
- Dhaka::GrammarSymbol
- Dhaka::Lexeme
- Dhaka::Lexer
- Dhaka::LexerRun
- Dhaka::LexerSpecification
- Dhaka::LexerSupport
- Dhaka::LexerSupport::AcceptAction
- Dhaka::LexerSupport::CheckpointAction
- Dhaka::LexerSupport::DFARun
- Dhaka::LexerSupport::InvalidRegexException
- Dhaka::LexerSupport::LookaheadAcceptAction
- Dhaka::LexerSupport::RegexParser
- Dhaka::LexerSupport::RegexTokenizer
- Dhaka::LexerSupport::State
- Dhaka::LexerSupport::StateMachine
- Dhaka::NoStartProductionsError
- Dhaka::ParseErrorResult
- Dhaka::ParseSuccessResult
- Dhaka::ParseTreeCompositeNode
- Dhaka::ParseTreeLeafNode
- Dhaka::Parser
- Dhaka::ParserMethods
- Dhaka::PrecedenceBuilder
- Dhaka::Production
- Dhaka::ProductionBuilder
- Dhaka::Token
- Dhaka::Tokenizer
- Dhaka::TokenizerErrorResult
- Dhaka::TokenizerState
- Dhaka::TokenizerSuccessResult

No matching classes.

# Dhaka::Lexer

The lexer generator. To generate a lexer from a lexer specification `MyLexerSpecification`:

    lexer = Dhaka::Lexer.new(MyLexerSpecification)

To compile this lexer as `MyLexer` to a string of Ruby source:

    lexer.compile_to_ruby_source_as(:MyLexer)

### Attributes

specification[R]

### Public Class Methods

new(specification) click to toggle source

Creates a new lexer from a given specification.

    # File lib/dhaka/lexer/lexer.rb, line 11
    def initialize(specification)
      dfas           = {}
      @specification = specification
      specification.items.each do |pattern, item|
        dfas[pattern] = LexerSupport::DFA.new(pattern)
      end
      super(ItemSet.new(dfas.values.collect{|dfa| dfa.start_state}))
    end

### Public Instance Methods

action_for_pattern(pattern) click to toggle source

    # File lib/dhaka/lexer/lexer.rb, line 38
    def action_for_pattern pattern #:nodoc
      @specification.items[pattern].action
    end

compile_to_ruby_source_as(lexer_class_name) click to toggle source

Compiles the lexer to Ruby code that when executed, reloads all the states and actions of the lexer into a class named `lexer_class_name`.

    # File lib/dhaka/lexer/lexer.rb, line 22
    def compile_to_ruby_source_as lexer_class_name
      result  =   "class #{lexer_class_name} < Dhaka::CompiledLexer\n\n"
      result <<   "  self.specification = #{specification.name}\n\n"
      result <<   "  start_with #{start_state.object_id}\n\n"
      @states.each do |key, state|
        result << "#{state.compile_to_ruby_source}\n\n"
      end
      result <<   "end"
      result
    end

lex(input) click to toggle source

Returns a LexerRun that tokenizes `input`.

    # File lib/dhaka/lexer/lexer.rb, line 34
    def lex input
      LexerRun.new(self, input)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.