Class: Lexeme::Ruleset

Inherits:
Object
  • Object
show all
Defined in:
lib/lexeme/ruleset.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Ruleset

Returns a new instance of Ruleset.

Yields:

  • (_self)

Yield Parameters:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/lexeme/ruleset.rb', line 3

def initialize(&block)
  @rules  = []
  @ignore = [] 
  
  # this is here to capture any other
  # symbols that could be identified
  # as var names, function names ...
  @unknown = Rule.new(nil, /^\w+$/)

  # this skips all whitespaces by default
  @ignore << /^\s+/

  yield self if block_given?
end

Instance Method Details

#identifiable?(string) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lexeme/ruleset.rb', line 34

def identifiable?(string)
  @rules.each do |r|
    return true if string =~ r.regex
  end

  return true if string =~ @unknown.regex
  
  @ignore.each do |i|
    return true if string =~ i
  end

  false
end

#identify(string, line) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/lexeme/ruleset.rb', line 48

def identify(string, line)
  @rules.each do |r|
    return Token.new(r.name, string, line) if string =~ r.regex
  end
  
  return Token.new(@unknown.name, string, line) if 
    string =~ @unknown.regex

  nil
end

#ignorable?(string) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/lexeme/ruleset.rb', line 26

def ignorable?(string)
  @ignore.each do |i|
    return true if string =~ i
  end

  false
end

#ignore(regex) ⇒ Object



22
23
24
# File 'lib/lexeme/ruleset.rb', line 22

def ignore(regex)
  @ignore << regex
end

#rule(name, regex) ⇒ Object



18
19
20
# File 'lib/lexeme/ruleset.rb', line 18

def rule(name, regex)
  @rules << Rule.new(name, regex)
end