Class: EPUB::Parser::CFI

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/epub/parser/cfi.rb

Constant Summary collapse

UNICODE_CHARACTER_EXCLUDING_SPECIAL_CHARS_AND_SPACE_AND_DOT_AND_COLON_AND_TILDE_AND_ATMARK_AND_SOLIDUS_AND_EXCLAMATION_MARK_PATTERN =

excluding special chars and space(u0020) and dot(u002E) and colon(u003A) and tilde(u007E) and atmark(u0040) and solidus(u002F) and exclamation mark(u0021)

/\u0009|\u000A|\u000D|[\u0022-\u0027]|[\u002A-\u002B]|\u002D|[\u0030-\u0039]|\u003C|[\u003E-\u0040]|[\u0041-\u005A]|\u005C|[\u005F-\u007D]|[\u007F-\uD7FF]|[\uE000-\uFFFD]|[\u10000-\u10FFFF]/
UNICODE_CHARACTER_PATTERN =
Regexp.union(UNICODE_CHARACTER_EXCLUDING_SPECIAL_CHARS_AND_SPACE_AND_DOT_AND_COLON_AND_TILDE_AND_ATMARK_AND_SOLIDUS_AND_EXCLAMATION_MARK_PATTERN, Regexp.new(Regexp.escape(EPUB::CFI::SPECIAL_CHARS), / \.:~@!/))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug: false) ⇒ CFI

Returns a new instance of CFI.



19
20
21
22
# File 'lib/epub/parser/cfi.rb', line 19

def initialize(debug: false)
  @yydebug = debug
  super()
end

Class Method Details

.parse(string, debug: false) ⇒ Object



14
15
16
# File 'lib/epub/parser/cfi.rb', line 14

def parse(string, debug: false)
  new(debug: debug).parse(string)
end

Instance Method Details

#next_tokenObject



77
78
79
# File 'lib/epub/parser/cfi.rb', line 77

def next_token
  @q.shift
end

#parse(string) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/epub/parser/cfi.rb', line 24

def parse(string)
  if string.start_with? 'epubcfi('
    string = string['epubcfi('.length .. -2]
  end
  @scanner = StringScanner.new(string, true)
  @q = []
  until @scanner.eos?
    case
    when @scanner.scan(/[1-9]/)
      @q << [:DIGIT_NON_ZERO, @scanner[0]]
    when @scanner.scan(/0/)
      @q << [:ZERO, @scanner[0]]
    when @scanner.scan(/ /)
      @q << [:SPACE, @scanner[0]]
    when @scanner.scan(/\^/)
      @q << [:CIRCUMFLEX, @scanner[0]]
    when @scanner.scan(/\[/)
      @q << [:OPENING_SQUARE_BRACKET, @scanner[0]]
    when @scanner.scan(/\]/)
      @q << [:CLOSING_SQUARE_BRACKET, @scanner[0]]
    when @scanner.scan(/\(/)
      @q << [:OPENING_PARENTHESIS, @scanner[0]]
    when @scanner.scan(/\)/)
      @q << [:CLOSING_PARENTHESIS, @scanner[0]]
    when @scanner.scan(/,/)
      @q << [:COMMA, @scanner[0]]
    when @scanner.scan(/;/)
      @q << [:SEMICOLON, @scanner[0]]
    when @scanner.scan(/=/)
      @q << [:EQUAL, @scanner[0]]
    when @scanner.scan(/\./)
      @q << [:DOT, @scanner[0]]
    when @scanner.scan(/:/)
      @q << [:COLON, @scanner[0]]
    when @scanner.scan(/~/)
      @q << [:TILDE, @scanner[0]]
    when @scanner.scan(/@/)
      @q << [:ATMARK, @scanner[0]]
    when @scanner.scan(/\//)
      @q << [:SOLIDUS, @scanner[0]]
    when @scanner.scan(/!/)
      @q << [:EXCLAMATION_MARK, @scanner[0]]
    when @scanner.scan(UNICODE_CHARACTER_EXCLUDING_SPECIAL_CHARS_AND_SPACE_AND_DOT_AND_COLON_AND_TILDE_AND_ATMARK_AND_SOLIDUS_AND_EXCLAMATION_MARK_PATTERN)
      @q << [:UNICODE_CHARACTER_EXCLUDING_SPECIAL_CHARS_AND_SPACE_AND_DOT_AND_COLON_AND_TILDE_AND_ATMARK_AND_SOLIDUS_AND_EXCLAMATION_MARK, @scanner[0]]
    else
      raise 'unexpected character'
    end
  end
  @q << [false, false]

  do_parse
end