Class: Teeth::RuleStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/teeth/rule_statement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, regex, options = {}) ⇒ RuleStatement

Returns a new instance of RuleStatement.



9
10
11
12
13
# File 'lib/teeth/rule_statement.rb', line 9

def initialize(name, regex, options={})
  @name, @regex = name, regex
  @strip_ends, @skip_line, @begin = options[:strip_ends], options[:skip_line], options[:begin]
  @ignore = options[:ignore]
end

Instance Attribute Details

#beginObject (readonly)

Returns the value of attribute begin.



7
8
9
# File 'lib/teeth/rule_statement.rb', line 7

def begin
  @begin
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/teeth/rule_statement.rb', line 7

def name
  @name
end

#regexObject (readonly)

Returns the value of attribute regex.



7
8
9
# File 'lib/teeth/rule_statement.rb', line 7

def regex
  @regex
end

#skip_lineObject (readonly)

Returns the value of attribute skip_line.



7
8
9
# File 'lib/teeth/rule_statement.rb', line 7

def skip_line
  @skip_line
end

#strip_endsObject (readonly)

Returns the value of attribute strip_ends.



7
8
9
# File 'lib/teeth/rule_statement.rb', line 7

def strip_ends
  @strip_ends
end

Instance Method Details

#==(other) ⇒ Object



15
16
17
# File 'lib/teeth/rule_statement.rb', line 15

def ==(other)
  other.kind_of?(RuleStatement) && other.name == name && other.regex == regex
end

#function_bodyObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/teeth/rule_statement.rb', line 27

def function_body
  code = ""
  code += "  BEGIN(#{@begin});\n" if @begin
  if skip_line
    code += "  return EOF_KVPAIR;\n"
  else
    code += "  KVPAIR #{name.to_s} = {\"#{name.to_s}\", #{yytext_statement}};\n" +
    "  return #{name.to_s};\n"
  end
  code
end

#scanner_codeObject



19
20
21
22
23
24
25
# File 'lib/teeth/rule_statement.rb', line 19

def scanner_code
  if @ignore
    regex
  else
    "#{regex} {\n" + function_body + "}"
  end
end

#yytext_statementObject



39
40
41
# File 'lib/teeth/rule_statement.rb', line 39

def yytext_statement
  strip_ends ? "strip_ends(yytext)" : "yytext"
end