Class: Rouge::RegexLexer::StateDSL

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ StateDSL

Returns a new instance of StateDSL.



63
64
65
# File 'lib/rouge/regex_lexer.rb', line 63

def initialize(rules)
  @rules = rules
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



62
63
64
# File 'lib/rouge/regex_lexer.rb', line 62

def rules
  @rules
end

Instance Method Details

#mixin(lexer_name) ⇒ Object

Mix in the rules from another state into this state. The rules from the mixed-in state will be tried in order before moving on to the rest of the rules in this state.



102
103
104
# File 'lib/rouge/regex_lexer.rb', line 102

def mixin(lexer_name)
  rules << lexer_name.to_s
end

#rule(re, token, next_state = nil) ⇒ Object #rule(re, &callback) ⇒ Object

Define a new rule for this state.

Parameters:

  • re (Regexp)

    a regular expression for this rule to test.

  • tok (String) (defaults to: nil)

    the token type to yield if ‘re` matches.

  • next_state (#to_s) (defaults to: nil)

    (optional) a state to push onto the stack if ‘re` matches. If `next_state` is `:pop!`, the state stack will be popped instead.

  • callback (Proc)

    a block that will be evaluated in the context of the lexer if ‘re` matches. This block has access to a number of lexer methods, including Rouge::RegexLexer#push, Rouge::RegexLexer#pop!, Rouge::RegexLexer#token, and Rouge::RegexLexer#delegate. The first argument can be used to access the match groups.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rouge/regex_lexer.rb', line 86

def rule(re, tok=nil, next_state=nil, &callback)
  callback ||= case next_state
  when :pop!
    proc { token tok; pop! }
  when Symbol
    proc { token tok; push next_state }
  else
    proc { token tok }
  end

  rules << Rule.new(re, callback)
end