forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoccomment_parser.rb
More file actions
151 lines (126 loc) · 2.75 KB
/
doccomment_parser.rb
File metadata and controls
151 lines (126 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
module ActionScript
module ParseDoc
class DocCommentParser
def initialize(lexer)
@lex = lexer
@handler = nil
end
def handler=(handler)
@handler = handler
end
# this is the main parser entry-point
def parse_comment
return unless @lex.peek_next # TODO: can be removed once we have EOFToken
@handler.comment_start(@lex.peek_next.lineno)
while @lex.peek_next
parse_line
end
@handler.comment_end
end
def parse_line
maybe_skip(StarsToken)
parse_whitespace
if lookahead?(ParaAtTagToken)
@handler.start_paragraph_tag(expect(ParaAtTagToken))
end
until lookahead?(EndOfLineToken) || eof?
if lookahead?(InlineAtTagToken)
parse_inline_tag
else
eat_text_token
end
end
unless eof?
eat_text_token_of_kind(EndOfLineToken)
end
end
def parse_whitespace
if lookahead?(WhitespaceToken)
eat_text_token_of_kind(WhitespaceToken)
end
end
def parse_inline_tag
tok = expect(InlineAtTagToken)
@handler.start_inline_tag(tok)
until lookahead?(RBraceToken)
err("end of input before closing brace for #{tok.inspect}") if eof?
if lookahead?(LBraceToken)
parse_brace_pair
elsif lookahead?(EndOfLineToken)
eat_text_token
maybe_skip(StarsToken)
else
eat_text_token
end
end
expect(RBraceToken)
@handler.end_inline_tag
end
def parse_brace_pair
eat_text_token_of_kind(LBraceToken)
until lookahead?(RBraceToken) || eof?
if lookahead?(LBraceToken)
parse_brace_pair
elsif lookahead?(EndOfLineToken)
eat_text_token
maybe_skip(StarsToken)
else
eat_text_token
end
end
eat_text_token_of_kind(RBraceToken)
end
# treats the text token, whatever kind it may be, as text without special
# meaning
def eat_text_token
@handler.text(@lex.get_next)
end
def eat_text_token_of_kind(kind)
@handler.text(expect(kind))
end
private
def expect(kind)
tok = @lex.get_next
unless tok.is_a?(kind)
err("Expected '#{kind}' but found '#{tok.inspect}'");
end
tok
end
def eof?
@lex.peek_next.nil?
end
def lookahead?(kind)
@lex.peek_next.is_a?(kind)
end
def lookaheads?(*kinds)
tnext = @lex.peek_next
kinds.each do |kind|
return true if tnext.is_a?(kind)
end
false
end
def speculate(kind)
if lookahead?(kind)
expect(kind)
yield
end
end
def maybe_skip(kind)
if lookahead?(kind)
expect(kind)
end
end
def err(msg)
raise msg
end
end
class DocCommentHandler
def comment_start(lineno); end
def comment_end; end
def text(text); end
def start_paragraph_tag(tag); end
def start_inline_tag(tag); end
def end_inline_tag; end
end
end # module Parse
end # module ActionScript