Class: FortIO::Namelist::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/fortio-namelist/fortran_namelist.tab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Scanner

Returns a new instance of Scanner.



20
21
22
23
# File 'lib/fortio-namelist/fortran_namelist.tab.rb', line 20

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

Instance Attribute Details

#in_namelistObject

Returns the value of attribute in_namelist.



25
26
27
# File 'lib/fortio-namelist/fortran_namelist.tab.rb', line 25

def in_namelist
  @in_namelist
end

Instance Method Details

#debug_infoObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fortio-namelist/fortran_namelist.tab.rb', line 27

def debug_info
  lines  = @s.string.split(/\n/)
  lineno = @s.string[0...@s.pos].split(/\n/).size
  info = ""
  if lineno > 1
    info << format("   %4i: %s\n", lineno-1, lines[lineno-2])
  end
  info << format(">> %4i: %s\n", lineno, lines[lineno-1])
  if lineno <= lines.size - 1
    info << format("   %4i: %s\n", lineno+1, lines[lineno])
  end
  info
end

#yylexObject



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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/fortio-namelist/fortran_namelist.tab.rb', line 41

def yylex
  while @s.rest?
    unless @in_namelist
      case
      when @s.scan(/\A([\$&])/)              ### {$|&}
        @in_namelist = "dummy"
        return [
          @s[0], 
          nil
        ]
      when @s.scan(/\A[^\$&]/)
        next
      end       
    else
      case
      when @s.scan(/\A\(/)
        return [
          '(',
          nil
        ]
      when @s.scan(/\A\)/)
        return [
          ')',
          nil
        ]
      when @s.scan(/\A\:/)
        return [
          ':',
          nil
        ]
      when @s.scan(/\A[+-]?(\d+)\.(\d+)?([ED][+-]?(\d+))?/i) ### float
        return [                              ### 1.2E+3, 1.E+3, 1.2E3
          :FLOAT,                             ### 1.2, 1.
          @s[0].sub(/D/i,'e').sub(/\.e/,".0e").to_f
        ]
      when @s.scan(/\A[+-]?\.(\d+)([ED][+-]?(\d+))?/i)       ### float
        return [                              ### .2E+3, -.2E+3, .2E3
          :FLOAT,                             ### .2, -.2
          @s[0].sub(/D/i,'e').sub(/\./, '0.').to_f
        ]
      when @s.scan(/\A[+-]?(\d+)[ED][+-]?(\d+)/i)            ### float
        return [                              ### 12E+3, 12E3, 0E0
          :FLOAT, 
          @s[0].sub(/D/i,'e').to_f
        ]
      when @s.scan(/\A\d+[a-z_]\w*/i)         ### STRING-Like
        return [
          :STRINGLIKE,
          @s[0]
        ]
      when @s.scan(/\A[\-\+]?\d+/)            ### digits
        return [
          :DIGITS, 
          Integer(@s[0])
        ]
      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,/)                     ### ,
        @s.scan(/\A[ \t]+/)
        while @s.scan(/\A\n[ \t]*/) or @s.scan(/\A\![^\n]*/)
          ### skip comment
        end
        if @s.scan(/\A\&[ \t]*\n[ \t]*\&/)  ### & &
          return [
            ',',
            nil
          ]
        elsif @s.match?(/\A[a-z]\w*\s*,/i) 
          return [
            ',', 
            nil
          ]
        elsif @s.match?(/\A[a-z]\w*/i) or @s.match?(/\A[\&\$\/\!]/)
          return [
            :COMMA, 
            nil
          ]
        elsif @s.match?(/\A,/)
          return [
            :NIL,
            nil
          ]
        else
          return [
            ',',
            nil
          ]
        end
      when @s.scan(/\A\&[ \t]*\n[ \t]*\&/)      ### & &
        next            
      when @s.scan(/\A[\$&\/=\(\):*]/)        ### {$|&|/|,|=|(|)|:|*}
        return [
          @s[0], 
          nil
        ]
      when @s.scan(/\A_\w*/i)                 ### STRING-Like
        return [
          :STRINGLIKE,
          @s[0]
        ]
      when @s.scan(/\A\.t[\w\d_]*\.?/i)             ### LOGICAL true
        return [                 
          :LOGICAL,
          true,
        ]
      when @s.scan(/\A\.f[\w\d_]*\.?/i)             ### LOGICAL false
        return [
          :LOGICAL,
          false,
        ]
      when @s.match?(/\At[^\w]/i)             ### LOGICAL true
        @s.scan(/\At/i)
        ms = @s[0]
        if @s.match?(/\A[ \t]*=/)
          return [
            :IDENT,
            ms
          ]
        else
          return [                 
            :LOGICAL,
            true,
          ]
        end
      when @s.match?(/\Af[^\w]/i)             ### LOGICAL false
        @s.scan(/\Af/i)
        ms = @s[0]
        if @s.match?(/\A[ \t]*=/)
          return [
            :IDENT,
            ms
          ]
        else
          return [                 
            :LOGICAL,
            false,
          ]
        end
      when @s.scan(/\A[a-z]\w*/i)             ### IDENT or LOGICAL
        return [
          :IDENT,
          @s[0]
        ]
      when @s.scan(/\A\n/)                    ### newline
        return [
          :NL,
          nil
        ]
        next
      when @s.scan(/\A[ \t]+/)                ### blank
        next
      when @s.scan(/\A![^\n]*?\n/)            ### comment
        next
      else
        @s.rest =~ /\A(.*)$/
        raise "namelist parse error ('#{$1}')\n" + debug_info
      end
    end
  end
end