Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Broke up KPeg::CodeGenerator#output into smaller pieces.
  • Loading branch information...
commit f03687fecb7e80aa31d0a75fcf08175e8e126174 1 parent 7c7159d
@drbrain drbrain authored
View
5 History.txt
@@ -1,4 +1,4 @@
-=== 1.1
+=== 0.10
* Minor enhancements
* In standalone parsers generation of a default initialize method may be
@@ -6,12 +6,13 @@
%% 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
View
3  README.rdoc
@@ -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
88 lib/kpeg/code_generator.rb
@@ -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)
View
2  lib/kpeg/format_parser.rb
@@ -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
View
2  lib/kpeg/string_escape.rb
@@ -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
View
72 test/test_kpeg_code_generator.rb
@@ -14,6 +14,7 @@ def test_dot
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = .
def _root
@@ -24,6 +25,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", ".")
+ # :startdoc:
end
STR
@@ -43,6 +45,7 @@ def test_str
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = "hello"
def _root
@@ -53,6 +56,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "\\\"hello\\\"")
+ # :startdoc:
end
STR
@@ -72,6 +76,7 @@ def test_reg
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = /[0-9]/
def _root
@@ -82,6 +87,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "/[0-9]/")
+ # :startdoc:
end
STR
@@ -104,6 +110,7 @@ def test_reg_unicode
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = /./
def _root
@@ -114,6 +121,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "/./")
+ # :startdoc:
end
STR
else
@@ -121,6 +129,7 @@ def _root
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = /./u
def _root
@@ -131,6 +140,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "/./u")
+ # :startdoc:
end
STR
end
@@ -151,6 +161,7 @@ def test_char_range
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = [a-z]
def _root
@@ -168,6 +179,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "[a-z]")
+ # :startdoc:
end
STR
@@ -189,6 +201,7 @@ def test_char_range_in_seq
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = [a-z] "hello"
def _root
@@ -220,6 +233,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "[a-z] \\\"hello\\\"")
+ # :startdoc:
end
STR
@@ -242,6 +256,7 @@ def test_any
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = ("hello" | "world")
def _root
@@ -263,6 +278,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "(\\\"hello\\\" | \\\"world\\\")")
+ # :startdoc:
end
STR
@@ -298,6 +314,7 @@ def test_maybe
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = "hello"?
def _root
@@ -313,6 +330,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "\\\"hello\\\"?")
+ # :startdoc:
end
STR
@@ -347,6 +365,7 @@ def test_kleene
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = "hello"*
def _root
@@ -361,6 +380,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "\\\"hello\\\"*")
+ # :startdoc:
end
STR
@@ -398,6 +418,7 @@ def test_many
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = "hello"+
def _root
@@ -418,6 +439,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "\\\"hello\\\"+")
+ # :startdoc:
end
STR
@@ -462,6 +484,7 @@ def test_multiple
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = "hello"[5, 9]
def _root
@@ -488,6 +511,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "\\\"hello\\\"[5, 9]")
+ # :startdoc:
end
STR
@@ -505,6 +529,7 @@ def test_seq
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = "hello" "world"
def _root
@@ -529,6 +554,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "\\\"hello\\\" \\\"world\\\"")
+ # :startdoc:
end
STR
@@ -561,6 +587,7 @@ def test_andp
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = &"hello"
def _root
@@ -573,6 +600,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "&\\\"hello\\\"")
+ # :startdoc:
end
STR
@@ -598,6 +626,7 @@ def test_andp_for_action
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = &{ !defined? @fail }
def _root
@@ -610,6 +639,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "&{ !defined? @fail }")
+ # :startdoc:
end
STR
@@ -636,6 +666,7 @@ def test_notp
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = !"hello"
def _root
@@ -649,6 +680,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "!\\\"hello\\\"")
+ # :startdoc:
end
STR
@@ -674,6 +706,7 @@ def test_notp_for_action
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = !{ defined? @fail }
def _root
@@ -687,6 +720,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "!{ defined? @fail }")
+ # :startdoc:
end
STR
@@ -715,6 +749,7 @@ def test_ref
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# greeting = "hello"
def _greeting
@@ -733,6 +768,7 @@ def _root
Rules = {}
Rules[:_greeting] = rule_info("greeting", "\\\"hello\\\"")
Rules[:_root] = rule_info("root", "greeting")
+ # :startdoc:
end
STR
@@ -753,6 +789,7 @@ def test_invoke
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# greeting = "hello"
def _greeting
@@ -771,6 +808,7 @@ def _root
Rules = {}
Rules[:_greeting] = rule_info("greeting", "\\\"hello\\\"")
Rules[:_root] = rule_info("root", "@greeting")
+ # :startdoc:
end
STR
@@ -791,6 +829,7 @@ def test_invoke_with_args
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# greeting = "hello"
def _greeting(a,b)
@@ -809,6 +848,7 @@ def _root
Rules = {}
Rules[:_greeting] = rule_info("greeting", "\\\"hello\\\"")
Rules[:_root] = rule_info("root", "@greeting(1,2)")
+ # :startdoc:
end
STR
@@ -836,6 +876,7 @@ def test_foreign_invoke
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
def setup_foreign_grammar
@_grammar_blah = TestKPegCodeGenerator::TestParser.new(nil)
end
@@ -849,6 +890,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "%blah.greeting")
+ # :startdoc:
end
STR
@@ -869,6 +911,7 @@ def test_foreign_invoke_with_args
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
def setup_foreign_grammar
@_grammar_blah = TestKPegCodeGenerator::TestParser.new(nil)
end
@@ -882,6 +925,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "%blah.greeting2(1,2)")
+ # :startdoc:
end
STR
@@ -901,6 +945,7 @@ def test_tag
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = "hello":t
def _root
@@ -912,6 +957,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "\\\"hello\\\":t")
+ # :startdoc:
end
STR
@@ -929,6 +975,7 @@ def test_noname_tag
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = "hello"
def _root
@@ -939,6 +986,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "\\\"hello\\\"")
+ # :startdoc:
end
STR
@@ -957,6 +1005,7 @@ def test_tag_maybe
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# hello = < "hello" > {text}
def _hello
@@ -1016,6 +1065,7 @@ def _root
Rules = {}
Rules[:_hello] = rule_info("hello", "< \\\"hello\\\" > {text}")
Rules[:_root] = rule_info("root", "hello?:lots {lots}")
+ # :startdoc:
end
STR
@@ -1043,6 +1093,7 @@ def test_tag_multiple
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# hello = < "hello" > {text}
def _hello
@@ -1103,6 +1154,7 @@ def _root
Rules = {}
Rules[:_hello] = rule_info("hello", "< \\\"hello\\\" > {text}")
Rules[:_root] = rule_info("root", "hello*:lots {lots}")
+ # :startdoc:
end
STR
@@ -1133,6 +1185,7 @@ def test_tag_many
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# hello = < "hello" > {text}
def _hello
@@ -1200,6 +1253,7 @@ def _root
Rules = {}
Rules[:_hello] = rule_info("hello", "< \\\"hello\\\" > {text}")
Rules[:_root] = rule_info("root", "hello+:lots {lots}")
+ # :startdoc:
end
STR
@@ -1228,6 +1282,7 @@ def test_action
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = {3 + 4}
def _root
@@ -1239,6 +1294,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "{3 + 4}")
+ # :startdoc:
end
STR
@@ -1260,6 +1316,7 @@ def test_collect
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = < "hello" > { text }
def _root
@@ -1289,6 +1346,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "< \\\"hello\\\" > { text }")
+ # :startdoc:
end
STR
@@ -1310,6 +1368,7 @@ def test_bounds
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = @< "hello" > { bounds }
def _root
@@ -1339,6 +1398,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", "@< \\\"hello\\\" > { bounds }")
+ # :startdoc:
end
STR
@@ -1402,6 +1462,7 @@ def test_directive_footer
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = .
def _root
@@ -1412,6 +1473,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", ".")
+ # :startdoc:
end
# require 'some/subclass'
@@ -1435,6 +1497,7 @@ def test_directive_header
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = .
def _root
@@ -1445,6 +1508,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", ".")
+ # :startdoc:
end
STR
@@ -1466,6 +1530,7 @@ def test_directive_pre_class
# some comment
class Test < KPeg::CompiledParser
+ # :stopdoc:
# root = .
def _root
@@ -1476,6 +1541,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", ".")
+ # :startdoc:
end
STR
@@ -1511,6 +1577,7 @@ class Test < KPeg::CompiledParser
attr_reader :foo
+ # :stopdoc:
# root = .
def _root
@@ -1521,6 +1588,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", ".")
+ # :startdoc:
end
STR
@@ -1572,6 +1640,7 @@ def test_ast_generation
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
module AST
class Node; end
@@ -1611,6 +1680,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", ".")
+ # :startdoc:
end
STR
@@ -1632,6 +1702,7 @@ def test_ast_generation_in_different_location
require 'kpeg/compiled_parser'
class Test < KPeg::CompiledParser
+ # :stopdoc:
module MegaAST
class Node; end
@@ -1657,6 +1728,7 @@ def _root
Rules = {}
Rules[:_root] = rule_info("root", ".")
+ # :startdoc:
end
STR
Please sign in to comment.
Something went wrong with that request. Please try again.