Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result value object: Breaking API change proposed for 1.0 #23

Open
maetl opened this issue Sep 3, 2017 · 1 comment
Open

Result value object: Breaking API change proposed for 1.0 #23

maetl opened this issue Sep 3, 2017 · 1 comment

Comments

@maetl
Copy link
Owner

maetl commented Sep 3, 2017

Rather than having to switch between grammar.generate and grammar.evaluate to return a string or raw evaluation tree, this behaviour could be unified by returning a result value object from #generate.

As part of this change, grammar.evaluate would probably be removed from the public API and be considered internal-only.

Rough sketch of the API:

# @return [Calyx::Result]
result = grammar.generate

# Generated text string
result.text

# Generated text converted to a symbol
result.symbol

# Expression tree
result.tree

# Alternative interface
result.to_sym
result.to_s
result.to_exp

This allows both the expanded string and the underlying tree of selected syntax nodes to be easily accessible for all grammar runs, rather than having to pick an output format and call the appropriate method.

It’s also an extension point for overlaying metadata/tagging with chosen productions in future.

If the result object implements #to_s and #inspect it will be straightforward to update a lot of existing usages. However it is a breaking API change so does have potential to cause disruption,
which is why I’d like to get it in before 1.0, so that the API can stabilise then.

@maetl maetl changed the title Breaking API change proposed for 1.0 Result value object: Breaking API change proposed for 1.0 Sep 21, 2017
maetl added a commit that referenced this issue Sep 21, 2017
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.
@maetl
Copy link
Owner Author

maetl commented Sep 21, 2017

Upgrade Guide

Calling #evaluate on the grammar instance will now trigger a deprecation warning.

grammar = Calyx::Grammar.new do
  # ...
end

grammar.evaluate

In 0.17, the above behaviour can be reproduced by replacing the call to #evaluate with #generate_result.

grammar = Calyx::Grammar.new do
  # ...
end

grammar.generate_result.tree

In 1.0, #generate_result will move to #generate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant