treetop 1.1.4 → 1.2.0

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 (39) hide show
  1. data/README +1 -1
  2. data/Rakefile +3 -2
  3. data/doc/contributing_and_planned_features.markdown +1 -1
  4. data/doc/using_in_ruby.markdown +2 -2
  5. data/examples/lambda_calculus/arithmetic.rb +551 -0
  6. data/examples/lambda_calculus/arithmetic_test.rb +1 -1
  7. data/examples/lambda_calculus/lambda_calculus.rb +39 -72
  8. data/examples/lambda_calculus/lambda_calculus_test.rb +2 -2
  9. data/examples/lambda_calculus/test_helper.rb +3 -3
  10. data/lib/treetop.rb +3 -0
  11. data/lib/treetop/bootstrap_gen_1_metagrammar.rb +22 -14
  12. data/lib/treetop/compiler.rb +1 -2
  13. data/lib/treetop/compiler/grammar_compiler.rb +12 -5
  14. data/lib/treetop/compiler/metagrammar.rb +931 -558
  15. data/lib/treetop/compiler/metagrammar.treetop +26 -6
  16. data/lib/treetop/compiler/node_classes/anything_symbol.rb +10 -2
  17. data/lib/treetop/compiler/node_classes/atomic_expression.rb +4 -0
  18. data/lib/treetop/compiler/node_classes/character_class.rb +10 -1
  19. data/lib/treetop/compiler/node_classes/choice.rb +2 -4
  20. data/lib/treetop/compiler/node_classes/parsing_expression.rb +8 -17
  21. data/lib/treetop/compiler/node_classes/parsing_rule.rb +3 -3
  22. data/lib/treetop/compiler/node_classes/predicate.rb +1 -1
  23. data/lib/treetop/compiler/node_classes/repetition.rb +3 -4
  24. data/lib/treetop/compiler/node_classes/sequence.rb +4 -4
  25. data/lib/treetop/compiler/node_classes/terminal.rb +11 -1
  26. data/lib/treetop/compiler/ruby_builder.rb +2 -2
  27. data/lib/treetop/ruby_extensions.rb +1 -1
  28. data/lib/treetop/runtime.rb +0 -3
  29. data/lib/treetop/runtime/compiled_parser.rb +42 -34
  30. data/lib/treetop/runtime/node_cache.rb +1 -1
  31. data/lib/treetop/runtime/syntax_node.rb +51 -32
  32. data/lib/treetop/runtime/terminal_parse_failure.rb +7 -24
  33. data/lib/treetop/runtime/terminal_syntax_node.rb +7 -2
  34. metadata +12 -7
  35. data/examples/TALK +0 -33
  36. data/lib/treetop/compiler/load_grammar.rb +0 -7
  37. data/lib/treetop/compiler/metagrammar. +0 -0
  38. data/lib/treetop/runtime/parse_failure.rb +0 -32
  39. data/lib/treetop/runtime/parse_result.rb +0 -30
@@ -12,7 +12,7 @@ module Treetop
12
12
  end
13
13
 
14
14
  def store(parse_result)
15
- if parse_result.failure?
15
+ if parse_result.nil?
16
16
  parse_results[parse_result.index] = parse_result
17
17
  else
18
18
  parse_results[parse_result.interval.begin] = parse_result
@@ -1,53 +1,72 @@
1
1
  module Treetop
2
2
  module Runtime
3
- class SyntaxNode < ParseResult
3
+ class SyntaxNode
4
4
  attr_reader :input, :interval
5
-
6
- def initialize(input, interval, elements = nil, nested_results = elements)
7
- super(input, nested_results || [])
5
+
6
+ def initialize(input, interval, elements = nil)
7
+ @input = input
8
8
  @interval = interval
9
9
  @elements = elements
10
10
  end
11
-
12
- def success?
13
- true
14
- end
15
-
16
- def failure?
17
- false
18
- end
19
11
 
20
12
  def terminal?
21
13
  @elements.nil?
22
14
  end
23
-
15
+
24
16
  def nonterminal?
25
17
  !terminal?
26
18
  end
27
-
19
+
28
20
  def elements
29
- @elements || [self]
21
+ @elements
30
22
  end
31
-
23
+
32
24
  def text_value
33
25
  input[interval]
34
26
  end
35
-
36
- def update_nested_results(nested_results)
37
- new_nested_failures = collect_nested_failures_at_maximum_index(nested_results)
38
-
39
- return if new_nested_failures.empty?
40
- @nested_failures = new_nested_failures and return if nested_failures.empty?
41
-
42
- current_nested_failure_index = nested_failures.first.index
43
- new_nested_failure_index = new_nested_failures.first.index
44
-
45
- if new_nested_failure_index > current_nested_failure_index
46
- @nested_failures = new_nested_failures
47
- elsif new_nested_failure_index == current_nested_failure_index
48
- @nested_failures += new_nested_failures
49
- end
27
+
28
+ def empty?
29
+ interval.first == interval.last && interval.exclude_end?
30
+ end
31
+
32
+ def extension_modules
33
+ local_extensions =
34
+ class <<self
35
+ included_modules-Object.included_modules
36
+ end
37
+ if local_extensions.size > 0
38
+ local_extensions
39
+ else
40
+ [] # There weren't any; must be a literal node
41
+ end
42
+ end
43
+
44
+ def inspect(indent="")
45
+ em = extension_modules
46
+ interesting_methods = methods-[em.last ? em.last.methods : nil]-self.class.instance_methods
47
+ im = interesting_methods.size > 0 ? " (#{interesting_methods.join(",")})" : ""
48
+ tv = text_value
49
+ tv = "...#{tv[-20..-1]}" if tv.size > 20
50
+
51
+ indent +
52
+ self.class.to_s.sub(/.*:/,'') +
53
+ em.map{|m| "+"+m.to_s.sub(/.*:/,'')}*"" +
54
+ " offset=#{interval.first}" +
55
+ ", #{tv.inspect}" +
56
+ im +
57
+ (elements && elements.size > 0 ?
58
+ ":" +
59
+ (@elements||[]).map{|e|
60
+ begin
61
+ "\n"+e.inspect(indent+" ")
62
+ rescue # Defend against inspect not taking a parameter
63
+ "\n"+indent+" "+e.inspect
64
+ end
65
+ }.join("") :
66
+ ""
67
+ )
68
+
50
69
  end
51
70
  end
52
71
  end
53
- end
72
+ end
@@ -1,33 +1,16 @@
1
1
  module Treetop
2
2
  module Runtime
3
- class TerminalParseFailure < ParseFailure
4
- attr_reader :expected_string
3
+ class TerminalParseFailure
4
+ attr_reader :index, :expected_string
5
5
 
6
- def initialize(input, index, expected_string)
7
- super(input, index)
8
- @expected_string = expected_string
6
+ def initialize(index, expected_string)
7
+ @index = index
8
+ @expected_string = expected_string
9
9
  end
10
10
 
11
- def nested_failures
12
- [self]
13
- end
14
-
15
11
  def to_s
16
- "String matching #{expected_string} expected at line #{line}, column #{column} (index #{index})."
17
- end
18
-
19
- def ==(other_failure)
20
- eql?(other_failure)
21
- end
22
-
23
- def eql?(other_failure)
24
- return false unless other_failure.instance_of?(TerminalParseFailure)
25
- expected_string.eql?(other_failure.expected_string) && index.eql?(other_failure.index)
26
- end
27
-
28
- def hash
29
- [index, expected_string].hash
12
+ "String matching #{expected_string} expected."
30
13
  end
31
14
  end
32
15
  end
33
- end
16
+ end
@@ -6,7 +6,12 @@ module Treetop
6
6
  super(input, interval, [])
7
7
  end
8
8
 
9
-
9
+ def inspect(indent="")
10
+ indent+
11
+ self.class.to_s.sub(/.*:/,'') +
12
+ " offset=#{interval.first}" +
13
+ " #{text_value.inspect}"
14
+ end
10
15
  end
11
16
  end
12
- end
17
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: treetop
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.4
7
- date: 2007-12-02 00:00:00 -08:00
6
+ version: 1.2.0
7
+ date: 2008-01-11 00:00:00 -08:00
8
8
  summary: A Ruby-based text parsing and interpretation DSL
9
9
  require_paths:
10
10
  - lib
@@ -42,8 +42,6 @@ files:
42
42
  - lib/treetop/runtime.rb
43
43
  - lib/treetop/compiler/grammar_compiler.rb
44
44
  - lib/treetop/compiler/lexical_address_space.rb
45
- - lib/treetop/compiler/load_grammar.rb
46
- - lib/treetop/compiler/metagrammar.
47
45
  - lib/treetop/compiler/metagrammar.rb
48
46
  - lib/treetop/compiler/metagrammar.treetop
49
47
  - lib/treetop/compiler/node_classes
@@ -70,8 +68,6 @@ files:
70
68
  - lib/treetop/runtime/compiled_parser.rb
71
69
  - lib/treetop/runtime/node_cache.rb
72
70
  - lib/treetop/runtime/parse_cache.rb
73
- - lib/treetop/runtime/parse_failure.rb
74
- - lib/treetop/runtime/parse_result.rb
75
71
  - lib/treetop/runtime/syntax_node.rb
76
72
  - lib/treetop/runtime/terminal_parse_failure.rb
77
73
  - lib/treetop/runtime/terminal_syntax_node.rb
@@ -93,7 +89,7 @@ files:
93
89
  - doc/images/paren_language_output.png
94
90
  - doc/images/top_background.png
95
91
  - examples/lambda_calculus
96
- - examples/TALK
92
+ - examples/lambda_calculus/arithmetic.rb
97
93
  - examples/lambda_calculus/arithmetic.treetop
98
94
  - examples/lambda_calculus/arithmetic_node_classes.rb
99
95
  - examples/lambda_calculus/arithmetic_test.rb
@@ -125,3 +121,12 @@ dependencies:
125
121
  - !ruby/object:Gem::Version
126
122
  version: 2.0.2
127
123
  version:
124
+ - !ruby/object:Gem::Dependency
125
+ name: polyglot
126
+ version_requirement:
127
+ version_requirements: !ruby/object:Gem::Version::Requirement
128
+ requirements:
129
+ - - ">"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.0.0
132
+ version:
data/examples/TALK DELETED
@@ -1,33 +0,0 @@
1
- Before talk?
2
- ------------
3
- - non node-instantiating expressions extend their results with inline modules
4
- - allow load_grammar and require statements at the top of treetop files
5
- - deal with facets versioning issue
6
-
7
-
8
-
9
- Function application... the left-recursion problem
10
- --------------------------------------------------
11
- f g x
12
-
13
- rule expression
14
- application / function / variable
15
- end
16
-
17
- rule application
18
- expression space expression
19
- end
20
-
21
- Function application... how can we avoid left recursion?
22
- --------------------------------------------------------
23
- rule expression
24
- application / function / variable
25
- end
26
-
27
- rule application
28
- operator space expression
29
- end
30
-
31
- rule operator
32
- function / variable
33
- end
@@ -1,7 +0,0 @@
1
- class Object
2
- def load_grammar(path)
3
- adjusted_path = path =~ /\.treetop\Z/ ? path : path + '.treetop'
4
- compiler = Treetop::Compiler::GrammarCompiler.new
5
- Object.class_eval(compiler.ruby_source(adjusted_path))
6
- end
7
- end
File without changes
@@ -1,32 +0,0 @@
1
- module Treetop
2
- module Runtime
3
- class ParseFailure < ParseResult
4
- attr_reader :index
5
-
6
- def initialize(input, index, nested_results = [])
7
- super(input, nested_results)
8
- @index = index
9
- end
10
-
11
- def line
12
- input.line_of(index)
13
- end
14
-
15
- def column
16
- input.column_of(index)
17
- end
18
-
19
- def success?
20
- false
21
- end
22
-
23
- def failure?
24
- true
25
- end
26
-
27
- def interval
28
- @interval ||= (index...index)
29
- end
30
- end
31
- end
32
- end
@@ -1,30 +0,0 @@
1
- module Treetop
2
- module Runtime
3
- class ParseResult
4
- attr_reader :input, :nested_failures
5
-
6
- def initialize(input, nested_results = [])
7
- @input = input
8
- @nested_failures = collect_nested_failures_at_maximum_index(nested_results)
9
- end
10
-
11
- def collect_nested_failures_at_maximum_index(results)
12
- maximum_index = 0
13
- nested_failures = []
14
-
15
- results.each do |result|
16
- next if result.nested_failures.empty?
17
- index_of_nested_failures = result.nested_failures.first.index
18
- if index_of_nested_failures > maximum_index
19
- maximum_index = index_of_nested_failures
20
- nested_failures = result.nested_failures
21
- elsif index_of_nested_failures == maximum_index
22
- nested_failures += result.nested_failures
23
- end
24
- end
25
-
26
- return nested_failures.uniq
27
- end
28
- end
29
- end
30
- end