Class: Solidity::Parser

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

Constant Summary collapse

SINGLE_QUOTE =

todo/fix - check if [^‘] includes/matches newline too (yes)? – add n for multi-line!

%r{'
  ( \\\\. | [^'] )*
'}x
DOUBLE_QUOTE =
%r{"
 ( \\\\. | [^"] )*
"}x
NAME =
/[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
  | $
  | (?=['"])
  | (?=\/(\/|\*))
  | (?=pragma\b)
  | (?=contract\b)
  | (?=abstract\b)
  | (?=library\b)
  | (?=interface\b)
/x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ Parser

Returns a new instance of Parser.



21
22
23
# File 'lib/solidity.rb', line 21

def initialize( txt )
  @txt = txt
end

Class Method Details

.read(path) ⇒ Object



14
15
16
17
# File 'lib/solidity.rb', line 14

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

Instance Method Details

#_norm_whitespace(str) ⇒ Object



61
62
63
64
65
66
# File 'lib/solidity.rb', line 61

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



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

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/solidity.rb', line 144

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/solidity.rb', line 163

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