Class: Rattler::BackEnd::ParserGenerator::RuleSetGenerator
- Inherits:
-
Object
- Object
- Rattler::BackEnd::ParserGenerator::RuleSetGenerator
- Includes:
- Parsers
- Defined in:
- lib/rattler/back_end/parser_generator/rule_set_generator.rb
Constant Summary collapse
Instance Method Summary collapse
- #gen_grammar_def(grammar_name) ⇒ Object
- #gen_includes(includes) ⇒ Object
- #gen_module(grammar) ⇒ Object
- #gen_parser(grammar, opts = {}) ⇒ Object
- #gen_parser_def(parser_name, base_name) ⇒ Object
- #gen_requires(requires) ⇒ Object
- #gen_rules(rules, opts = {}) ⇒ Object
- #gen_start_rule(start_rule) ⇒ Object
- #generate(parser, opts = {}) ⇒ Object
-
#initialize(g) ⇒ RuleSetGenerator
constructor
A new instance of RuleSetGenerator.
- #module_block(decl) ⇒ Object
- #nest_modules(path) ⇒ Object
Methods included from Parsers
Constructor Details
#initialize(g) ⇒ RuleSetGenerator
Returns a new instance of RuleSetGenerator.
10 11 12 13 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 10 def initialize(g) @g = g @rule_generator = RuleGenerator.new(@g) end |
Instance Method Details
#gen_grammar_def(grammar_name) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 70 def gen_grammar_def(grammar_name) nest_modules(grammar_name.split('::')) do |name| (@g << "# @private").newline module_block("module #{name} #:nodoc:") { yield } end end |
#gen_includes(includes) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 46 def gen_includes(includes) unless includes.empty? includes.each {|_| (@g << "include #{_}").newline } @g.newline end end |
#gen_module(grammar) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 36 def gen_module(grammar) if grammar.grammar_name gen_grammar_def(grammar.grammar_name) { yield } elsif grammar.parser_name gen_parser_def(grammar.parser_name, grammar.base_name) { yield } else yield end end |
#gen_parser(grammar, opts = {}) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 23 def gen_parser(grammar, opts={}) gen_requires grammar.requires gen_module(grammar) do gen_includes grammar.includes gen_rules grammar.rules, opts end end |
#gen_parser_def(parser_name, base_name) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 77 def gen_parser_def(parser_name, base_name) nest_modules(parser_name.split('::')) do |name| (@g << "# @private").newline module_block("class #{name} < #{base_name} #:nodoc:") { yield } end end |
#gen_requires(requires) ⇒ Object
31 32 33 34 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 31 def gen_requires(requires) requires.each {|_| (@g << "require #{_}").newline } @g.newline end |
#gen_rules(rules, opts = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 53 def gen_rules(rules, opts={}) gen_start_rule rules.start_rule if rules.start_rule @g.intersperse(rules, :newlines => 2) do |rule| if opts[:standalone] @rule_generator.gen_rule_standalone rule, rules else @rule_generator.gen_rule_modular rule end end end |
#gen_start_rule(start_rule) ⇒ Object
64 65 66 67 68 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 64 def gen_start_rule(start_rule) (@g << "# @private").newline @g.block('def start_rule #:nodoc:') { @g << ":#{start_rule}"}.newline @g.newline end |
#generate(parser, opts = {}) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 15 def generate(parser, opts={}) case parser when Grammar then gen_parser parser, opts when RuleSet then gen_rules parser, opts end self end |
#module_block(decl) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 93 def module_block(decl) @g.block(decl) do @g.newline yield @g.newline end end |
#nest_modules(path) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/rattler/back_end/parser_generator/rule_set_generator.rb', line 84 def nest_modules(path) mod, *rest = path if rest.empty? yield mod else @g.block("module #{mod}") { nest_modules(rest) {|_| yield _ } } end end |