blob: ffa3138e18888b97e49233a0cffecc0f80e32ff6 (
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
|
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
literal '!\n', ';\n'
# Ignore whitespace.
ignore /rl_ws/
# Open and close id
token id /rl_id/
end
#
# 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' )
}
end # tags
cons Tags: tags[]
Tags.TS = cons tags::tag_stack ["sentinal"]
parse tags::start(Tags)[stdin]
|