Class: Solidity::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/solidity/parser.rb

Constant Summary collapse

SINGLE_QUOTE =
%r{'
  ( \\\\. | [^'] )*
'}x
DOUBLE_QUOTE =
%r{"
 ( \\\\. | [^"] )*
"}x
NAME =

from the solidity grammar

> An identifier in solidity has to start with a letter,
>  a dollar-sign or an underscore and
>  may additionally contain numbers after the first symbol.

Identifier

 : IdentifierStart IdentifierPart* ;

fragment
  IdentifierStart
  : [a-zA-Z$_] ;

fragment
  IdentifierPart
 : [a-zA-Z0-9$_] ;
/[a-zA-Z$_][a-zA-Z0-9$_]*/
END_OF_LINE =
/\n|$/
END_OF_LINE_OR_COMMENT_OR_KEYWORD_OR_QUOTE =

inline comments (multi- or end-of-line)

/ \n
  | $
  | (?=['"])
  | (?=\/(\/|\*))
  | (?=\bpragma\b)
  | (?=\bcontract\b)
  | (?=\babstract\b)
  | (?=\blibrary\b)
  | (?=\binterface\b)
/x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ Parser

Returns a new instance of Parser.



12
13
14
# File 'lib/solidity/parser.rb', line 12

def initialize( txt )
  @txt = txt
end

Class Method Details

.read(path) ⇒ Object



5
6
7
8
# File 'lib/solidity/parser.rb', line 5

def self.read( path )
   txt = read_text( path )
   new( txt )
end

Instance Method Details

#_norm_whitespace(str) ⇒ Object



96
97
98
99
100
101
# File 'lib/solidity/parser.rb', line 96

def _norm_whitespace( str )
   ## change newlines to spaces and
   ##   all multiple spaces to one
   str = str.gsub( /[ \t\n\r]+/, ' ' )
   str.strip
end

#_quick_pass_oneObject



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
# File 'lib/solidity/parser.rb', line 103

def _quick_pass_one
  ## note:  CANNOT handle inline comments
  ##           in pragma, contract, etc.  (get "silently" slurped)
  ##         report a parse error - if comments slurped - why? why not?
  ##


  tree = []

  s = StringScanner.new( @txt )

  loop do
    s.skip( /[ \t]+/ )  ## note: do NOT skip newlines here; pass along blank/empty lines for now - why? why not?
    if s.check( "'" )   ## single-quoted string
       str = s.scan( SINGLE_QUOTE )
       tree << [:string, str]
    elsif s.check( '"' )  ## double-quoted string
       str = s.scan( DOUBLE_QUOTE )
       tree << [:string, str]
    elsif s.check( '/*' )
       comment = s.scan_until( /\*\// )
       ## print "multi-line comment:"
       ## pp comment
       tree << [:comment, comment]
    elsif s.check( '//' )
       comment = s.scan_until( END_OF_LINE ).rstrip
       ## print "comment:"
       ## pp comment
       tree << [:comment, comment]
    else
       name = s.check( NAME )
       case name
       when 'pragma'
          code = s.scan_until( /;/ )
          code = _norm_whitespace( code )
          ## print "pragma:"
          ## pp code
          tree << [:pragma, code]
       when 'contract'
          code = s.scan_until( /(?=\{)/ )
          code = _norm_whitespace( code )
          ## print "contract:"
          ## pp code
          tree << [:contract, code]
       when 'abstract'
         code = s.scan_until( /(?=\{)/ )
         code = _norm_whitespace( code )
         ## print "abstract contract:"
         ## pp code
         tree << [:abstract_contract, code]
       when 'library'
         code = s.scan_until( /(?=\{)/ )
         code = _norm_whitespace( code )
         ## print "library:"
         ## pp code
         tree << [:library, code]
       when 'interface'
         code = s.scan_until( /(?=\{)/ )
         code = _norm_whitespace( code )
         ## print "interface:"
         ## pp code
         tree << [:interface, code]
       else
         ## slurp chunk ,that is, until newline or comment or tracked keyword
         chunk = s.scan_until( END_OF_LINE_OR_COMMENT_OR_KEYWORD_OR_QUOTE ).rstrip
         ## puts "chunk: >#{chunk.inspect}<"
         tree << chunk
       end
    end
    break if s.eos?
   end

  tree
end

#outlineObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/solidity/parser.rb', line 179

def outline
   buf = String.new( '' )
   tree = _quick_pass_one

   tree.each do |node|
      if node.is_a?( Array )
         case node[0]
         when :contract           then  buf << node[1] << "\n"
         when :abstract_contract  then  buf << node[1] << "\n"
         when :interface          then  buf << node[1] << "\n"
         when :library            then  buf << node[1] << "\n"
         else
         end
      end
   end

   buf
end

#pragmasObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/solidity/parser.rb', line 198

def pragmas
   buf = String.new( '' )
   tree = _quick_pass_one

   tree.each do |node|
      if node.is_a?( Array )
         case node[0]
         when :pragma             then  buf << node[1] << "\n"
         end
      end
   end

   buf
end