treetop 1.1.4 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/Rakefile +3 -2
- data/doc/contributing_and_planned_features.markdown +1 -1
- data/doc/using_in_ruby.markdown +2 -2
- data/examples/lambda_calculus/arithmetic.rb +551 -0
- data/examples/lambda_calculus/arithmetic_test.rb +1 -1
- data/examples/lambda_calculus/lambda_calculus.rb +39 -72
- data/examples/lambda_calculus/lambda_calculus_test.rb +2 -2
- data/examples/lambda_calculus/test_helper.rb +3 -3
- data/lib/treetop.rb +3 -0
- data/lib/treetop/bootstrap_gen_1_metagrammar.rb +22 -14
- data/lib/treetop/compiler.rb +1 -2
- data/lib/treetop/compiler/grammar_compiler.rb +12 -5
- data/lib/treetop/compiler/metagrammar.rb +931 -558
- data/lib/treetop/compiler/metagrammar.treetop +26 -6
- data/lib/treetop/compiler/node_classes/anything_symbol.rb +10 -2
- data/lib/treetop/compiler/node_classes/atomic_expression.rb +4 -0
- data/lib/treetop/compiler/node_classes/character_class.rb +10 -1
- data/lib/treetop/compiler/node_classes/choice.rb +2 -4
- data/lib/treetop/compiler/node_classes/parsing_expression.rb +8 -17
- data/lib/treetop/compiler/node_classes/parsing_rule.rb +3 -3
- data/lib/treetop/compiler/node_classes/predicate.rb +1 -1
- data/lib/treetop/compiler/node_classes/repetition.rb +3 -4
- data/lib/treetop/compiler/node_classes/sequence.rb +4 -4
- data/lib/treetop/compiler/node_classes/terminal.rb +11 -1
- data/lib/treetop/compiler/ruby_builder.rb +2 -2
- data/lib/treetop/ruby_extensions.rb +1 -1
- data/lib/treetop/runtime.rb +0 -3
- data/lib/treetop/runtime/compiled_parser.rb +42 -34
- data/lib/treetop/runtime/node_cache.rb +1 -1
- data/lib/treetop/runtime/syntax_node.rb +51 -32
- data/lib/treetop/runtime/terminal_parse_failure.rb +7 -24
- data/lib/treetop/runtime/terminal_syntax_node.rb +7 -2
- metadata +12 -7
- data/examples/TALK +0 -33
- data/lib/treetop/compiler/load_grammar.rb +0 -7
- data/lib/treetop/compiler/metagrammar. +0 -0
- data/lib/treetop/runtime/parse_failure.rb +0 -32
- data/lib/treetop/runtime/parse_result.rb +0 -30
@@ -1,53 +1,72 @@
|
|
1
1
|
module Treetop
|
2
2
|
module Runtime
|
3
|
-
class SyntaxNode
|
3
|
+
class SyntaxNode
|
4
4
|
attr_reader :input, :interval
|
5
|
-
|
6
|
-
def initialize(input, interval, elements = nil
|
7
|
-
|
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
|
21
|
+
@elements
|
30
22
|
end
|
31
|
-
|
23
|
+
|
32
24
|
def text_value
|
33
25
|
input[interval]
|
34
26
|
end
|
35
|
-
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
4
|
-
attr_reader :expected_string
|
3
|
+
class TerminalParseFailure
|
4
|
+
attr_reader :index, :expected_string
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
|
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
|
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
|
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.
|
7
|
-
date:
|
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/
|
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
|
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
|