Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
$:.unshift File.dirname(__FILE__) + "/../lib" | ||
|
||
require 'pp' | ||
require "parslet" | ||
|
||
# Demonstrates modular parsers, split out over many classes. Please look at | ||
# ip_address.rb as well. | ||
|
||
module ALanguage | ||
include Parslet | ||
|
||
# Parslet rules are really a special kind of method. Mix them into your | ||
# classes! | ||
rule(:a_language) { str('aaa') } | ||
end | ||
|
||
# Parslet parsers are parslet atoms as well. Create an instance and chain them | ||
# to your other rules. | ||
# | ||
class BLanguage < Parslet::Parser | ||
root :blang | ||
|
||
rule(:blang) { str('bbb') } | ||
end | ||
|
||
# Parslet atoms are really Ruby values, pass them around. | ||
c_language = Parslet.str('ccc') | ||
|
||
class Language < Parslet::Parser | ||
def initialize(c_language) | ||
@c_language = c_language | ||
super() | ||
end | ||
|
||
root :root | ||
|
||
include ALanguage | ||
|
||
rule(:root) { str('a(') >> a_language >> str(')') >> space | | ||
str('b(') >> BLanguage.new >> str(')') >> space | | ||
str('c(') >> @c_language >> str(')') >> space } | ||
rule(:space) { str(' ').maybe } | ||
end | ||
|
||
Language.new(c_language).parse('a(aaa)') | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kschiess
Author
Owner
|
||
Language.new(c_language).parse('b(bbb)') | ||
Language.new(c_language).parse('c(ccc)') |
It might be a good idea to add the expected output as well, in a comment. Even though the idea is to run the example, with a comment I can also view the examples online and get what it is about.