Skip to content

Commit

Permalink
Support multiple states when parsing rules (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
tancnle authored Nov 11, 2024
1 parent b22c4f8 commit ad96ec4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/rouge/regex_lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ def rule(re, tok=nil, next_state=nil, &callback)
puts " yielding: #{tok.qualname}, #{stream[0].inspect}" if @debug
@output_stream.call(tok, stream[0])
end
when Array
proc do |stream|
puts " yielding: #{tok.qualname}, #{stream[0].inspect}" if @debug
@output_stream.call(tok, stream[0])
for i_next_state in next_state do
next @stack.pop if i_next_state == :pop!
next @stack.push(@stack.last) if i_next_state == :push

state = @states[i_next_state] || self.class.get_state(i_next_state)
puts " pushing :#{state.name}" if @debug
@stack.push(state)
end
end
else
raise "invalid next state: #{next_state.inspect}"
end
Expand Down
27 changes: 27 additions & 0 deletions spec/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ def incr
assert { types == %w(digit lt gt) }
end

it 'supports multiple states' do
lexer = Class.new(Rouge::RegexLexer) do
state :root do
rule %r/\w+/, 'text'
rule %r/(=)(\s*)/ do
groups 'operator', 'whitespace'
push :value
end
end

state :value do
rule %r/\d+/, 'digit', :pop!
rule %r/\[/, 'punctuation', [:pop!, :array]
end

state :array do
rule %r/\]/, 'punctuation', :pop!
rule %r//, 'token', :value
end
end

result = lexer.lex('numbers=[1]')

types = result.map { |(t,_)| t }
assert { types == %w(text operator punctuation digit punctuation) }
end

it 'delegates' do
class MasterLexer < Rouge::RegexLexer
state :root do
Expand Down

0 comments on commit ad96ec4

Please sign in to comment.