Class: Solidity::Parser
- Inherits:
-
Object
- Object
- Solidity::Parser
- Defined in:
- lib/solidity/parser.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 | $ | (?=['"]) | (?=\/(\/|\*)) | (?=\bpragma\b) | (?=\bcontract\b) | (?=\babstract\b) | (?=\blibrary\b) | (?=\binterface\b) /x
Class Method Summary collapse
Instance Method Summary collapse
- #_norm_whitespace(str) ⇒ Object
- #_quick_pass_one ⇒ Object
-
#initialize(txt) ⇒ Parser
constructor
A new instance of Parser.
- #outline ⇒ Object
- #pragmas ⇒ Object
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
52 53 54 55 56 57 |
# File 'lib/solidity/parser.rb', line 52 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_one ⇒ Object
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 |
# File 'lib/solidity/parser.rb', line 59 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 |
#outline ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/solidity/parser.rb', line 135 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 |
#pragmas ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/solidity/parser.rb', line 154 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 |