File: date_time_parser.rl

package info (click to toggle)
ruby-mail 2.7.1%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,436 kB
  • sloc: ruby: 71,596; makefile: 3
file content (62 lines) | stat: -rw-r--r-- 1,351 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# frozen_string_literal: true
require 'mail/utilities'
require 'mail/parser_tools'

%%{
  machine date_time;
  alphtype int;

  # Date
  action date_s { date_s = p }
  action date_e { date_time.date_string = chars(data, date_s, p-1) }

  # Time
  action time_s { time_s = p }
  action time_e { date_time.time_string = chars(data, time_s, p-1) }

  # No-op actions
  action comment_s {}
  action comment_e {}
  action phrase_s {}
  action phrase_e {}
  action qstr_s {}
  action qstr_e {}

  include rfc5322_date_time "rfc5322_date_time.rl";
  main := date_time;
}%%

module Mail::Parsers
  module DateTimeParser
    extend Mail::ParserTools

    DateTimeStruct = Struct.new(:date_string, :time_string, :error)

    %%write data noprefix;

    def self.parse(data)
      data = data.dup.force_encoding(Encoding::ASCII_8BIT) if data.respond_to?(:force_encoding)

      raise Mail::Field::NilParseError.new(Mail::DateTimeElement) if data.nil?

      date_time = DateTimeStruct.new([])

      # Parser state
      date_s = time_s = nil

      # 5.1 Variables Used by Ragel
      p = 0
      eof = pe = data.length
      stack = []

      %%write init;
      %%write exec;

      if p != eof || cs < %%{ write first_final; }%%
        raise Mail::Field::IncompleteParseError.new(Mail::DateTimeElement, data, p)
      end

      date_time
    end
  end
end