forked from bobzhang/ocaml-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.tex
265 lines (188 loc) · 6.95 KB
/
notes.tex
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
Record with syntax
\begin{ocamlcode}
{ (s) with b = 4.1 };
\end{ocamlcode}
Dirac notation
I try to modify the ocaml syntax with camlp4 to add the Dirac notation
\verb\|v >\.
Have you any solution ?
I have tried with
\begin{ocamlcode}
EXTEND
expr : LEVEL "simple"
[["|"; e=expr ;">" -> <:expr< ket $e$>>]
];
END;;
\end{ocamlcode}
but I have the following message
\begin{ocamlcode}
|z>;;
^^
Parse error: [expr] expected after '>' (in [expr])
\end{ocamlcode}
I can replace \verb|>| by \verb|:| but it is not the dirac notation.
\begin{ocamlcode}
e = expr LEVEL "simple"
\end{ocamlcode}
You don't want to allow any expression here since it will be
ambiguous(\verb|3 > 4| will be parsed as an expr and the parser will
go on) with the '>' operator.
You even perhaps don't want an expression here |(f x y z)> seems not
really readable, you can allow only identifiers.
\begin{ocamlcode}
[["|"; id = LIDENT ;">" -> <:expr< ket $lid:id$>> ]]
\end{ocamlcode}
Hope this helps,
List quotation
> Before we just had to do this:
>
> <:ctyp< < $list:ml$ > >>
>
> Is there anything simpler than my solution? Is a shortcut available?
In fact there is some shortcuts but it was not for all constructions
(fixed now in CVS).
There is functions underlining these shortcuts like Ast.tySem_of_list
that join a list with semicolons.
I can explain briefly the change...
Before there was lists inside the tree. In order to allow a fully
reflective mechanism we now avoid these from the AST.
So there is now many more concrete syntax for any part of the AST
(match branches, let bindings, record field bindings...).
(p, None, e) -> <:match_case< $p$ -> $e$ >>
(p, Some guard, e) -> <:match_case< $p$ when $guard$ -> $e$ >>
(loc, field, true, typ) -> <:ctyp< { $lid:field$ : mutable $typ$ } >>
...
If m1 and m2 are to match branches (like (p, None, e) for the old one):
<:match_case< $m1$ | $m2$ >>
You want some default cases:
<:match_case< A -> 1 | $m1$ | B x -> x | $m2$ | _ -> assert False >>
So, when one wrote something like:
let cases = List.mapi (fun i (_,e) -> <:patt< $int:string_of_int i$
>> , None, e) brs in
let cases = cases @ [<:patt< _ >>, None, <:expr< raise Ulexing.Error >>] in
let actions = <:expr< match __ulex_state_0 lexbuf with [ $list:cases$ ] >> in
We now can write:
let cases = List.mapi (fun i (_,e) -> <:match_case< $`int:i$ -> $e$ >>) brs in
<:expr< match __ulex_state_0 lexbuf with
[ $list:cases$
| _ -> raise Ulexing.Error ] } >>
Where the error case is inlined in the match directly.
This <:patt< $int:string_of_int i$ >>, None, e is a typical thing
where no syntax were available to it.
Now you can write <:match_case< $`int:i$ -> $e$ >> since any part of
the abstract syntax have a quotation in concrete syntax.
You can note that in `` [ $list:cases$ '' I used the $list:...$
antiquotation that was used to inject some list in a tree. In 3.10 you
can still do that, but that's a sugar for a function call that will
join all your trees by the proper separator (bar here).
Generally all flags: private, mutable, virtual, rec, .., to/downto now
have their own antiquotation.
<:expr< let $rec:is_rec$ f f = f in f >>
<:class_str_item< value $mutable:is_mutable$ x = 42 >>
<:ctyp< < meth1 : int ; meth2 : float $..:raw_variable$ > >>
...
However there is some exceptions to that rule.
A private type is a type surrounded by a node "private".
That's now the same thing for mutable types.
You can define this function if you want:
let mkmutable is_mutable = if is_mutable then <:ctyp< mutable $t$ >> else t
Or rewrite a little the code to take a better profit of this change.
BTW: camlp4/Camlp4Parsers/Camlp4OCamlRevisedParser.ml contains many of
your answers.
However if it's just a matter of patching the token stream you can do
it more easily by adding a token filter (Camlp4.Sig.Token.Filter).
With camlp4 3.10.0+beta, the following syntax extension does not work, but it used to work in the "old" camlp4.
File pa_toto.ml:
----------------
(* ocamlc -c -I +camlp4 -pp camlp4orf pa_toto.ml *)
open Camlp4.PreCast
open Syntax
EXTEND Gram
GLOBAL: str_item;
str_item: LEVEL "top" [
[ "TEST"; "{"; l = item_list; "}" -> <:str_item< >> ]
];
item_list: [
[ x = item; ";"; l = SELF -> x :: l
| x = item; ";" -> [x]
| x = item -> [x] ]
];
item: [
[ mut = OPT "mutable"; name = LIDENT -> () ]
];
END
IMHO having rules starting with OPT don't fit very well with the
camlp4 parsing mechanism.
It works in the 3.09 version but I don't know why.
I think that you shouldn't rely on that anymore.
In 3.09 we used to allow defining "infix" keywords by using antiquotations in inside the "EXTEND" and "DELETE_RULE" directives as follows:
--------------------------------------------------------------
(*
* The prefix version of the infix expression
*)
let prefix_name op =
"prefix_" ^ op
(*
* Add an infix keyword.
*)
let add_infix (keyword : string) =
EXTEND
GLOBAL: expr;
expr: LEVEL "expr1"
["expr1" LEFTA
[ e1 = expr; op = $keyword$; e2 = expr ->
<:expr< $lid:prefix_name op$ $e1$ $e2$ >>
]];
END
(*
* Remove the infix keyword.
*)
let remove_infix (keyword : string) =
DELETE_RULE
expr:
expr; $keyword$; expr
END
--------------------------------------------------------------
This does not seem to be supported in 3.10. Is there some other way to achieve a similar effect in 3.10? Thanks!
Aleksey
You can try something like
.... op = KEYWORD $x$ ...
This is somewhat different in the new version of Camlp4 even if one
cannot dynamically extend the default lexer.
1/ One can now change a Camlp4 module by another, since the
implementation is functorized.
2/ The default lexer now keeps all bits of the input including layout
information.
3/ One can setup a custom token filter, that can handle the
indentation, and then drop the layout information (The default filter
drops layout information).
However the new Camlp4 implementation provided in 3.10 lift that
shortcoming by giving a full reflection system. So if you want to use
the original syntax everywhere you can use camlp4of that provided
quotation (<:expr<...>>, patt, ctyp...) in the original syntax
including syntactic sugars.
Here is a 3.10 valid extension:
$ cat ext.ml
open Camlp4.PreCast;;
open Syntax;;
EXTEND Gram
expr: LEVEL "top"
[[ "yield"; e1 = expr LEVEL "simple";
"continue"; e2 = expr LEVEL "simple" ->
<:expr< [< $e1$; $e2$ >] >> ]];
END;;
% $
I don't know which classes you want to register with the Objective-C
runtime, but an option is to require users to write .mli files (not
too hard since they can be generated with ocamlc -i).
Then you can use camlp4 to parse the .mli and generate the
registration code to be included in the .ml file. You just have to be
careful about "open" statements.
Since they are all built using ocamlbuild, there's no \verb|open|
statement actually.
Camlp5 Lexer -- todo
ulex
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: