annotate tools/ncparse.mly @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 /* ncparse.mly */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 /* Copyright (c) 2017 J. M. Spivey */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 %token <string> WORD OPEN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 %token <char> CHAR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 %token LPAREN RPAREN CLOSE EOF COMMA SPACE ATSIGN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 %type <unit> text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 %start text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 %nonassoc LOW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 %nonassoc COMMA SPACE ATSIGN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 %nonassoc HIGH
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 %{
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 open Print
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 let outch ch = print_char ch
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 let out s = print_string s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 let out_frag f = printf "$" [f]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 let fJoin f1 f2 = fExt (fun prf -> prf "$$" [f1; f2])
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 %}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 %%
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 text :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 prog EOF { () } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 prog :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 /* empty */ { () }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 | prog WORD { out $2 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 | prog SPACE { out " " }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 | prog CLOSE { out ">" }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 | prog LPAREN { out "(" }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 | prog RPAREN { out ")" }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 | prog COMMA { out "," }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 | prog ATSIGN { out "@" }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 | prog CHAR { outch $2 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 | prog node { out_frag $2 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 node :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 OPEN bal1 args CLOSE { fMeta "(Node ($$, $))" [fStr $1; $2; $3] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 bal1 :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 bal %prec HIGH { $1 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 args :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 /* empty */ { fStr "[]" }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 | COMMA blank arglist { fMeta "[$]" [$3] }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 | COMMA blank ATSIGN bal { $4 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 arglist :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 bal %prec HIGH { $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 | arglist COMMA bal %prec HIGH { fMeta "$;$" [$1; $3] } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 bal :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 /* empty */ %prec LOW { fStr "" }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 | bal item { fJoin $1 $2 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 | bal LPAREN bal RPAREN { fMeta "$($)" [$1; $3] } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 item :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 WORD { fStr $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 | CHAR { fChr $1 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 | SPACE { fStr " " }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 | COMMA { fStr "," }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 | ATSIGN { fStr "@" }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 | node { $1 } ;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 blank :
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 /* empty */ %prec LOW { () }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 | SPACE { () } ;