Class: RText::Parser
- Inherits:
-
Object
- Object
- RText::Parser
- Defined in:
- lib/rtext/parser.rb
Defined Under Namespace
Classes: InternalError, Problem
Constant Summary collapse
- AnyValue =
[:identifier, :integer, :float, :string, :boolean, :reference, :generic]
Instance Method Summary collapse
- #consume(*args) ⇒ Object
- #discard_until(kind) ⇒ Object
- #next_token_kind(idx = 0) ⇒ Object
- #parse(tokens, options) ⇒ Object
- #parse_annotation ⇒ Object
- #parse_argument(arg_list) ⇒ Object
- #parse_argument_list(arg_list) ⇒ Object
- #parse_argument_value ⇒ Object
- #parse_argument_value_list ⇒ Object
- #parse_block_element(element_list, comments) ⇒ Object
- #parse_comment ⇒ Object
- #parse_element_list(comments) ⇒ Object
- #parse_eol_comment(comments) ⇒ Object
- #parse_labeled_block_element(comments) ⇒ Object
-
#parse_statement(is_root = false, allow_unassociated_comment = false) ⇒ Object
parse a statement with optional leading comment or an unassociated comment.
- #parse_statement_block(element_list, comments) ⇒ Object
- #parse_value ⇒ Object
- #report_consume_problem(message, line) ⇒ Object
Instance Method Details
#consume(*args) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rtext/parser.rb', line 199 def consume(*args) t = @tokens.first if t.nil? @non_consume_count += 1 report_consume_problem("Unexpected end of file, expected #{args.join(", ")}", @last_line) return nil end if args.include?(t.kind) @tokens.shift @consume_problem_reported = false @non_consume_count = 0 puts "consuming #{t.kind} #{t.value}" if @debug t else if t.kind == :error @tokens.shift @non_consume_count = 0 report_consume_problem("Parse error on token '#{t.value}'", t.line) return nil else value = " '#{t.value}'" if t.value @non_consume_count += 1 report_consume_problem("Unexpected #{t.kind}#{value}, expected #{args.join(", ")}", t.line) return nil end end end |
#discard_until(kind) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rtext/parser.rb', line 187 def discard_until(kind) t = @tokens.shift if t puts "discarding #{t.kind} #{t.value}" if @debug while t.kind != kind t = @tokens.shift break unless t puts "discarding #{t.kind} #{t.value}" if @debug end end end |
#next_token_kind(idx = 0) ⇒ Object
183 184 185 |
# File 'lib/rtext/parser.rb', line 183 def next_token_kind(idx=0) @tokens[idx] && @tokens[idx].kind end |
#parse(tokens, options) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rtext/parser.rb', line 8 def parse(tokens, ) @dsc_visitor = [:descent_visitor] @asc_visitor = [:ascent_visitor] @problems = [:problems] || [] @non_consume_count = 0 @consume_problem_reported = false @tokens = tokens @last_line = @tokens.last && @tokens.last.line #@debug = true begin while next_token_kind statement = parse_statement(true, true) end rescue InternalError end end |
#parse_annotation ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/rtext/parser.rb', line 70 def parse_annotation result = nil while next_token_kind == :annotation result ||= [] result << consume(:annotation) consume(:newline) end result end |
#parse_argument(arg_list) ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/rtext/parser.rb', line 142 def parse_argument(arg_list) if next_token_kind == :label label = consume(:label) arg_list << [label, parse_argument_value] else arg_list << parse_argument_value end end |
#parse_argument_list(arg_list) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rtext/parser.rb', line 130 def parse_argument_list(arg_list) first = true while (AnyValue + [",", "[", :label, :error]).include?(next_token_kind) unless first success = consume(",") consume(:newline) if success && next_token_kind == :newline end first = false parse_argument(arg_list) end end |
#parse_argument_value ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/rtext/parser.rb', line 151 def parse_argument_value if next_token_kind == "[" parse_argument_value_list else parse_value end end |
#parse_argument_value_list ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rtext/parser.rb', line 159 def parse_argument_value_list consume("[") consume(:newline) if next_token_kind == :newline first = true result = [] while (AnyValue + [",", :error]).include?(next_token_kind) unless first success = consume(",") consume(:newline) if success && next_token_kind == :newline end first = false result << parse_value end consume(:newline) if next_token_kind == :newline && next_token_kind(1) == "]" consume("]") result end |
#parse_block_element(element_list, comments) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/rtext/parser.rb', line 98 def parse_block_element(element_list, comments) if next_token_kind == :label label = consume(:label) element_list << [label, parse_labeled_block_element(comments)] else statement = parse_statement(false, true) element_list << statement if statement end end |
#parse_comment ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/rtext/parser.rb', line 60 def parse_comment result = nil while next_token_kind == :comment result ||= [] result << consume(:comment) consume(:newline) end result end |
#parse_element_list(comments) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rtext/parser.rb', line 117 def parse_element_list(comments) consume("[") parse_eol_comment(comments) result = [] while next_token_kind && next_token_kind != "]" statement = parse_statement(false, true) result << statement if statement end consume("]") parse_eol_comment(comments) result end |
#parse_eol_comment(comments) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/rtext/parser.rb', line 80 def parse_eol_comment(comments) if next_token_kind == :comment comment = consume(:comment) comments << [comment, :eol] end nl = consume(:newline) discard_until(:newline) unless nl end |
#parse_labeled_block_element(comments) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/rtext/parser.rb', line 108 def parse_labeled_block_element(comments) if next_token_kind == "[" parse_element_list(comments) else parse_eol_comment(comments) parse_statement end end |
#parse_statement(is_root = false, allow_unassociated_comment = false) ⇒ Object
parse a statement with optional leading comment or an unassociated comment
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 |
# File 'lib/rtext/parser.rb', line 26 def parse_statement(is_root=false, allow_unassociated_comment=false) comments = [] comment = parse_comment annotation = parse_annotation if (next_token_kind && next_token_kind == :identifier) || !allow_unassociated_comment comments << [ comment, :above] if comment command = consume(:identifier) if command @dsc_visitor.call(command) arg_list = [] parse_argument_list(arg_list) element_list = [] if next_token_kind == "{" parse_statement_block(element_list, comments) end parse_eol_comment(comments) @asc_visitor.call(command, arg_list, element_list, comments, annotation, is_root) else discard_until(:newline) nil end elsif comment # if there is no statement, the comment is non-optional comments << [ comment, :unassociated ] @asc_visitor.call(nil, nil, nil, comments, nil, nil) nil else # die expecting an identifier (next token is not an identifier) consume(:identifier) discard_until(:newline) nil end end |
#parse_statement_block(element_list, comments) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/rtext/parser.rb', line 89 def parse_statement_block(element_list, comments) consume("{") parse_eol_comment(comments) while next_token_kind && next_token_kind != "}" parse_block_element(element_list, comments) end consume("}") end |
#parse_value ⇒ Object
179 180 181 |
# File 'lib/rtext/parser.rb', line 179 def parse_value consume(*AnyValue) end |
#report_consume_problem(message, line) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/rtext/parser.rb', line 230 def report_consume_problem(, line) problem = Problem.new(, line) if @non_consume_count > 100 # safety check, stop reoccuring problems to avoid endless loops @problems << Problem.new("Internal error", line) puts [@problems.last., @problems.last.line].inspect if @debug raise InternalError.new else if !@consume_problem_reported @consume_problem_reported = true @problems << problem puts [@problems.last., @problems.last.line].inspect if @debug end end end |