Skip to content

Commit c23df9f

Browse files
committed
Added ANTLR grammar
1 parent 323fef5 commit c23df9f

2 files changed

Lines changed: 146 additions & 0 deletions

File tree

grammar/DiceNotation.g4

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2014-2019 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
/**
18+
* Dice notation grammar.
19+
*
20+
* This is the notation which RPGs and other tabletop games use to represent operations with dice.
21+
*/
22+
grammar DiceNotation;
23+
24+
options { tokenVocab=DiceNotationLexer; }
25+
26+
/**
27+
* Rules.
28+
*/
29+
30+
notation
31+
:
32+
dice
33+
| number
34+
| addOp
35+
;
36+
37+
addOp
38+
:
39+
multOp (ADDOPERATOR multOp)*
40+
;
41+
42+
multOp
43+
:
44+
operand (MULTOPERATOR operand)*
45+
;
46+
47+
operand
48+
:
49+
dice
50+
| number
51+
| LPAREN notation RPAREN
52+
;
53+
54+
dice
55+
:
56+
ADDOPERATOR? DIGIT? DSEPARATOR DIGIT
57+
;
58+
59+
number
60+
:
61+
ADDOPERATOR? DIGIT
62+
;

grammar/DiceNotationLexer.g4

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright 2014-2019 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
/**
18+
* Dice notation lexical rules.
19+
*/
20+
lexer grammar DiceNotationLexer;
21+
22+
/**
23+
* Tokens.
24+
*/
25+
26+
// Dice markers
27+
28+
DSEPARATOR
29+
:
30+
( 'd' | 'D' )
31+
;
32+
33+
DIGIT
34+
:
35+
('0'..'9')+
36+
;
37+
38+
// Operation tokens
39+
40+
ADDOPERATOR
41+
:
42+
( ADD | SUB )
43+
;
44+
45+
MULTOPERATOR
46+
:
47+
( MULT | DIV )
48+
;
49+
50+
fragment ADD
51+
:
52+
'+'
53+
;
54+
55+
fragment SUB
56+
:
57+
'-'
58+
;
59+
60+
fragment MULT
61+
:
62+
'*'
63+
;
64+
65+
fragment DIV
66+
:
67+
'/'
68+
;
69+
70+
LPAREN
71+
:
72+
'('
73+
;
74+
75+
RPAREN
76+
:
77+
')'
78+
;
79+
80+
// Skippable tokens
81+
82+
WS:
83+
[\t\r\n]+ -> skip
84+
;

0 commit comments

Comments
 (0)