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.


48
49
50
# File 'lib/rouge/regex_lexer.rb', line 48

def initialize(rules)
  @rules = rules
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules


47
48
49
# File 'lib/rouge/regex_lexer.rb', line 47

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.


94
95
96
# File 'lib/rouge/regex_lexer.rb', line 94

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.


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rouge/regex_lexer.rb', line 71

def rule(re, tok=nil, next_state=nil, &callback)
  if block_given?
    next_state = tok
  else
    tok = Token[tok]

    callback = proc do
      token tok
      case next_state
      when :pop!
        pop!
      when Symbol
        push next_state
      end # else pass
    end
  end

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