Permalink
Browse files

Fixed tests that would not fail the first go-around.

Broke up KPeg::CodeGenerator#output into smaller pieces.
  • Loading branch information...
drbrain committed Mar 8, 2012
1 parent 7c7159d commit f03687fecb7e80aa31d0a75fcf08175e8e126174
Showing with 134 additions and 38 deletions.
  1. +3 −2 History.txt
  2. +3 −0 README.rdoc
  3. +54 −34 lib/kpeg/code_generator.rb
  4. +1 −1 lib/kpeg/format_parser.rb
  5. +1 −1 lib/kpeg/string_escape.rb
  6. +72 −0 test/test_kpeg_code_generator.rb
View
@@ -1,17 +1,18 @@
-=== 1.1
+=== 0.10
* Minor enhancements
* In standalone parsers generation of a default initialize method may be
disabled with the custom_initialize variable:
%% custom_initialize = true
* Added a pre-class directive for adding class comments
+ * Generated code is now surrounded by startdoc/stopdoc.
* Bug fixes
* Hoe plugin now overwrites generated files
* Directives and variables now round-trip through KPeg::GrammarRenderer
-=== 1.0 / 2012-04-06
+=== 0.9 / 2012-04-06
* Minor enhancements
* Added arbitrary directives to the kpeg grammar
@@ -185,6 +185,9 @@ header::
Placed before any generated code
pre-class::
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
View
@@ -334,42 +334,28 @@ def output
code = []
- if header = @grammar.directives['header']
- code << header.action.strip
- code << "\n"
- end
+ output_header(code)
+ output_grammar(code)
+ output_footer(code)
- pre_class = @grammar.directives['pre-class']
+ @output = code.join
+ end
- if @standalone
- if pre_class
- code << pre_class.action.strip
- code << "\n"
- end
- code << "class #{@name}\n"
+ ##
+ # Output of class end and footer
- cp = standalone_region("compiled_parser.rb")
- cpi = standalone_region("compiled_parser.rb", "INITIALIZE")
- pp = standalone_region("position.rb")
+ def output_footer(code)
+ code << "end\n"
- 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"
+ if footer = @grammar.directives['footer']
+ code << footer.action
end
+ end
- @grammar.setup_actions.each do |act|
- code << "\n#{act.action}\n\n"
- end
+ ##
+ # Output of grammar and rules
+ def output_grammar(code)
code << " # :stopdoc:\n"
handle_ast(code)
@@ -436,14 +422,48 @@ def output
code << " Rules[:#{method_name name}] = rule_info(\"#{name}\", \"#{rend}\")\n"
end
- code << " # :stopdoc:\n"
- code << "end\n"
+ code << " # :startdoc:\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
- @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
def make(str)
@@ -3174,5 +3174,5 @@ def _ast_root
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_root] = rule_info("ast_root", "(ast_constant:c \"(\" ast_words:w \")\" { [c, w] } | ast_constant:c \"()\"? { [c, []] })")
- # :stopdoc:
+ # :startdoc:
end
@@ -603,5 +603,5 @@ def _embed
Rules[:_root] = rule_info("root", "segment*:s { @text = s.join }")
Rules[:_embed_seg] = rule_info("embed_seg", "(\"\#\" { \"\\\\\#\" } | segment)")
Rules[:_embed] = rule_info("embed", "embed_seg*:s { @text = s.join }")
- # :stopdoc:
+ # :startdoc:
end
Oops, something went wrong.

0 comments on commit f03687f

Please sign in to comment.