Class: Rattler::Compiler::ParserGenerator::GrammarGenerator
Instance Method Summary
collapse
#gen_rules, #gen_start_rule, #initialize
Instance Method Details
#gen_cli(cli, module_name) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 68
def gen_cli(cli, module_name)
@g.newline.newline.block('if __FILE__ == $0') do
%w{rubygems rattler}.each {|_| (@g << "require '#{_}'").newline }
@g << "Rattler::Util::#{cli}.run(#{module_name})"
end
end
|
#gen_encoding ⇒ Object
24
25
26
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 24
def gen_encoding
(@g << "# encoding: utf-8").newline
end
|
#gen_grammar_def(grammar_name) ⇒ Object
50
51
52
53
54
55
56
57
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 50
def gen_grammar_def(grammar_name)
@g.newline
nest_modules(grammar_name.split('::')) do |name|
(@g << "# @private").newline
module_block("module #{name} #:nodoc:") { yield }
end
gen_cli(:GrammarCLI, grammar_name)
end
|
#gen_includes(includes) ⇒ Object
43
44
45
46
47
48
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 43
def gen_includes(includes)
unless includes.empty?
includes.each {|_| (@g << "include #{_}").newline }
@g.newline
end
end
|
#gen_module(grammar) ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 33
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
15
16
17
18
19
20
21
22
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 15
def gen_parser(grammar, opts={})
gen_encoding
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
59
60
61
62
63
64
65
66
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 59
def gen_parser_def(parser_name, base_name)
@g.newline
nest_modules(parser_name.split('::')) do |name|
(@g << "# @private").newline
module_block("class #{name} < #{base_name} #:nodoc:") { yield }
end
gen_cli(:ParserCLI, parser_name)
end
|
#gen_requires(requires) ⇒ Object
28
29
30
31
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 28
def gen_requires(requires)
requires.each {|_| @g.newline << "require #{_}" }
@g.newline unless requires.empty?
end
|
#generate(parser, opts = {}) ⇒ Object
7
8
9
10
11
12
13
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 7
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
84
85
86
87
88
89
90
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 84
def module_block(decl)
@g.block(decl) do
@g.newline
yield
@g.newline
end
end
|
#nest_modules(path) ⇒ Object
75
76
77
78
79
80
81
82
|
# File 'lib/rattler/compiler/parser_generator/grammar_generator.rb', line 75
def nest_modules(path)
mod, *rest = path
if rest.empty?
yield mod
else
@g.block("module #{mod}") { nest_modules(rest) {|_| yield _ } }
end
end
|