Class: Ripper::TokenPattern
Do not use. This class is for internal use only.
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Exceptions:
| |
Inherits: | Object |
Defined in: | ext/ripper/lib/ripper/lexer.rb |
Constant Summary
Class Method Summary
- .compile
- .new(pattern) ⇒ TokenPattern constructor
Instance Method Summary
- #match(str)
- #match_list(tokens)
- #compile(pattern) private
- #map_token(tok) private
- #map_tokens(tokens) private
Constructor Details
.new(pattern) ⇒ TokenPattern
# File 'ext/ripper/lib/ripper/lexer.rb', line 248
def initialize(pattern) @source = pattern @re = compile(pattern) end
Class Method Details
.compile
[ GitHub ]# File 'ext/ripper/lib/ripper/lexer.rb', line 245
alias compile new
Instance Method Details
#compile(pattern) (private)
[ GitHub ]# File 'ext/ripper/lib/ripper/lexer.rb', line 266
def compile(pattern) if m = /[^\w\s$()\[\]{}?*+\.]/.match(pattern) raise CompileError, "invalid char in pattern: #{m[0].inspect}" end buf = +'' pattern.scan(/(?:\w|\$\(|[()\[\]\{\}?*\.]+)/) do |tok| case tok when /\w/ buf.concat map_token(tok) when '$(' buf.concat '(' when '(' buf.concat '(?:' when /[?*\[\])\.]/ buf.concat tok else raise 'must not happen' end end Regexp.compile(buf) rescue RegexpError => err raise CompileError, err. end
#map_token(tok) (private)
[ GitHub ]# File 'ext/ripper/lib/ripper/lexer.rb', line 301
def map_token(tok) MAP[tok] or raise CompileError, "unknown token: #{tok}" end
#map_tokens(tokens) (private)
[ GitHub ]# File 'ext/ripper/lib/ripper/lexer.rb', line 290
def map_tokens(tokens) tokens.map {|pos,type,str| map_token(type.to_s.delete_prefix('on_')) }.join end
#match(str)
[ GitHub ]# File 'ext/ripper/lib/ripper/lexer.rb', line 253
def match(str) match_list(::Ripper.lex(str)) end
#match_list(tokens)
[ GitHub ]# File 'ext/ripper/lib/ripper/lexer.rb', line 257
def match_list(tokens) if m = @re.match(map_tokens(tokens)) then MatchData.new(tokens, m) else nil end end