Skip to content

Instantly share code, notes, and snippets.

@eddyb
Last active May 15, 2017 11:22
Show Gist options
  • Save eddyb/a1615ea186d0ce700e5d to your computer and use it in GitHub Desktop.
Save eddyb/a1615ea186d0ce700e5d to your computer and use it in GitHub Desktop.
## NOTE: Space between elements of syntax rules is used to
## signify that whitespace is permitted at that position.
whitespace: ([ \n] | '#'[^\n]*'\n')*
# "Tokens"
## x is a variable, xx a function, Xx an array, XX a constant.
array_name: [A-Z]([a-z][a-zA-Z.]*)?
const_name: [A-Z][A-Z][A-Z._]*
fn_name: [a-z][a-z][a-z._]*
var_name: [a-z]
## Literals
int_literal: '0''x'[a-fA-F0-9]+ | '0' | [1-9][0-9]*
char_literal: '\''.
literal: int_literal | char_literal
## Binary operators
word_op: [-+*&|^]
cmp_op: [!=<>]'=' | [<>]
# Grammar
program: array_name* const* (fn | predicate_fn)*
## Items
const: const_name literal
fn: fn_name '(' var_list? ')' ('-''>' fn_ret)? block?
fn_ret: '_' | '(' '_' (',' '_')+ ')'
predicate_fn: '?' fn_name '(' var_list? ')' bool_block?
var_list: var_name (',' var_name)*
## Statements
block: '{' (stmt ';')* expr? '}'
bool_block: '{' (stmt ';')* or_expr '}'
stmt: assignment | while_loop
assignment: (lvalue (',' lvalue)* (':''=' | '='))? expr
lvalue: var_name | array_name '[' expr ']'
while_loop: '@' or_expr block
## Expressions
expr: word_expr | cond_expr
cond_expr: ('?' or_expr block ':')* block
word_expr: leaf_expr (word_op leaf_expr)?
or_expr: and_expr ('|''|' and_expr)*
and_expr: bool_expr ('&''&' bool_expr)*
bool_expr: leaf_expr (cmp_op leaf_expr | '~' predicate)
leaf_expr: const_val
| lvalue
| '!' leaf_expr
| '(' expr (',' expr)* ')'
| fn_call
const_val: literal | const_name
fn_call: fn_name '(' (expr (',' expr)*)? ')'
## Predicates
predicate: const_val
| fn_call
| array_name '[' predicate (',' predicate)* ']'
## NOTE: Space between elements of syntax rules is used to
## signify that whitespace is permitted at that position.
whitespace: ([ \n] | '#'[^\n]*'\n')*
# "Tokens"
## x is a variable, xx a function, Xx an array, XX a constant.
array_name: [A-Z]([a-z][a-zA-Z.]*)?
const_name: [A-Z][A-Z][A-Z._]*
fn_name: [a-z][a-z][a-z._]*
var_name: [a-z]
## Literals
int_literal: '0''x'[a-fA-F0-9]+ | '0' | [1-9][0-9]*
char_literal: '\''.
literal: int_literal | char_literal
## Binary operators
word_op: [-+*&|^]
cmp_op: [!=<>]'=' | [<>]
# Grammar
program: array_name* const* fn*
## Items
const: const_name literal
fn: fn_name '(' var_list? ')' ('-''>' fn_ret)? block?
fn_ret: '_' | '(' '_' (',' '_')+ ')'
var_list: var_name (',' var_name)*
## Statements
block: '{' (stmt ';')* expr? '}'
stmt: assignment | while_loop
assignment: (lvalue (',' lvalue)* (':''=' | '='))? expr
lvalue: var_name | array_name '[' expr ']'
while_loop: '@' or_expr block
## Expressions
expr: word_expr | cond_expr
cond_expr: ('?' or_expr block ':')* block
word_expr: leaf_expr (word_op leaf_expr)?
or_expr: and_expr ('|''|' and_expr)*
and_expr: bool_expr ('&''&' bool_expr)*
bool_expr: leaf_expr cmp_op leaf_expr
leaf_expr: const_val
| lvalue
| '!' leaf_expr
| '(' expr (',' expr)* ')'
| fn_call
const_val: literal | const_name
fn_call: fn_name '(' (expr (',' expr)*)? ')'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment