10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-09 07:33:53 +01:00
quantum_package/ocaml/Message_lexer.mll

276 lines
8.5 KiB
OCaml
Raw Normal View History

2017-02-27 19:24:19 +01:00
{
type kw_type =
| TEXT of string
| WORD of string
| INTEGER of int
| FLOAT of float
| NONE
| ADD_TASK
| DEL_TASK
| GET_TASK
2017-11-27 16:31:00 +01:00
| GET_TASKS
2017-02-27 19:24:19 +01:00
| TASK_DONE
| DISCONNECT
| CONNECT
| NEW_JOB
| END_JOB
| TERMINATE
2017-05-10 00:04:34 +02:00
| ABORT
2017-11-26 23:47:11 +01:00
| GET_DATA
| PUT_DATA
2017-02-27 19:24:19 +01:00
| OK
| ERROR
| SET_STOPPED
| SET_RUNNING
| SET_WAITING
2017-02-28 13:28:36 +01:00
type state_tasks = { state : string ; tasks : string list ; }
type state_taskids = { state : string ; task_ids : int list ; }
type state_taskids_clientid = { state : string ; task_ids : int list ; client_id : int ; }
2017-02-27 19:24:19 +01:00
type state_clientid = { state : string ; client_id : int ; }
2017-11-27 16:31:00 +01:00
type state_clientid_ntasks = { state : string ; client_id : int ; n_tasks : int}
2017-02-27 19:24:19 +01:00
type state_tcp_inproc = { state : string ; push_address_tcp : string ; push_address_inproc : string ; }
type psi = { client_id: int ; n_state: int ; n_det: int ; psi_det_size: int ;
2017-05-03 21:15:54 +02:00
n_det_generators: int option ; n_det_selectors: int option ; }
2017-11-29 13:52:52 +01:00
type state_client_id_key = { state: string ; client_id: int ; key: string }
2017-02-27 19:24:19 +01:00
type msg =
2017-02-28 13:28:36 +01:00
| AddTask_ of state_tasks
| DelTask_ of state_taskids
2017-02-27 19:24:19 +01:00
| GetTask_ of state_clientid
2017-11-27 16:31:00 +01:00
| GetTasks_ of state_clientid_ntasks
2017-02-28 13:28:36 +01:00
| TaskDone_ of state_taskids_clientid
2017-02-27 19:24:19 +01:00
| Disconnect_ of state_clientid
| Connect_ of string
| NewJob_ of state_tcp_inproc
| EndJob_ of string
| Terminate_
2017-05-10 00:04:34 +02:00
| Abort_
2017-11-29 13:52:52 +01:00
| GetData_ of state_client_id_key
| PutData_ of state_client_id_key
2017-02-27 19:24:19 +01:00
| Ok_
| Error_ of string
| SetStopped_
| SetRunning_
| SetWaiting_
}
let word = [^' ' '\t' '\n']+
2017-02-28 13:28:36 +01:00
let text = [^ ' ' '|']+[^ '|']+
2017-02-27 19:24:19 +01:00
let integer = ['0'-'9']+
let real = '-'? integer '.' integer (['e' 'E'] '-'? integer)?
let white = [' ' '\t']+
rule get_text = parse
| text as t { TEXT t }
2017-02-28 13:28:36 +01:00
| eof { TERMINATE }
2017-02-27 19:24:19 +01:00
| _ { NONE }
2017-02-28 13:28:36 +01:00
and get_int = parse
2017-02-27 19:24:19 +01:00
| integer as i { INTEGER (int_of_string i) }
2017-02-28 13:28:36 +01:00
| eof { TERMINATE }
| _ { NONE }
and get_word = parse
| word as w { WORD w }
| eof { TERMINATE }
| _ { NONE }
and kw = parse
2017-02-27 19:24:19 +01:00
| "add_task" { ADD_TASK }
| "del_task" { DEL_TASK }
| "get_task" { GET_TASK }
2017-11-27 16:31:00 +01:00
| "get_tasks" { GET_TASKS }
2017-02-27 19:24:19 +01:00
| "task_done" { TASK_DONE }
| "disconnect" { DISCONNECT }
| "connect" { CONNECT }
| "new_job" { NEW_JOB }
| "end_job" { END_JOB }
2017-11-26 23:47:11 +01:00
| "put_data" { PUT_DATA }
| "get_data" { GET_DATA }
2017-02-27 19:24:19 +01:00
| "terminate" { TERMINATE }
2017-05-10 00:04:34 +02:00
| "abort" { ABORT }
2017-02-27 19:24:19 +01:00
| "ok" { OK }
| "error" { ERROR }
| "set_stopped" { SET_STOPPED }
| "set_running" { SET_RUNNING }
| "set_waiting" { SET_WAITING }
| _ { NONE }
{
2017-02-28 13:28:36 +01:00
let rec read_text ?(accu=[]) lexbuf =
2017-02-27 19:24:19 +01:00
let token =
get_text lexbuf
in
match token with
2017-02-28 13:28:36 +01:00
| TEXT t -> read_text ~accu:(t::accu) lexbuf
| TERMINATE -> List.rev accu
| NONE -> read_text ~accu lexbuf
2017-02-27 19:24:19 +01:00
| _ -> failwith "Error in MessageLexer (2)"
and read_word lexbuf =
let token =
2017-02-28 13:28:36 +01:00
get_word lexbuf
2017-02-27 19:24:19 +01:00
in
match token with
| WORD w -> w
| NONE -> read_word lexbuf
| _ -> failwith "Error in MessageLexer (3)"
and read_int lexbuf =
let token =
2017-02-28 13:28:36 +01:00
get_int lexbuf
2017-02-27 19:24:19 +01:00
in
match token with
| INTEGER i -> i
| NONE -> read_int lexbuf
| _ -> failwith "Error in MessageLexer (4)"
2017-02-28 13:28:36 +01:00
and read_ints ?(accu=[]) lexbuf =
let token =
get_int lexbuf
in
match token with
| INTEGER i -> read_ints ~accu:(i::accu) lexbuf
| TERMINATE -> List.rev accu
| NONE -> read_ints ~accu lexbuf
| _ -> failwith "Error in MessageLexer (4)"
2017-02-27 19:24:19 +01:00
and parse_rec lexbuf =
let token =
kw lexbuf
in
match token with
| ADD_TASK ->
let state = read_word lexbuf in
2017-02-28 13:28:36 +01:00
let tasks = read_text lexbuf in
AddTask_ { state ; tasks }
2017-02-27 19:24:19 +01:00
| DEL_TASK ->
2017-02-28 13:28:36 +01:00
let state = read_word lexbuf in
let task_ids = read_ints lexbuf in
DelTask_ { state ; task_ids }
2017-02-27 19:24:19 +01:00
| GET_TASK ->
let state = read_word lexbuf in
let client_id = read_int lexbuf in
GetTask_ { state ; client_id }
2017-11-27 16:31:00 +01:00
| GET_TASKS ->
let state = read_word lexbuf in
let client_id = read_int lexbuf in
let n_tasks = read_int lexbuf in
GetTasks_ { state ; client_id ; n_tasks }
2017-02-27 19:24:19 +01:00
| TASK_DONE ->
let state = read_word lexbuf in
2017-02-28 13:28:36 +01:00
let client_id = read_int lexbuf in
let task_ids = read_ints lexbuf in
TaskDone_ { state ; task_ids ; client_id }
2017-02-27 19:24:19 +01:00
| DISCONNECT ->
let state = read_word lexbuf in
let client_id = read_int lexbuf in
Disconnect_ { state ; client_id }
2017-11-26 23:47:11 +01:00
| GET_DATA ->
2017-11-29 13:52:52 +01:00
let state = read_word lexbuf in
2017-02-27 19:24:19 +01:00
let client_id = read_int lexbuf in
2017-11-26 23:47:11 +01:00
let key = read_word lexbuf in
2017-11-29 13:52:52 +01:00
GetData_ { state ; client_id ; key }
2017-02-27 19:24:19 +01:00
2017-11-26 23:47:11 +01:00
| PUT_DATA ->
2017-11-29 13:52:52 +01:00
let state = read_word lexbuf in
2017-05-03 21:15:54 +02:00
let client_id = read_int lexbuf in
2017-11-26 23:47:11 +01:00
let key = read_word lexbuf in
2017-11-29 13:52:52 +01:00
PutData_ { state ; client_id ; key }
2017-05-03 21:15:54 +02:00
2017-02-27 19:24:19 +01:00
| CONNECT ->
let socket = read_word lexbuf in
Connect_ socket
| NEW_JOB ->
let state = read_word lexbuf in
let push_address_tcp = read_word lexbuf in
let push_address_inproc = read_word lexbuf in
NewJob_ { state ; push_address_tcp ; push_address_inproc }
| END_JOB ->
let state = read_word lexbuf in
EndJob_ state
| ERROR ->
2017-02-28 13:28:36 +01:00
let message = List.hd (read_text lexbuf) in
2017-02-27 19:24:19 +01:00
Error_ message
| OK -> Ok_
| SET_WAITING -> SetWaiting_
| SET_RUNNING -> SetRunning_
| SET_STOPPED -> SetStopped_
| TERMINATE -> Terminate_
2017-05-10 00:04:34 +02:00
| ABORT -> Abort_
2017-02-27 19:24:19 +01:00
| NONE -> parse_rec lexbuf
| _ -> failwith "Error in MessageLexer"
let parse message =
let lexbuf =
Lexing.from_string message
in
parse_rec lexbuf
let debug () =
let l = [
"add_task state_pouet Task pouet zob" ;
2017-02-28 13:28:36 +01:00
"add_task state_pouet Task pouet zob |Task2 zob | Task3 prout" ;
2017-02-27 19:24:19 +01:00
"del_task state_pouet 12345" ;
2017-02-28 13:28:36 +01:00
"del_task state_pouet 12345 | 6789 | 10 | 11" ;
2017-02-27 19:24:19 +01:00
"get_task state_pouet 12" ;
2017-11-27 16:31:00 +01:00
"get_tasks state_pouet 12 23" ;
2017-02-27 19:24:19 +01:00
"task_done state_pouet 12 12345";
2017-02-28 13:28:36 +01:00
"task_done state_pouet 12 12345 | 678 | 91011";
2017-02-27 19:24:19 +01:00
"connect tcp";
"disconnect state_pouet 12";
"new_job state_pouet tcp://test.com:12345 ipc:///dev/shm/x.socket";
"end_job state_pouet";
"terminate" ;
2017-05-10 00:04:34 +02:00
"abort" ;
2017-02-27 19:24:19 +01:00
"set_running" ;
"set_stopped" ;
"set_waiting" ;
"ok" ;
"error my_error" ;
"get_psi 12" ;
"put_psi 12 2 1000 10000 800 900" ;
"put_psi 12 2 1000 10000"
]
|> List.map parse
in
List.map (function
2017-02-28 13:28:36 +01:00
| AddTask_ { state ; tasks } -> Printf.sprintf "ADD_TASK state:\"%s\" tasks:{\"%s\"}" state (String.concat "\"}|{\"" tasks)
| DelTask_ { state ; task_ids } -> Printf.sprintf "DEL_TASK state:\"%s\" task_ids:{%s}" state (String.concat "|" @@ List.map string_of_int task_ids)
2017-02-27 19:24:19 +01:00
| GetTask_ { state ; client_id } -> Printf.sprintf "GET_TASK state:\"%s\" task_id:%d" state client_id
2017-11-27 16:31:00 +01:00
| GetTasks_ { state ; client_id ; n_tasks } -> Printf.sprintf "GET_TASKS state:\"%s\" task_id:%d n_tasks:%d" state client_id n_tasks
2017-02-28 13:28:36 +01:00
| TaskDone_ { state ; task_ids ; client_id } -> Printf.sprintf "TASK_DONE state:\"%s\" task_ids:{%s} client_id:%d" state (String.concat "|" @@ List.map string_of_int task_ids) client_id
2017-02-27 19:24:19 +01:00
| Disconnect_ { state ; client_id } -> Printf.sprintf "DISCONNECT state:\"%s\" client_id:%d" state client_id
| Connect_ socket -> Printf.sprintf "CONNECT socket:\"%s\"" socket
| NewJob_ { state ; push_address_tcp ; push_address_inproc } -> Printf.sprintf "NEW_JOB state:\"%s\" tcp:\"%s\" inproc:\"%s\"" state push_address_tcp push_address_inproc
| EndJob_ state -> Printf.sprintf "END_JOB state:\"%s\"" state
2017-11-29 13:52:52 +01:00
| GetData_ { state ; client_id; key } -> Printf.sprintf "GET_DATA state:%s client_id:%d key:%s" state client_id key
| PutData_ { state ; client_id ; key } -> Printf.sprintf "PUT_DATA state:%s client_id:%d key:%s" state client_id key
2017-02-27 19:24:19 +01:00
| Terminate_ -> "TERMINATE"
2017-05-10 00:04:34 +02:00
| Abort_ -> "ABORT"
2017-02-27 19:24:19 +01:00
| SetWaiting_ -> "SET_WAITING"
| SetStopped_ -> "SET_STOPPED"
| SetRunning_ -> "SET_RUNNING"
| Ok_ -> "OK"
| Error_ s -> Printf.sprintf "ERROR: \"%s\"" s
) l
|> List.iter print_endline
}