summaryrefslogtreecommitdiff
path: root/test/colm.d/accumbt3.lm
blob: 6d039451d7f477af6a14a874123d74dc7641ce5b (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
#
# Tokens
#

# Any single character can be a literal
lex
	# Ignore whitespace.
	ignore /[ \t\n\r\v]+/

	# Open and close id
	token id /[a-zA-Z_][a-zA-Z0-9_]*/

	token open_paren /'('/ 
	{
		parse_stop NC: nested_comment(input)[]
		print( 'discarding: ', NC, '\n' )
	}
end

#
# Token translation
#

lex
	literal `( `)
	token nc_data /[^()]+/
end

def nc_item 
	[nc_data]
|	[nested_comment]

def nested_comment 
	[`( nc_item* `)]

def nested [id*]

#
# Accumulator.
#
context accum_bt

NestedParser: accum<nested>

lex
	ignore /[ \t]+/
	token word /[a-zA-Z0-9/*+_\-]+/
	token stuff /[a-zA-Z0-9()/*+_\- ]+/
	literal `! `;
	token NL /'\n'/

end

def A1 []
	{ print( "A1\n" ) }

def A2 []
	{ print( "A2\n" ) }

def item
	[word]
	{
		send NestedParser [' ']
		send NestedParser [$r1]
		send NestedParser [' ']
	}
|
	[stuff]
	{
		send NestedParser [' ']
		send NestedParser [$r1]
		send NestedParser [' ']
	}

def two 
	[A1 item* `! NL]
|	[A2 item* `; NL]

end # accum_bt

AccumBt: accum_bt = new accum_bt()
AccumBt->NestedParser = new parser<nested>()

parse Two: accum_bt::two(AccumBt)[ stdin ]

send AccumBt->NestedParser [] eos
Nested: nested = AccumBt->NestedParser->tree

print( '\n------------\n' )
print( ^Nested, '\n' )
print( ^Two, '\n' )

##### IN #####
hello there ( (this is a nested comment /*sdf asd_++_stuff) ) and this is not ;
##### EXP #####
A1
discarding: ( (this is a nested comment /*sdf asd_++_stuff) )
A2
discarding: ( (this is a nested comment /*sdf asd_++_stuff) )

------------
hello there  and this is not
hello there ( (this is a nested comment /*sdf asd_++_stuff) ) and this is not ;