summaryrefslogtreecommitdiff
path: root/doc/2_03_def.adoc
blob: bd3b87aa9bba450c568f979a2ac29a0c6bfde8c3 (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
Def
===

The 'def' is where colm realy shines.
A 'def' is somewhere between a struct and a regular expression.
Again one example is much more clearer.

[source,chapel]
.assign.lm
----
lex start
	token id / ('a' .. 'z' | 'A' .. 'Z' ) + /
	token value / ( 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )+ /
	literal `= `;
	ignore / [ \t\n] /
end

def assignment
	[ id `=  value `;]

	
def assignment_list
	[assignment assignment_list]
|	[assignment]
|	[]

parse Simple: assignment_list[ stdin ]

for I:assignment in Simple {
	print( I.id, "->", I.value, "\n" )
}
----

After the compilation we can pipe some input to it's stdin.

[source,bash]
----
/opt/colm/bin/colm assign.lm
echo -e 'b=3;a=1;\n c=2;' |./assign
----

This gives us:

----
b->3
a->1
c->2
----

NOTE: this also illustrates how to read from 'stdin'.