/
parser.clj
130 lines (97 loc) · 2.71 KB
/
parser.clj
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
(ns eu.dnetlib.dlms.parser
(:gen-class
:name eu.dnetlib.dlms.ClojureDQLParser
:implements [eu.dnetlib.dlms.jdbc.parser.IDQLParser]
)
(:use [eu.dnetlib.clojure.clarsec]
[eu.dnetlib.dlms.ast]
[eu.dnetlib.clojure.monad])
)
(declare instantiation invocation literal)
(declare xpath)
(def expression
(delay (either
instantiation
invocation
literal)))
(def stringLit
(>>== stringLiteral make-string-lit))
(def number
(>>== natural make-number-lit))
(def reference
(>>== identifier make-reference))
(def structureDef
(let-bind [label identifier
_ (symb "=")
val expression]
(result (make-struct-def label val))))
(def structure
(>>== (brackets (sepBy structureDef comma))
make-struct))
(def literal
(either structure number stringLit reference))
(def argList
(delay (sepBy expression comma)))
(def instantiation
(let-bind [_ (symb "new")
set identifier
args (parens argList)]
(result (make-instantiation set args))))
(def invocation
(let-bind [target identifier
_ (string ".")
method identifier
args (parens argList)]
(result (make-call target method args))))
(defn decl [typ]
(let-bind [name identifier
_ (symb "=")
e expression]
(result (make-decl-init typ name e))))
(defn assign [name]
(let-bind [_ (symb "=")
e expression]
(result (make-assign name e))))
(def predecl
(let-bind [name identifier]
(either (decl name) (assign name))))
(def tagname (either (symb ".")
(let-bind [attr (option "" (string "@"))
name (either identifier (symb "*"))]
(result (str attr name)))))
(def binaryPredicate
(delay
(let-bind [xp xpath
op (symb "=")
expr (either xpath (>>== expression make-xpath-expression))]
(result (make-binary-predicate op xp expr)))))
(def predicate (delay (either binaryPredicate (>>== xpath make-simple-predicate))))
(def tagexp
(delay
(let-bind [axis (optional (followedBy identifier (symb "::")))
tag tagname
pred (optional (brackets predicate))]
(result (make-tagexp axis tag pred)))))
(def xpath
(delay
(>>== (sepBy tagexp (symb "/"))
make-xpath)))
(def fieldList
(sepBy identifier comma))
(def select
(let-bind [_ (symb "select")
fields (option [] (parens fieldList))
xp xpath]
(result (make-select fields xp))))
(def statement
(either predecl
select
(>>== expression make-run-expr)))
(def body
(followedBy (sepBy1 statement semi) (optional semi)))
(def source
(followedBy body (lexeme eof)))
(defn -main []
(println (parse source "1")))
(defn -parse [this strn]
(:value (parse source strn)))