Skip to content

Support multiple states when parsing rules #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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