This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathparser.mly
112 lines (99 loc) · 3.03 KB
/
parser.mly
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
%{
open Parsetree
open Ast_helper
let (--) i j =
let rec aux n acc =
if n < i then acc else aux (n-1) (n :: acc)
in aux j []
let symbol_rloc start_pos end_pos = {
Location.loc_start = start_pos;
Location.loc_end = end_pos;
Location.loc_ghost = false;
}
let mklsexpr d sp ep = Lsexpr.mk ~loc:(symbol_rloc sp ep) d
let unlsexpr d = List.map (fun i -> i.lsexpr_desc) d
%}
%token LPAREN RPAREN LSQBR RSQBR NIL TRUE FALSE REST_ARGS EOF
%token <int> SQUOTE
%token <int> UNQUOTE
%token <int> INTEGER
%token <float> DOUBLE
%token <string> SYMBOL
%token <char> CHAR
%token <string> STRING
%start prog
%type <Parsetree.prog> prog
%type <Parsetree.atom> atom
%type <Parsetree.sexpr> sexpr
%type <Parsetree.sexpr> vsexpr
%type <Parsetree.sexpr> tsexpr
%type <Parsetree.sexpr list> tsexprs
%type <Parsetree.lsexpr list> sexprs
%%
prog:
sexprs EOF { Prog($1) }
atom:
NIL { Nil }
| TRUE { Bool(true) }
| FALSE { Bool(false) }
| INTEGER { Int($1) }
| DOUBLE { Double($1) }
| CHAR { Char($1) }
| STRING { String($1) }
| SYMBOL { Symbol($1) }
sexpr:
atom { Atom $1 }
| REST_ARGS SYMBOL { Atom (RestArgs $2) }
| LPAREN sexprs RPAREN { List (unlsexpr $2) }
vsexpr:
LSQBR sexprs RSQBR { Array (unlsexpr $2) }
| LSQBR RSQBR { Array [] }
tsexpr:
sexpr { $1 }
| vsexpr { $1 }
tsexprs:
tsexpr sexprs { $1::(unlsexpr $2) }
sexprs:
tsexpr { [mklsexpr $1 $startpos $endpos] }
| SQUOTE tsexpr
{
let towrap = $2 in
let w1 = List.fold_left (fun a b -> SQuote a) towrap (1--$1) in
[mklsexpr w1 $startpos $endpos]
}
| UNQUOTE tsexpr
{
let towrap = $2 in
let w1 = List.fold_left (fun a b -> Unquote a) towrap (1--$1) in
[mklsexpr w1 $startpos $endpos]
}
| SQUOTE UNQUOTE tsexpr
{
let towrap = $3 in
let w1 = List.fold_left (fun a b -> Unquote a) towrap (1--$2) in
let w2 = List.fold_left (fun a b -> SQuote a) w1 (1--$1) in
[mklsexpr w2 $startpos $endpos]
}
| tsexprs { List.map (fun i -> mklsexpr i $startpos $endpos) $1 }
| SQUOTE tsexprs
{
let towrap = List.hd $2 in
let w1 = List.fold_left (fun a b -> SQuote a) towrap (1--$1) in
(mklsexpr w1 $startpos $endpos)::
(List.map (fun i -> mklsexpr i $startpos $endpos) (List.tl $2))
}
| UNQUOTE tsexprs
{
let towrap = List.hd $2 in
let w1 = List.fold_left (fun a b -> Unquote a) towrap (1--$1) in
(mklsexpr w1 $startpos $endpos)::
(List.map (fun i -> mklsexpr i $startpos $endpos) (List.tl $2))
}
| SQUOTE UNQUOTE tsexprs
{
let towrap = List.hd $3 in
let w1 = List.fold_left (fun a b -> Unquote a) towrap (1--$2) in
let w2 = List.fold_left (fun a b -> SQuote a) w1 (1--$1) in
(mklsexpr w2 $startpos $endpos)::
(List.map (fun i -> mklsexpr i $startpos $endpos) (List.tl $3))
}