Collection of methods for writing parsers against RDoc::RubyLex and RDoc::RubyToken
Adds a token listener obj, but you should probably use token_listener
# File lib/rdoc/parser/ruby_tools.rb, line 12
12: def add_token_listener(obj)
13: @token_listeners ||= []
14: @token_listeners << obj
15: end
Fetches the next token from the scanner
# File lib/rdoc/parser/ruby_tools.rb, line 20
20: def get_tk
21: tk = nil
22:
23: if @tokens.empty? then
24: tk = @scanner.token
25: @read.push @scanner.get_readed
26: puts "get_tk1 => #{tk.inspect}" if $TOKEN_DEBUG
27: else
28: @read.push @unget_read.shift
29: tk = @tokens.shift
30: puts "get_tk2 => #{tk.inspect}" if $TOKEN_DEBUG
31: end
32:
33: tk = nil if TkEND_OF_SCRIPT === tk
34:
35: if TkSYMBEG === tk then
36: set_token_position tk.line_no, tk.char_no
37:
38: case tk1 = get_tk
39: when TkId, TkOp, TkSTRING, TkDSTRING, TkSTAR, TkAMPER then
40: if tk1.respond_to?(:name) then
41: tk = Token(TkSYMBOL).set_text(":" + tk1.name)
42: else
43: tk = Token(TkSYMBOL).set_text(":" + tk1.text)
44: end
45:
46: # remove the identifier we just read (we're about to replace it with a
47: # symbol)
48: @token_listeners.each do |obj|
49: obj.pop_token
50: end if @token_listeners
51: else
52: warn("':' not followed by identifier or operator")
53: tk = tk1
54: end
55: end
56:
57: # inform any listeners of our shiny new token
58: @token_listeners.each do |obj|
59: obj.add_token(tk)
60: end if @token_listeners
61:
62: tk
63: end
# File lib/rdoc/parser/ruby_tools.rb, line 65
65: def get_tk_until(*tokens)
66: read = []
67:
68: loop do
69: tk = get_tk
70: case tk when *tokens then unget_tk tk; break end
71: read << tk
72: end
73:
74: read
75: end
Retrieves a String representation of the read tokens
# File lib/rdoc/parser/ruby_tools.rb, line 80
80: def get_tkread
81: read = @read.join("")
82: @read = []
83: read
84: end
Peek equivalent for get_tkread
# File lib/rdoc/parser/ruby_tools.rb, line 89
89: def peek_read
90: @read.join('')
91: end
Peek at the next token, but don’t remove it from the stream
# File lib/rdoc/parser/ruby_tools.rb, line 96
96: def peek_tk
97: unget_tk(tk = get_tk)
98: tk
99: end
Removes the token listener obj
# File lib/rdoc/parser/ruby_tools.rb, line 104
104: def remove_token_listener(obj)
105: @token_listeners.delete(obj)
106: end
Resets the tools
# File lib/rdoc/parser/ruby_tools.rb, line 111
111: def reset
112: @read = []
113: @tokens = []
114: @unget_read = []
115: @nest = 0
116: end
Skips whitespace tokens including newlines if skip_nl is true
# File lib/rdoc/parser/ruby_tools.rb, line 121
121: def skip_tkspace(skip_nl = true) # HACK dup
122: tokens = []
123:
124: while TkSPACE === (tk = get_tk) or (skip_nl and TkNL === tk) do
125: tokens.push tk
126: end
127:
128: unget_tk tk
129: tokens
130: end
Has obj listen to tokens
# File lib/rdoc/parser/ruby_tools.rb, line 135
135: def token_listener(obj)
136: add_token_listener obj
137: yield
138: ensure
139: remove_token_listener obj
140: end
Returns tk to the scanner
# File lib/rdoc/parser/ruby_tools.rb, line 145
145: def unget_tk(tk)
146: @tokens.unshift tk
147: @unget_read.unshift @read.pop
148:
149: # Remove this token from any listeners
150: @token_listeners.each do |obj|
151: obj.pop_token
152: end if @token_listeners
153: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.