Module: RLSM::RE::ParserHelpers

Included in:
Parser
Defined in:
lib/rlsm/regexp_parser.rb

Overview

:nodoc:

Constant Summary collapse

OpenBracket =

:nodoc:

'('
CloseBracket =
')'
UnionSymbol =
'|'
StarSymbol =
'*'
EmptyWordSymbol =
'@'
LetterRegexp =
/[a-zA-Z0-9]/

Instance Method Summary collapse

Instance Method Details

#close_bracket?(char) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/rlsm/regexp_parser.rb', line 18

def close_bracket?(char)
  char.to_s == CloseBracket
end

#empty_set?(input) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rlsm/regexp_parser.rb', line 38

def empty_set?(input)
  !input.any? { |position| letter?(position) or empty_symbol?(position) } 
end

#empty_symbol?(char) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rlsm/regexp_parser.rb', line 30

def empty_symbol?(char)
  char.to_s == EmptyWordSymbol
end

#empty_word?(input) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/rlsm/regexp_parser.rb', line 42

def empty_word?(input)
  input.any? { |position| empty_symbol?(position) } and
    input.all? { |position| !letter?(position) } and not
    input.join.include?(OpenBracket + CloseBracket)
end

#letter?(char) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rlsm/regexp_parser.rb', line 34

def letter?(char)
  char.to_s =~ LetterRegexp
end

#open_bracket?(char) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rlsm/regexp_parser.rb', line 14

def open_bracket?(char)
  char.to_s == OpenBracket
end

#single_letter?(input) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/rlsm/regexp_parser.rb', line 48

def single_letter?(input)
  input.size == 1 and letter?(input[0])
end

#star?(input) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
# File 'lib/rlsm/regexp_parser.rb', line 62

def star?(input)
  return false unless star_symbol?(input[-1])
  return true if input.size == 2
  
  return star?(input[0..-2]) if star_symbol?(input[-2])

  open_bracket?(input[0]) and close_bracket?(input[-2]) and
    !Parser.unbalanced_brackets?(input[1..-3])
end

#star_symbol?(char) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rlsm/regexp_parser.rb', line 26

def star_symbol?(char)
  char.to_s == StarSymbol
end

#union?(input) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/rlsm/regexp_parser.rb', line 52

def union?(input)
  depth = 0
  input.each do |position|
    return true if depth == 0 and union_symbol?(position)
    depth += position.weight
  end

  false
end

#union_symbol?(char) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rlsm/regexp_parser.rb', line 22

def union_symbol?(char)
  char.to_s == UnionSymbol
end