rley 0.1.10 → 0.1.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGELOG.md +5 -0
- data/README.md +1 -0
- data/lib/rley/constants.rb +1 -1
- data/lib/rley/formatter/debug.rb +1 -1
- data/lib/rley/formatter/json.rb +2 -11
- data/lib/rley/parse_tree_visitor.rb +3 -3
- data/lib/rley/parser/earley_parser.rb +4 -4
- data/lib/rley/parser/parsing.rb +27 -15
- data/lib/rley/ptree/parse_tree.rb +1 -1
- data/lib/rley/syntax/grammar.rb +1 -1
- data/lib/rley/syntax/grammar_builder.rb +16 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MDNkYmEzYTFiNmUyMzk2MTgwOTczZmNlMTVjMDU3ZDIzNjI4YWNmYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZWQ1MmMzMDA2NzcxOTUzM2ZjMDg0Yjg2OTg0MTVhODgzOTQ1OTExNg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjhmMDlmZjk2ZDYwY2EwNDBjZjIxYWMyMDA1ZjQ4MTVmOWE0NjRhZmI4OTg1
|
10
|
+
MWZiMzQwNzFkOGIyMzJmZTdmMTg2NTRkOGVmM2ViNjg0MzU1MjllODJkZTIx
|
11
|
+
NzQ4NDA3NmUwYzY4YzBhMmYwODg1MGIzNGJmMDU3NDBjMzJmZDY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OTBhM2JiNzRiNzJlNmNmZWRjODJhMzY3MzkyMDdkNDRmNzcyYzFiMGNmYThm
|
14
|
+
YWVhMTBjMzkzMDI1NzMyNTNiOTkzZGFlODAxYzcyYWQ0Y2QyNGEwZWUxYjBj
|
15
|
+
YTk0OTViZWVkODk5N2U4MmUxZWJlZTBjY2QxOTNmMDk5YmIxZjU=
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
### 0.1.11 / 2014-12-16
|
2
|
+
* [FIX] Fixed all but one YARD (documentation) warnings. Most of them were due to mismatch
|
3
|
+
in method argument names between source code and documentation.
|
4
|
+
* [CHANGE] File `README.md` added Gemnasium badge (for the gem dependency checks)
|
5
|
+
|
1
6
|
### 0.1.10 / 2014-12-14
|
2
7
|
* [CHANGE] Added more examples in `examples` folder (e.g. parsing then parse tree ).
|
3
8
|
* [CHANGE] File `rley.rb`: added more requires of the library to ease integration.
|
data/README.md
CHANGED
@@ -6,6 +6,7 @@ Rley
|
|
6
6
|
[![Build Status](https://travis-ci.org/famished-tiger/Rley.svg?branch=master)](https://travis-ci.org/famished-tiger/Rley)
|
7
7
|
[![Coverage Status](https://img.shields.io/coveralls/famished-tiger/Rley.svg)](https://coveralls.io/r/famished-tiger/Rley?branch=master)
|
8
8
|
[![Gem Version](https://badge.fury.io/rb/rley.svg)](http://badge.fury.io/rb/rley)
|
9
|
+
[![Dependency Status](https://gemnasium.com/famished-tiger/Rley.svg)](https://gemnasium.com/famished-tiger/Rley)
|
9
10
|
|
10
11
|
### What is Rley? ###
|
11
12
|
__Rley__ is a Ruby implementation of a Earley parser.
|
data/lib/rley/constants.rb
CHANGED
data/lib/rley/formatter/debug.rb
CHANGED
@@ -68,7 +68,7 @@ module Rley # This module is used as a namespace
|
|
68
68
|
# Notification of a visit event: the visitor completed the visit of
|
69
69
|
# a non-terminal node
|
70
70
|
# @param _nonterm [NonTerminalNode]
|
71
|
-
def after_non_terminal(
|
71
|
+
def after_non_terminal(_nonterm)
|
72
72
|
dedent
|
73
73
|
output_event(__method__, indentation)
|
74
74
|
end
|
data/lib/rley/formatter/json.rb
CHANGED
@@ -38,7 +38,7 @@ module Rley # This module is used as a namespace
|
|
38
38
|
# Method called by a ParseTreeVisitor to which the formatter subscribed.
|
39
39
|
# Notification of a visit event: the visitor is about to visit
|
40
40
|
# a non-terminal node
|
41
|
-
# @param
|
41
|
+
# @param nonterm_node [NonTerminalNode]
|
42
42
|
def before_non_terminal(nonterm_node)
|
43
43
|
separator = sibling_flags[-1] ? ",\n" : "\n"
|
44
44
|
name = nonterm_node.symbol.name
|
@@ -60,7 +60,7 @@ module Rley # This module is used as a namespace
|
|
60
60
|
# Method called by a ParseTreeVisitor to which the formatter subscribed.
|
61
61
|
# Notification of a visit event: the visitor is about to visit
|
62
62
|
# a terminal node
|
63
|
-
# @param
|
63
|
+
# @param term_node [TerminalNode]
|
64
64
|
def before_terminal(term_node)
|
65
65
|
separator = sibling_flags[-1] ? ",\n" : "\n"
|
66
66
|
name = term_node.symbol.name
|
@@ -82,15 +82,6 @@ module Rley # This module is used as a namespace
|
|
82
82
|
print_text("\n", '}')
|
83
83
|
end
|
84
84
|
|
85
|
-
|
86
|
-
# Method called by a ParseTreeVisitor to which the formatter subscribed.
|
87
|
-
# Notification of a visit event: the visitor completed the visit of
|
88
|
-
# a non-terminal node
|
89
|
-
# @param _nonterm [NonTerminalNode]
|
90
|
-
def after_non_terminal(_)
|
91
|
-
end
|
92
|
-
|
93
|
-
|
94
85
|
# Method called by a ParseTreeVisitor to which the formatter subscribed.
|
95
86
|
# Notification of a visit event: the visitor completed the visit
|
96
87
|
# of the given parse tree
|
@@ -43,7 +43,7 @@ module Rley # This module is used as a namespace
|
|
43
43
|
|
44
44
|
|
45
45
|
# Visit event. The visitor is about to visit the given non terminal node.
|
46
|
-
# @param
|
46
|
+
# @param aNonTerminalNode [NonTerminalNode] the node to visit.
|
47
47
|
def start_visit_nonterminal(aNonTerminalNode)
|
48
48
|
broadcast(:before_non_terminal, aNonTerminalNode)
|
49
49
|
end
|
@@ -63,7 +63,7 @@ module Rley # This module is used as a namespace
|
|
63
63
|
|
64
64
|
# Visit event. The visitor is visiting the
|
65
65
|
# given terminal node.
|
66
|
-
# @param
|
66
|
+
# @param aTerminalNode [TerminalNode] the terminal to visit.
|
67
67
|
def visit_terminal(aTerminalNode)
|
68
68
|
broadcast(:before_terminal, aTerminalNode)
|
69
69
|
broadcast(:after_terminal, aTerminalNode)
|
@@ -72,7 +72,7 @@ module Rley # This module is used as a namespace
|
|
72
72
|
|
73
73
|
# Visit event. The visitor has completed its visit of the given
|
74
74
|
# non-terminal node.
|
75
|
-
# @param
|
75
|
+
# @param aNonTerminalNode [NonTerminalNode] the node to visit.
|
76
76
|
def end_visit_nonterminal(aNonTerminalNode)
|
77
77
|
broadcast(:after_non_terminal, aNonTerminalNode)
|
78
78
|
end
|
@@ -20,8 +20,8 @@ module Rley # This module is used as a namespace
|
|
20
20
|
# after "advancing" the dot
|
21
21
|
attr_reader(:next_mapping)
|
22
22
|
|
23
|
-
# @param aGrammar [Grammar]
|
24
|
-
#
|
23
|
+
# @param aGrammar [Grammar] The grammar of the language
|
24
|
+
# (to use by the parser).
|
25
25
|
def initialize(aGrammar)
|
26
26
|
@grammar = aGrammar
|
27
27
|
@dotted_items = build_dotted_items(grammar)
|
@@ -32,7 +32,7 @@ module Rley # This module is used as a namespace
|
|
32
32
|
# Parse a sequence of input tokens.
|
33
33
|
# @param aTokenSequence [Array] Array of Tokens objects returned by a
|
34
34
|
# tokenizer/scanner/lexer.
|
35
|
-
# @return
|
35
|
+
# @return [Parsing] an object that embeds the parse results.
|
36
36
|
def parse(aTokenSequence)
|
37
37
|
result = Parsing.new(start_dotted_item, aTokenSequence)
|
38
38
|
last_token_index = aTokenSequence.size
|
@@ -159,7 +159,7 @@ module Rley # This module is used as a namespace
|
|
159
159
|
# such that the dot appears after terminal.
|
160
160
|
# @param aParsing [Parsing] the object that encapsulates the results
|
161
161
|
# result of the parsing process
|
162
|
-
# @param
|
162
|
+
# @param aTerminal [Terminal] a terminal symbol that
|
163
163
|
# immediately follows a dot
|
164
164
|
# @param aPosition [Fixnum] position in the input token sequence.
|
165
165
|
def scanning(aParsing, aTerminal, aPosition)
|
data/lib/rley/parser/parsing.rb
CHANGED
@@ -32,22 +32,19 @@ module Rley # This module is used as a namespace
|
|
32
32
|
def parse_tree()
|
33
33
|
state_set_index = chart.state_sets.size - 1
|
34
34
|
parse_state = end_parse_state
|
35
|
-
curr_dotted_item = parse_state.dotted_rule
|
36
35
|
full_range = { low: 0, high: state_set_index }
|
37
|
-
|
36
|
+
start_production = chart.start_dotted_rule.production
|
37
|
+
ptree = PTree::ParseTree.new(start_production, full_range)
|
38
|
+
return ptree if parse_state.nil?
|
38
39
|
loop do
|
40
|
+
curr_dotted_item = parse_state.dotted_rule
|
39
41
|
# Look at the symbol on left of the dot
|
40
42
|
curr_symbol = curr_dotted_item.prev_symbol
|
41
43
|
case curr_symbol
|
42
44
|
when Syntax::Terminal
|
43
45
|
state_set_index -= 1
|
44
|
-
ptree
|
45
|
-
|
46
|
-
ptree.current_node.token = tokens[state_set_index]
|
47
|
-
end
|
48
|
-
state_set = chart[state_set_index]
|
49
|
-
parse_state = state_set.predecessor_state(parse_state)
|
50
|
-
curr_dotted_item = parse_state.dotted_rule
|
46
|
+
parse_state = predecessor_state_terminal(ptree, state_set_index,
|
47
|
+
parse_state)
|
51
48
|
|
52
49
|
when Syntax::NonTerminal
|
53
50
|
# Retrieve complete states
|
@@ -58,10 +55,7 @@ module Rley # This module is used as a namespace
|
|
58
55
|
ptree.current_node.range = { low: parse_state.origin }
|
59
56
|
node_range = ptree.current_node.range
|
60
57
|
ptree.add_children(curr_dotted_item.production, node_range)
|
61
|
-
|
62
|
-
a_node = ptree.current_node
|
63
|
-
a_node.token = tokens[state_set_index - 1] unless a_node.token
|
64
|
-
end
|
58
|
+
link_node_to_token(ptree, state_set_index - 1)
|
65
59
|
|
66
60
|
when NilClass
|
67
61
|
lhs = curr_dotted_item.production.lhs
|
@@ -69,7 +63,6 @@ module Rley # This module is used as a namespace
|
|
69
63
|
break if new_states.empty?
|
70
64
|
# TODO: make this more robust
|
71
65
|
parse_state = new_states[0]
|
72
|
-
curr_dotted_item = parse_state.dotted_rule
|
73
66
|
ptree.step_up(state_set_index)
|
74
67
|
ptree.current_node.range = { low: parse_state.origin }
|
75
68
|
break if ptree.root == ptree.current_node
|
@@ -96,7 +89,7 @@ module Rley # This module is used as a namespace
|
|
96
89
|
# a new state like: <next dotted rule, s.origin, aPosition + 1>
|
97
90
|
# In other words, we place the dotted rules in the next state set
|
98
91
|
# such that the dot appears after terminal.
|
99
|
-
# @param
|
92
|
+
# @param aTerminal [Terminal] a terminal symbol that
|
100
93
|
# immediately follows a dot
|
101
94
|
# @param aPosition [Fixnum] position in the input token sequence.
|
102
95
|
# @param nextMapping [Proc or Lambda] code to evaluate in order to
|
@@ -153,6 +146,25 @@ module Rley # This module is used as a namespace
|
|
153
146
|
candidate_states = last_chart_entry.states_for(start_production)
|
154
147
|
return candidate_states.find(&:complete?)
|
155
148
|
end
|
149
|
+
|
150
|
+
# Go to the predecessor state for the given terminal
|
151
|
+
def predecessor_state_terminal(aParseTree, aStateSetIndex, current_state)
|
152
|
+
aParseTree.step_back(aStateSetIndex)
|
153
|
+
link_node_to_token(aParseTree, aStateSetIndex)
|
154
|
+
state_set = chart[aStateSetIndex]
|
155
|
+
state_set.predecessor_state(current_state)
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
# If the current node is a terminal node
|
160
|
+
# then link the token to that node
|
161
|
+
def link_node_to_token(aParseTree, aStateSetIndex)
|
162
|
+
if aParseTree.current_node.is_a?(PTree::TerminalNode)
|
163
|
+
a_node = aParseTree.current_node
|
164
|
+
a_node.token = tokens[aStateSetIndex] unless a_node.token
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
156
168
|
end # class
|
157
169
|
end # module
|
158
170
|
end # module
|
@@ -56,7 +56,7 @@ module Rley # This module is used as a namespace
|
|
56
56
|
end
|
57
57
|
|
58
58
|
# Move the current node to the parent node.
|
59
|
-
# @param
|
59
|
+
# @param _tokenPos [Fixnum] position of the matching input token
|
60
60
|
def step_up(_tokenPos)
|
61
61
|
current_path.pop(2)
|
62
62
|
end
|
data/lib/rley/syntax/grammar.rb
CHANGED
@@ -24,7 +24,7 @@ module Rley # This module is used as a namespace
|
|
24
24
|
# A Hash with pairs of the kind: symbol name => grammar symbol
|
25
25
|
attr_reader(:name2symbol)
|
26
26
|
|
27
|
-
# @param
|
27
|
+
# @param theProductions [Array of Production] the list of productions
|
28
28
|
# of the grammar.
|
29
29
|
def initialize(theProductions)
|
30
30
|
@rules = []
|
@@ -25,21 +25,31 @@ module Rley # This module is used as a namespace
|
|
25
25
|
|
26
26
|
# Retrieve a grammar symbol from its name.
|
27
27
|
# Raise an exception if not found.
|
28
|
-
# @param [
|
28
|
+
# @param aSymbolName [String] the name of a symbol grammar.
|
29
29
|
# @return [GrmSymbol] the retrieved symbol.
|
30
30
|
def [](aSymbolName)
|
31
31
|
return symbols[aSymbolName]
|
32
32
|
end
|
33
33
|
|
34
34
|
# Add the terminal symbols of the language
|
35
|
-
# terminalSymbols [String or Terminal] one or more
|
36
|
-
# to add to the grammar.
|
35
|
+
# @param terminalSymbols [String or Terminal] one or more
|
36
|
+
# terminal symbols to add to the grammar.
|
37
37
|
def add_terminals(*terminalSymbols)
|
38
38
|
new_symbs = build_symbols(Terminal, terminalSymbols)
|
39
39
|
symbols.merge!(new_symbs)
|
40
40
|
end
|
41
41
|
|
42
|
-
|
42
|
+
|
43
|
+
# Add a production rule in the grammar.
|
44
|
+
# @param aProductionRepr [Hash] A Hash-based representation of the
|
45
|
+
# production. It consists of a key-value pair of the form:
|
46
|
+
# String => Array.
|
47
|
+
# Where the key is the name of the non-terminal appearing in the
|
48
|
+
# left side of the rule.
|
49
|
+
# The value, an Array, is a sequence of grammar symbol names.
|
50
|
+
# The rule is created and inserted in the grammar.
|
51
|
+
# Example:
|
52
|
+
# builder.add_production('A' => ['a', 'A', 'c'])
|
43
53
|
def add_production(aProductionRepr)
|
44
54
|
aProductionRepr.each_pair do |(lhs_name, rhs_repr)|
|
45
55
|
lhs = get_nonterminal(lhs_name)
|
@@ -78,7 +88,7 @@ module Rley # This module is used as a namespace
|
|
78
88
|
|
79
89
|
# Add the given grammar symbols.
|
80
90
|
# @param aClass [Class] The class of grammar symbols to instantiate.
|
81
|
-
# @param
|
91
|
+
# @param theSymbols [Array] array of elements are treated as follows:
|
82
92
|
# if the element is already a grammar symbol, then it added as is,
|
83
93
|
# otherwise it is considered as the name of a grammar symbol
|
84
94
|
# of the specified class to build.
|
@@ -97,7 +107,7 @@ module Rley # This module is used as a namespace
|
|
97
107
|
# returned as is. Otherwise, the argument is treated as a name
|
98
108
|
# for a new instance of the given class.
|
99
109
|
# @param aClass [Class] The class of grammar symbols to instantiate
|
100
|
-
# @param
|
110
|
+
# @param aSymbolArg [GrmSymbol-like or String]
|
101
111
|
# @return [Array] list of grammar symbols
|
102
112
|
def build_symbol(aClass, aSymbolArg)
|
103
113
|
if aSymbolArg.kind_of?(GrmSymbol)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rley
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dimitri Geshef
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|