sequitur 0.1.23 → 0.1.25
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +11 -437
- data/CHANGELOG.md +9 -0
- data/Gemfile +0 -2
- data/LICENSE.txt +1 -1
- data/README.md +2 -3
- data/Rakefile +0 -2
- data/appveyor.yml +10 -10
- data/examples/inductive_english.rb +35 -0
- data/examples/integer_sample.rb +0 -1
- data/examples/porridge.rb +9 -9
- data/examples/word_sample.rb +4 -5
- data/lib/sequitur/constants.rb +7 -4
- data/lib/sequitur/digram.rb +11 -11
- data/lib/sequitur/dynamic_grammar.rb +12 -12
- data/lib/sequitur/formatter/base_formatter.rb +2 -2
- data/lib/sequitur/formatter/base_text.rb +8 -9
- data/lib/sequitur/formatter/debug.rb +10 -4
- data/lib/sequitur/grammar_visitor.rb +7 -7
- data/lib/sequitur/production.rb +203 -205
- data/lib/sequitur/production_ref.rb +18 -20
- data/lib/sequitur/sequitur_grammar.rb +135 -137
- data/lib/sequitur/symbol_sequence.rb +29 -32
- data/lib/sequitur.rb +6 -6
- data/sig/lib/sequitur/constants.rbs +10 -0
- data/sig/lib/sequitur/digram.rbs +37 -0
- data/sig/lib/sequitur/dynamic_grammar.rbs +58 -0
- data/sig/lib/sequitur/formatter/base_formatter.rbs +20 -0
- data/sig/lib/sequitur/formatter/base_text.rbs +62 -0
- data/sig/lib/sequitur/formatter/debug.rbs +89 -0
- data/sig/lib/sequitur/production.rbs +120 -0
- data/sig/lib/sequitur/production_ref.rbs +73 -0
- data/sig/lib/sequitur/sequitur_grammar.rbs +55 -0
- data/sig/lib/sequitur/symbol_sequence.rbs +83 -0
- data/sig/lib/sequitur.rbs +9 -0
- data/spec/sequitur/digram_spec.rb +13 -12
- data/spec/sequitur/dynamic_grammar_spec.rb +5 -11
- data/spec/sequitur/formatter/base_text_spec.rb +70 -72
- data/spec/sequitur/formatter/debug_spec.rb +90 -92
- data/spec/sequitur/grammar_visitor_spec.rb +70 -71
- data/spec/sequitur/production_ref_spec.rb +92 -92
- data/spec/sequitur/production_spec.rb +30 -34
- data/spec/sequitur/sequitur_grammar_spec.rb +47 -46
- data/spec/sequitur/symbol_sequence_spec.rb +102 -105
- data/spec/spec_helper.rb +0 -1
- metadata +28 -17
- data/.travis.yml +0 -29
@@ -0,0 +1,89 @@
|
|
1
|
+
module Sequitur
|
2
|
+
module Formatter
|
3
|
+
# A formatter class that can render the notification events
|
4
|
+
# from a grammar visitor
|
5
|
+
# @example
|
6
|
+
# some_grammar = ... # Points to a DynamicGrammar-like object
|
7
|
+
# # Output the result to the standard console output
|
8
|
+
# formatter = Sequitur::Formatter::Debug.new(STDOUT)
|
9
|
+
# # Render the visit notifications
|
10
|
+
# formatter.run(some_grammar.visitor)
|
11
|
+
class Debug < BaseFormatter
|
12
|
+
# @return [Integer] Current indentation level
|
13
|
+
attr_accessor indentation: Integer
|
14
|
+
|
15
|
+
# Constructor.
|
16
|
+
# @param anIO [IO] The output stream to which the rendered grammar
|
17
|
+
# is written.
|
18
|
+
def initialize: (IO anIO) -> void
|
19
|
+
|
20
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
21
|
+
# Notification of a visit event: the visitor is about to visit a grammar
|
22
|
+
# @param _ [DynamicGrammar]
|
23
|
+
def before_grammar: (DynamicGrammar _) -> untyped
|
24
|
+
|
25
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
26
|
+
# Notification of a visit event: the visitor is about to visit
|
27
|
+
# a production
|
28
|
+
# @param _ [Production]
|
29
|
+
def before_production: (Production _) -> untyped
|
30
|
+
|
31
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
32
|
+
# Notification of a visit event: the visitor is about to visit
|
33
|
+
# the rhs of a production
|
34
|
+
# @param _ [Array]
|
35
|
+
def before_rhs: (::Array[untyped] _) -> untyped
|
36
|
+
|
37
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
38
|
+
# Notification of a visit event: the visitor is about to visit
|
39
|
+
# a terminal symbol from the rhs of a production
|
40
|
+
# @param _ [Object]
|
41
|
+
def before_terminal: (untyped _) -> untyped
|
42
|
+
|
43
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
44
|
+
# Notification of a visit event: the visitor completed the visit of
|
45
|
+
# a terminal symbol from the rhs of a production
|
46
|
+
# @param _ [Object]
|
47
|
+
def after_terminal: (untyped _) -> untyped
|
48
|
+
|
49
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
50
|
+
# Notification of a visit event: the visitor is about to visit
|
51
|
+
# a non-terminal (= an allusion to a production) in the rhs of a
|
52
|
+
# production
|
53
|
+
# @param _ [Production] a production occurring in the rhs
|
54
|
+
def before_non_terminal: (Production _) -> untyped
|
55
|
+
|
56
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
57
|
+
# Notification of a visit event: the visitor completed the visit of
|
58
|
+
# a non-terminal symbol from the rhs of a production.
|
59
|
+
# @param _ [Object]
|
60
|
+
def after_non_terminal: (untyped _) -> untyped
|
61
|
+
|
62
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
63
|
+
# Notification of a visit event: the visitor completed the visit of
|
64
|
+
# the rhs of a production
|
65
|
+
# @param _ [Array]
|
66
|
+
def after_rhs: (::Array[untyped] _) -> untyped
|
67
|
+
|
68
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
69
|
+
# Notification of a visit event: the visitor completed the visit
|
70
|
+
# of a production
|
71
|
+
# @param _ [Production]
|
72
|
+
def after_production: (Production _) -> untyped
|
73
|
+
|
74
|
+
# Method called by a GrammarVisitor to which the formatter subscribed.
|
75
|
+
# Notification of a visit event: the visitor completed the visit
|
76
|
+
# of a grammar
|
77
|
+
# @param _ [DynamicGrammar]
|
78
|
+
def after_grammar: (DynamicGrammar _) -> untyped
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def indent: () -> Integer
|
83
|
+
|
84
|
+
def dedent: () -> Integer
|
85
|
+
|
86
|
+
def output_event: (Symbol anEvent, Integer indentationLevel) -> nil
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module Sequitur
|
2
|
+
# In a context-free grammar, a production is a rule in which
|
3
|
+
# its left-hand side (LHS) consists solely of a non-terminal symbol
|
4
|
+
# and the right-hand side (RHS) consists of a sequence of symbols.
|
5
|
+
# The symbols in RHS can be either terminal or non-terminal symbols.
|
6
|
+
# The rule stipulates that the LHS is equivalent to the RHS,
|
7
|
+
# in other words every occurrence of the LHS can be substituted to
|
8
|
+
# corresponding RHS.
|
9
|
+
# Implementation note: the object id of the production is taken as its LHS.
|
10
|
+
class Production
|
11
|
+
# @return [Sequitur::SymbolSequence] The right-hand side (rhs)
|
12
|
+
# consists of a sequence of grammar symbols
|
13
|
+
attr_reader rhs: untyped
|
14
|
+
|
15
|
+
# @return [Integer] The reference count (= how times other productions reference this one)
|
16
|
+
attr_reader refcount: untyped
|
17
|
+
|
18
|
+
# @return [Array<Sequitur::Digram>] The sequence of digrams appearing in the RHS
|
19
|
+
attr_reader digrams: untyped
|
20
|
+
|
21
|
+
# Constructor.
|
22
|
+
# Build a production with an empty RHS.
|
23
|
+
def initialize: () -> void
|
24
|
+
|
25
|
+
# Identity testing.
|
26
|
+
# @param other [Production, ProductionRef] another production or production reference.
|
27
|
+
# @return [TrueClass, FalseClass] true when the receiver and other are the same.
|
28
|
+
def ==: ((Production | ProductionRef) other) -> bool
|
29
|
+
|
30
|
+
# Is the rhs empty?
|
31
|
+
# @return [TrueClass, FalseClass] true if the rhs has no members.
|
32
|
+
def empty?: () -> bool
|
33
|
+
|
34
|
+
# Increment the reference count by one.
|
35
|
+
# @return [Integer]
|
36
|
+
def incr_refcount: () -> Integer
|
37
|
+
|
38
|
+
# Decrement the reference count by one.
|
39
|
+
# @return [Integer]
|
40
|
+
def decr_refcount: () -> Integer
|
41
|
+
|
42
|
+
# Select the references to production appearing in the rhs.
|
43
|
+
# @return [Array<ProductionRef>]
|
44
|
+
def references: () -> Array[Sequitur::ProductionRef]
|
45
|
+
|
46
|
+
# Look in the rhs all the references to a production passed a argument.
|
47
|
+
# @param a_prod [Production, ProductionRef] The production to search for.
|
48
|
+
# @return [Array<ProductionRef>]
|
49
|
+
def references_of: ((Production | ProductionRef) a_prod) -> Array[ProductionRef]
|
50
|
+
|
51
|
+
# Enumerate the digrams appearing in the right-hand side (rhs)
|
52
|
+
# @return [Array<Sequitur::Digram>] the list of digrams found in rhs of this production.
|
53
|
+
def recalc_digrams: () -> Array[Sequitur::Digram]
|
54
|
+
|
55
|
+
# Does the rhs have exactly one digram only (= 2 symbols)?
|
56
|
+
# @return [TrueClass, FalseClass] true when the rhs contains exactly two symbols.
|
57
|
+
def single_digram?: () -> bool
|
58
|
+
|
59
|
+
# Detect whether the last digram occurs twice
|
60
|
+
# Assumption: when a digram occurs twice in a production then it must occur
|
61
|
+
# at the end of the rhs
|
62
|
+
# @return [TrueClass, FalseClass] true when the digram occurs twice in rhs.
|
63
|
+
def repeated_digram?: () -> bool
|
64
|
+
|
65
|
+
# Retrieve the last digram appearing in the RHS (if any).
|
66
|
+
# @return [Sequitur::Digram, NilClass] last digram in the rhs otherwise nil.
|
67
|
+
def last_digram: () -> (nil | Sequitur::Digram)
|
68
|
+
|
69
|
+
# Emit a text representation of the production rule.
|
70
|
+
# Text is of the form:
|
71
|
+
# object id of production : rhs as space-separated sequence of symbols.
|
72
|
+
# @return [String]
|
73
|
+
def to_string: () -> ::String
|
74
|
+
|
75
|
+
# Add a (grammar) symbol at the end of the RHS.
|
76
|
+
# @param aSymbol [Object] A (grammar) symbol to add.
|
77
|
+
def append_symbol: (untyped aSymbol) -> untyped
|
78
|
+
|
79
|
+
# Clear the right-hand side.
|
80
|
+
# Any referenced production has its reference counter decremented.
|
81
|
+
def clear_rhs: () -> untyped
|
82
|
+
|
83
|
+
# Find all the positions where the digram occurs in the rhs
|
84
|
+
# @param symb1 [Object] first symbol of the digram
|
85
|
+
# @param symb2 [Object] second symbol of the digram
|
86
|
+
# @return [Array<Integer>] the list of indices where the digram occurs in rhs.
|
87
|
+
# @example
|
88
|
+
# # Given the production p : a b c a b a b d
|
89
|
+
# #Then ...
|
90
|
+
# p.positions_of(a, b) # => [0, 3, 5]
|
91
|
+
# # Caution: "overlapping" digrams shouldn't be counted
|
92
|
+
# # Given the production p : a a b a a a c d
|
93
|
+
# # Then ...
|
94
|
+
# p.positions_of(a, a) # => [0, 3]
|
95
|
+
def positions_of: (untyped symb1, untyped symb2) -> Array[Integer]
|
96
|
+
|
97
|
+
# Given that the production P passed as argument has exactly 2 symbols
|
98
|
+
# in its rhs s1 s2, substitute in the rhs of self all occurrences of
|
99
|
+
# s1 s2 by a reference to P.
|
100
|
+
# @param another [Production, ProductionRef] a production that
|
101
|
+
# consists exactly of one digram (= 2 symbols).
|
102
|
+
def reduce_step: ((Production | ProductionRef) another) -> untyped
|
103
|
+
|
104
|
+
# Replace every occurrence of 'another' production in self.rhs by
|
105
|
+
# the symbols in the rhs of 'another'.
|
106
|
+
# @param another [Production, ProductionRef] a production that
|
107
|
+
# consists exactly of one digram (= 2 symbols).
|
108
|
+
# @example Synopsis
|
109
|
+
# # Given the production p_A : a p_B b p_B c
|
110
|
+
# # And the production p_B : x y
|
111
|
+
# # Then...
|
112
|
+
# p_A.derive_step(p_B)
|
113
|
+
# #Modifies p_A as into: p_A -> a x y b x y c
|
114
|
+
def derive_step: ((Production | ProductionRef) another) -> untyped
|
115
|
+
|
116
|
+
# Part of the 'visitee' role in Visitor design pattern.
|
117
|
+
# @param aVisitor[Sequitur::GrammarVisitor]
|
118
|
+
def accept: (Sequitur::GrammarVisitor aVisitor) -> untyped
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Sequitur
|
2
|
+
# A production reference is a grammar symbol that may appear in the right-hand
|
3
|
+
# side of a production P1 and that refers to a production P2.
|
4
|
+
# Every time a production P2 appears in the left-hand side of
|
5
|
+
# production P1, this is implemented by inserting a production reference to P2
|
6
|
+
# in the appropriate position in the RHS of P1.
|
7
|
+
# In the literature, production references are also called non terminal
|
8
|
+
# symbols
|
9
|
+
# @example
|
10
|
+
# # Given a production rule...
|
11
|
+
# prod = Sequitur::Production.new
|
12
|
+
# puts prod.refcount # outputs 0
|
13
|
+
# # ... Build a reference to it
|
14
|
+
# ref = Sequitur::ProductionRef.new(prod)
|
15
|
+
# # ... Production reference count is updated...
|
16
|
+
# puts prod.refcount # outputs 1
|
17
|
+
class ProductionRef
|
18
|
+
# @return [Sequitur::Production] Link to the production to reference.
|
19
|
+
attr_reader production: Sequitur::Production
|
20
|
+
|
21
|
+
# Constructor
|
22
|
+
# @param target [Production, ProductionRef]
|
23
|
+
# The production that is being referenced.
|
24
|
+
def initialize: ((Production | ProductionRef) target) -> void
|
25
|
+
|
26
|
+
# Copy constructor invoked by dup or clone methods.
|
27
|
+
# @param orig [ProductionRef]
|
28
|
+
# @example
|
29
|
+
# prod = Sequitur::Production.new
|
30
|
+
# ref = Sequitur::ProductionRef.new(prod)
|
31
|
+
# copy_ref = ref.dup
|
32
|
+
# puts prod.refcount # outputs 2
|
33
|
+
def initialize_copy: (ProductionRef orig) -> void
|
34
|
+
|
35
|
+
# Emit the text representation of a production reference.
|
36
|
+
# @return [String]
|
37
|
+
def to_s: () -> String
|
38
|
+
|
39
|
+
alias to_string to_s
|
40
|
+
|
41
|
+
# Equality testing.
|
42
|
+
# A production ref is equal to another one when its
|
43
|
+
# refers to the same production or when it is compared to
|
44
|
+
# the production it refers to.
|
45
|
+
# @param other [Production, ProductionRef]
|
46
|
+
# @return [TrueClass, FalseClass]
|
47
|
+
def ==: ((Production | ProductionRef) other) -> bool
|
48
|
+
|
49
|
+
# Produce a hash value.
|
50
|
+
# A reference has no identity on its own,
|
51
|
+
# the method returns the hash value of the
|
52
|
+
# referenced production
|
53
|
+
# @return [Integer] the hash value
|
54
|
+
def hash: () -> Integer
|
55
|
+
|
56
|
+
# Make this reference point to the given production.
|
57
|
+
# @param aProduction [Production, ProductionRef] the production
|
58
|
+
# to refer to
|
59
|
+
def bind_to: ((Production | ProductionRef) aProduction) -> (nil | untyped)
|
60
|
+
|
61
|
+
# Clear the reference to the target production.
|
62
|
+
def unbind: () -> nil
|
63
|
+
|
64
|
+
# Check that the this object doesn't refer to any production.
|
65
|
+
# @return [TrueClass, FalseClass] true when this object doesn't
|
66
|
+
# point to a production.
|
67
|
+
def unbound?: () -> bool
|
68
|
+
|
69
|
+
# Part of the 'visitee' role in the Visitor design pattern.
|
70
|
+
# @param aVisitor [Sequitur::GrammarVisitor] the visitor
|
71
|
+
def accept: (Sequitur::GrammarVisitor aVisitor) -> untyped
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Sequitur
|
2
|
+
# Module for classes implementing the Sequitur algorithm
|
3
|
+
# Specialization of the DynamicGrammar class.
|
4
|
+
# A Sequitur grammar is a context-free grammar that is entirely built
|
5
|
+
# from a sequence of input tokens through the Sequitur algorithm.
|
6
|
+
class SequiturGrammar < DynamicGrammar
|
7
|
+
# Build the grammar from an enumerator of tokens.
|
8
|
+
# @param anEnum [Enumerator] an enumerator that will iterate
|
9
|
+
# over the input tokens.
|
10
|
+
def initialize: (untyped anEnum) -> void
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
# Struct used for internal purposes
|
15
|
+
CollisionDiagnosis: untyped
|
16
|
+
|
17
|
+
# Assuming that a new input token was added to the start production,
|
18
|
+
# enforce the digram unicity and rule utility rules
|
19
|
+
# begin
|
20
|
+
# if a digram D occurs twice in the grammar then
|
21
|
+
# add a production P : D (if not already there)
|
22
|
+
# replace both Ds with R (reduction step).
|
23
|
+
# end
|
24
|
+
# if a production P : RHS in referenced only once then
|
25
|
+
# replace P by its RHS (derivation step)
|
26
|
+
# remove P from grammar
|
27
|
+
# end
|
28
|
+
# end until digram unicity and rule utility are met
|
29
|
+
def enforce_rules: () -> untyped
|
30
|
+
|
31
|
+
# Check whether a digram is used twice in the grammar.
|
32
|
+
# Return an empty Hash if each digram appears once.
|
33
|
+
# Otherwise return a Hash with a pair of the form: digram => [Pi, Pk]
|
34
|
+
# Where Pi, Pk are two productions where the digram occurs.
|
35
|
+
def detect_collision: () -> untyped
|
36
|
+
|
37
|
+
# When a collision diagnosis indicates that a given
|
38
|
+
# digram d occurs twice in the grammar
|
39
|
+
# Then create a new production that will have
|
40
|
+
# the symbols of d as its rhs members.
|
41
|
+
def restore_unicity: (untyped aDiagnosis) -> untyped
|
42
|
+
|
43
|
+
# Return a production that is used less than twice in the grammar.
|
44
|
+
def detect_useless_production: () -> untyped
|
45
|
+
|
46
|
+
# Given the passed production P is referenced only once.
|
47
|
+
# Then replace P by its RHS where it is referenced.
|
48
|
+
# And delete P
|
49
|
+
def restore_utility: (untyped prod_index) -> untyped
|
50
|
+
|
51
|
+
# Create a new production that will have the symbols from digram
|
52
|
+
# as its rhs members.
|
53
|
+
def build_production_for: (untyped aDigram) -> untyped
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Sequitur
|
2
|
+
# Module for classes implementing the Sequitur algorithm
|
3
|
+
# Represents a sequence (concatenation) of grammar symbols
|
4
|
+
# as they appear in rhs of productions
|
5
|
+
class SymbolSequence
|
6
|
+
# The sequence of symbols itself
|
7
|
+
attr_reader symbols: Array[untyped]
|
8
|
+
|
9
|
+
# Create an empty sequence
|
10
|
+
def initialize: () -> void
|
11
|
+
|
12
|
+
# Copy constructor invoked by dup or clone methods.
|
13
|
+
# @param orig [SymbolSequence]
|
14
|
+
def initialize_copy: (untyped orig) -> untyped
|
15
|
+
|
16
|
+
# Clear the symbol sequence.
|
17
|
+
def clear: () -> untyped
|
18
|
+
|
19
|
+
# Tell whether the sequence is empty.
|
20
|
+
# @[true / false] true only if the sequence has no symbol in it.
|
21
|
+
def empty?: () -> untyped
|
22
|
+
|
23
|
+
# Count the number of elements in the sequence.
|
24
|
+
# @[Fixnum] the number of elements
|
25
|
+
def size: () -> untyped
|
26
|
+
|
27
|
+
# Append a grammar symbol at the end of the sequence.
|
28
|
+
# @param aSymbol [Object] The symbol to append.
|
29
|
+
def <<: (untyped aSymbol) -> (nil | untyped)
|
30
|
+
|
31
|
+
# Retrieve the element from the sequence at given position.
|
32
|
+
# @param anIndex [Fixnum] A zero-based index of the element to access.
|
33
|
+
def []: (untyped anIndex) -> untyped
|
34
|
+
|
35
|
+
# Equality testing.
|
36
|
+
# @param other [SymbolSequence or Array] the other other sequence
|
37
|
+
# to compare to.
|
38
|
+
# @true when an item from self equals the corresponding
|
39
|
+
# item from 'other'
|
40
|
+
def ==: (untyped other) -> untyped
|
41
|
+
|
42
|
+
# Select the references to production appearing in the rhs.
|
43
|
+
# @[Array of ProductionRef]
|
44
|
+
def references: () -> untyped
|
45
|
+
|
46
|
+
# Select the references of the given production appearing in the rhs.
|
47
|
+
# @param aProduction [Production]
|
48
|
+
# @[Array of ProductionRef]
|
49
|
+
def references_of: (untyped aProduction) -> untyped
|
50
|
+
|
51
|
+
# Emit a text representation of the symbol sequence.
|
52
|
+
# Text is of the form: space-separated sequence of symbols.
|
53
|
+
# @[String]
|
54
|
+
def to_string: () -> untyped
|
55
|
+
|
56
|
+
# Insert at position the elements from another sequence.
|
57
|
+
# @param position [Fixnum] A zero-based index of the symbols to replace.
|
58
|
+
# @param another [SymbolSequence] A production with a two-elements rhs
|
59
|
+
# (a single digram).
|
60
|
+
def insert_at: (untyped position, untyped another) -> untyped
|
61
|
+
|
62
|
+
# Given that the production P passed as argument has exactly 2 symbols
|
63
|
+
# in its rhs s1 s2, substitute in the rhs of self all occurrences of
|
64
|
+
# s1 s2 by a reference to P.
|
65
|
+
# @param index [Integer] the position of a two symbol sequence to be replaced
|
66
|
+
# by the production
|
67
|
+
# @param aProduction [Production, ProductionRef] a production that
|
68
|
+
# consists exactly of one digram (= 2 symbols).
|
69
|
+
def reduce_step: (Integer index, (Production | ProductionRef) aProduction) -> untyped
|
70
|
+
|
71
|
+
# Remove the element at given position
|
72
|
+
# @param position [Integer] a zero-based index.
|
73
|
+
def delete_at: (untyped position) -> untyped
|
74
|
+
|
75
|
+
# Part of the 'visitee' role in Visitor design pattern.
|
76
|
+
# @param aVisitor[GrammarVisitor]
|
77
|
+
def accept: (untyped aVisitor) -> untyped
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def invalidate_refs: () -> untyped
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Sequitur
|
2
|
+
# Build a Sequitur-generated grammar based on the sequence of input tokens.
|
3
|
+
#
|
4
|
+
# @param tokens [String, Enumerator] The input sequence of input tokens.
|
5
|
+
# Can be a sequence of characters (i.e. a String) or an Enumerator.
|
6
|
+
# Tokens returned by enumerator should respond to the :hash message.
|
7
|
+
# @return [SequiturGrammar] a grammar that encodes the input.
|
8
|
+
def self.build_from: ((String | ::Enumerator[untyped]) tokens) -> SequiturGrammar
|
9
|
+
end
|
@@ -5,38 +5,40 @@ require_relative '../spec_helper'
|
|
5
5
|
# Load the class under test
|
6
6
|
require_relative '../../lib/sequitur/digram'
|
7
7
|
|
8
|
-
|
9
|
-
describe Digram do
|
8
|
+
describe Sequitur::Digram do
|
10
9
|
let(:two_symbols) { %i[b c] }
|
11
10
|
let(:production) { double('sample-production') }
|
11
|
+
def make_digram(symb1, symb2, production)
|
12
|
+
Sequitur::Digram.new(symb1, symb2, production)
|
13
|
+
end
|
12
14
|
|
13
15
|
context 'Standard creation & initialization:' do
|
14
16
|
it 'should be created with 3 arguments' do
|
15
|
-
instance =
|
17
|
+
instance = make_digram(:b, :c, production)
|
16
18
|
|
17
19
|
expect(instance.symbols).to eq(two_symbols)
|
18
20
|
expect(instance.production).to eq(production)
|
19
21
|
end
|
20
22
|
|
21
23
|
it 'should return the production that it refers to' do
|
22
|
-
instance =
|
24
|
+
instance = make_digram(:b, :c, production)
|
23
25
|
expect(instance.production).to eq(production)
|
24
26
|
end
|
25
27
|
|
26
28
|
it 'should whether its symbols are the same' do
|
27
|
-
|
28
|
-
|
29
|
+
instance1 = make_digram(:a, :a, production)
|
30
|
+
expect(instance1).to be_repeating
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
+
instance1 = make_digram(:a, :b, production)
|
33
|
+
expect(instance1).not_to be_repeating
|
32
34
|
end
|
33
35
|
end # context
|
34
36
|
|
35
37
|
context 'Provided services:' do
|
36
38
|
it 'should compare itself to another digram' do
|
37
|
-
instance1 =
|
38
|
-
same =
|
39
|
-
different =
|
39
|
+
instance1 = make_digram(:a, :b, production)
|
40
|
+
same = make_digram(:a, :b, production)
|
41
|
+
different = make_digram(:b, :c, production)
|
40
42
|
|
41
43
|
expect(instance1).to eq(instance1)
|
42
44
|
expect(instance1).to eq(same)
|
@@ -45,6 +47,5 @@ describe Digram do
|
|
45
47
|
end
|
46
48
|
end # context
|
47
49
|
end # describe
|
48
|
-
end # module
|
49
50
|
|
50
51
|
# End of file
|
@@ -5,14 +5,13 @@ require_relative '../spec_helper'
|
|
5
5
|
# Load the class under test
|
6
6
|
require_relative '../../lib/sequitur/dynamic_grammar'
|
7
7
|
|
8
|
-
|
9
|
-
describe DynamicGrammar do
|
8
|
+
describe Sequitur::DynamicGrammar do
|
10
9
|
# Factory method. Build a production with the given sequence
|
11
10
|
# of symbols as its rhs.
|
12
11
|
def build_production(*symbols)
|
13
|
-
prod = Production.new
|
12
|
+
prod = Sequitur::Production.new
|
14
13
|
symbols.each { |symb| prod.append_symbol(symb) }
|
15
|
-
|
14
|
+
prod
|
16
15
|
end
|
17
16
|
|
18
17
|
let(:p_a) { build_production(:a) }
|
@@ -20,10 +19,9 @@ describe DynamicGrammar do
|
|
20
19
|
let(:p_c) { build_production(:c) }
|
21
20
|
let(:p_bc) { build_production(p_b, p_c) }
|
22
21
|
|
23
|
-
|
24
22
|
context 'Creation & initialization:' do
|
25
23
|
it 'should be created without parameter' do
|
26
|
-
expect { DynamicGrammar.new }.not_to raise_error
|
24
|
+
expect { Sequitur::DynamicGrammar.new }.not_to raise_error
|
27
25
|
end
|
28
26
|
|
29
27
|
it 'should have an empty start/start production' do
|
@@ -33,7 +31,6 @@ describe DynamicGrammar do
|
|
33
31
|
end
|
34
32
|
end # context
|
35
33
|
|
36
|
-
|
37
34
|
context 'Adding productions to the grammar:' do
|
38
35
|
it 'should add a simple production' do
|
39
36
|
subject.add_production(p_a)
|
@@ -60,7 +57,6 @@ describe DynamicGrammar do
|
|
60
57
|
end
|
61
58
|
end # context
|
62
59
|
|
63
|
-
|
64
60
|
context 'Removing a production from the grammar:' do
|
65
61
|
it 'should remove an existing production' do
|
66
62
|
subject.add_production(p_a) # index = 1
|
@@ -91,7 +87,7 @@ describe DynamicGrammar do
|
|
91
87
|
context 'Visiting:' do
|
92
88
|
it 'should return a visitor' do
|
93
89
|
expect { subject.visitor }.not_to raise_error
|
94
|
-
expect(subject.visitor).to be_kind_of(GrammarVisitor)
|
90
|
+
expect(subject.visitor).to be_kind_of(Sequitur::GrammarVisitor)
|
95
91
|
end
|
96
92
|
|
97
93
|
it 'should accept a visitor' do
|
@@ -133,7 +129,6 @@ describe DynamicGrammar do
|
|
133
129
|
end
|
134
130
|
end # context
|
135
131
|
|
136
|
-
|
137
132
|
context 'Generating a text representation of itself:' do
|
138
133
|
it 'should generate a text representation when empty' do
|
139
134
|
expectation = "#{subject.start.object_id} : ."
|
@@ -141,6 +136,5 @@ describe DynamicGrammar do
|
|
141
136
|
end
|
142
137
|
end # context
|
143
138
|
end # describe
|
144
|
-
end # module
|
145
139
|
|
146
140
|
# End of file
|