# This file is a Parsing Expression Grammar (PEG) for the Koi # programming language. It is interpreted by the PEG engine Treetop. # More info on PEGs: http://en.wikipedia.org/wiki/Parsing_expression_grammar # More info on Treetop: http://treetop.rubyforge.org/ grammar KoiReferenceParser ################## # Program structure rule block statement+ end rule statement space? ( hash_assignment / assignment / if / unless / function_call ) ';'? space? end ################## # Literals rule nil "nil" end rule true "true" end rule false "false" end rule integer ('+' / '-')? [0-9]+ end rule float ('+' / '-')? [0-9]+ '.' [0-9]+ end rule string '"' ([^"\\] / "\\" . )* '"' end rule identifier '$'? [a-zA-Z] [a-zA-Z0-9_]* end ################## # Hash literals rule hash '{' key_value_list? '}' end rule key_value_list key_value+ end rule key_value space? expression space? '=>' space? expression space? ','? end ################## # Assignment rule hash_assignment identifier hash_accessor_list space? assignment_operator space? ( hash / function_definition / expression ) end rule assignment identifier space? assignment_operator space? ( hash / function_definition / expression ) end ################## # Flow control rule if 'if(' space? expression space? ')' space block space? 'end' space? end rule unless 'unless(' space? expression space? ')' space block space? 'end' space? end ################## # Functions rule function_call identifier space? '(' space? expression? space? ','? expression? space? ')' end rule function_definition 'function(' space? identifier space? ')' block space? 'end' end ################## # Hash component access rule hash_access identifier hash_accessor_list end rule hash_accessor_list hash_accessor+ end rule hash_accessor '[' expression ']' end ################## # Expressions rule expression space? (hash_access / comparative / additive) end rule comparative additive space? comparative_operator space? additive end rule additive multitive space? additive_operator space? additive / multitive end rule multitive primary space? multitive_operator space? multitive / primary end rule primary function_call / nil / false / true / identifier / float / integer / string / '(' space? expression space? ')' end ################## # Operator sets rule comparative_operator equality_operator / inequality_operator / greater_than_operator / less_than_operator end rule additive_operator addition_operator / subtraction_operator end rule multitive_operator multiplication_operator / division_operator end ################## # Operators rule assignment_operator '=' end rule addition_operator '+' end rule subtraction_operator '-' end rule multiplication_operator '*' end rule division_operator '/' end rule equality_operator '==' end rule inequality_operator '!=' end rule greater_than_operator '>' end rule less_than_operator '<' end ################## # Whitespace rule space [\s]+ end end