Module: RDoc::Parser::RubyTools
Overview
Collection of methods for writing parsers against RDoc::RubyLex and RDoc::RubyToken
Constant Summary
Constants included from RubyToken
RubyToken::EXPR_ARG, RubyToken::EXPR_BEG, RubyToken::EXPR_CLASS, RubyToken::EXPR_DOT, RubyToken::EXPR_END, RubyToken::EXPR_FNAME, RubyToken::EXPR_MID, RubyToken::NEWLINE_TOKEN, RubyToken::Symbol, RubyToken::TkReading2Token, RubyToken::TkSymbol2Token, RubyToken::TkToken2Reading, RubyToken::TokenDefinitions
Instance Method Summary collapse
-
#add_token_listener(obj) ⇒ Object
Adds a token listener
obj
, but you should probably use token_listener. -
#get_tk ⇒ Object
Fetches the next token from the scanner.
-
#get_tk_until(*tokens) ⇒ Object
Reads and returns all tokens up to one of
tokens
. -
#get_tkread ⇒ Object
Retrieves a String representation of the read tokens.
-
#peek_read ⇒ Object
Peek equivalent for get_tkread.
-
#peek_tk ⇒ Object
Peek at the next token, but don’t remove it from the stream.
-
#remove_token_listener(obj) ⇒ Object
Removes the token listener
obj
. -
#reset ⇒ Object
Resets the tools.
-
#skip_tkspace(skip_nl = true) ⇒ Object
Skips whitespace tokens including newlines if
skip_nl
is true. -
#token_listener(obj) ⇒ Object
Has
obj
listen to tokens. -
#unget_tk(tk) ⇒ Object
Returns
tk
to the scanner.
Methods included from RubyToken
#Token, def_token, #set_token_position
Instance Method Details
#add_token_listener(obj) ⇒ Object
Adds a token listener obj
, but you should probably use token_listener
12 13 14 15 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 12 def add_token_listener(obj) @token_listeners ||= [] @token_listeners << obj end |
#get_tk ⇒ Object
Fetches the next token from the scanner
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 20 def get_tk tk = nil if @tokens.empty? then tk = @scanner.token @read.push @scanner.get_readed puts "get_tk1 => #{tk.inspect}" if $TOKEN_DEBUG else @read.push @unget_read.shift tk = @tokens.shift puts "get_tk2 => #{tk.inspect}" if $TOKEN_DEBUG end tk = nil if TkEND_OF_SCRIPT === tk if TkSYMBEG === tk then set_token_position tk.line_no, tk.char_no case tk1 = get_tk when TkId, TkOp, TkSTRING, TkDSTRING, TkSTAR, TkAMPER then if tk1.respond_to?(:name) then tk = Token(TkSYMBOL).set_text(":" + tk1.name) else tk = Token(TkSYMBOL).set_text(":" + tk1.text) end # remove the identifier we just read to replace it with a symbol @token_listeners.each do |obj| obj.pop_token end if @token_listeners else tk = tk1 end end # inform any listeners of our shiny new token @token_listeners.each do |obj| obj.add_token(tk) end if @token_listeners tk end |
#get_tk_until(*tokens) ⇒ Object
Reads and returns all tokens up to one of tokens
. Leaves the matched token in the token list.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 67 def get_tk_until(*tokens) read = [] loop do tk = get_tk case tk when *tokens then unget_tk tk break end read << tk end read end |
#get_tkread ⇒ Object
Retrieves a String representation of the read tokens
88 89 90 91 92 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 88 def get_tkread read = @read.join("") @read = [] read end |
#peek_read ⇒ Object
Peek equivalent for get_tkread
97 98 99 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 97 def peek_read @read.join('') end |
#peek_tk ⇒ Object
Peek at the next token, but don’t remove it from the stream
104 105 106 107 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 104 def peek_tk unget_tk(tk = get_tk) tk end |
#remove_token_listener(obj) ⇒ Object
Removes the token listener obj
112 113 114 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 112 def remove_token_listener(obj) @token_listeners.delete(obj) end |
#reset ⇒ Object
Resets the tools
119 120 121 122 123 124 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 119 def reset @read = [] @tokens = [] @unget_read = [] @nest = 0 end |
#skip_tkspace(skip_nl = true) ⇒ Object
Skips whitespace tokens including newlines if skip_nl
is true
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 129 def skip_tkspace(skip_nl = true) # HACK dup tokens = [] while TkSPACE === (tk = get_tk) or (skip_nl and TkNL === tk) do tokens.push tk end unget_tk tk tokens end |
#token_listener(obj) ⇒ Object
Has obj
listen to tokens
143 144 145 146 147 148 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 143 def token_listener(obj) add_token_listener obj yield ensure remove_token_listener obj end |
#unget_tk(tk) ⇒ Object
Returns tk
to the scanner
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rdoc/parser/ruby_tools.rb', line 153 def unget_tk(tk) @tokens.unshift tk @unget_read.unshift @read.pop # Remove this token from any listeners @token_listeners.each do |obj| obj.pop_token end if @token_listeners nil end |