treetop 1.6.6 → 1.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +18 -0
  3. data/History.txt +18 -0
  4. data/README.md +2 -0
  5. data/Rakefile +10 -43
  6. data/Treetop.tmbundle/Preferences/Comments.tmPreferences +28 -0
  7. data/Treetop.tmbundle/Snippets/grammar ___ end.tmSnippet +20 -0
  8. data/Treetop.tmbundle/Snippets/rule ___ end.tmSnippet +18 -0
  9. data/Treetop.tmbundle/Support/nibs/SyntaxTreeViewer.nib/designable.nib +1524 -0
  10. data/Treetop.tmbundle/Support/nibs/SyntaxTreeViewer.nib/keyedobjects.nib +0 -0
  11. data/Treetop.tmbundle/Support/syntax_tree_viewer.rb +117 -0
  12. data/Treetop.tmbundle/Syntaxes/Treetop Grammar.tmLanguage +358 -0
  13. data/Treetop.tmbundle/info.plist +10 -0
  14. data/doc/pitfalls_and_advanced_techniques.markdown +6 -0
  15. data/doc/syntactic_recognition.markdown +2 -2
  16. data/lib/treetop/compiler/grammar_compiler.rb +6 -3
  17. data/lib/treetop/compiler/metagrammar.rb +97 -77
  18. data/lib/treetop/compiler/metagrammar.treetop +1 -1
  19. data/lib/treetop/compiler/ruby_builder.rb +2 -2
  20. data/lib/treetop/ruby_extensions/string.rb +0 -6
  21. data/lib/treetop/runtime/compiled_parser.rb +15 -2
  22. data/lib/treetop/version.rb +1 -1
  23. data/treetop.gemspec +25 -157
  24. metadata +15 -61
  25. data/doc/site.rb +0 -112
  26. data/doc/sitegen.rb +0 -65
  27. data/spec/compiler/and_predicate_spec.rb +0 -36
  28. data/spec/compiler/anything_symbol_spec.rb +0 -47
  29. data/spec/compiler/character_class_spec.rb +0 -304
  30. data/spec/compiler/choice_spec.rb +0 -89
  31. data/spec/compiler/circular_compilation_spec.rb +0 -30
  32. data/spec/compiler/failure_propagation_functional_spec.rb +0 -21
  33. data/spec/compiler/grammar_compiler_spec.rb +0 -113
  34. data/spec/compiler/grammar_spec.rb +0 -44
  35. data/spec/compiler/multibyte_chars_spec.rb +0 -38
  36. data/spec/compiler/namespace_spec.rb +0 -42
  37. data/spec/compiler/nonterminal_symbol_spec.rb +0 -40
  38. data/spec/compiler/not_predicate_spec.rb +0 -52
  39. data/spec/compiler/occurrence_range_spec.rb +0 -186
  40. data/spec/compiler/one_or_more_spec.rb +0 -35
  41. data/spec/compiler/optional_spec.rb +0 -37
  42. data/spec/compiler/parenthesized_expression_spec.rb +0 -34
  43. data/spec/compiler/parsing_rule_spec.rb +0 -61
  44. data/spec/compiler/repeated_subrule_spec.rb +0 -29
  45. data/spec/compiler/semantic_predicate_spec.rb +0 -176
  46. data/spec/compiler/sequence_spec.rb +0 -129
  47. data/spec/compiler/terminal_spec.rb +0 -177
  48. data/spec/compiler/terminal_symbol_spec.rb +0 -40
  49. data/spec/compiler/test_grammar.treetop +0 -7
  50. data/spec/compiler/test_grammar.tt +0 -7
  51. data/spec/compiler/test_grammar_do.treetop +0 -7
  52. data/spec/compiler/test_grammar_magic_coding.treetop +0 -8
  53. data/spec/compiler/test_grammar_magic_encoding.treetop +0 -8
  54. data/spec/compiler/tt_compiler_spec.rb +0 -224
  55. data/spec/compiler/zero_or_more_spec.rb +0 -58
  56. data/spec/composition/a.treetop +0 -11
  57. data/spec/composition/b.treetop +0 -11
  58. data/spec/composition/c.treetop +0 -10
  59. data/spec/composition/d.treetop +0 -10
  60. data/spec/composition/f.treetop +0 -17
  61. data/spec/composition/grammar_composition_spec.rb +0 -40
  62. data/spec/composition/subfolder/e_includes_c.treetop +0 -15
  63. data/spec/ruby_extensions/string_spec.rb +0 -32
  64. data/spec/runtime/compiled_parser_spec.rb +0 -153
  65. data/spec/runtime/syntax_node_spec.rb +0 -77
  66. data/spec/spec_helper.rb +0 -123
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'treetop'
4
+ require "#{ENV["TM_SUPPORT_PATH"]}/lib/ui"
5
+
6
+ # Monkey-patch SyntaxNode in a gross violation of its privacy.
7
+ module Treetop
8
+ module Runtime
9
+ class SyntaxNode
10
+ def interesting_methods
11
+ # Copied from SyntaxNode#inspect
12
+ methods-[extension_modules.last ? extension_modules.last.methods : nil]-self.class.instance_methods
13
+ end
14
+
15
+ def to_hash
16
+ {
17
+ "class_and_modules" => self.class.to_s.sub(/.*:/,'') + extension_modules.map{|m| "+"+m.to_s.sub(/.*:/,'')}*"",
18
+ "offset" => interval.first,
19
+ "text_value" => text_value,
20
+ "methods" => interesting_methods * ", ",
21
+ "elements" => elements ? elements.map {|e| e.to_hash} : []
22
+ }
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ class SyntaxTreeViewer
29
+ def self.nib_path
30
+ File.dirname(__FILE__) + "/SyntaxTreeViewer.nib"
31
+ end
32
+
33
+ def self.new_with_unknown_grammar
34
+ parser = if ENV['TM_SCOPE'] =~ /source\.ruby/ && ENV['TM_SELECTED_TEXT']
35
+ eval(ENV['TM_SELECTED_TEXT'])
36
+ else
37
+ parser_file = if ENV['TM_SCOPE'] =~ /source\.treetop/
38
+ ENV['TM_FILEPATH']
39
+ else
40
+ ask_for_grammar
41
+ end
42
+ return unless parser_file
43
+ Treetop.load(parser_file)
44
+ end
45
+ new(parser.new)
46
+ end
47
+
48
+ def self.ask_for_grammar
49
+ files = TextMate::UI.request_file("title" => "Select a Grammar File")
50
+ if files.nil? || files.empty?
51
+ nil
52
+ else
53
+ files[0]
54
+ end
55
+ end
56
+
57
+ def self.nib_path
58
+ File.dirname(__FILE__) + "/nibs/SyntaxTreeViewer.nib"
59
+ end
60
+
61
+ def initialize(_parser)
62
+ @parser = _parser
63
+ run_parser!
64
+ end
65
+
66
+ def dialog
67
+ TextMate::UI.dialog(
68
+ :nib => self.class.nib_path,
69
+ :parameters => parameters
70
+ ) do |d|
71
+ d.wait_for_input do |params|
72
+ if params["returnArgument"]
73
+ self.input = params["returnArgument"]
74
+ run_parser!
75
+ d.parameters = parameters
76
+ true
77
+ else
78
+ false
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ def input
85
+ ENV['TM_TREETOP_SYNTAX_VIEWER_INPUT'] ||= ""
86
+ end
87
+
88
+ def input=(new_input)
89
+ ENV['TM_TREETOP_SYNTAX_VIEWER_INPUT'] = new_input
90
+ end
91
+
92
+ private
93
+ def run_parser!
94
+ if input && !input.empty?
95
+ @syntax_tree = @parser.parse(input)
96
+ TextMate::UI.alert(:warning, "Parse error", @parser.failure_reason) unless @syntax_tree
97
+ end
98
+ end
99
+
100
+ def parameters
101
+ p = {
102
+ "input" => input,
103
+ "syntax_tree" => @syntax_tree ? @syntax_tree.to_hash : {},
104
+ "selected_tab" => !input.empty? && @syntax_tree ? "Syntax Tree" : "Input"
105
+ }
106
+ puts p
107
+ p
108
+ end
109
+ end
110
+
111
+ if __FILE__ == $0
112
+ Dir.chdir("/Users/aobrien/Work/canopy/play/sparql/")
113
+ require "/Users/aobrien/Work/canopy/play/sparql/lib/sparql"
114
+ v = SyntaxTreeViewer.new(SparqlParser.new)
115
+ v.input = %{PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?foo WHERE { ?x foaf:knows ?y . ?z foaf:knows ?x .}}
116
+ v.dialog
117
+ end
@@ -0,0 +1,358 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>fileTypes</key>
6
+ <array>
7
+ <string>treetop</string>
8
+ <string>tt</string>
9
+ </array>
10
+ <key>foldingStartMarker</key>
11
+ <string>(module|grammer|rule).*$</string>
12
+ <key>foldingStopMarker</key>
13
+ <string>^\s*end</string>
14
+ <key>keyEquivalent</key>
15
+ <string>^~T</string>
16
+ <key>name</key>
17
+ <string>Treetop Grammar</string>
18
+ <key>patterns</key>
19
+ <array>
20
+ <dict>
21
+ <key>include</key>
22
+ <string>#comment</string>
23
+ </dict>
24
+ <dict>
25
+ <key>begin</key>
26
+ <string>^(\s*)(module) ([A-Z]\w+)</string>
27
+ <key>beginCaptures</key>
28
+ <dict>
29
+ <key>2</key>
30
+ <dict>
31
+ <key>name</key>
32
+ <string>keyword.begin.module.treetop</string>
33
+ </dict>
34
+ <key>3</key>
35
+ <dict>
36
+ <key>name</key>
37
+ <string>entity.name.module.treetop</string>
38
+ </dict>
39
+ </dict>
40
+ <key>end</key>
41
+ <string>^\1(end)$</string>
42
+ <key>endCaptures</key>
43
+ <dict>
44
+ <key>1</key>
45
+ <dict>
46
+ <key>name</key>
47
+ <string>keyword.end.module.treetop</string>
48
+ </dict>
49
+ </dict>
50
+ <key>name</key>
51
+ <string>meta.module.treetop</string>
52
+ <key>patterns</key>
53
+ <array>
54
+ <dict>
55
+ <key>include</key>
56
+ <string>$self</string>
57
+ </dict>
58
+ </array>
59
+ </dict>
60
+ <dict>
61
+ <key>begin</key>
62
+ <string>^(\s*)(grammar) ([A-Z]\w+)</string>
63
+ <key>beginCaptures</key>
64
+ <dict>
65
+ <key>2</key>
66
+ <dict>
67
+ <key>name</key>
68
+ <string>keyword.begin.grammar.treetop</string>
69
+ </dict>
70
+ <key>3</key>
71
+ <dict>
72
+ <key>name</key>
73
+ <string>entity.name.grammar.treetop</string>
74
+ </dict>
75
+ </dict>
76
+ <key>end</key>
77
+ <string>^\1(end)$</string>
78
+ <key>endCaptures</key>
79
+ <dict>
80
+ <key>1</key>
81
+ <dict>
82
+ <key>name</key>
83
+ <string>keyword.end.grammar.treetop</string>
84
+ </dict>
85
+ </dict>
86
+ <key>name</key>
87
+ <string>meta.grammar.treetop</string>
88
+ <key>patterns</key>
89
+ <array>
90
+ <dict>
91
+ <key>include</key>
92
+ <string>$self</string>
93
+ </dict>
94
+ </array>
95
+ </dict>
96
+ <dict>
97
+ <key>captures</key>
98
+ <dict>
99
+ <key>1</key>
100
+ <dict>
101
+ <key>name</key>
102
+ <string>keyword.include.treetop</string>
103
+ </dict>
104
+ <key>2</key>
105
+ <dict>
106
+ <key>name</key>
107
+ <string>entity.name.include.treetop</string>
108
+ </dict>
109
+ </dict>
110
+ <key>match</key>
111
+ <string>(include)\s+(\w+)</string>
112
+ <key>name</key>
113
+ <string>meta.include.treetop</string>
114
+ </dict>
115
+ <dict>
116
+ <key>begin</key>
117
+ <string>\b(rule)\b (\w+)</string>
118
+ <key>beginCaptures</key>
119
+ <dict>
120
+ <key>1</key>
121
+ <dict>
122
+ <key>name</key>
123
+ <string>keyword.begin.rule.treetop</string>
124
+ </dict>
125
+ <key>2</key>
126
+ <dict>
127
+ <key>name</key>
128
+ <string>entity.name.rule.treetop</string>
129
+ </dict>
130
+ </dict>
131
+ <key>end</key>
132
+ <string>^\s+\bend\b\s*$</string>
133
+ <key>endCaptures</key>
134
+ <dict>
135
+ <key>0</key>
136
+ <dict>
137
+ <key>name</key>
138
+ <string>keyword.end.rule.treetop</string>
139
+ </dict>
140
+ </dict>
141
+ <key>name</key>
142
+ <string>meta.rule.treetop</string>
143
+ <key>patterns</key>
144
+ <array>
145
+ <dict>
146
+ <key>include</key>
147
+ <string>#comment</string>
148
+ </dict>
149
+ <dict>
150
+ <key>include</key>
151
+ <string>#strings</string>
152
+ </dict>
153
+ <dict>
154
+ <key>include</key>
155
+ <string>#character-class</string>
156
+ </dict>
157
+ <dict>
158
+ <key>match</key>
159
+ <string>\/</string>
160
+ <key>name</key>
161
+ <string>keyword.operator.or.treetop</string>
162
+ </dict>
163
+ <dict>
164
+ <key>match</key>
165
+ <string>&lt;\w+?&gt;</string>
166
+ <key>name</key>
167
+ <string>variable.class-instance.treetop</string>
168
+ </dict>
169
+ <dict>
170
+ <key>match</key>
171
+ <string>\w+?:</string>
172
+ <key>name</key>
173
+ <string>support.operand.treetop</string>
174
+ </dict>
175
+ <dict>
176
+ <key>begin</key>
177
+ <string>\{</string>
178
+ <key>end</key>
179
+ <string>\}</string>
180
+ <key>name</key>
181
+ <string>meta.embedded-ruby.treetop</string>
182
+ <key>patterns</key>
183
+ <array>
184
+ <dict>
185
+ <key>include</key>
186
+ <string>source.ruby</string>
187
+ </dict>
188
+ </array>
189
+ </dict>
190
+ </array>
191
+ </dict>
192
+ </array>
193
+ <key>repository</key>
194
+ <dict>
195
+ <key>character-class</key>
196
+ <dict>
197
+ <key>patterns</key>
198
+ <array>
199
+ <dict>
200
+ <key>match</key>
201
+ <string>\\[wWsSdDhH]|\.</string>
202
+ <key>name</key>
203
+ <string>constant.character.character-class.regexp</string>
204
+ </dict>
205
+ <dict>
206
+ <key>match</key>
207
+ <string>\\.</string>
208
+ <key>name</key>
209
+ <string>constant.character.escape.backslash.regexp</string>
210
+ </dict>
211
+ <dict>
212
+ <key>begin</key>
213
+ <string>(\[)(\^)?</string>
214
+ <key>beginCaptures</key>
215
+ <dict>
216
+ <key>1</key>
217
+ <dict>
218
+ <key>name</key>
219
+ <string>punctuation.definition.character-class.regexp</string>
220
+ </dict>
221
+ <key>2</key>
222
+ <dict>
223
+ <key>name</key>
224
+ <string>keyword.operator.negation.regexp</string>
225
+ </dict>
226
+ </dict>
227
+ <key>end</key>
228
+ <string>(\])</string>
229
+ <key>endCaptures</key>
230
+ <dict>
231
+ <key>1</key>
232
+ <dict>
233
+ <key>name</key>
234
+ <string>punctuation.definition.character-class.regexp</string>
235
+ </dict>
236
+ </dict>
237
+ <key>name</key>
238
+ <string>constant.other.character-class.set.regexp</string>
239
+ <key>patterns</key>
240
+ <array>
241
+ <dict>
242
+ <key>include</key>
243
+ <string>#character-class</string>
244
+ </dict>
245
+ <dict>
246
+ <key>captures</key>
247
+ <dict>
248
+ <key>2</key>
249
+ <dict>
250
+ <key>name</key>
251
+ <string>constant.character.escape.backslash.regexp</string>
252
+ </dict>
253
+ <key>4</key>
254
+ <dict>
255
+ <key>name</key>
256
+ <string>constant.character.escape.backslash.regexp</string>
257
+ </dict>
258
+ </dict>
259
+ <key>match</key>
260
+ <string>(.|(\\.))\-([^\]]|(\\.))</string>
261
+ <key>name</key>
262
+ <string>constant.other.character-class.range.regexp</string>
263
+ </dict>
264
+ <dict>
265
+ <key>match</key>
266
+ <string>&amp;&amp;</string>
267
+ <key>name</key>
268
+ <string>keyword.operator.intersection.regexp</string>
269
+ </dict>
270
+ </array>
271
+ </dict>
272
+ </array>
273
+ </dict>
274
+ <key>comment</key>
275
+ <dict>
276
+ <key>captures</key>
277
+ <dict>
278
+ <key>1</key>
279
+ <dict>
280
+ <key>name</key>
281
+ <string>punctuation.definition.comment.treetop</string>
282
+ </dict>
283
+ </dict>
284
+ <key>match</key>
285
+ <string>(#).*$\n?</string>
286
+ <key>name</key>
287
+ <string>comment.line.number-sign.treetop</string>
288
+ </dict>
289
+ <key>strings</key>
290
+ <dict>
291
+ <key>patterns</key>
292
+ <array>
293
+ <dict>
294
+ <key>begin</key>
295
+ <string>'</string>
296
+ <key>beginCaptures</key>
297
+ <dict>
298
+ <key>0</key>
299
+ <dict>
300
+ <key>name</key>
301
+ <string>punctuation.definition.string.begin.treetop</string>
302
+ </dict>
303
+ </dict>
304
+ <key>end</key>
305
+ <string>'</string>
306
+ <key>endCaptures</key>
307
+ <dict>
308
+ <key>0</key>
309
+ <dict>
310
+ <key>name</key>
311
+ <string>punctuation.definition.string.end.treetop</string>
312
+ </dict>
313
+ </dict>
314
+ <key>name</key>
315
+ <string>string.quoted.single.treetop</string>
316
+ <key>patterns</key>
317
+ <array>
318
+ <dict>
319
+ <key>match</key>
320
+ <string>\\(u\h{4}|.)</string>
321
+ <key>name</key>
322
+ <string>constant.character.escape.antlr</string>
323
+ </dict>
324
+ </array>
325
+ </dict>
326
+ <dict>
327
+ <key>begin</key>
328
+ <string>"</string>
329
+ <key>beginCaptures</key>
330
+ <dict>
331
+ <key>0</key>
332
+ <dict>
333
+ <key>name</key>
334
+ <string>punctuation.definition.string.begin.treetop</string>
335
+ </dict>
336
+ </dict>
337
+ <key>end</key>
338
+ <string>"</string>
339
+ <key>endCaptures</key>
340
+ <dict>
341
+ <key>0</key>
342
+ <dict>
343
+ <key>name</key>
344
+ <string>punctuation.definition.string.end.treetop</string>
345
+ </dict>
346
+ </dict>
347
+ <key>name</key>
348
+ <string>string.quoted.double.treetop</string>
349
+ </dict>
350
+ </array>
351
+ </dict>
352
+ </dict>
353
+ <key>scopeName</key>
354
+ <string>source.treetop</string>
355
+ <key>uuid</key>
356
+ <string>A1604A34-0B73-4D5A-9499-87D881DFA8D5</string>
357
+ </dict>
358
+ </plist>