Skip to content

Instantly share code, notes, and snippets.

@yshl

yshl/aaa_parser.rb

Last active May 15, 2016
Embed
What would you like to do?
パーサーの練習
#!/usr/bin/ruby
require 'parslet'
class AAAParser < Parslet::Parser
root :all
rule(:all){
# 全体は line の 0 回以上の繰り返しの後に
line.repeat >>
# statement があるかもしれない
statement.maybe
}
rule(:line){
# line は statement の後に newline を付けたもの
statement >> newline
}
rule(:statement){
# statement は文字 a の 0 回以上の繰り返し
str('a').repeat
}
rule(:newline){
# newline は文字 "\n"
str("\n")
}
end
ap=AAAParser.new
p ap.parse("aaaaa\naaa\n") # OK
begin
ap.parse("aabaa\n") # エラー
rescue Parslet::ParseFailed => e
puts e.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment