/
mouse.trx
110 lines (83 loc) · 3.58 KB
/
mouse.trx
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
106
107
108
109
110
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
## Author: Adam Koprowski
## A TRX grammar for PEG grammar's in the Mouse format.
## Mouse is a PEG tool by Roman Redziejowski
## http://www.romanredz.se/freesoft.htm
## this grammar was (manuallly) transcribed from grammar.mouse
## (i.e. grammar.peg coming in the distribution of Mouse)
## after preforming certain simplifications on the grammar.
{{
open MouseAst
}}
/** Grammar **/
+Grammar : {MouseAst.mouse_grammar} <- Space (&. Rule)*:grammar EOT {{ grammar : mouse_grammar }}
/** Rule **/
Rule <- Name:name EQUAL RuleRhs:rhs DiagName?:desc SEMI {{ Rule(name, rhs, desc) }}
/** right-hand side **/
RuleRhs <- Sequence:seq Actions (SLASH Sequence:seq Actions {{seq}})*:seqs {{ seq::seqs }}
Choice <- Sequence:seq (SLASH Sequence:seq {{seq}})*:seqs {{ seq::seqs }}
Sequence <- Prefixed+
Prefixed <- PREFIX:prefix Space Primary:item SUFFIX:suffix Space {{ (prefix, item, suffix) }}
Primary <- / Name:name {{ Call name }}
/ LPAREN Choice:exp RPAREN {{ Parens exp }}
/ ANY {{ Any }}
/ StringLit:lit {{ Literal lit }}
/ Range:range {{ let (c1, c2) = range in Range(c1, c2) }}
/ CharClass:cc {{ CharClass cc }}
Actions <- OnSucc OnFail $
OnSucc <- (LWING AND? Name? RWING)? $
OnFail <- (TILDA LWING Name? RWING)? $
Name <- (Letter (Letter / Digit)* $_) Space {{ __1 }}
DiagName <- "<" ((!">" Char)+ $_) ">" Space {{ __2 }}
StringLit <- [\"] ((![\"] Char)+ $_):lit [\"] Space {{ lit }}
CharClass <- "[" (!"]" Char)+:chars "]" Space {{ chars }}
Range <- "[" Char:c1 "-" Char:c2 "]" Space {{ (c1, c2) }}
Char <- Escape
/ ![\r\n\\] .
Escape <- "\\u" (HexDigit HexDigit HexDigit HexDigit $_):hex
{{ let s = Base.String.from_hex hex in
assert(String.length s = 2 && Char.code (String.get s 0) = 0);
String.get s 1
}}
/ "\\t" {{ '\t' }}
/ "\\n" {{ '\n' }}
/ "\\r" {{ '\r' }}
/ !"\\u""\\" .:c {{ c }}
Letter <- [a-z] / [A-Z]
Digit <- [0-9]
HexDigit <- [0-9] / [a-f] / [A-F]
PREFIX <- [&] {{ `AND }}
/ [!] {{ `NOT }}
/ "" {{ `NORMAL }}
SUFFIX <- [?] {{ `OPTION }}
/ [*] {{ `STAR }}
/ [+] {{ `PLUS }}
/ "" {{ `NORMAL }}
EQUAL <- "=" Space {{ __1 }}
SEMI <- ";" Space {{ __1 }}
SLASH <- "/" Space {{ __1 }}
AND <- "&" Space {{ __1 }}
LPAREN <- "(" Space {{ __1 }}
RPAREN <- ")" Space {{ __1 }}
LWING <- "{" Space {{ __1 }}
RWING <- "}" Space {{ __1 }}
TILDA <- "~" Space {{ __1 }}
ANY <- "_" Space {{ __1 }}
Space <- ([ \r\n\t] $ / Comment)* $
Comment <- "//" (!EOL .)* EOL $
/* end of line */
EOL <- [\r\n] $ / !. $
/* end of text */
EOT <- !. $