Class: Ast::Tokeniser::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/ast_ast/tokeniser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, regex, proc = nil, &block) ⇒ Rule

Creates a new Rule instance

Parameters:

  • name (Symbol)

    Name of the token to be created.

  • regex (Regexp)

    Regular expression to be matched

  • proc (Proc) (defaults to: nil)

    Optional proc to be executed with match(es)



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

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/ast_ast/tokeniser.rb', line 6

def name
  @name
end

#procObject

Returns the value of attribute proc.



6
7
8
# File 'lib/ast_ast/tokeniser.rb', line 6

def proc
  @proc
end

#regexObject

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.

Examples:

Single tokens created (returns String)


class Klass < Ast::Tokeniser
  rule(:word, /[a-z]+/) {|i| i.reverse}
end

Klass.tokenise("backwards sdrawrof")
#=> [[:word, "sdrawkcab"], [:word, "forwards"]]

Multiple tokens created (returns Array)


class Klass < Ast::Tokeniser
  rule(:letter, /[a-z]+/) {|i| i.split('')}
end

Klass.tokenise("split up")
#=> [[:letter, "s"], [:letter, "p"], [:letter, "l"], [:letter, "i"], 
#     [:letter, "t"], [:letter, "u"], [:letter, "p"]]

Parameters:

  • val (String)

    the string that was matched to @regex

Returns:

  • (String, Array)


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