You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Map substitutions are bidirectional. The direction of match to target can be changed between left to right and right to left by flipping the < and > applicators.
Example:
calyx.grammar({start: "{@animal} {verb} {@animal>posessive} {appendage}",animal: ["Snowball","Santa’s Little Helper"]posessive: {"Snowball": "her","Santa’s Little Helper": "his"},verb: ["chases","licks","bites"],appendage: ["tail","paw"]})
Extension to productions
Grammar rules will need to support objects with string key/values representing a PairedMap or Dictionary production, as an alternative to Choice and WeightedChoice productions.
The parsing algorithm branching in (whatever is currently used for) build_ast needs to change to check for this new shape and create the production:
classRuledefself.build_ast(productions,registry)ifproductions.first.is_a?(Hash)# TODO: test that key is a stringifproductions.first.first.last.is_a?(String)# If value of the production is a strings then this is a# paired mapping production.Syntax::PairedMapping.parse(productions.first,registry)else# Otherwise, we assume this is a weighted choice declaration and# convert the hash to an arraySyntax::WeightedChoices.parse(productions.first.to_a,registry)endelsifproductions.first.is_a?(Enumerable)# TODO: this needs to change to support attributed/tagged grammarsSyntax::WeightedChoices.parse(productions,registry)elseSyntax::Choices.parse(productions,registry)endend
New syntax nodes
AST node for PairedMapping which needs to be lexed by hitting < and > symbols in the same position as the current . for modifier dereferencing.
The lookup itself is a unary function that applies either lhs to rhs or rhs to lhs key/value lookups. The Ruby version uses a modified radix tree dictionary with custom rules for skipping and concatenating wildcard matches.
This could probably be optimised a lot better, but the question right now is whether to optimise for performance or maintenance. There are a few details of the Ruby prototype that feel overengineered and could be compacted (like having separate node and edge structs and storing data on the edges, which feels more baroque than it needs to be).
Test cases
describe'wildcard match'dolet(:paired_map)doCalyx::Syntax:: PairedMapping.parse({"%y"=>"%ies","%s"=>"%ses","%"=>"%s"},registry)endspecify'lookup from key to value'doexpect(paired_map.value_for('ferry')).toeq('ferries')expect(paired_map.value_for('bus')).toeq('buses')expect(paired_map.value_for('car')).toeq('cars')endspecify'lookup from value to key'doexpect(paired_map.key_for('ferries')).toeq('ferry')expect(paired_map.key_for('buses')).toeq('bus')expect(paired_map.key_for('cars')).toeq('car')endend
Test case informing patterns and guidance for authors
Contradictory/recursive logic that some people might attempt (how to warn, what expectations to set, etc):
From the original design docs:
Example:
Extension to productions
Grammar rules will need to support objects with string key/values representing a
PairedMap
orDictionary
production, as an alternative toChoice
andWeightedChoice
productions.The parsing algorithm branching in (whatever is currently used for)
build_ast
needs to change to check for this new shape and create the production:New syntax nodes
AST node for
PairedMapping
which needs to be lexed by hitting<
and>
symbols in the same position as the current.
for modifier dereferencing.The lookup itself is a unary function that applies either lhs to rhs or rhs to lhs key/value lookups. The Ruby version uses a modified radix tree dictionary with custom rules for skipping and concatenating wildcard matches.
This could probably be optimised a lot better, but the question right now is whether to optimise for performance or maintenance. There are a few details of the Ruby prototype that feel overengineered and could be compacted (like having separate node and edge structs and storing data on the edges, which feels more baroque than it needs to be).
Test cases
Test case informing patterns and guidance for authors
Contradictory/recursive logic that some people might attempt (how to warn, what expectations to set, etc):
Need to document the full bidirectional matrix:
The text was updated successfully, but these errors were encountered: