Class: Rouge::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/rouge/lexer.rb

Direct Known Subclasses

Rouge::Lexers::Text, RegexLexer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &b) ⇒ Lexer

-*- instance methods -*- #



77
78
79
80
# File 'lib/rouge/lexer.rb', line 77

def initialize(opts={}, &b)
  options(opts)
  @lazy_load_proc = b
end

Class Method Details

.aliases(*args) ⇒ Object



55
56
57
# File 'lib/rouge/lexer.rb', line 55

def aliases(*args)
  args.each { |arg| Lexer.register(arg, self) }
end

.default_optionsObject



36
37
38
# File 'lib/rouge/lexer.rb', line 36

def default_options
  @default_options ||= {}
end

.extension_registryObject



65
66
67
# File 'lib/rouge/lexer.rb', line 65

def extension_registry
  @extension_registry ||= {}
end

.extensions(*exts) ⇒ Object



59
60
61
62
63
# File 'lib/rouge/lexer.rb', line 59

def extensions(*exts)
  exts.each do |ext|
    Lexer.extension_registry[ext] = self
  end
end

.find(name) ⇒ Object



40
41
42
# File 'lib/rouge/lexer.rb', line 40

def find(name)
  registry[name.to_s]
end

.lex(stream, opts = {}, &b) ⇒ Object



17
18
19
# File 'lib/rouge/lexer.rb', line 17

def lex(stream, opts={}, &b)
  new(opts).lex(stream, &b)
end

.make(opts = {}, &b) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/rouge/lexer.rb', line 7

def make(opts={}, &b)
  _sup = self

  Class.new(self) do
    @lazy_load_proc = b
    @default_options = _sup.default_options.merge(opts)
    @parent = _sup
  end
end

.new(*a, &b) ⇒ Object



31
32
33
34
# File 'lib/rouge/lexer.rb', line 31

def new(*a, &b)
  force_load!
  super(*a, &b)
end

.register(name, lexer) ⇒ Object



44
45
46
# File 'lib/rouge/lexer.rb', line 44

def register(name, lexer)
  registry[name.to_s] = lexer
end

.tag(t = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/rouge/lexer.rb', line 48

def tag(t=nil)
  return @tag if t.nil?

  @tag = t.to_s
  aliases @tag
end

Instance Method Details

#debug(&b) ⇒ Object



96
97
98
# File 'lib/rouge/lexer.rb', line 96

def debug(&b)
  puts(b.call) if option :debug
end

#get_tokens(stream) ⇒ Object



100
101
102
# File 'lib/rouge/lexer.rb', line 100

def get_tokens(stream)
  lex(stream).to_a
end

#lex(string, &b) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rouge/lexer.rb', line 104

def lex(string, &b)
  return enum_for(:lex, string) unless block_given?

  last_token = nil
  last_val = nil
  stream_tokens(StringScanner.new(string)) do |tok, val|
    next if val.empty?

    if tok == last_token
      last_val << val
      next
    end

    b.call(last_token, last_val) if last_token
    last_token = tok
    last_val = val
  end

  b.call(last_token, last_val) if last_token
end

#option(k, v = :absent) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/rouge/lexer.rb', line 88

def option(k, v=:absent)
  if v == :absent
    options[k]
  else
    options({ k => v })
  end
end

#options(o = {}) ⇒ Object



82
83
84
85
86
# File 'lib/rouge/lexer.rb', line 82

def options(o={})
  (@options ||= {}).merge!(o)

  self.class.default_options.merge(@options)
end

#stream_tokens(stream, &b) ⇒ Object



125
126
127
# File 'lib/rouge/lexer.rb', line 125

def stream_tokens(stream, &b)
  raise 'abstract'
end