Class: FortranFormatScanner

Inherits:
Object
  • Object
show all
Defined in:
ext/fortio/lib/fortio/fortran_format.tab.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ FortranFormatScanner

Returns a new instance of FortranFormatScanner.



14
15
16
# File 'ext/fortio/lib/fortio/fortran_format.tab.rb', line 14

def initialize (text)
  @s = StringScanner.new(text)
end

Instance Method Details

#yylexObject



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/fortio/lib/fortio/fortran_format.tab.rb', line 18

def yylex
  while @s.rest?
    case
    when @s.scan(/\A(ES|[FEDG])(\d+)\.(\d+)/i) ### {F|E|D|G|ES}w.d
      return [
        @s[1].upcase.to_sym, 
        [
          @s[2].to_i, 
          @s[3].to_i
        ]
      ]
    when @s.scan(/\AE(\d+)/i)               ### Ed (for exponential)
      return [
        :EXP,
        @s[1].to_i
      ]
    when @s.scan(/\AI(\d+)(\.(\d+))?/i)     ### Iw(.d)
      return [
        :I, 
        [
          @s[1].to_i, 
          @s[3] ? @s[3].to_i : nil
        ]
      ]
    when @s.scan(/\AL(\d+)?/i)              ### L(w)
      return [
        :L, 
        @s[1] ? @s[1].to_i : nil
      ]
    when @s.scan(/\AA(\d+)?/i)              ### A(w)
      return [
        :A, 
        @s[1] ? @s[1].to_i : nil
      ]
    when @s.scan(/\AX/i)                    ### X
      return [
        :X,
        nil
      ]
    when @s.scan(/\ATL(\d+)?/i)             ### TLw
      return [
        :TL,
        @s[1] ? @s[1].to_i : 1
      ]
    when @s.scan(/\ATR(\d+)?/i)             ### TRw
      return [
        :TR,
        @s[1] ? @s[1].to_i : 1
      ]
    when @s.scan(/\AT(\d+)?/i)              ### Tw
      return [
        :T,
        @s[1] ? @s[1].to_i : 1
      ]
    when @s.scan(/\A([+-]?\d+)P/i)          ### {+|-|}P
      return [
        :P,
        @s[1].to_i
      ]
    when @s.scan(/\AS[PS]?/i)               ### S,SP,SS
      return [
        :Sp,
        @s[0] =~ /SP/i ? true : false
      ]
    when @s.scan(/\AB[NZ]/i)                ### BN,BZ
      return [
        :B,
        @s[0] =~ /BZ/i ? true : false
      ]
    when @s.match?(/\A(\d+)?H/i)            ### Hollerith 
      count  = @s[1] ? @s[1].to_i : 1
      if @s.scan(/\A(\d+)?H(.{#{count}})/)
        return [
          :H,
          [count, @s[2]]
        ]
     else
      raise "invalid horeris descriptor"
      end
    when @s.scan(/\A'((?:''|[^'])*)'/)      ### 'quoted string'
      return [
        :STRING, 
        @s[1].gsub(/''/, "'")
      ]
    when @s.scan(/\A"((?:""|[^"])*)"/)      ### 'double-quoted string'
      return [
        :STRING, 
        @s[1].gsub(/""/, '"')
      ]
    when @s.scan(/\A(\d+)/)                 ### digits
      return [
        :DIGITS, 
        @s[1].to_i
      ]
    when @s.scan(/\A([\(\)\/\$\:])/)        ### {(|)|/|$}
      return [
        @s[1], 
        nil
      ]
    when @s.scan(/:/)
     raise("format descriptor ':' is not supported.")
    when @s.scan(/\A,/)                     ### blank
      next
    when @s.scan(/\A\s+/)                   ### blank
      next
    else
      raise "FortranFormat parse error\n\t#{@s.string}\n\t#{' '*@s.pos}^"
    end
  end
end