summaryrefslogtreecommitdiffhomepage
path: root/tools/dfagen/dfagen/parser.g
diff options
context:
space:
mode:
authorNicolas Schodet2008-03-17 22:53:01 +0100
committerNicolas Schodet2008-03-17 22:53:01 +0100
commit164ac3a34cbac441e82b256c97cb8784ea9d482c (patch)
treeb0db1276083d168b50e6aa2be9621368a36184cd /tools/dfagen/dfagen/parser.g
parent388a90600023cca7d7b28702fa4cc75ed5074123 (diff)
* tools/dfagen:
- added dfagen.
Diffstat (limited to 'tools/dfagen/dfagen/parser.g')
-rw-r--r--tools/dfagen/dfagen/parser.g62
1 files changed, 62 insertions, 0 deletions
diff --git a/tools/dfagen/dfagen/parser.g b/tools/dfagen/dfagen/parser.g
new file mode 100644
index 00000000..62f9f4d1
--- /dev/null
+++ b/tools/dfagen/dfagen/parser.g
@@ -0,0 +1,62 @@
+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"
+
+ 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: " " STATE {{ s = State (STATE) }}
+ "\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] }}
+ | "\\." )
+ ( comments {{ t.comments = comments }}
+ ) ?
+ {{ return t }}
+
+ rule comments: COMMENTS {{ c = COMMENTS.strip () }}
+ ( COMMENTS {{ c += '\n' + COMMENTS.strip () }}
+ ) * {{ return c }}
+