Skip to content

Commit

Permalink
Fixed tests that would not fail the first go-around.
Browse files Browse the repository at this point in the history
Broke up KPeg::CodeGenerator#output into smaller pieces.
  • Loading branch information
drbrain committed Mar 8, 2012
1 parent 7c7159d commit f03687f
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 38 deletions.
5 changes: 3 additions & 2 deletions History.txt
@@ -1,17 +1,18 @@
=== 1.1 === 0.10


* Minor enhancements * Minor enhancements
* In standalone parsers generation of a default initialize method may be * In standalone parsers generation of a default initialize method may be
disabled with the custom_initialize variable: disabled with the custom_initialize variable:


%% custom_initialize = true %% custom_initialize = true
* Added a pre-class directive for adding class comments * Added a pre-class directive for adding class comments
* Generated code is now surrounded by startdoc/stopdoc.


* Bug fixes * Bug fixes
* Hoe plugin now overwrites generated files * Hoe plugin now overwrites generated files
* Directives and variables now round-trip through KPeg::GrammarRenderer * Directives and variables now round-trip through KPeg::GrammarRenderer


=== 1.0 / 2012-04-06 === 0.9 / 2012-04-06


* Minor enhancements * Minor enhancements
* Added arbitrary directives to the kpeg grammar * Added arbitrary directives to the kpeg grammar
Expand Down
3 changes: 3 additions & 0 deletions README.rdoc
Expand Up @@ -185,6 +185,9 @@ header::
Placed before any generated code Placed before any generated code
pre-class:: pre-class::
Placed before the class definition to provide a class comment Placed before the class definition to provide a class comment
footer::
Placed after the end of the class (for requiring files dependent upon the
parser's namespace


== Generating and running your parser == Generating and running your parser


Expand Down
88 changes: 54 additions & 34 deletions lib/kpeg/code_generator.rb
Expand Up @@ -334,42 +334,28 @@ def output


code = [] code = []


if header = @grammar.directives['header'] output_header(code)
code << header.action.strip output_grammar(code)
code << "\n" output_footer(code)
end


pre_class = @grammar.directives['pre-class'] @output = code.join
end


if @standalone ##
if pre_class # Output of class end and footer
code << pre_class.action.strip
code << "\n"
end
code << "class #{@name}\n"


cp = standalone_region("compiled_parser.rb") def output_footer(code)
cpi = standalone_region("compiled_parser.rb", "INITIALIZE") code << "end\n"
pp = standalone_region("position.rb")


cp.gsub!(/include Position/, pp) if footer = @grammar.directives['footer']
code << " # :stopdoc:\n" code << footer.action
code << cpi << "\n" unless @grammar.variables['custom_initialize']
code << cp << "\n"
code << " # :startdoc:\n"
else
code << "require 'kpeg/compiled_parser'\n\n"
if pre_class
code << pre_class.action.strip
code << "\n"
end
code << "class #{@name} < KPeg::CompiledParser\n"
end end
end


@grammar.setup_actions.each do |act| ##
code << "\n#{act.action}\n\n" # Output of grammar and rules
end


def output_grammar(code)
code << " # :stopdoc:\n" code << " # :stopdoc:\n"
handle_ast(code) handle_ast(code)


Expand Down Expand Up @@ -436,14 +422,48 @@ def output
code << " Rules[:#{method_name name}] = rule_info(\"#{name}\", \"#{rend}\")\n" code << " Rules[:#{method_name name}] = rule_info(\"#{name}\", \"#{rend}\")\n"
end end


code << " # :stopdoc:\n" code << " # :startdoc:\n"
code << "end\n" end


if footer = @grammar.directives['footer'] ##
code << footer.action # Output up to the user-defined setup actions

def output_header(code)
if header = @grammar.directives['header']
code << header.action.strip
code << "\n"
end end


@output = code.join pre_class = @grammar.directives['pre-class']

if @standalone
if pre_class
code << pre_class.action.strip
code << "\n"
end
code << "class #{@name}\n"

cp = standalone_region("compiled_parser.rb")
cpi = standalone_region("compiled_parser.rb", "INITIALIZE")
pp = standalone_region("position.rb")

cp.gsub!(/include Position/, pp)
code << " # :stopdoc:\n"
code << cpi << "\n" unless @grammar.variables['custom_initialize']
code << cp << "\n"
code << " # :startdoc:\n"
else
code << "require 'kpeg/compiled_parser'\n\n"
if pre_class
code << pre_class.action.strip
code << "\n"
end
code << "class #{@name} < KPeg::CompiledParser\n"
end

@grammar.setup_actions.each do |act|
code << "\n#{act.action}\n\n"
end
end end


def make(str) def make(str)
Expand Down
2 changes: 1 addition & 1 deletion lib/kpeg/format_parser.rb
Expand Up @@ -3174,5 +3174,5 @@ def _ast_root
Rules[:_ast_sp] = rule_info("ast_sp", "(\" \" | \"\\t\")*") Rules[:_ast_sp] = rule_info("ast_sp", "(\" \" | \"\\t\")*")
Rules[:_ast_words] = rule_info("ast_words", "(ast_words:r ast_sp \",\" ast_sp ast_word:w { r + [w] } | ast_word:w { [w] })") Rules[:_ast_words] = rule_info("ast_words", "(ast_words:r ast_sp \",\" ast_sp ast_word:w { r + [w] } | ast_word:w { [w] })")
Rules[:_ast_root] = rule_info("ast_root", "(ast_constant:c \"(\" ast_words:w \")\" { [c, w] } | ast_constant:c \"()\"? { [c, []] })") Rules[:_ast_root] = rule_info("ast_root", "(ast_constant:c \"(\" ast_words:w \")\" { [c, w] } | ast_constant:c \"()\"? { [c, []] })")
# :stopdoc: # :startdoc:
end end
2 changes: 1 addition & 1 deletion lib/kpeg/string_escape.rb
Expand Up @@ -603,5 +603,5 @@ def _embed
Rules[:_root] = rule_info("root", "segment*:s { @text = s.join }") Rules[:_root] = rule_info("root", "segment*:s { @text = s.join }")
Rules[:_embed_seg] = rule_info("embed_seg", "(\"\#\" { \"\\\\\#\" } | segment)") Rules[:_embed_seg] = rule_info("embed_seg", "(\"\#\" { \"\\\\\#\" } | segment)")
Rules[:_embed] = rule_info("embed", "embed_seg*:s { @text = s.join }") Rules[:_embed] = rule_info("embed", "embed_seg*:s { @text = s.join }")
# :stopdoc: # :startdoc:
end end

0 comments on commit f03687f

Please sign in to comment.