start         : er imports? decls? python_block?

er            : "EscapeRoom(" er_parameters ")"
er_parameters : param_title "," param_size "," param_scenarios "," param_events "," param_transitions

imports       : import_obj+
import_obj    : "import Object." ID

decls          : (var|decl)+
decl           : add_view|add_object|add_sound

add_view : "." ID         ".add_View" "(" view ")"
add_sound    : "." ID         ".add_Sound"    "(" sound    ")"
add_object : "." cenario_id ".add_Object" "(" object ")"


var            : VAR "=" value
value          : ID|python_call|list_text_constructor|text_constructor|size_constructor|position_constructor|number_constructor|view_constructor|object_constructor|object_imported|sound_constructor|cenario_constructor|event_constructor|challenge_constructor|transition_constructor

python_call              : python_local|python_function
python_local             : "Python.Local." ID
python_function          : "Python.Function." FUNC

list_text              : list_text_arg|list_text_constructor|list_text_python_call
list_text_arg          : ID
list_text_python_call  : python_call
list_text_constructor  : "[" text ("," text)* "]"
 
text                    : text_arg|text_constructor|text_python_call
text_arg                : ID
text_python_call        : python_call
text_constructor        : TEXTO
     
size                  : size_arg|size_constructor|size_python_call
size_arg              : ID
size_python_call      : python_call
size_constructor      : "[" number "," number "]"
     
position                  : position_arg|position_constructor|position_python_call
position_arg              : ID
position_python_call      : python_call
position_constructor      : "(" number "," number ")"
     
number                   : number_arg|number_constructor|number_python_call
number_arg               : ID
number_python_call       : python_call
number_constructor       : NUM
     
views                  : "[" view ("," view)* "]"
                         | "[" "]"
view                   : view_arg|view_constructor_id|view_python_call
view_arg               : ID
view_id                : ID
view_python_call       : python_call
view_constructor       : view_static|view_animated
view_constructor_id    : view_static_id|view_animated_id
view_static          : "View.Static" "(" param_image ("," param_position)? ("," param_size)? ")"
view_animated          : "View.Animated" "(" param_images "," param_repetitions "," param_time_sprite ("," param_position)? ("," param_size)? ")"
view_static_id       : "View.Static." ID "(" param_image ("," param_position)? ("," param_size)? ")"
view_animated_id       : "View.Animated." ID "(" param_images "," param_repetitions "," param_time_sprite ("," param_position)? ("," param_size)? ")"
 
sounds                     : "[" sound ("," sound)* "]"
                         | "[" "]"
sound                      : sound_arg|sound_constructor_id|sound_python_call
sound_arg                  : ID
sound_id                   : ID
sound_python_call          : python_call
sound_constructor          : "Sound" "(" param_source ")"
sound_constructor_id       : "Sound." ID "(" param_source ")"
 
objects                  : "[" object ("," object)* "]"
object                   : object_imported_id|object_arg|object_constructor_id|object_python_call
object_arg               : ID
object_id                : ID
object_python_call       : python_call
object_imported          : "Object." ID ("(" (param_position|param_size|(param_position "," param_size)) ")")?
object_constructor       : "Object" "(" (param_view_inicial ",")? param_views ("," param_position)? ("," param_size)? ("," param_sounds)? ")"
object_imported_id       : "Object." ID "." ID ("(" (param_position|param_size|(param_position "," param_size)) ")")?
object_constructor_id    : "Object." ID "(" (param_view_inicial ",")? param_views ("," param_position)? ("," param_size)? ("," param_sounds)? ")"


scenarios                 : "[" cenario ("," cenario)* "]"
                         | "[" "]"
cenario                  : cenario_arg|cenario_constructor_id|cenario_python_call
cenario_arg              : ID
cenario_id               : ID
cenario_python_call      : python_call
cenario_constructor      : "Scenario" "(" param_view_inicial "," param_views "," param_objects ("," param_sounds)? ")"
cenario_constructor_id   : "Scenario." ID "(" param_view_inicial "," param_views "," param_objects ("," param_sounds)? ")"


transitions               : "[" transition ("," transition)* "]"
                         | "[" "]"
transition                : transition_arg|transition_constructor_id|transition_python_call
transition_arg            : ID
transition_id             : ID
transition_python_call    : python_call
transition_constructor    : "Transition" "(" param_background "," param_music "," param_story "," (param_prox_cena|param_prox_trans)")"
transition_constructor_id : "Transition." ID "(" param_background "," param_music "," param_story "," (param_prox_cena|param_prox_trans)")"


challenge                  : challenge_arg|challenge_constructor_id|challenge_python_call
challenge_arg              : ID
challenge_python_call      : python_call
challenge_constructor      : challenge_question|challenge_motion|challenge_multiple_choice|challenge_connection|challenge_sequence|challenge_puzzle|challenge_slidepuzzle|challenge_socket
challenge_question         : "Challenge.Question"         "(" param_question "," param_answer       "," param_sucess   "," param_fail                   ")"
challenge_motion          : "Challenge.Motion"          "(" param_object   "," param_object_trigger "," param_sucess    "," param_fail                  ")"
challenge_multiple_choice : "Challenge.Multiple_Choice" "(" param_question "," param_choices       "," param_answer "," param_sucess "," param_fail  ")"
challenge_connection          : "Challenge.Connection"          "(" param_question "," param_list1         "," param_list2   "," param_sucess "," param_fail  ")"
challenge_sequence        : "Challenge.Sequence"        "(" param_question "," param_sequence      "," param_sucess   "," param_fail                   ")"
challenge_puzzle           : "Challenge.Puzzle"           "(" param_image   "," param_sucess                                                              ")"
challenge_slidepuzzle      : "Challenge.SlidePuzzle"      "(" param_image   "," param_sucess                                                              ")"
challenge_socket           : "Challenge.Socket"           "(" param_host     "," param_port      "," param_message  "," param_sucess "," param_fail ")"
challenge_constructor_id      : challenge_question_id|challenge_motion_id|challenge_multiple_choice_id|challenge_connection_id|challenge_sequence_id|challenge_puzzle_id|challenge_slidepuzzle_id|challenge_socket_id
challenge_question_id         : "Challenge.Question."         ID "(" param_question "," param_answer       "," param_sucess   "," param_fail                   ")"
challenge_motion_id          : "Challenge.Motion."          ID "(" param_object   "," param_object_trigger "," param_sucess    "," param_fail                  ")"
challenge_multiple_choice_id : "Challenge.Multiple_Choice." ID "(" param_question "," param_choices       "," param_answer "," param_sucess "," param_fail  ")"
challenge_connection_id          : "Challenge.Connection."          ID "(" param_question "," param_list1         "," param_list2   "," param_sucess "," param_fail  ")"
challenge_sequence_id        : "Challenge.Sequence."        ID "(" param_question "," param_sequence      "," param_sucess   "," param_fail                   ")"
challenge_puzzle_id           : "Challenge.Puzzle."           ID "(" param_image   "," param_sucess                                                              ")"
challenge_slidepuzzle_id      : "Challenge.SlidePuzzle."      ID "(" param_image   "," param_sucess                                                              ")"
challenge_socket_id           : "Challenge.Socket."           ID "(" param_host     "," param_port           "," param_message  "," param_sucess "," param_fail ")"




events                  : "[" event ("," event)* "]"
                         | "[" "]"
event                   : event_arg|event_constructor_id|event_python_call
event_arg               : ID
event_id                : ID
event_python_call       : python_call
event_constructor       : "Event"     "(" (param_if ",")? param_then ("," param_repetitions)? ")"
event_constructor_id    : "Event." ID "(" (param_if ",")? param_then ("," param_repetitions)? ")"

param_scenarios      : "sscenarios"          "=" scenarios
param_then           : "then"             "=" posconditions
param_view_inicial   : "initial_view"    "=" view_id
param_views          : "views"           "=" views
param_choices        : "choices"          "=" list_text
param_events         : "events"           "=" events
param_fail           : "fail"             "=" event_id
param_source         : "source"             "=" text
param_background     : "background"        "=" view
param_story          : "story"          "=" text
param_host           : "host"              "=" text
param_image          : "image"            "=" text
param_images         : "images"           "=" list_text
param_list1          : "list1"            "=" list_text
param_list2          : "list2"            "=" list_text
param_message        : "message"          "=" text
param_music          : "music"            "=" sound
param_object         : "object_id"         "=" object_id
param_object_trigger : "object_trigger"    "=" object_id
param_objects        : "objects"           "=" objects
param_question       : "question"          "=" text
param_port           : "port"              "=" number
param_position       : "position"           "=" position
param_prox_cena      : "next_scenario"   "=" cenario_id
param_prox_trans     : "next_transition" "=" transition_id
param_if             : "if"                "=" preconditions
param_sequence       : "sequence"         "=" list_text
param_sounds           : "sounds"              "=" sounds
param_sucess         : "sucess"           "=" event_id
param_repetitions    : "repetitions"        "=" number
param_answer         : "answer"          "=" text
param_size           : "size"           "=" size
param_time_sprite    : "time_sprite"       "=" number
param_title          : "title"            "=" text
param_transitions    : "transitions"        "=" transitions

preconditions            : precondition|preconds_and|preconds_ou|preconds_not|preconds_group
preconds_and               : preconditions "and" preconditions 
preconds_ou              : preconditions "or" preconditions
preconds_not             : "not" preconditions            
preconds_group           : "(" preconditions ")"          

precondition                 : precond_click_obj | precond_click_not_obj | precond_obj_is_view | precond_ev_already_hap | precond_obj_in_use | precond_already_passed
precond_click_obj            : "click" object_id                                
precond_click_not_obj        : "click not" object_id                           
precond_obj_is_view          : object_id "is" view_id                               
precond_ev_already_hap       : event_id "already happened"
precond_obj_in_use           : object_id "is in use"           
precond_already_passed       : number "seconds have already passed"                

posconditions                  : poscondition ("and" poscondition)*
poscondition                   : poscond_obj_muda_view|poscond_obj_vai_inv|poscond_fim_de_jogo|poscond_mostra_msg|poscond_obj_muda_tam|poscond_obj_muda_pos|poscond_muda_cena|poscond_remove_obj|poscond_play_sound|poscond_comeca_des|poscond_trans
poscond_obj_muda_view          : object_id "change to" view_id                  
poscond_obj_vai_inv            : object_id "goes to inventory"         
poscond_fim_de_jogo            : "end of game"                      
poscond_mostra_msg             : "show message" text "in" position
poscond_obj_muda_tam           : object_id "change size to" size     
poscond_obj_muda_pos           : object_id "move to" position     
poscond_muda_cena              : "change to scenario" cenario_id                
poscond_remove_obj             : object_id "is removed"           
poscond_play_sound             : "play" sound_id "of" ID                        
poscond_comeca_des             : "start challenge" challenge_arg                
poscond_trans                  : "transition" transition_id

python_block : "Python" "=" PYTHON_CODE


INICIAL : /inicial/
REVERSE : /\.reverse\(\)/

ID            : /[a-z][\w\-_]*/
VAR           : /\.[a-z][\w\-_]*/
ARGN          : /\$\d+/
PYTHON_CODE   : /(.|\n)+/
FUNC          : /\w+\((?:[^()]*|\([^()]*\))*\)/
COMMENT: /#[^\n]*/

%import common.ESCAPED_STRING   -> TEXTO
%import common.SIGNED_NUMBER    -> NUM

%import common.C_COMMENT
%import common.WS

%ignore C_COMMENT
%ignore WS


