Skip to content

Instantly share code, notes, and snippets.

@nathanmerrill
Created March 20, 2017 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanmerrill/a370ea47442e374e7db3dd21c472c7fc to your computer and use it in GitHub Desktop.
Save nathanmerrill/a370ea47442e374e7db3dd21c472c7fc to your computer and use it in GitHub Desktop.
grammar Elegance;
prog: eleStatement* EOF;
eleStatement: (e_class | function | assignment | function_call) ;
e_class :
CLASS_DECLARATION IDENTIFIER parameter_list?
type_parameters?
OPEN_CURLY
eleStatement*
CLOSE_CURLY;
function:
FUNCTION_DECLARATION IDENTIFIER parameter_list
COLON type
OPEN_CURLY
eleStatement*
CLOSE_CURLY;
assignment:
(VALUE_DECLARATION | VARIABLE_DECLARATION | OPTION_DECLARATION)? IDENTIFIER
(COLON type)?
EQUAL
(expression);
function_call: expression argument_list;
argument_list:
OPEN_PAREN
(argument
(COMMA argument)*
(COMMA named_argument)*
| named_argument
(COMMA named_argument)*
)?
CLOSE_PAREN;
argument: expression;
named_argument: IDENTIFIER EQUAL expression;
parameter_list:
OPEN_PAREN
(parameter
(COMMA parameter)*
)?
CLOSE_PAREN;
parameter:
expression
(COLON type)?;
type_parameters:
OPEN_ANGLE_BRACKET
IDENTIFIER
(COMMA IDENTIFIER )*
CLOSE_ANGLE_BRACKET ;
type_arguments:
OPEN_ANGLE_BRACKET
IDENTIFIER
(COMMA IDENTIFIER )*
CLOSE_ANGLE_BRACKET ;
trait_argument: IDENTIFIER (EQUAL INTEGER)? ;
trait_arguments:
OPEN_BRACKET
trait_argument
(COMMA trait_argument)*
CLOSE_BRACKET;
type: IDENTIFIER type_arguments? trait_arguments? ;
expression:
expression PERIOD IDENTIFIER |
expression argument_list |
expression OR expression |
expression AND expression |
expression (IS | EQ | NEQ) expression |
expression (LEQ | GEQ | LT | GT) expression |
expression (PLUS | MINUS) expression |
expression (MULT | DIV) expression |
OPEN_PAREN expression CLOSE_PAREN |
literal |
IDENTIFIER;
literal: INTEGER | FLOAT | list | tuple | dictionary | set | STRING ;
list: OPEN_BRACKET (expression (COMMA expression)* )? CLOSE_BRACKET ;
tuple: TUPLE_START (expression (COMMA expression)* )? CLOSE_BRACKET ;
set: SET_START (expression (COMMA expression)* )? CLOSE_BRACKET ;
dictionary: DICT_START (expression COLON expression (COMMA expression COLON expression)* )? CLOSE_BRACKET ;
INTEGER: '0' | ('+' | '-')? [1-9][0-9]* ;
FLOAT: ('0' | ('+' | '-')? [1-9][0-9]* ) '.' [0-9]+;
STRING: '"' ('\\'. | ~('"' | '\\'))* '"' | '\'' ('\\'. | ~('\'' | '\\'))* '\'';
SET_START: 's[';
TUPLE_START: 't[';
DICT_START: 'd[';
CLASS_DECLARATION : 'class' ;
FUNCTION_DECLARATION: 'fun' ;
VALUE_DECLARATION: 'val' ;
VARIABLE_DECLARATION: 'var' ;
OPTION_DECLARATION: 'option' ;
OPEN_CURLY: '{' ;
CLOSE_CURLY: '}' ;
OPEN_PAREN: '(' ;
CLOSE_PAREN: ')';
OPEN_ANGLE_BRACKET: '<' ;
CLOSE_ANGLE_BRACKET: '>';
OPEN_BRACKET: '[' ;
CLOSE_BRACKET: ']' ;
COLON: ':' ;
COMMA: ',';
PERIOD: '.' ;
EQUAL: '=';
OR: 'or';
AND: 'and';
IS: 'is';
EQ: 'eq';
NEQ: 'neq';
LEQ: 'leq';
GEQ: 'geq';
LT: 'lt';
GT: 'gt';
PLUS: '+';
MINUS: '-';
MULT: '*';
DIV: '/';
IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]*;
UNDERSCORE: '_';
WS: ('\n' | '\r' | '\t' | ' ')+ -> skip;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment