Skip to content
Browse files

KPeg::CodeGenerator#standalone_region now produces cleaner output

  • Loading branch information...
1 parent 5ac8167 commit 92ca439f8aead5c5103f25b606e795ff1fc7f5e6 @drbrain drbrain committed Mar 7, 2012
Showing with 40 additions and 13 deletions.
  1. +2 −0 Rakefile
  2. +8 −3 lib/kpeg/code_generator.rb
  3. +3 −4 lib/kpeg/format_parser.rb
  4. +1 −0 lib/kpeg/position.rb
  5. +4 −6 lib/kpeg/string_escape.rb
  6. +22 −0 test/test_kpeg_code_generator.rb
View
2 Rakefile
@@ -35,6 +35,8 @@ PARSER_FILES = %w[
PARSER_FILES.map do |parser_file|
file parser_file => 'lib/kpeg/compiled_parser.rb'
+ file parser_file => 'lib/kpeg/code_generator.rb'
+ file parser_file => 'lib/kpeg/position.rb'
end
desc "build the parser"
View
11 lib/kpeg/code_generator.rb
@@ -314,11 +314,16 @@ def output_op(code, op, indent=2)
def standalone_region(path, marker = "STANDALONE")
expanded_path = File.expand_path("../#{path}", __FILE__)
cp = File.read(expanded_path)
- start = cp.index("# #{marker} START")
- fin = cp.index("# #{marker} END")
+
+ start_marker = "# #{marker} START"
+ end_marker = /^\s*# #{Regexp.escape marker} END/
+
+ start = cp.index(start_marker) + start_marker.length + 1 # \n
+ fin = cp.index(end_marker)
unless start and fin
- abort "#{marker} boundaries in #{path} missing for standalone generation"
+ abort("#{marker} boundaries in #{path} missing " \
+ "for standalone generation")
end
cp[start..fin]
View
7 lib/kpeg/format_parser.rb
@@ -1,5 +1,4 @@
class KPeg::FormatParser
-# STANDALONE START
# Prepares for parsing +str+. If you define a custom initialize you must
# call this method before #parse
@@ -18,7 +17,7 @@ def setup_parser(str, debug=false)
attr_reader :failing_rule_offset
attr_accessor :result, :pos
- # STANDALONE START
+
def current_column(target=pos)
if c = string.rindex("\n", target-1)
return target - c - 1
@@ -46,7 +45,7 @@ def lines
lines
end
- #
+
def get_text(start)
@string[start..@pos-1]
@@ -340,7 +339,7 @@ def self.rule_info(name, rendered)
RuleInfo.new(name, rendered)
end
- #
+
require 'kpeg/grammar'
View
1 lib/kpeg/position.rb
@@ -1,6 +1,7 @@
module KPeg
module Position
# STANDALONE START
+
def current_column(target=pos)
if c = string.rindex("\n", target-1)
return target - c - 1
View
10 lib/kpeg/string_escape.rb
@@ -1,5 +1,4 @@
class KPeg::StringEscape
-# INITIALIZE START
# This is distinct from setup_parser so that a standalone parser
# can redefine #initialize and still have access to the proper
@@ -8,8 +7,7 @@ def initialize(str, debug=false)
setup_parser(str, debug)
end
- #
-# STANDALONE START
+
# Prepares for parsing +str+. If you define a custom initialize you must
# call this method before #parse
@@ -28,7 +26,7 @@ def setup_parser(str, debug=false)
attr_reader :failing_rule_offset
attr_accessor :result, :pos
- # STANDALONE START
+
def current_column(target=pos)
if c = string.rindex("\n", target-1)
return target - c - 1
@@ -56,7 +54,7 @@ def lines
lines
end
- #
+
def get_text(start)
@string[start..@pos-1]
@@ -350,7 +348,7 @@ def self.rule_info(name, rendered)
RuleInfo.new(name, rendered)
end
- #
+
attr_reader :text
View
22 test/test_kpeg_code_generator.rb
@@ -1351,6 +1351,28 @@ def _root
assert_equal [0,5], code.result
end
+ def test_standalone_region
+ gram = KPeg.grammar do |g|
+ g.root = g.dot
+ end
+
+ cg = KPeg::CodeGenerator.new "Test", gram
+
+ expected = <<-EXPECTED
+
+ # This is distinct from setup_parser so that a standalone parser
+ # can redefine #initialize and still have access to the proper
+ # parser setup code.
+ def initialize(str, debug=false)
+ setup_parser(str, debug)
+ end
+
+ EXPECTED
+
+ assert_equal expected,
+ cg.standalone_region('compiled_parser.rb', 'INITIALIZE')
+ end
+
def test_parse_error
gram = KPeg.grammar do |g|
g.world = "world"

0 comments on commit 92ca439

Please sign in to comment.
Something went wrong with that request. Please try again.