aboutsummaryrefslogtreecommitdiff
blob: 1d5a6aff92d2996cd605b12866d9a26dc2d25f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! /usr/bin/python
#
# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
# Copyright(c) 2004, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2
#
# $Header: $

from tree import *
import parser

class BuildPropertiesParser(parser.Parser):

	def parse(self, ins):
		""" Parse an input stream containing an ant build.properties file. Return a 
		structured document represented by tree.Node

		@param ins - input stream
		@return tree.Node containing the structured representation
		"""

		lineno = 0
		continued_line = False
		inside_html_comment = False
		attrib = ""
		value = ""
		root = Node()

		for x in ins.readlines():
			lineno += 1
			x = x.strip()

			if inside_html_comment and x.find("-->") != -1:
				inside_html_comment = False
				x = x.split("-->", 1)[0]

			if x.find("<!--") != -1:
				inside_html_comment = True

			if inside_html_comment:
				continue

			if continued_line:
				continued_line = False
				value += x.strip("\"")

				if len(value) and value[-1] == "\\":
					value = value[:-1]
					continued_line = True
					continue

				root.add_kid(Node(attrib,value))
				continue

			if len(x) == 0 or x[:1] == "#":
				continue

			x = x.split("#", 1)[0]
			xs = x.split("=", 2)

			if len(xs) > 1:
				attrib = xs[0].strip()
				value = xs[1].strip().strip("\"")

				if value != "" and value[-1] == "\\":
					value = value[:-1]
					continued_line = True
					continue

				root.add_kid(Node(attrib,value))

			else:
				raise ParseError("Malformed line " + str(lineno))

		return root
		
	def output(self, ous, tree):
		tree.output(ous, "", " = ", "")

if __name__ == "__main__":
	print "This is not an executable module"