summaryrefslogtreecommitdiff
path: root/test/tags4.lm
blob: f710378c54e6d698ef2b5626d8241f659900c9fa (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
##### LM #####
#
#
# This is somewhat broken. missing_close_id is cuasing close ids to be parseed
# when they shouldn't. Maybe remove it.
#
#

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

	literal `> `/>

	# 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

	#
	# Tokens
	#

	lex
		ignore /space+/

		literal `< `</ `<!DOCTYPE
		token close_tag 
			/'</' [\t ]* [a-zA-Z]+ '>'/

		token doc_data /[^<]+/
		token comment /'<!--' any* :>> '-->'/
	end

	#
	# Tags
	#

	bool inTagStack( id: str )
	{
		LocalTagStack: tag_stack = TagStack
		for Tag: tag_id in LocalTagStack {
			if id == Tag.data
				return true
		}
		return false
	}

	# 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 missing_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>

			if ( inTagStack( match_text ) ) {
				print( 'CLOSE \'' match_text '\' IN TAG STACK\n' )

				# The tag is in the stack, send missing close tags until we get to it.
				match TagStack [Top:tag_id Rest:tag_stack] 
				TagStack = Rest
				while ( Top.data != match_text ) {
					print( 'SENDING missing close\n' )
					input.push( make_token( typeid<missing_close_id> '' ) )
					match TagStack [Top2:tag_id Rest2:tag_stack] 
					Top = Top2
					TagStack = Rest2
				}

				print( 'SENDING close\n' )
				input.push( make_token( typeid<close_id> input.pull( match_length ) ) )
			}
			else {
				print( 'CLOSE \'' match_text '\' NOT IN TAG STACK\n' )
				# The tag is not in the tag stack so send the id as a stray close.
				input.push( make_token( typeid<stray_close> 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 /"[" [^\]]* "]"/
		token dt_close /'>'/
	end

	# 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* close_tag]

	def unclosed_tag 
		[open_tag item* missing_close_id]

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

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

	#
	# Stray close tags
	#
	def stray_close 
		[close_tag]


	#
	# 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]
	|	[unclosed_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
	}
end # tags

# 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
#				}
#			}
#
#			str empty = ''
#			missing_close_id Missing = construct missing_close_id [empty]
#			opt_close_tag EmptyCloseTag = 
#				construct opt_close_tag [Missing]
#
#			# 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 )
#        {
#			close_id CloseId = construct close_id 
#				[TagId.data]
#
#            opt_close_tag CloseTag = 
#                construct opt_close_tag ['</' CloseId '>']
#
#            # 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: tags::start(Tags)[ stdin ]
print( HTML )

#print_xml( HTML )
#for C: close_tag in HTML 
#	print( C '\n' )
##### IN #####
<t1>

  <t2>
  <a href="foo">&FOO</a>
  <t3>
  </t3>

</t1>
##### EXP #####
<t1>

  <t2>
  <a href="foo">&FOO</a>
  <t3>
  </t3>

</t1>