-
Notifications
You must be signed in to change notification settings - Fork 2
/
Numscript.g4
105 lines (86 loc) · 2.76 KB
/
Numscript.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
grammar Numscript;
// Tokens
WS: [ \t\r\n]+ -> skip;
NEWLINE: [\r\n]+;
MULTILINE_COMMENT: '/*' (MULTILINE_COMMENT | .)*? '*/' -> skip;
LINE_COMMENT: '//' .*? NEWLINE -> skip;
VARS: 'vars';
MAX: 'max';
SOURCE: 'source';
DESTINATION: 'destination';
SEND: 'send';
FROM: 'from';
UP: 'up';
TO: 'to';
REMAINING: 'remaining';
ALLOWING: 'allowing';
UNBOUNDED: 'unbounded';
OVERDRAFT: 'overdraft';
KEPT: 'kept';
SAVE: 'save';
LPARENS: '(';
RPARENS: ')';
LBRACKET: '[';
RBRACKET: ']';
LBRACE: '{';
RBRACE: '}';
COMMA: ',';
EQ: '=';
STAR: '*';
MINUS: '-';
RATIO_PORTION_LITERAL: [0-9]+ [ ]? '/' [ ]? [0-9]+;
PERCENTAGE_PORTION_LITERAL: [0-9]+ ('.' [0-9]+)? '%';
STRING: '"' ('\\"' | ~[\r\n"])* '"';
IDENTIFIER: [a-z]+ [a-z_]*;
NUMBER: MINUS? [0-9]+;
VARIABLE_NAME: '$' [a-z_]+ [a-z0-9_]*;
ACCOUNT: '@' [a-zA-Z0-9_-]+ (':' [a-zA-Z0-9_-]+)*;
ASSET: [A-Z/0-9]+;
monetaryLit:
LBRACKET (asset = literal) (amt = literal) RBRACKET;
portion:
RATIO_PORTION_LITERAL # ratio
| PERCENTAGE_PORTION_LITERAL # percentage;
literal:
ASSET # assetLiteral
| STRING # stringLiteral
| ACCOUNT # accountLiteral
| VARIABLE_NAME # variableLiteral
| NUMBER # numberLiteral
| monetaryLit # monetaryLiteral
| portion # portionLiteral;
functionCallArgs: literal ( COMMA literal)*;
functionCall: IDENTIFIER LPARENS functionCallArgs? RPARENS;
varOrigin: EQ functionCall;
varDeclaration:
type_ = IDENTIFIER name = VARIABLE_NAME varOrigin?;
varsDeclaration: VARS LBRACE varDeclaration* RBRACE;
program: varsDeclaration? statement* EOF;
sentAllLit: LBRACKET (asset = literal) STAR RBRACKET;
cap: monetaryLit # litCap | VARIABLE_NAME # varCap;
allotment:
portion # portionedAllotment
| VARIABLE_NAME # portionVariable
| REMAINING # remainingAllotment;
source:
address = literal ALLOWING UNBOUNDED OVERDRAFT # srcAccountUnboundedOverdraft
| address = literal ALLOWING OVERDRAFT UP TO maxOvedraft = literal # srcAccountBoundedOverdraft
| literal # srcAccount
| LBRACE allotmentClauseSrc+ RBRACE # srcAllotment
| LBRACE source* RBRACE # srcInorder
| MAX cap FROM source # srcCapped;
allotmentClauseSrc: allotment FROM source;
keptOrDestination:
TO destination # destinationTo
| KEPT # destinationKept;
destinationInOrderClause: MAX literal keptOrDestination;
destination:
literal # destAccount
| LBRACE allotmentClauseDest+ RBRACE # destAllotment
| LBRACE destinationInOrderClause* REMAINING keptOrDestination RBRACE # destInorder;
allotmentClauseDest: allotment keptOrDestination;
sentValue: literal # sentLiteral | sentAllLit # sentAll;
statement:
SEND sentValue LPARENS SOURCE EQ source DESTINATION EQ destination RPARENS # sendStatement
| SAVE sentValue FROM literal # saveStatement
| functionCall # fnCallStatement;