/
trx.trx
115 lines (91 loc) · 4.19 KB
/
trx.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
111
112
113
114
115
(*
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/>.
*)
{{
open Parser_utils
}}
(**
{6 Auxiliary functions}
*)
let deco v = (=Opa_parser.deco(v));
pos <- Opa_parser.pos
spacing <- Opa_lexer.spacing
ml_identifier <- Opa_parser.ml_identifier
deco_ml_identifier <- Opa_parser.deco_ml_identifier
(**
{6 Main grammar}
*)
;Expression = deco(Expression_aux)
Expression_aux <- Opa_lexer.BAR? ParserSeq:e (Opa_lexer.BAR ParserSeq:e {{e}})*:l {{ Trx_ast.Expr (e::l) }}
ExpressionAsFunction <- Opa_lexer.PARSER Expression:expr Opa_lexer.END? {{ expr }}
Item = deco(Item_aux)
Item_aux <-
/ SubruleName?:name Prefix:prefix Primary:primary Suffix:suffix
{{ {Trx_ast.item_name=name; Trx_ast.item_prefix=prefix; Trx_ast.item_primary=primary; Trx_ast.item_suffix=suffix} }}
/ [~] AutobindIdentifierCall:e Suffix:suffix
(* the only place when ~ is allowed is when saying ~p or ~X.p*, but not ~{X.p}, not ~[abc], etc. *)
{{ let name,((_,label) as e) = e in
{Trx_ast.item_name=Some name; Trx_ast.item_prefix=`NORMAL; Trx_ast.item_primary=(Trx_ast.Rule e), label; Trx_ast.item_suffix=suffix} }}
ParserSeq = deco(ParserSeq_aux)
ParserSeq_aux <- pos:pos Item+:items ParserCode?:pc {{ {Trx_ast.seq_items=items; Trx_ast.seq_code=pc} }}
ParserCode <- Opa_lexer.rarrow Opa_parser.expr:e {{ e }}
SubruleName <- ml_identifier:v EQUAL {{ v }}
PrefixElement <- AND {{ `AND }}
/ NOT {{ `NOT }}
SuffixElement <- QUESTION {{ `QUESTION }}
/ STAR {{ `STAR }}
/ PLUS {{ `PLUS }}
Prefix <- PrefixElement?:v {{ Option.default `NORMAL v }}
Suffix <- SuffixElement?:v {{ Option.default `NORMAL v }}
Primary = deco(Primary_aux)
Primary_aux <-
/ Opa_parser.opa_in_braces:e {{ Trx_ast.Code e }}
/ IdentifierCall:e {{ Trx_ast.Rule e }}
/ OPEN Expression:exp CLOSE {{ Trx_ast.Parens exp }}
/ Literal:lit TILDE?:cs {{ Trx_ast.Literal(lit, cs=None) }}
/ String_expr.string_with_opa:e {{ Trx_ast.DynamicLiteral e }}
/ Class:cls {{ Trx_ast.Class cls }}
/ DOT {{ Trx_ast.Any }}
Class <- '[' (! ']' Range:r {{ r }})*:cls ']' {{ cls }}
Range <- RangeChar:c1 '-' RangeChar:c2 {{ `RANGE(c1, c2) }}
/ RangeChar:c {{ `ONE(c) }}
RangeChar <- "\\[" {{ Char.code '[' }}
/ "\\]" {{ Char.code ']' }}
/ "\\-" {{ Char.code '-' }}
/ Char
Char <- Opa_lexer.char_char
(**
{6 Lexical analysis}
*)
/** parser subrule call */
IdentifierCall <-
/ (=deco(Opa_parser.ident_call_dot))
/ deco_ml_identifier:n !EQUAL {{ let s,a = n in (ident s a : ('a,'b) SurfaceAst.expr) }}
/** parser subrule call with autobind */
AutobindIdentifierCall <- IdentifierCall:e {{( get_meaningful_called_ident e, e : string * (string,'b) SurfaceAst.expr) }}
(* forbid braces [{...}] in literals, if a literal contains braces then we want to interpret
it as an embedded OPA expression. Literal braces need escaping. *)
Literal <- ['] (!['{}] Char {{ __2 }})* ['] {{ BaseString.concat_map "" Cactutf.cons __2 }}
/ ["] (!["{}] Char {{ __2 }})* ["] {{ BaseString.concat_map "" Cactutf.cons __2 }}
AND <- spacing [&]
NOT <- spacing [!]
QUESTION <- spacing [?]
STAR <- spacing [*]
PLUS <- spacing [+]
OPEN <- [(] (* no spacing to avoid f(x) being parsed (which would mean f (~x -> x) which is not usually what the user meant) *)
CLOSE <- spacing [)]
DOT <- spacing [.]
TILDE <- [~] (* no spacing before this one not to mix "mlk"~ t and "mlk" ~f *)
EQUAL <- spacing [=]
COMMA <- spacing [,]
SEMI <- spacing [;]