lib/parser/koi-reference-parser.treetop in koi-reference-parser-0.0.2 vs lib/parser/koi-reference-parser.treetop in koi-reference-parser-0.0.3
- old
+ new
@@ -1,15 +1,27 @@
+# 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+ <Block>
end
rule statement
space? ( hash_assignment / assignment / if / unless / function_call ) ';'? space? <Statement>
end
+ ##################
+ # Literals
+
rule nil
"nil" <NilLiteral>
end
rule true
@@ -34,10 +46,28 @@
rule identifier
'$'? [a-zA-Z] [a-zA-Z0-9_]* <Identifier>
end
+ ##################
+ # Hash literals
+
+ rule hash
+ '{' key_value_list? '}' <HashLiteral>
+ end
+
+ rule key_value_list
+ key_value+ <KeyValueList>
+ end
+
+ rule key_value
+ space? expression space? '=>' space? expression space? ','? <KeyValue>
+ end
+
+ ##################
+ # Assignment
+
rule hash_assignment
identifier hash_accessor_list space? assignment_operator space? ( hash / function_definition / expression ) <HashAssignment>
end
rule assignment
@@ -68,94 +98,70 @@
rule function_definition
'function(' space? identifier space? ')'
block
space? 'end' <FunctionDefinition>
- end
-
+ end
+
##################
- # Whitespace
+ # Hash component access
- rule space
- [\s]+ <Whitespace>
+ rule hash_access
+ identifier hash_accessor_list <HashAccess>
end
- ##################
- # Hashes
-
rule hash_accessor_list
hash_accessor+ <HashAccessorList>
end
rule hash_accessor
'[' expression ']' <HashAccessor>
end
-
- rule hash
- '{' key_value_list? '}' <HashLiteral>
- end
-
- rule key_value_list
- key_value+ <KeyValueList>
- end
-
- rule key_value
- space? expression space? '=>' space? expression space? ','? <KeyValue>
- end
-
- rule key
- space? expression space? <Key>
- end
-
- rule value
- space? expression space? <Value>
- end
##################
# Expressions
rule expression
space? (hash_access / comparative / additive) <Expression>
end
- rule hash_access
- identifier hash_accessor_list <HashAccess>
- end
-
rule comparative
- additive space? (equality_operator / inequality_operator / greater_than_operator / less_than_operator) space? additive <ComparativeExpression>
+ additive space? comparative_operator space? additive <ComparativeExpression>
end
rule additive
multitive space? additive_operator space? additive <AdditiveExpression>
/
multitive
end
-
- rule additive_operator
- addition_operator
- /
- subtraction_operator
- end
rule multitive
primary space? multitive_operator space? multitive <MultitiveExpression>
/
primary
end
- rule multitive_operator
- multiplication_operator
- /
- division_operator
- end
-
rule primary
function_call / nil / false / true / identifier / float / integer / string
/
'(' space? expression space? ')' <Expression>
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
@@ -190,8 +196,15 @@
'>' <GreaterThanOperator>
end
rule less_than_operator
'<' <LessThanOperator>
+ end
+
+ ##################
+ # Whitespace
+
+ rule space
+ [\s]+
end
end