Class: Rattler::Parsers::ActionCode
- Inherits:
-
Object
- Object
- Rattler::Parsers::ActionCode
- Defined in:
- lib/rattler/parsers/action_code.rb
Overview
ActionCode
defines abstract ruby code with variables that can be bound to captured parse results to produce concrete ruby code for semantic actions.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#param_names ⇒ Object
readonly
Returns the value of attribute param_names.
Instance Method Summary collapse
-
#arg_bindings(args) ⇒ Hash
Matchers for parameters in the ruby code associated with
args
as replacements values. -
#bind(scope) ⇒ String
Bind parameters and variables in the code to parse results in
scope
. -
#initialize(code) ⇒ ActionCode
constructor
A new instance of ActionCode.
-
#scoped_bindings(scope) ⇒ Hash
Matchers for variables in the ruby code associated with replacement values.
Constructor Details
#initialize(code) ⇒ ActionCode
Returns a new instance of ActionCode.
12 13 14 15 16 17 18 19 20 |
# File 'lib/rattler/parsers/action_code.rb', line 12 def initialize(code) if md = /\A\s*\|([^|]*)\|(.*)\Z/.match(code) @param_names = md[1].split(',').map {|_| _.strip } @body = md[2].strip else @param_names = [] @body = code.strip end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
22 23 24 |
# File 'lib/rattler/parsers/action_code.rb', line 22 def body @body end |
#param_names ⇒ Object (readonly)
Returns the value of attribute param_names.
22 23 24 |
# File 'lib/rattler/parsers/action_code.rb', line 22 def param_names @param_names end |
Instance Method Details
#arg_bindings(args) ⇒ Hash
Returns matchers for parameters in the ruby code associated with args
as replacements values.
43 44 45 46 47 48 |
# File 'lib/rattler/parsers/action_code.rb', line 43 def arg_bindings(args) if param_names.count > args.count raise ArgumentError, 'more parameter names than arguments' end to_bindings param_names.zip(args) end |
#bind(scope) ⇒ String
Bind parameters and variables in the code to parse results in scope
29 30 31 |
# File 'lib/rattler/parsers/action_code.rb', line 29 def bind(scope) bind_in body, scope end |
#scoped_bindings(scope) ⇒ Hash
Returns matchers for variables in the ruby code associated with replacement values.
36 37 38 |
# File 'lib/rattler/parsers/action_code.rb', line 36 def scoped_bindings(scope) to_bindings(scope.bindings).merge(arg_bindings(scope.captures)) end |