summaryrefslogtreecommitdiff
path: root/test/tags1.lm
blob: 385ba515e207f4b0c7f7da8b902fb76f1d00272f (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
context tags
{
	# Open and close tags by rewriting to generic close tags. Won't work if
	# interested in unclosed tags because a token can start as not close_id, but
	# then become a close id during the course of parsing.

	#
	# Regular Definitions
	#
	rl rl_ws /[ \t\n\r\v]+/
	rl rl_id /[a-zA-Z_][a-zA-Z0-9_]*/

	#
	# Tokens
	#

	# Any single character can be a literal
	lex start
	{
		literal '!\n', ';\n'

		# Ignore whitespace.
		ignore /rl_ws/

		# Open and close id
		token id /rl_id/
	}

	#
	# Global Data
	#

	def tag_stack 
		[id tag_stack]
	|	[]

	TS: tag_stack

	#
	# Productions
	#

	def open_tag 
		[id]
		{
			match lhs [Id:id]
			match TS [Top:id Rest:tag_stack]
			if Id.data == Top.data {
				reject
			} else {
				TS = construct tag_stack [Id TS]
			}
		}

	def close_tag 
		[id]
		{
			match lhs [Id: id]
			match TS [Top: id Rest: tag_stack]

			if Id.data == Top.data
				TS = construct tag_stack [Rest]
			else
				reject
		}

	def tag 
		[open_tag tag* close_tag]

	def start
		[tag* ';\n']
		{
			print_xml( TS )
			print_xml( lhs )
			print( 'got structure\n' )
		}

	|	[id* ';\n']
		{
			print_xml( TS )
			print_xml( lhs )
			print( 'failed\n' )
		}
}

cons Tags: tags[]
Tags.TS = cons tags::tag_stack ["sentinal"]
parse tags::start(Tags)[stdin]