-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce value object which encapsulates a generated result
As described in #23, this introduces a result object which wraps the expression tree returned from evaluating a grammar and provides access to the result as a raw tree, flattened string or symbol. The generated result object is returned from a new `Calyx::Grammar#generate_result` method. `Calyx::Grammar#generate` still returns a string and `Calyx::Grammar#evaluate` now fires a deprecation warning and will be removed soon.
- Loading branch information
Showing
5 changed files
with
111 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Calyx | ||
# Value object representing a generated grammar result. | ||
class Result | ||
def initialize(expression) | ||
@expression = expression.freeze | ||
end | ||
|
||
# The syntax tree of nested nodes representing the production rules which | ||
# generated this result. | ||
# | ||
# @return [Array] | ||
def tree | ||
@expression | ||
end | ||
|
||
alias_method :to_exp, :tree | ||
|
||
# The generated text string produced by the grammar. | ||
# | ||
# @return [String] | ||
def text | ||
@expression.flatten.reject do |obj| | ||
obj.is_a?(Symbol) | ||
end.join | ||
end | ||
|
||
alias_method :to_s, :text | ||
|
||
# A symbol produced by converting the generated text string where possible. | ||
# | ||
# @return [Symbol] | ||
def symbol | ||
text.to_sym | ||
end | ||
|
||
alias_method :to_sym, :symbol | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'spec_helper' | ||
|
||
describe Calyx::Result do | ||
describe '#tree' do | ||
specify 'wraps expression tree' do | ||
tree = Calyx::Result.new([:root, [:leaf, 'atom']]).tree | ||
expect(tree).to eq([:root, [:leaf, 'atom']]) | ||
end | ||
|
||
specify 'expression tree is immutable' do | ||
tree = Calyx::Result.new([:root, [:leaf, 'atom']]).tree | ||
expect { tree << [:leaf, 'atom'] }.to raise_error(RuntimeError) | ||
end | ||
|
||
specify '#to_exp aliases #tree' do | ||
result = Calyx::Result.new([:root, [:leaf, 'atom']]) | ||
expect(result.to_exp).to eq(result.tree) | ||
end | ||
end | ||
|
||
describe '#text' do | ||
specify 'flattens expression tree to string' do | ||
expr = [:root, [:branch, [:leaf, 'one'], [:leaf, ' '], [:leaf, 'two']]] | ||
text = Calyx::Result.new(expr).text | ||
expect(text).to eq('one two') | ||
end | ||
|
||
specify '#to_s aliases #text' do | ||
result = Calyx::Result.new([:root, [:leaf, 'atom']]) | ||
expect(result.to_s).to eq(result.text) | ||
end | ||
|
||
specify '#to_s interpolates automatically' do | ||
result = Calyx::Result.new([:root, [:leaf, 'atom']]) | ||
expect("#{result}").to eq(result.text) | ||
end | ||
end | ||
|
||
describe '#symbol' do | ||
specify 'flattens expression tree to symbol' do | ||
symbol = Calyx::Result.new([:root, [:leaf, 'atom']]).symbol | ||
expect(symbol).to eq(:atom) | ||
end | ||
|
||
specify '#to_sym aliases #symbol' do | ||
result = Calyx::Result.new([:root, [:leaf, 'atom']]) | ||
expect(result.to_sym).to eq(result.symbol) | ||
end | ||
end | ||
end |