Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 36 additions & 32 deletions asteroid/grammar.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/**************************************************************************************
Grammar for Asteroid written in EBNF/PEG format.
(c) University of Rhode Island

Notes:
- We generate documentation directly from this grammar file. In order to
be properly formatted in the target documentation please respect the
indentation given below.
- Use C++ style comments for any comments that you DO want to be displayed
in the target documentation.
- C style comments for any comments that you DO NOT want to be displayed
in the target documentation -- like the one you are reading right now...
**************************************************************************************/

#define NOTE(x)

NOTE("
**************************************************************************************
Grammar for Asteroid written in EBNF/PEG format.
(c) University of Rhode Island

Notes:
- We generate documentation directly from this grammar file. In order to
be properly formatted in the target documentation please respect the
indentation given below.
- Use C++ style comments for any comments that you DO want to be displayed
in the target documentation.
- Use the NOTE macro for any comments that you DO NOT want to be displayed
in the target documentation -- like the one you are reading right now...
**************************************************************************************
")
////////////////////////////////////////////////////////////////////////////////////////
// statements

Expand Down Expand Up @@ -61,37 +66,36 @@

////////////////////////////////////////////////////////////////////////////////////////
// expressions/patterns
/*
NOTE: There is no syntactic difference between a pattern
and an expression. We introduce the 'pattern' nonterminal
to highlight the SEMANTIC difference between patterns and
expressions.
*/
NOTE("
There is no syntactic difference between a pattern and an expression. We introduce
the 'pattern' nonterminal to highlight the SEMANTIC difference between patterns and
expressions.
")
exp
: pattern

pattern
: PATTERN WITH? exp
| '%[' exp ']%' binding_list? /* constraint-only pattern match */
| '%[' exp ']%' binding_list? NOTE(" constraint-only pattern match ")
| head_tail

head_tail
: conditional ('|' exp)?

/*
conditional patterns are now supported via 'pattern if cond'
no else part. Since this overlaps with conditional expressions
we check for correct usage semantically.
*/
NOTE("
conditional patterns are now supported via 'pattern if cond'
no else part. Since this overlaps with conditional expressions
we check for correct usage semantically.
")
conditional
: compound (IF exp (ELSE exp)?)?

compound
: logic_exp0
(
(IS pattern) |
(IN exp) | /* exp has to be a list */
(TO exp (STEP exp)?) | /* list comprehension */
(IN exp) | NOTE(" exp has to be a list ")
(TO exp (STEP exp)?) | NOTE(" list comprehension ")
)?

logic_exp0
Expand All @@ -113,7 +117,7 @@
: call_or_index (('*' | '/') call_or_index)*

call_or_index
: primary (primary | '@' primary)* (':' pattern)? /* named pattern when ': pattern' exists */
: primary (primary | '@' primary)* (':' pattern)? NOTE(" conditional pattern when ': pattern' exists ")

////////////////////////////////////////////////////////////////////////////////////////
// primary expressions/patterns
Expand All @@ -126,16 +130,16 @@
| FALSE
| NONE
| ID
| '*' call_or_index binding_list? /* pattern dereferencing */
| '*' call_or_index binding_list? NOTE(" pattern dereferencing ")
| NOT call_or_index
| MINUS call_or_index
| PLUS call_or_index
| '(' tuple_stuff ')' /* tuple/parenthesized expr */
| '[' list_stuff ']' /* list or list access */
| '(' tuple_stuff ')' NOTE(" tuple/parenthesized expr ")
| '[' list_stuff ']' NOTE(" list or list access ")
| function_const
| TYPEMATCH // TYPEMATCH == '%'<typename>

/* Note: binding lists are only supported for constraint patterns */
NOTE(" Note: binding lists are only supported for constraint patterns ")

binding_list
: BIND binding_list_suffix
Expand Down
Loading