summaryrefslogtreecommitdiff
path: root/tools/dfagen/dfagen/parser.g
blob: e510ed2214211745d29ceb238d981c15a124a93f (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
from dfagen.automaton import *

%%

parser AutomatonParser:
    ignore:		"(#.*?)?\n"
    token EOF:		"$"
    token COMMENTS:	"  .+?\n"
    token STATE:	"\w+"
    token EVENT:	"\w([\w ]*\w)?"
    token QUALIFIER:	"\w([\w ]*\w)?"
    token ATITLE:	".*?\n"
    token ATTR:		"\w([\w =]*\w)?"

    rule automaton:	ATITLE		{{ a = Automaton (ATITLE.strip ()) }}
			( comments	{{ a.comments = comments }}
				) ?
			"States:\n"
			( statedef	{{ a.add_state (statedef) }}
				) *
			"Events:\n"
			( eventdef	{{ a.add_event (eventdef) }}
				) *
			( transdef<<a>>
				) *
			EOF		{{ return a }}

    rule statedef:			{{ initial = False }}
			" " ( "\*"	{{ initial = True }}
				) ?
			STATE		{{ s = State (STATE, initial = initial) }}
			( "\s*\[\s*"
				ATTR	{{ s.attributes = ATTR }}
				"\s*\]" ) ?
			"\n"
			( comments	{{ s.comments = comments }}
				) ?
					{{ return s }}

    rule eventdef:	" " EVENT	{{ e = Event (EVENT) }}
			"\n"
			( comments	{{ e.comments = comments }}
				) ?
					{{ return e }}

    rule transdef<<a>>:	transsl<<a>>
			( trans<<a>>	{{ for s in transsl: s.add_branch (trans) }}
				) *

    rule transsl<<a>>:			{{ sl = [ ] }}
			STATE		{{ sl.append (a.states[STATE]) }}
			( ",\s*" STATE	{{ sl.append (a.states[STATE]) }}
				) *
			":\n"		{{ return sl }}

    rule trans<<a>>:	" " EVENT	{{ t = TransitionBranch (a.events[EVENT]) }}
			( ":\s*" QUALIFIER {{ t.name = QUALIFIER }}
				) ?
			"\s*->\s*"
			( STATE		{{ t.to = a.states[STATE] }}
				| "\\." )
			( "\s*\[\s*"
				ATTR	{{ t.attributes = ATTR }}
				"\s*\]" ) ?
			( comments	{{ t.comments = comments }}
				) ?
					{{ return t }}

    rule comments:	COMMENTS	{{ c = COMMENTS.strip () }}
			( COMMENTS	{{ c += '\n' + COMMENTS.strip () }}
				) *	{{ return c }}