Created
September 23, 2009 21:36
-
-
Save msassak/192294 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CommandParser | |
| %%{ | |
| machine wire_command_parser; | |
| action parse_error { | |
| @sess.parse_error | |
| } | |
| action command { | |
| command = data[0..p].pack("c*") | |
| @sess.dispatch(command) | |
| } | |
| action arg_command { | |
| command = data[0...p].pack("c*") | |
| json = data[p+1..eof].pack("c*") | |
| @sess.dispatch(command, json) | |
| } | |
| Command = ( | |
| 'LIST_STEP_DEFINITIONS' | 'OK' | 'FATAL' | | |
| 'TABLE_DIFF_OK' | 'TABLE_DIFF_KO' | |
| ); | |
| ArgCommand = ( | |
| 'STEP_DEFINITIONS' | 'INVOKE' | 'GROUPS_FOR_STEP_NAME' | 'GROUPS' | | |
| 'DIFF' | 'FAIL' | 'EXCEPTION' | |
| )':'; | |
| main := ( Command @command | ArgCommand @arg_command ) @err(parse_error); | |
| }%% | |
| def initialize(sess) | |
| @sess = sess | |
| %% write data; | |
| end | |
| def handle(data) | |
| data = data.unpack("c*") | |
| eof = data.length | |
| %% write init; | |
| %% write exec; | |
| end | |
| end | |
| class Session | |
| def parse_error | |
| puts "Parse Error!" | |
| end | |
| def dispatch(command, json=nil) | |
| puts "#{command}: #{json or "No data"}" | |
| end | |
| end | |
| s = Session.new | |
| p = CommandParser.new(s) | |
| p.handle("LIST_STEP_DEFINITIONS") | |
| p.handle('INVOKE:{ "command" : "INVOKE", "args" : { "id" : "some id", "args" : [ "foo", "bar" ] } }') | |
| p.handle('GROUPS_FOR_STEP_NAME:{ "command" : "GROUPS_FOR_STEP_NAME", "id": "foo", "step_name": "My pet step" }') | |
| p.handle('DIFF:{ "command" : "DIFF", "foo": "bar" }') | |
| p.handle('FAIL:{ "command" : "FAIL", "msg": "Exception in foo step" }') | |
| p.handle("BLARGLE") | |
| p.handle(" LIST_STEP_DEFINITIONS") | |
| p.handle("DIFF:") # This should throw a parse error, but currently it does not :-( | |
| p.handle("TABLE_DIFF_O") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment