Class: Yaparc::Identifier

Inherits:
Object
  • Object
show all
Includes:
Parsable
Defined in:
lib/yaparc/identifier.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 Method Summary collapse

Methods included from Parsable

#parse

Constructor Details

#initialize(regex: nil, exclude: nil) ⇒ Identifier

Returns a new instance of Identifier.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yaparc/identifier.rb', line 10

def initialize(regex: nil, exclude: nil)
  identifier_regex = ::Yaparc::Regex.new(regex || IDENTIFIER_REGEX)

  tokenizer = Tokenize.new(identifier_regex)

  unless exclude
    @parser = proc { tokenizer }
    return
  end

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

    case result = Yaparc::Alt.new(*keyword_parsers).parse(input)
    when Yaparc::OK
      Yaparc::FailParser.new
    else # Fail or Error
      tokenizer
    end
  end
end