treetop 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. data/README +3 -0
  2. data/Rakefile +35 -0
  3. data/bin/tt +25 -0
  4. data/lib/treetop.rb +10 -6
  5. data/lib/treetop/compiler.rb +7 -0
  6. data/lib/treetop/compiler/grammar_compiler.rb +21 -0
  7. data/lib/treetop/compiler/lexical_address_space.rb +17 -0
  8. data/lib/treetop/compiler/load_grammar.rb +7 -0
  9. data/lib/treetop/compiler/metagrammar.rb +2441 -0
  10. data/lib/treetop/compiler/metagrammar.treetop +384 -0
  11. data/lib/treetop/compiler/node_classes.rb +18 -0
  12. data/lib/treetop/compiler/node_classes/anything_symbol.rb +10 -0
  13. data/lib/treetop/compiler/node_classes/atomic_expression.rb +9 -0
  14. data/lib/treetop/compiler/node_classes/character_class.rb +10 -0
  15. data/lib/treetop/compiler/node_classes/choice.rb +31 -0
  16. data/lib/treetop/compiler/node_classes/declaration_sequence.rb +24 -0
  17. data/lib/treetop/compiler/node_classes/grammar.rb +28 -0
  18. data/lib/treetop/compiler/node_classes/inline_module.rb +27 -0
  19. data/lib/treetop/compiler/node_classes/nonterminal.rb +11 -0
  20. data/lib/treetop/compiler/node_classes/optional.rb +19 -0
  21. data/lib/treetop/compiler/node_classes/parenthesized_expression.rb +9 -0
  22. data/lib/treetop/compiler/node_classes/parsing_expression.rb +132 -0
  23. data/lib/treetop/compiler/node_classes/parsing_rule.rb +55 -0
  24. data/lib/treetop/compiler/node_classes/predicate.rb +45 -0
  25. data/lib/treetop/compiler/node_classes/repetition.rb +56 -0
  26. data/lib/treetop/compiler/node_classes/sequence.rb +64 -0
  27. data/lib/treetop/compiler/node_classes/terminal.rb +10 -0
  28. data/lib/treetop/compiler/node_classes/treetop_file.rb +9 -0
  29. data/lib/treetop/compiler/ruby_builder.rb +109 -0
  30. data/lib/treetop/ruby_extensions.rb +2 -0
  31. data/lib/treetop/ruby_extensions/string.rb +19 -0
  32. data/lib/treetop/runtime.rb +9 -0
  33. data/lib/treetop/runtime/compiled_parser.rb +66 -0
  34. data/lib/treetop/runtime/node_cache.rb +27 -0
  35. data/lib/treetop/runtime/parse_cache.rb +19 -0
  36. data/lib/treetop/runtime/parse_failure.rb +32 -0
  37. data/lib/treetop/runtime/parse_result.rb +30 -0
  38. data/lib/treetop/runtime/syntax_node.rb +53 -0
  39. data/lib/treetop/runtime/terminal_parse_failure.rb +33 -0
  40. data/lib/treetop/runtime/terminal_syntax_node.rb +12 -0
  41. data/test/compilation_target/target.rb +143 -0
  42. data/test/compilation_target/target.treetop +15 -0
  43. data/test/compilation_target/target_test.rb +56 -0
  44. data/test/compiler/and_predicate_test.rb +33 -0
  45. data/test/compiler/anything_symbol_test.rb +24 -0
  46. data/test/compiler/character_class_test.rb +45 -0
  47. data/test/compiler/choice_test.rb +49 -0
  48. data/test/compiler/circular_compilation_test.rb +20 -0
  49. data/test/compiler/failure_propagation_functional_test.rb +20 -0
  50. data/test/compiler/grammar_compiler_test.rb +58 -0
  51. data/test/compiler/grammar_test.rb +33 -0
  52. data/test/compiler/nonterminal_symbol_test.rb +15 -0
  53. data/test/compiler/not_predicate_test.rb +35 -0
  54. data/test/compiler/one_or_more_test.rb +30 -0
  55. data/test/compiler/optional_test.rb +32 -0
  56. data/test/compiler/parsing_rule_test.rb +30 -0
  57. data/test/compiler/sequence_test.rb +68 -0
  58. data/test/compiler/terminal_symbol_test.rb +35 -0
  59. data/test/compiler/test_grammar.treetop +7 -0
  60. data/test/compiler/zero_or_more_test.rb +51 -0
  61. data/test/composition/a.treetop +11 -0
  62. data/test/composition/b.treetop +11 -0
  63. data/test/composition/c.treetop +10 -0
  64. data/test/composition/d.treetop +10 -0
  65. data/test/composition/grammar_composition_test.rb +23 -0
  66. data/test/parser/syntax_node_test.rb +53 -0
  67. data/test/parser/terminal_parse_failure_test.rb +22 -0
  68. data/test/ruby_extensions/string_test.rb +33 -0
  69. data/test/screw/Rakefile +16 -0
  70. data/test/screw/unit.rb +37 -0
  71. data/test/screw/unit/assertion_failed_error.rb +14 -0
  72. data/test/screw/unit/assertions.rb +615 -0
  73. data/test/screw/unit/auto_runner.rb +227 -0
  74. data/test/screw/unit/collector.rb +45 -0
  75. data/test/screw/unit/collector/dir.rb +107 -0
  76. data/test/screw/unit/collector/objectspace.rb +28 -0
  77. data/test/screw/unit/error.rb +48 -0
  78. data/test/screw/unit/failure.rb +45 -0
  79. data/test/screw/unit/sugar.rb +25 -0
  80. data/test/screw/unit/test_case.rb +176 -0
  81. data/test/screw/unit/test_result.rb +73 -0
  82. data/test/screw/unit/test_suite.rb +70 -0
  83. data/test/screw/unit/ui.rb +4 -0
  84. data/test/screw/unit/ui/console/test_runner.rb +118 -0
  85. data/test/screw/unit/ui/fox/test_runner.rb +268 -0
  86. data/test/screw/unit/ui/gtk/test_runner.rb +416 -0
  87. data/test/screw/unit/ui/gtk2/testrunner.rb +465 -0
  88. data/test/screw/unit/ui/test_runner_mediator.rb +58 -0
  89. data/test/screw/unit/ui/test_runner_utilities.rb +46 -0
  90. data/test/screw/unit/ui/tk/test_runner.rb +260 -0
  91. data/test/screw/unit/util.rb +4 -0
  92. data/test/screw/unit/util/backtrace_filter.rb +40 -0
  93. data/test/screw/unit/util/observable.rb +82 -0
  94. data/test/screw/unit/util/proc_wrapper.rb +48 -0
  95. data/test/test_helper.rb +89 -0
  96. metadata +127 -69
  97. data/lib/treetop/api.rb +0 -3
  98. data/lib/treetop/api/load_grammar.rb +0 -16
  99. data/lib/treetop/api/malformed_grammar_exception.rb +0 -9
  100. data/lib/treetop/grammar.rb +0 -7
  101. data/lib/treetop/grammar/grammar.rb +0 -48
  102. data/lib/treetop/grammar/grammar_builder.rb +0 -35
  103. data/lib/treetop/grammar/parsing_expression_builder.rb +0 -5
  104. data/lib/treetop/grammar/parsing_expression_builder_helper.rb +0 -121
  105. data/lib/treetop/grammar/parsing_expressions.rb +0 -18
  106. data/lib/treetop/grammar/parsing_expressions/and_predicate.rb +0 -17
  107. data/lib/treetop/grammar/parsing_expressions/anything_symbol.rb +0 -20
  108. data/lib/treetop/grammar/parsing_expressions/character_class.rb +0 -24
  109. data/lib/treetop/grammar/parsing_expressions/node_instantiating_parsing_expression.rb +0 -14
  110. data/lib/treetop/grammar/parsing_expressions/node_propagating_parsing_expression.rb +0 -4
  111. data/lib/treetop/grammar/parsing_expressions/nonterminal_symbol.rb +0 -42
  112. data/lib/treetop/grammar/parsing_expressions/not_predicate.rb +0 -18
  113. data/lib/treetop/grammar/parsing_expressions/one_or_more.rb +0 -12
  114. data/lib/treetop/grammar/parsing_expressions/optional.rb +0 -14
  115. data/lib/treetop/grammar/parsing_expressions/ordered_choice.rb +0 -27
  116. data/lib/treetop/grammar/parsing_expressions/parsing_expression.rb +0 -36
  117. data/lib/treetop/grammar/parsing_expressions/predicate.rb +0 -25
  118. data/lib/treetop/grammar/parsing_expressions/repeating_parsing_expression.rb +0 -29
  119. data/lib/treetop/grammar/parsing_expressions/sequence.rb +0 -41
  120. data/lib/treetop/grammar/parsing_expressions/terminal_parsing_expression.rb +0 -11
  121. data/lib/treetop/grammar/parsing_expressions/terminal_symbol.rb +0 -31
  122. data/lib/treetop/grammar/parsing_expressions/zero_or_more.rb +0 -11
  123. data/lib/treetop/grammar/parsing_rule.rb +0 -10
  124. data/lib/treetop/metagrammar.rb +0 -2
  125. data/lib/treetop/metagrammar/metagrammar.rb +0 -14
  126. data/lib/treetop/metagrammar/metagrammar.treetop +0 -320
  127. data/lib/treetop/parser.rb +0 -11
  128. data/lib/treetop/parser/node_cache.rb +0 -25
  129. data/lib/treetop/parser/parse_cache.rb +0 -17
  130. data/lib/treetop/parser/parse_failure.rb +0 -22
  131. data/lib/treetop/parser/parse_result.rb +0 -26
  132. data/lib/treetop/parser/parser.rb +0 -24
  133. data/lib/treetop/parser/sequence_syntax_node.rb +0 -14
  134. data/lib/treetop/parser/syntax_node.rb +0 -31
  135. data/lib/treetop/parser/terminal_parse_failure.rb +0 -18
  136. data/lib/treetop/parser/terminal_syntax_node.rb +0 -7
  137. data/lib/treetop/protometagrammar.rb +0 -16
  138. data/lib/treetop/protometagrammar/anything_symbol_expression_builder.rb +0 -13
  139. data/lib/treetop/protometagrammar/block_expression_builder.rb +0 -17
  140. data/lib/treetop/protometagrammar/character_class_expression_builder.rb +0 -25
  141. data/lib/treetop/protometagrammar/grammar_expression_builder.rb +0 -38
  142. data/lib/treetop/protometagrammar/nonterminal_symbol_expression_builder.rb +0 -45
  143. data/lib/treetop/protometagrammar/ordered_choice_expression_builder.rb +0 -21
  144. data/lib/treetop/protometagrammar/parsing_rule_expression_builder.rb +0 -23
  145. data/lib/treetop/protometagrammar/parsing_rule_sequence_expression_builder.rb +0 -14
  146. data/lib/treetop/protometagrammar/prefix_expression_builder.rb +0 -25
  147. data/lib/treetop/protometagrammar/primary_expression_builder.rb +0 -71
  148. data/lib/treetop/protometagrammar/protometagrammar.rb +0 -25
  149. data/lib/treetop/protometagrammar/sequence_expression_builder.rb +0 -37
  150. data/lib/treetop/protometagrammar/suffix_expression_builder.rb +0 -33
  151. data/lib/treetop/protometagrammar/terminal_symbol_expression_builder.rb +0 -52
  152. data/lib/treetop/protometagrammar/trailing_block_expression_builder.rb +0 -30
  153. data/lib/treetop/ruby_extension.rb +0 -11
@@ -1,11 +0,0 @@
1
- dir = File.dirname(__FILE__)
2
- require "#{dir}/parser/parser"
3
- require "#{dir}/parser/parse_result"
4
- require "#{dir}/parser/syntax_node"
5
- require "#{dir}/parser/terminal_syntax_node"
6
- require "#{dir}/parser/sequence_syntax_node"
7
- require "#{dir}/parser/parse_failure"
8
- require "#{dir}/parser/node_cache"
9
- require "#{dir}/parser/parse_cache"
10
- require "#{dir}/parser/parse_failure"
11
- require "#{dir}/parser/terminal_parse_failure"
@@ -1,25 +0,0 @@
1
- module Treetop
2
- class NodeCache
3
- attr_reader :parse_results
4
-
5
- def initialize
6
- @parse_results = {}
7
- end
8
-
9
- def empty?
10
- parse_results.empty?
11
- end
12
-
13
- def store(parse_result)
14
- if parse_result.failure?
15
- parse_results[parse_result.index] = parse_result
16
- else
17
- parse_results[parse_result.interval.begin] = parse_result
18
- end
19
- end
20
-
21
- def [](start_index)
22
- parse_results[start_index]
23
- end
24
- end
25
- end
@@ -1,17 +0,0 @@
1
- module Treetop
2
- class ParseCache
3
- attr_reader :node_caches
4
-
5
- def initialize
6
- @node_caches = {}
7
- end
8
-
9
- def [](parsing_expression)
10
- node_caches[parsing_expression] ||= NodeCache.new
11
- end
12
-
13
- def empty?
14
- node_caches.empty?
15
- end
16
- end
17
- end
@@ -1,22 +0,0 @@
1
- module Treetop
2
- class ParseFailure < ParseResult
3
- attr_reader :index
4
-
5
- def initialize(index, nested_failures = [])
6
- super(nested_failures)
7
- @index = index
8
- end
9
-
10
- def success?
11
- false
12
- end
13
-
14
- def failure?
15
- true
16
- end
17
-
18
- def interval
19
- @interval ||= (index...index)
20
- end
21
- end
22
- end
@@ -1,26 +0,0 @@
1
- module Treetop
2
- class ParseResult
3
- attr_reader :nested_failures
4
-
5
- def initialize(nested_failures = [])
6
- @nested_failures = select_failures_at_maximum_index(nested_failures)
7
- end
8
-
9
- protected
10
- def select_failures_at_maximum_index(failures)
11
- maximum_index = 0
12
- failures_at_maximum_index = []
13
-
14
- failures.each do |failure|
15
- if failure.index > maximum_index
16
- failures_at_maximum_index = [failure]
17
- maximum_index = failure.index
18
- elsif failure.index == maximum_index
19
- failures_at_maximum_index << failure
20
- end
21
- end
22
-
23
- return failures_at_maximum_index
24
- end
25
- end
26
- end
@@ -1,24 +0,0 @@
1
- module Treetop
2
- class Parser
3
- attr_reader :grammar, :parse_cache
4
-
5
- def initialize(grammar)
6
- @grammar = grammar
7
- @parse_cache = ParseCache.new
8
- end
9
-
10
- def parse(input)
11
- @parse_cache = ParseCache.new
12
- result = grammar.root.parse_at(input, 0, self)
13
- if result.success? and result.interval.end == input.size
14
- return result
15
- else
16
- return ParseFailure.new(result.interval.end, result.nested_failures)
17
- end
18
- end
19
-
20
- def node_cache_for(parsing_expression)
21
- parse_cache[parsing_expression]
22
- end
23
- end
24
- end
@@ -1,14 +0,0 @@
1
- module Treetop
2
- class SequenceSyntaxNode < SyntaxNode
3
- attr_reader :elements
4
-
5
- def initialize(input, interval, elements, nested_failures = [])
6
- super(input, interval, nested_failures)
7
- @elements = elements
8
- end
9
-
10
- def empty?
11
- elements.empty?
12
- end
13
- end
14
- end
@@ -1,31 +0,0 @@
1
- module Treetop
2
- class SyntaxNode < ParseResult
3
- attr_reader :input, :interval
4
-
5
- def initialize(input, interval, nested_failures = [])
6
- super(nested_failures)
7
- @input = input
8
- @interval = interval
9
- end
10
-
11
- def update_nested_failures(failures)
12
- @nested_failures = select_failures_at_maximum_index(nested_failures + failures)
13
- end
14
-
15
- def text_value
16
- input[interval]
17
- end
18
-
19
- def success?
20
- true
21
- end
22
-
23
- def failure?
24
- false
25
- end
26
-
27
- def epsilon?
28
- false
29
- end
30
- end
31
- end
@@ -1,18 +0,0 @@
1
- module Treetop
2
- class TerminalParseFailure < ParseFailure
3
- attr_reader :expression
4
-
5
- def initialize(index, expression)
6
- super(index)
7
- @expression = expression
8
- end
9
-
10
- def nested_failures
11
- [self]
12
- end
13
-
14
- def to_s
15
- "String matching #{expression} expected at position #{index}."
16
- end
17
- end
18
- end
@@ -1,7 +0,0 @@
1
- module Treetop
2
- class TerminalSyntaxNode < SyntaxNode
3
- def epsilon?
4
- text_value.eql? ""
5
- end
6
- end
7
- end
@@ -1,16 +0,0 @@
1
- dir = File.dirname(__FILE__)
2
- require "#{dir}/protometagrammar/protometagrammar"
3
- require "#{dir}/protometagrammar/grammar_expression_builder"
4
- require "#{dir}/protometagrammar/parsing_rule_sequence_expression_builder"
5
- require "#{dir}/protometagrammar/parsing_rule_expression_builder"
6
- require "#{dir}/protometagrammar/primary_expression_builder"
7
- require "#{dir}/protometagrammar/nonterminal_symbol_expression_builder"
8
- require "#{dir}/protometagrammar/terminal_symbol_expression_builder"
9
- require "#{dir}/protometagrammar/character_class_expression_builder"
10
- require "#{dir}/protometagrammar/anything_symbol_expression_builder"
11
- require "#{dir}/protometagrammar/sequence_expression_builder"
12
- require "#{dir}/protometagrammar/suffix_expression_builder"
13
- require "#{dir}/protometagrammar/prefix_expression_builder"
14
- require "#{dir}/protometagrammar/ordered_choice_expression_builder"
15
- require "#{dir}/protometagrammar/block_expression_builder"
16
- require "#{dir}/protometagrammar/trailing_block_expression_builder"
@@ -1,13 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class AnythingSymbolExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- exp(".") do
6
- def value(grammar = nil)
7
- AnythingSymbol.new
8
- end
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,17 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class BlockExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- seq('{', zero_or_more(choice(:block, anything_but_a_brace)), '}') do
6
- def value
7
- elements[1].text_value
8
- end
9
- end
10
- end
11
-
12
- def anything_but_a_brace
13
- seq(notp(choice('{', '}')), any)
14
- end
15
- end
16
- end
17
- end
@@ -1,25 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class CharacterClassExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- character_class
6
- end
7
-
8
- def character_class
9
- seq('[', one_or_more(char_class_char), ']') do
10
- def value(grammar = nil)
11
- CharacterClass.new(characters)
12
- end
13
-
14
- def characters
15
- elements[1].text_value
16
- end
17
- end
18
- end
19
-
20
- def char_class_char
21
- seq(notp(']'), choice(escaped(']'), any))
22
- end
23
- end
24
- end
25
- end
@@ -1,38 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class GrammarExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- seq('grammar', :space, grammar_name, :parsing_rule_sequence, optional(:space), 'end') do
6
- def value
7
- grammar = Grammar.new(grammar_name)
8
- parsing_rules(grammar).each do |parsing_rule|
9
- grammar.add_parsing_rule(parsing_rule)
10
- end
11
- return grammar
12
- end
13
-
14
- def grammar_name
15
- name_string = elements[2].text_value.chop
16
-
17
- if name_string.empty?
18
- nil
19
- else
20
- name_string.to_sym
21
- end
22
- end
23
-
24
- def parsing_rules(grammar)
25
- elements[3].value(grammar)
26
- end
27
- end
28
- end
29
-
30
- def grammar_name
31
- optional(
32
- seq(char_class('A-Z'),
33
- zero_or_more(char_class('a-z0-9_')),
34
- :space))
35
- end
36
- end
37
- end
38
- end
@@ -1,45 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class NonterminalSymbolExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- seq(notp(naked_keyword), nonterminal_symbol) do
6
- def value(grammar)
7
- nonterminal_symbol.value(grammar)
8
- end
9
-
10
- def nonterminal_symbol
11
- elements[1]
12
- end
13
- end
14
- end
15
-
16
- def naked_keyword
17
- seq(:keyword, notp(alphanumeric_char))
18
- end
19
-
20
- def nonterminal_symbol
21
- seq(alpha_char, zero_or_more(alphanumeric_char)) do
22
- def value(grammar)
23
- grammar.nonterminal_symbol(name)
24
- end
25
-
26
- def name
27
- text_value.to_sym
28
- end
29
- end
30
- end
31
-
32
- def alpha_char
33
- char_class('A-Za-z_')
34
- end
35
-
36
- def numeric_char
37
- char_class('0-9')
38
- end
39
-
40
- def alphanumeric_char
41
- choice(alpha_char, numeric_char)
42
- end
43
- end
44
- end
45
- end
@@ -1,21 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class OrderedChoiceExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- choice(ordered_choice, :sequence)
6
- end
7
-
8
- def ordered_choice
9
- two_or_more_delimited(:sequence, seq(optional(:space), "/", optional(:space))) do
10
- def value(grammar)
11
- OrderedChoice.new(element_values(grammar))
12
- end
13
-
14
- def element_values(grammar)
15
- elements.collect { |element| element.value(grammar) }
16
- end
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,23 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class ParsingRuleExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- seq('rule', :space, :nonterminal_symbol, :space, :ordered_choice, :space, 'end') do
6
- def value(grammar)
7
- ParsingRule.new(nonterminal_symbol.value(grammar),
8
- parsing_expression.value(grammar))
9
- end
10
-
11
- def nonterminal_symbol
12
- elements[2]
13
- end
14
-
15
- def parsing_expression
16
- elements[4]
17
- end
18
- end
19
- end
20
- end
21
- end
22
- end
23
-
@@ -1,14 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class ParsingRuleSequenceExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- zero_or_more_delimited(:parsing_rule, :space) do
6
- def value(grammar)
7
- elements.collect {|element| element.value(grammar) }
8
- end
9
- end
10
- end
11
- end
12
- end
13
- end
14
-
@@ -1,25 +0,0 @@
1
- module Treetop
2
- class Protometagrammar
3
- class PrefixExpressionBuilder < ParsingExpressionBuilder
4
- def build
5
- choice(and_predicate, not_predicate)
6
- end
7
-
8
- def and_predicate
9
- exp('&') do
10
- def value(parsing_expression)
11
- parsing_expression.and_predicate
12
- end
13
- end
14
- end
15
-
16
- def not_predicate
17
- exp('!') do
18
- def value(parsing_expression)
19
- parsing_expression.not_predicate
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end