Class: MessagePack::IDL::ParsletParser

Inherits:
Parslet::Parser
  • Object
show all
Defined in:
lib/msgpack/idl/parser/rule.rb

Constant Summary collapse

LINE_HEAD_FORMAT =
" % 4d: "
LINE_HEAD_SIZE =
(LINE_HEAD_FORMAT % 0).size
AFTER_BUFFER =
200
AFTER_LINES =
3
BEFORE_BUFFER =
200
BEFORE_LINES =
4

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.keyword(string, name = "k_"+string) ⇒ Object



36
37
38
39
40
# File 'lib/msgpack/idl/parser/rule.rb', line 36

def keyword(string, name="k_"+string)
	rule(name.to_sym) {
		space? >> str(string) >> boundary
	}
end

.separator(char, name) ⇒ Object



42
43
44
45
46
# File 'lib/msgpack/idl/parser/rule.rb', line 42

def separator(char, name)
	rule(name.to_sym) {
		space? >> str(char)
	}
end

.sequence(name, separator, element, min = 0) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/msgpack/idl/parser/rule.rb', line 24

def sequence(name, separator, element, min=0)
	if min == 0
		eval %[rule(:#{name.to_s.dump}) {
			(#{element}.as(:sequence_x) >> (#{separator} >> #{element}.as(:sequence_xs)).repeat).maybe.as(:sequence)
		}]
	else
		eval %[rule(:#{name.to_s.dump}) {
			(#{element}.as(:sequence_x) >> (#{separator} >> #{element}.as(:sequence_xs)).repeat(#{min-1})).as(:sequence)
		}]
	end
end

Instance Method Details



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/msgpack/idl/parser/rule.rb', line 450

def print_error(error, fname, out=STDERR)
	error_tree = self.root.error_tree

	last = error_tree
	until last.children.empty?
		last = last.children.last
	end
	last_cause = last.parslet.instance_eval('@last_cause')  # FIXME
	source = last_cause.source

	row, col = source.line_and_column(last_cause.pos)

	old_pos = source.pos
	begin
		source.pos = last_cause.pos - col + 1
		line, *after = source.read(AFTER_BUFFER).to_s.split("\n")
		after = after[0,AFTER_LINES]

		source.pos = last_cause.pos - col - BEFORE_BUFFER
		before = source.read(BEFORE_BUFFER).to_s.split("\n")
		before = before[-BEFORE_LINES,BEFORE_LINES] || []
	ensure
		source.pos = old_pos
	end

	if m = /[ \t\r\n]*/.match(line)
		heading = m[0]
	else
		heading = ""
	end

	out.puts "syntax error:"
	(
		error.to_s.split("\n") +
		error_tree.to_s.split("\n")
	).each {|ln|
		out.puts "  "+ln
	}

	out.puts ""
	out.puts "around line #{row} column #{heading.size}-#{col}:"
	out.puts ""

	before.each_with_index {|ln,i|
		l = row - after.size - 1 + i
		out.print LINE_HEAD_FORMAT % l
		out.puts ln
	}

	out.print LINE_HEAD_FORMAT % row
	out.puts line
	out.print " "*LINE_HEAD_SIZE
	out.puts heading + '^'*(col - heading.size)

	after.each_with_index {|ln,i|
		l = row + 1 + i
		out.print LINE_HEAD_FORMAT % l
		out.puts ln
	}

	out
end