PageRenderTime 14ms CodeModel.GetById 11ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/test/parser/test_parser.e

http://github.com/tybor/Liberty
Specman e | 156 lines | 136 code | 15 blank | 5 comment | 4 complexity | 086f651094bf6067ad0da58f5ccd2054 MD5 | raw file
  1class TEST_PARSER
  2
  3insert
  4	EIFFELTEST_TOOLS
  5
  6create {}
  7	make
  8
  9feature {}
 10	make is
 11		do
 12			--parse_code(create {REGULAR_FILE}.make("../../src/lib/kernel/tuple.e"))
 13			--die_with_code(0)
 14			parse_all(create {DIRECTORY}.scan("../../src"))
 15			parse_all(create {DIRECTORY}.scan("."))
 16		end
 17
 18	parse_all (a_directory: DIRECTORY) is
 19		require
 20			a_directory /= Void
 21		local
 22			dir: DIRECTORY; file: FILE; regular: REGULAR_FILE
 23			i: INTEGER
 24		do
 25			label_assert(a_directory.path.out, a_directory.exists)
 26			from
 27				i := a_directory.lower
 28			until
 29				i > a_directory.upper
 30			loop
 31				file := a_directory.file_at(i)
 32				if file.is_regular and then file.name.has_suffix(once ".e") then
 33					regular ::= file
 34					parse_code(regular)
 35				else
 36					inspect
 37						file.name.out
 38					when "eiffeltest", ".svn", "CVS", ".git", ".", ".." then
 39						-- ignored
 40					when "broken", "wrappers" then
 41						-- ignored
 42					else
 43						if file.is_directory then
 44							dir ::= file
 45							parse_all(dir)
 46						else
 47							-- ignored
 48						end
 49					end
 50				end
 51				i := i + 1
 52			end
 53		end
 54
 55	read_code (file: REGULAR_FILE) is
 56		local
 57			in: INPUT_STREAM
 58		do
 59			code.clear_count
 60			in := file.read
 61			from
 62				in.read_line
 63			until
 64				in.end_of_input
 65			loop
 66				code.append(in.last_string)
 67				code.extend('%N')
 68				in.read_line
 69			end
 70			code.append(in.last_string)
 71			in.disconnect
 72		end
 73
 74	generate_not_connected (filename: STRING): STRING is
 75		do
 76			Result := "Could not connect to " + filename
 77		end
 78
 79	parse_code (file: REGULAR_FILE) is
 80		do
 81			std_output.put_line(once "Parsing " + file.path)
 82			read_code(file)
 83			buffer.initialize_with(code)
 84			eiffel.reset
 85			if file.name.out.is_equal(once "tuple.e") then
 86				parser.eval(buffer, eiffel.table, once "Classes")
 87			else
 88				parser.eval(buffer, eiffel.table, once "Class")
 89			end
 90			message_assert(agent generate_syntax_error(file), parser.error = Void)
 91			generated.clear
 92			eiffel.generate(generated)
 93			message_assert(agent generate_generated_error(file), code.is_equal(generated.to_string))
 94		end
 95
 96	generate_generated_error (file: REGULAR_FILE): STRING is
 97		local
 98			tfw: TEXT_FILE_WRITE
 99		do
100			create tfw.connect_to(file.name.out)
101			tfw.put_string(code)
102			tfw.disconnect
103			tfw.connect_to(file.name.out + ".gen")
104			tfw.put_string(generated.to_string)
105			tfw.disconnect
106			Result := "Bad generation from " + file.path.out + "%N> diff " + file.name.out + " " + file.name.out + ".gen%N"
107		end
108
109	generate_syntax_error (file: REGULAR_FILE): STRING is
110		local
111			line, column, index: INTEGER
112		do
113			from
114				index := 1
115				line := 1
116				column := 1
117			until
118				index = parser.error.index
119			loop
120				if code.item(index) = '%N' then
121					line := line + 1
122					column := 1
123				else
124					column := column + 1
125				end
126				index := index + 1
127			end
128			Result := "Syntax error in " + file.path + ", line " + line.out + ", column " + column.out + ":%N" + parser.error.message
129		end
130
131	code: STRING is
132		once
133			create Result.make(0)
134		end
135
136	generated: STRING_OUTPUT_STREAM is
137		once
138			create Result.make
139		end
140
141	buffer: MINI_PARSER_BUFFER is
142		once
143			create Result
144		end
145
146	parser: DESCENDING_PARSER is
147		once
148			create Result.make
149		end
150
151	eiffel: EIFFEL_GRAMMAR is
152		once
153			create Result.make(create {LIBERTY_NODE_FACTORY}.make)
154		end
155
156end