cognita-treetop 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. data/README +164 -0
  2. data/Rakefile +35 -0
  3. data/bin/tt +25 -0
  4. data/doc/contributing_and_planned_features.markdown +103 -0
  5. data/doc/grammar_composition.markdown +65 -0
  6. data/doc/index.markdown +90 -0
  7. data/doc/pitfalls_and_advanced_techniques.markdown +51 -0
  8. data/doc/semantic_interpretation.markdown +189 -0
  9. data/doc/site.rb +110 -0
  10. data/doc/sitegen.rb +60 -0
  11. data/doc/syntactic_recognition.markdown +100 -0
  12. data/doc/using_in_ruby.markdown +21 -0
  13. data/examples/lambda_calculus/arithmetic.rb +551 -0
  14. data/examples/lambda_calculus/arithmetic.treetop +97 -0
  15. data/examples/lambda_calculus/arithmetic_node_classes.rb +7 -0
  16. data/examples/lambda_calculus/arithmetic_test.rb +54 -0
  17. data/examples/lambda_calculus/lambda_calculus +0 -0
  18. data/examples/lambda_calculus/lambda_calculus.rb +718 -0
  19. data/examples/lambda_calculus/lambda_calculus.treetop +132 -0
  20. data/examples/lambda_calculus/lambda_calculus_node_classes.rb +5 -0
  21. data/examples/lambda_calculus/lambda_calculus_test.rb +89 -0
  22. data/examples/lambda_calculus/test_helper.rb +18 -0
  23. data/lib/treetop.rb +8 -0
  24. data/lib/treetop/bootstrap_gen_1_metagrammar.rb +45 -0
  25. data/lib/treetop/compiler.rb +6 -0
  26. data/lib/treetop/compiler/grammar_compiler.rb +40 -0
  27. data/lib/treetop/compiler/lexical_address_space.rb +17 -0
  28. data/lib/treetop/compiler/metagrammar.rb +2887 -0
  29. data/lib/treetop/compiler/metagrammar.treetop +404 -0
  30. data/lib/treetop/compiler/node_classes.rb +19 -0
  31. data/lib/treetop/compiler/node_classes/anything_symbol.rb +18 -0
  32. data/lib/treetop/compiler/node_classes/atomic_expression.rb +14 -0
  33. data/lib/treetop/compiler/node_classes/character_class.rb +19 -0
  34. data/lib/treetop/compiler/node_classes/choice.rb +31 -0
  35. data/lib/treetop/compiler/node_classes/declaration_sequence.rb +24 -0
  36. data/lib/treetop/compiler/node_classes/grammar.rb +28 -0
  37. data/lib/treetop/compiler/node_classes/inline_module.rb +27 -0
  38. data/lib/treetop/compiler/node_classes/nonterminal.rb +13 -0
  39. data/lib/treetop/compiler/node_classes/optional.rb +19 -0
  40. data/lib/treetop/compiler/node_classes/parenthesized_expression.rb +9 -0
  41. data/lib/treetop/compiler/node_classes/parsing_expression.rb +138 -0
  42. data/lib/treetop/compiler/node_classes/parsing_rule.rb +55 -0
  43. data/lib/treetop/compiler/node_classes/predicate.rb +45 -0
  44. data/lib/treetop/compiler/node_classes/repetition.rb +55 -0
  45. data/lib/treetop/compiler/node_classes/sequence.rb +68 -0
  46. data/lib/treetop/compiler/node_classes/terminal.rb +20 -0
  47. data/lib/treetop/compiler/node_classes/transient_prefix.rb +9 -0
  48. data/lib/treetop/compiler/node_classes/treetop_file.rb +9 -0
  49. data/lib/treetop/compiler/ruby_builder.rb +113 -0
  50. data/lib/treetop/ruby_extensions.rb +2 -0
  51. data/lib/treetop/ruby_extensions/string.rb +42 -0
  52. data/lib/treetop/runtime.rb +5 -0
  53. data/lib/treetop/runtime/compiled_parser.rb +87 -0
  54. data/lib/treetop/runtime/interval_skip_list.rb +4 -0
  55. data/lib/treetop/runtime/interval_skip_list/head_node.rb +15 -0
  56. data/lib/treetop/runtime/interval_skip_list/interval_skip_list.rb +200 -0
  57. data/lib/treetop/runtime/interval_skip_list/node.rb +164 -0
  58. data/lib/treetop/runtime/syntax_node.rb +72 -0
  59. data/lib/treetop/runtime/terminal_parse_failure.rb +16 -0
  60. data/lib/treetop/runtime/terminal_syntax_node.rb +17 -0
  61. metadata +119 -0
@@ -0,0 +1,164 @@
1
+ class IntervalSkipList
2
+ class Node < HeadNode
3
+ attr_accessor :key
4
+ attr_reader :markers, :endpoint_of
5
+
6
+ def initialize(key, height, path)
7
+ super(height)
8
+ @key = key
9
+ @markers = []
10
+ @endpoint_of = []
11
+ update_forward_pointers(path)
12
+ promote_markers(path)
13
+ end
14
+
15
+ def all_forward_markers
16
+ markers.flatten
17
+ end
18
+
19
+ def delete(path)
20
+ 0.upto(top_level) do |i|
21
+ path[i].forward[i] = forward[i]
22
+ end
23
+ demote_markers(path)
24
+ end
25
+
26
+ def propagate_length_change(length_change)
27
+ cur_node = self
28
+ while cur_node do
29
+ cur_node.key += length_change
30
+ cur_node = cur_node.forward[0]
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ def update_forward_pointers(path)
37
+ 0.upto(top_level) do |i|
38
+ forward[i] = path[i].forward[i]
39
+ path[i].forward[i] = self
40
+ end
41
+ end
42
+
43
+ def promote_markers(path)
44
+ promoted = []
45
+ new_promoted = []
46
+ 0.upto(top_level) do |i|
47
+ incoming_markers = path[i].forward_markers[i]
48
+ markers.concat(incoming_markers)
49
+
50
+ incoming_markers.each do |marker|
51
+ if can_be_promoted_higher?(marker, i)
52
+ new_promoted.push(marker)
53
+ forward[i].delete_marker_from_path(marker, i, forward[i+1])
54
+ else
55
+ forward_markers[i].push(marker)
56
+ end
57
+ end
58
+
59
+ promoted.each do |marker|
60
+ if can_be_promoted_higher?(marker, i)
61
+ new_promoted.push(marker)
62
+ forward[i].delete_marker_from_path(marker, i, forward[i+1])
63
+ else
64
+ forward_markers[i].push(marker)
65
+ end
66
+ end
67
+
68
+ promoted = new_promoted
69
+ new_promoted = []
70
+ end
71
+ end
72
+
73
+
74
+ def can_be_promoted_higher?(marker, level)
75
+ level < top_level && forward[level + 1] && forward[level + 1].markers.include?(marker)
76
+ end
77
+
78
+ def delete_marker_from_path(marker, level, terminus)
79
+ cur_node = self
80
+ until cur_node == terminus
81
+ cur_node.forward_markers[level].delete(marker)
82
+ cur_node.markers.delete(marker)
83
+ cur_node = cur_node.forward[level]
84
+ end
85
+ end
86
+
87
+ def demote_markers(path)
88
+ demote_inbound_markers(path)
89
+ demote_outbound_markers(path)
90
+ end
91
+
92
+ def demote_inbound_markers(path)
93
+ demoted = []
94
+ new_demoted = []
95
+
96
+ top_level.downto(0) do |i|
97
+ incoming_markers = path[i].forward_markers[i].dup
98
+ incoming_markers.each do |marker|
99
+ unless forward_node_with_marker_at_or_above_level?(marker, i)
100
+ path[i].forward_markers[i].delete(marker)
101
+ new_demoted.push(marker)
102
+ end
103
+ end
104
+
105
+ demoted.each do |marker|
106
+ path[i + 1].place_marker_on_inbound_path(marker, i, path[i])
107
+
108
+ if forward[i].markers.include?(marker)
109
+ path[i].forward_markers[i].push(marker)
110
+ else
111
+ new_demoted.push(marker)
112
+ end
113
+ end
114
+
115
+ demoted = new_demoted
116
+ new_demoted = []
117
+ end
118
+ end
119
+
120
+ def demote_outbound_markers(path)
121
+ demoted = []
122
+ new_demoted = []
123
+
124
+ top_level.downto(0) do |i|
125
+ forward_markers[i].each do |marker|
126
+ new_demoted.push(marker) unless path[i].forward_markers[i].include?(marker)
127
+ end
128
+
129
+ demoted.each do |marker|
130
+ forward[i].place_marker_on_outbound_path(marker, i, forward[i + 1])
131
+ new_demoted.push(marker) unless path[i].forward_markers[i].include?(marker)
132
+ end
133
+
134
+ demoted = new_demoted
135
+ new_demoted = []
136
+ end
137
+ end
138
+
139
+ def forward_node_with_marker_at_or_above_level?(marker, level)
140
+ level.upto(top_level) do |i|
141
+ return true if forward[i].markers.include?(marker)
142
+ end
143
+ false
144
+ end
145
+
146
+ def place_marker_on_outbound_path(marker, level, terminus)
147
+ cur_node = self
148
+ until cur_node == terminus
149
+ cur_node.forward_markers[level].push(marker)
150
+ cur_node.markers.push(marker)
151
+ cur_node = cur_node.forward[level]
152
+ end
153
+ end
154
+
155
+ def place_marker_on_inbound_path(marker, level, terminus)
156
+ cur_node = self
157
+ until cur_node == terminus
158
+ cur_node.forward_markers[level].push(marker)
159
+ cur_node = cur_node.forward[level]
160
+ cur_node.markers.push(marker)
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,72 @@
1
+ module Treetop
2
+ module Runtime
3
+ class SyntaxNode
4
+ attr_reader :input, :interval, :elements
5
+ attr_accessor :parent
6
+
7
+ def initialize(input, interval, elements = nil)
8
+ @input = input
9
+ @interval = interval
10
+ if @elements = elements
11
+ elements.each do |element|
12
+ element.parent = self
13
+ end
14
+ end
15
+ end
16
+
17
+ def terminal?
18
+ @elements.nil?
19
+ end
20
+
21
+ def nonterminal?
22
+ !terminal?
23
+ end
24
+
25
+ def text_value
26
+ input[interval]
27
+ end
28
+
29
+ def empty?
30
+ interval.first == interval.last && interval.exclude_end?
31
+ end
32
+
33
+ def extension_modules
34
+ local_extensions =
35
+ class <<self
36
+ included_modules-Object.included_modules
37
+ end
38
+ if local_extensions.size > 0
39
+ local_extensions
40
+ else
41
+ [] # There weren't any; must be a literal node
42
+ end
43
+ end
44
+
45
+ def inspect(indent="")
46
+ em = extension_modules
47
+ interesting_methods = methods-[em.last ? em.last.methods : nil]-self.class.instance_methods
48
+ im = interesting_methods.size > 0 ? " (#{interesting_methods.join(",")})" : ""
49
+ tv = text_value
50
+ tv = "...#{tv[-20..-1]}" if tv.size > 20
51
+
52
+ indent +
53
+ self.class.to_s.sub(/.*:/,'') +
54
+ em.map{|m| "+"+m.to_s.sub(/.*:/,'')}*"" +
55
+ " offset=#{interval.first}" +
56
+ ", #{tv.inspect}" +
57
+ im +
58
+ (elements && elements.size > 0 ?
59
+ ":" +
60
+ (@elements||[]).map{|e|
61
+ begin
62
+ "\n"+e.inspect(indent+" ")
63
+ rescue # Defend against inspect not taking a parameter
64
+ "\n"+indent+" "+e.inspect
65
+ end
66
+ }.join("") :
67
+ ""
68
+ )
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,16 @@
1
+ module Treetop
2
+ module Runtime
3
+ class TerminalParseFailure
4
+ attr_reader :index, :expected_string
5
+
6
+ def initialize(index, expected_string)
7
+ @index = index
8
+ @expected_string = expected_string
9
+ end
10
+
11
+ def to_s
12
+ "String matching #{expected_string} expected."
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Treetop
2
+ module Runtime
3
+ class TerminalSyntaxNode < SyntaxNode
4
+
5
+ def initialize(input, interval)
6
+ super(input, interval, [])
7
+ end
8
+
9
+ def inspect(indent="")
10
+ indent+
11
+ self.class.to_s.sub(/.*:/,'') +
12
+ " offset=#{interval.first}" +
13
+ " #{text_value.inspect}"
14
+ end
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cognita-treetop
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.4
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Sobo
8
+ autorequire: treetop
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: nathansobo@gmail.com
18
+ executables:
19
+ - tt
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - Rakefile
27
+ - lib/treetop.rb
28
+ - lib/treetop
29
+ - lib/treetop/bootstrap_gen_1_metagrammar.rb
30
+ - lib/treetop/compiler.rb
31
+ - lib/treetop/compiler
32
+ - lib/treetop/compiler/grammar_compiler.rb
33
+ - lib/treetop/compiler/lexical_address_space.rb
34
+ - lib/treetop/compiler/metagrammar.rb
35
+ - lib/treetop/compiler/metagrammar.treetop
36
+ - lib/treetop/compiler/node_classes.rb
37
+ - lib/treetop/compiler/node_classes
38
+ - lib/treetop/compiler/node_classes/anything_symbol.rb
39
+ - lib/treetop/compiler/node_classes/atomic_expression.rb
40
+ - lib/treetop/compiler/node_classes/character_class.rb
41
+ - lib/treetop/compiler/node_classes/choice.rb
42
+ - lib/treetop/compiler/node_classes/declaration_sequence.rb
43
+ - lib/treetop/compiler/node_classes/grammar.rb
44
+ - lib/treetop/compiler/node_classes/inline_module.rb
45
+ - lib/treetop/compiler/node_classes/nonterminal.rb
46
+ - lib/treetop/compiler/node_classes/optional.rb
47
+ - lib/treetop/compiler/node_classes/parenthesized_expression.rb
48
+ - lib/treetop/compiler/node_classes/parsing_expression.rb
49
+ - lib/treetop/compiler/node_classes/parsing_rule.rb
50
+ - lib/treetop/compiler/node_classes/predicate.rb
51
+ - lib/treetop/compiler/node_classes/repetition.rb
52
+ - lib/treetop/compiler/node_classes/sequence.rb
53
+ - lib/treetop/compiler/node_classes/terminal.rb
54
+ - lib/treetop/compiler/node_classes/transient_prefix.rb
55
+ - lib/treetop/compiler/node_classes/treetop_file.rb
56
+ - lib/treetop/compiler/ruby_builder.rb
57
+ - lib/treetop/ruby_extensions.rb
58
+ - lib/treetop/ruby_extensions
59
+ - lib/treetop/ruby_extensions/string.rb
60
+ - lib/treetop/runtime.rb
61
+ - lib/treetop/runtime
62
+ - lib/treetop/runtime/compiled_parser.rb
63
+ - lib/treetop/runtime/interval_skip_list.rb
64
+ - lib/treetop/runtime/interval_skip_list
65
+ - lib/treetop/runtime/interval_skip_list/head_node.rb
66
+ - lib/treetop/runtime/interval_skip_list/interval_skip_list.rb
67
+ - lib/treetop/runtime/interval_skip_list/node.rb
68
+ - lib/treetop/runtime/syntax_node.rb
69
+ - lib/treetop/runtime/terminal_parse_failure.rb
70
+ - lib/treetop/runtime/terminal_syntax_node.rb
71
+ - bin/tt
72
+ - doc/contributing_and_planned_features.markdown
73
+ - doc/grammar_composition.markdown
74
+ - doc/index.markdown
75
+ - doc/pitfalls_and_advanced_techniques.markdown
76
+ - doc/semantic_interpretation.markdown
77
+ - doc/site.rb
78
+ - doc/sitegen.rb
79
+ - doc/syntactic_recognition.markdown
80
+ - doc/using_in_ruby.markdown
81
+ - examples/lambda_calculus
82
+ - examples/lambda_calculus/arithmetic.rb
83
+ - examples/lambda_calculus/arithmetic.treetop
84
+ - examples/lambda_calculus/arithmetic_node_classes.rb
85
+ - examples/lambda_calculus/arithmetic_test.rb
86
+ - examples/lambda_calculus/lambda_calculus
87
+ - examples/lambda_calculus/lambda_calculus.rb
88
+ - examples/lambda_calculus/lambda_calculus.treetop
89
+ - examples/lambda_calculus/lambda_calculus_node_classes.rb
90
+ - examples/lambda_calculus/lambda_calculus_test.rb
91
+ - examples/lambda_calculus/test_helper.rb
92
+ has_rdoc: false
93
+ homepage: http://github.com/cognita/treetop
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ version:
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "0"
110
+ version:
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.2.0
115
+ signing_key:
116
+ specification_version: 2
117
+ summary: A Ruby-based text parsing and interpretation DSL
118
+ test_files: []
119
+