summaryrefslogtreecommitdiff
path: root/test/tags3.lm
blob: 75ac2ab338076152279040657e68f00182bc0c61 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
context tags
{
	#
	# Regular Definitions
	#
	rl def_name_char /[\-A-Za-z0-9._:?]/
	rl def_name /[A-Za-z_:] def_name_char*/
	rl def_system_literal /'"' [^"]* '"' | "'" [^']* "'"/

	#
	# Scanner for tag names.
	#
	lex
		ignore /space+/
		token tag_id /def_name/
	end

	#
	# Scanner for attributes names
	#
	lex
		ignore  /space+/
		token attr_name /def_name_char+/
		literal '='
	end

	# Scanner for attribute values.
	lex
		ignore  /space+/
		token dquote_val /'"' ([^"] | '\\' any)* '"'/
		token squote_val /"'" ([^'] | '\\' any)* "'"/
		token unq_val /[^ \t\r\n<>"'] [^ \t\r\n<>]*/
	end

	literal '>', '/>'

	#
	# Tokens
	#

	lex
		ignore /space+/
		literal '<', '</', '<!DOCTYPE'
		token doc_data /[^<]+/
		token comment /"<!--" any* :>> "-->"/
	end

	#
	# Tags
	#

	# This scanner is just for the id in close tags. The id needs to be looked up
	# in the tag stack so we can determine if it is a stray. 
	lex
		# Ignore whitespace.
		ignore /space+/

		token stray_close_id //
		token close_id /def_name/
		{
			# If it is in the tag stack then it is a close_id. If not then it's a
			# stray_close_id.
			send_id: int = typeid<stray_close_id>

			LocalTagStack: tag_stack = TagStack
			for Tag: tag_id in LocalTagStack {
				T: tag_id = Tag
				if match_text == T.data {
					send_id = typeid<close_id>
					break
				}
			}

			input.push( make_token( send_id input.pull(match_length) ) )
		}
	end

	#
	# Tag Stack
	#

	def tag_stack 
		[tag_id tag_stack]
	|	[]

	TagStack: tag_stack

	#
	# Document Type
	#
	# This scanner handles inside DOCTYPE tags (except keywords).
	lex 
		ignore /space+/
		token dt_name /def_name/
		token dt_literal /def_system_literal/
		token dt_bl /"[" [^\]]* "]"/
	end

	token dt_close /'>'/

	# Using a separate scanner for the keywords in DOCTYPE prevents them from
	# covering dt_name
	lex 
		ignore /space+/
		literal 'SYSTEM', 'PUBLIC'
	end

	def DOCTYPE ['<!DOCTYPE' dt_name external_id dt_bl? dt_close]

	def external_id
		['SYSTEM' dt_literal?]
	|	['PUBLIC' dt_literal dt_literal?]

	#
	# Tags, with optionanal close.
	#

	def tag 
		[open_tag item* opt_close_tag]

	def open_tag 
		['<' tag_id attr* '>']
		{
			TagStack = construct tag_stack 
				[r2 TagStack]
		}

	def opt_close_tag
		['</' close_id '>']
		{
			match TagStack [Top:tag_id Rest:tag_stack] 
			if r2.data == Top.data
				TagStack = Rest
			else
				reject
		}

	|	[]
		{
			match TagStack [Top:tag_id Rest:tag_stack] 
			TagStack = Rest
		}

	#
	# Empty tags
	#
	def empty_tag 
		['<' tag_id attr* '/>']

	#
	# Stray close tags
	#
	def stray_close 
		['</' stray_close_id '>']


	#
	# Attributes
	#

	def attr
		[attr_name eql_attr_val?]

	def eql_attr_val ['=' attr_val]

	def attr_val
		[squote_val]
	|	[dquote_val]
	|	[unq_val]
	|	[]

	#
	# Items
	#

	def item 
		[DOCTYPE]
	|	[tag]
	|	[empty_tag]
	|	[stray_close]
	|	[doc_data]
	|	[comment]


	token trailing /any*/

	def start 
		[item* trailing]

	#
	# END GRAMMAR
	#

	int addDefaultAltTags( Start: ref start )
	{
		for T: open_tag in Start {
			require T 
				["<img" AttrList: attr* '>']
			
			haveAlt: bool = false
			for A: attr in T {
				if match A ["alt=" attr_val]
					haveAlt = true
			}

			if !haveAlt {
				for AL: attr* in T {
					if match AL [] {
						AL = construct attr* 
							[" alt=\"default alt\""]
						break
					}
				}
			}
		}
	}

	int printLinks( Start: start )
	{
		for A:tag in Start {
			require A
				["<a" AttrList: attr* ">" I: item* "</a>"]

			for Attr: attr in AttrList {
				if match Attr ["href = " AttrVal: attr_val]
					print( 'link: ' I '\ntarget: ' AttrVal '\n\n' )
			}
		}
	}


	bool should_close( TI: tag_id )
	{
		return true
	}

	bool should_flatten( TI: tag_id )
	{
		return true
	}

	# Finds unclosed tags and puts the content after the tag. Afterwards
	# all unclosed tags will be empty 'inside'.
	int flatten( Start: ref start )
	{
		for TL: item* in Start {
			require TL
				[OT: open_tag Inside: item* Trailing: item*]

			match OT 
				['<' TagId: tag_id attr* '>']

			if should_flatten( TagId )
			{
				require Inside
					[item item*]
				
				# Put Trailing at the end of inside.
				for END: item* in Inside {
					if match END [] {
						END = Trailing
						break
					}
				}

				EmptyCloseTag: opt_close_tag = 
					construct opt_close_tag []

				# Close the tag and put inside after it.
				TL = construct item*
					[OT EmptyCloseTag Inside]
			}
		}
	}

#	int close( Start: ref start )
#	{
#		for TL: item in Start {
#			require TL
#				[OpenTag: open_tag Inside: item*]
#
#			match OpenTag 
#				['<' TagId: tag_id attr* '>']
#
#			if should_close( TagId )
#			{
#				parse CloseId: close_id[ TagId.data ]
#
#				CloseTag: opt_close_tag = 
#					construct opt_close_tag ['</' CloseId.tree '>']
#
#				# Close the tag and put inside after it.
#				TL = construct item
#					[OpenTag Inside CloseTag]
#			}
#		}
#	}
}

cons Tags: tags[]
Tags.TagStack = construct tags::tag_stack []

parse HTML_P: tags::start(Tags)[ stdin ]
HTML: tags::start = HTML_P.tree
flatten( HTML )
print_xml( HTML )
printLinks( HTML )