サクサク読めて、アプリ限定の機能も多数!
トップへ戻る
衆院選
kschiess.github.io
Parslet parsers output deep nested hashes. Those are nice for printing, but hard to work with. The structure of the nested hashes is determined by the grammar and can thus vary largely. Testing for the presence of individual keys would produce code that is hard to read and maintain. This is why parslet also comes with a hash transformation engine. To construct such a transform, you have to derive
Let’s develop a small language that allows for simple computation together. Here’s a valid input file for that language: puts(1 + 2) puts(4 + 2) To install the parslet library, please do a gem install parslet Now let’s write the first part of our parser. For now, we’ll just recognize simple numbers like ‘1’ or ‘42’. require 'parslet' class Mini < Parslet::Parser rule(:integer) { match('[0-9]').rep
require 'parslet' include Parslet # Constructs a parser using a Parser Expression Grammar parser = str('"') >> ( str('\\').ignore >> any | str('"').absent? >> any ).repeat.as(:string) >> str('"') result = parser.parse %Q("this is a valid \\"string\\"") result # => {:string=>"this is a valid \"string\""@1} A small Ruby library for constructing parsers in the PEG (Parsing Expression Grammar) fashion
このページを最初にブックマークしてみませんか?
『kschiess.github.io』の新着エントリーを見る
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く