Sha256: 008ae0364856690365cf2421e8db057a175b6a6c3a347bccddece3a5171ab3ba

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

module Teeth
  
  class DuplicateRuleError < ScannerError
  end

  class RuleStatement
    attr_reader :name, :regex, :strip_ends, :skip_line, :begin
    
    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
    
    def ==(other)
      other.kind_of?(RuleStatement) && other.name == name && other.regex == regex
    end
    
    def scanner_code
      if @ignore
        regex
      else
        "#{regex} {\n" + function_body + "}"
      end
    end
        
    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
    
    def yytext_statement
      strip_ends ? "strip_ends(yytext)" : "yytext"
    end
    
  end
  
  class RuleStatementGroup < Array
    
    def add(name, regex, options={})
      push RuleStatement.new(name, regex, options)
    end
    
    def rule_names
      map { |rule_statement| rule_statement.name.to_s }
    end
    
    def method_missing(called_method_name, *args, &block)
      args[1] ||={}
      add(called_method_name, args[0], args[1])
    end
    
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
danielsdeleo-teeth-0.1.0 lib/rule_statement.rb
danielsdeleo-teeth-0.1.1 lib/rule_statement.rb
danielsdeleo-teeth-0.1.2 lib/rule_statement.rb
teeth-0.2.0 lib/teeth/rule_statement.rb