Class: Yaparc::Identifier

Inherits:
Object
  • Object
show all
Includes:
Parsable
Defined in:
lib/yaparc.rb

Overview

Constant Summary collapse

IDENTIFIER_REGEX =
/\A[a-zA-Z_]+[a-zA-Z0-9_]*/

Constants included from Parsable

Parsable::IS_ALPHANUM, Parsable::IS_CR, Parsable::IS_DIGIT, Parsable::IS_LOWER, Parsable::IS_SPACE, Parsable::IS_WHITESPACE

Instance Attribute Summary

Attributes included from Parsable

#tree

Instance Method Summary collapse

Methods included from Parsable

#parse

Constructor Details

#initialize(options = {}) ⇒ Identifier

Returns a new instance of Identifier.



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/yaparc.rb', line 464

def initialize(options = {})
  identifier_regex = if regex = options[:regex]
                       ::Yaparc::Regex.new(regex)
                     else
                       ::Yaparc::Regex.new(IDENTIFIER_REGEX)
                     end

  tokenizer = Tokenize.new(identifier_regex)

  if exclude = options[:exclude]
    @parser = lambda do |input|
      keyword_parsers = exclude.map {|keyword| Yaparc::String.new(keyword)}

      case result = Yaparc::Alt.new(*keyword_parsers).parse(input)
      when Yaparc::Result::OK
        Yaparc::Fail.new
      else # Result::Fail or Result::Error
        tokenizer
      end
    end
  else
    @parser = lambda do |input|
      tokenizer
    end
  end
end