Class: Copper::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/copper/parser.rb

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



3
4
5
# File 'lib/copper/parser.rb', line 3

def initialize
	@parser = CopperParser.new
end

Instance Method Details

#cc_parserObject



32
33
34
# File 'lib/copper/parser.rb', line 32

def cc_parser
	@parser
end

#clean_tree(root_node) ⇒ Object



36
37
38
39
40
# File 'lib/copper/parser.rb', line 36

def clean_tree(root_node)
	return if(root_node.elements.nil?)
	root_node.elements.delete_if { |node| !node.is_a?(CopperNode) }
	root_node.elements.each { |node| self.clean_tree(node) }
end

#parse(content) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/copper/parser.rb', line 7

def parse(content)
	root_node = @parser.parse(content)

	# Raise any errors.
	unless root_node
		@parser.failure_reason =~ /^(Expected .+) after/m
		if $1.nil?
			puts "#{@parser.failure_reason}"
		else
			puts "#{$1.gsub("\n", '$NEWLINE')}:"
		end
		puts content.lines.to_a[@parser.failure_line - 1]
		puts "#{'~' * (@parser.failure_column - 1)}^"
		return nil
	end

	puts root_node.inspect if $parser_debug

	clean_tree(root_node)

	puts root_node.inspect if $parser_debug

	root_node
end