Class: Ast::Tokeniser::Rule
- Inherits:
-
Object
- Object
- Ast::Tokeniser::Rule
- Defined in:
- lib/ast_ast/tokeniser.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#proc ⇒ Object
Returns the value of attribute proc.
-
#regex ⇒ Object
Returns the value of attribute regex.
Instance Method Summary collapse
-
#initialize(name, regex, proc = nil, &block) ⇒ Rule
constructor
Creates a new Rule instance.
-
#run(val) ⇒ String, Array
Runs the block that was given using either, the full match if there were no captures in the regex, or an array of the captures.
Constructor Details
#initialize(name, regex, proc = nil, &block) ⇒ Rule
Creates a new Rule instance
17 18 19 20 21 |
# File 'lib/ast_ast/tokeniser.rb', line 17 def initialize(name, regex, proc=nil, &block) @name = name @regex = regex @proc = proc || block end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/ast_ast/tokeniser.rb', line 6 def name @name end |
#proc ⇒ Object
Returns the value of attribute proc.
6 7 8 |
# File 'lib/ast_ast/tokeniser.rb', line 6 def proc @proc end |
#regex ⇒ Object
Returns the value of attribute regex.
6 7 8 |
# File 'lib/ast_ast/tokeniser.rb', line 6 def regex @regex end |
Instance Method Details
#run(val) ⇒ String, Array
Runs the block that was given using either, the full match if there were no captures in the regex, or an array of the captures.
If a String is returned, which will be the case most of the time, a single token is created with the value that is returned. But if an Array is returned then multiple tokens will be created for each item in the Array. See the examples for a better explanation.
54 55 56 57 58 59 60 |
# File 'lib/ast_ast/tokeniser.rb', line 54 def run(val) arr = val.match(@regex).to_a val = arr unless arr.empty? val = arr[0] if arr.size == 1 val = arr[0] if arr[0] == arr[1] # this happens with /(a|b|c)/ regexs @proc.call val end |