Skip to content

webdsl.stx

pdmosses/webdsl-statix/webdslstatix/trans/static-semantics/webdsl.stx

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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
module 

imports
  signatures/webdsl-statix-sig

  static-semantics/actions/built-ins
  static-semantics/entities/built-ins
  static-semantics/types/built-ins
  static-semantics/ui/built-ins

  static-semantics/entities/annotations

  static-semantics/webdsl-ac
  static-semantics/webdsl-actions
  static-semantics/webdsl-entities
  static-semantics/webdsl-expand
  static-semantics/webdsl-hql
  static-semantics/webdsl-modules
  static-semantics/webdsl-native
  static-semantics/webdsl-regex
  static-semantics/webdsl-search
  static-semantics/webdsl-services
  static-semantics/webdsl-types
  static-semantics/webdsl-ui

signature

  sorts
       // semantic type
       // used as return values of functional rules
     // dummy data: used to guide scope graph differ

  constructors
     : BOOL
     : BOOL

    : list(FormalArg) -> ORIGIN
    : list(NativeType) -> ORIGIN
    : list(TYPE) -> ORIGIN
    : ORIGIN // prop names should be unique. TODO: how about non-unique properties in incorrect programs?
    : int -> ORIGIN  // unary identity-derived function

  name-resolution
    labels
               // parent edge label for consecutive statements
               // function edge label for embedding functions, templates and pages in a scope
          // extend edge label for extending entities
         // inherit edge label for subclasses
             // definition edge label for embedding top-level definitions in the module scope
          // import edge label for importing another module
       // import edge label for importing statix libraries such as the built-in.app library
          // global edge label for edges from global scope to other modules

  relations
     : list(string) * scope

     : string * scope

     : string * scope

         : string * scope
     : string * scope
           : string * scope

     : string * ORIGIN * scope

          : string * scope

     : TYPE

rules // module declaration and resolving

   : scope * list(string) * scope
  declareMod(, , s_mod) :-
    !mod[m, s_mod] in s,
    resolveMod(s, m) == [_] | error $[Module [m] is already declared].

   : scope * list(string) -> list((path * (list(string) * scope)))
  resolveMod(s, m) = ss :-
    query mod filter e
              and { m' :- m' == (m, _) }
              in s |-> ss.

   : scope * list(string) -> list((path * (list(string) * scope)))
  resolveModWildcard(s, names) = /*modsOf(*/ ss /*)*/ :-
    query mod filter e
              and { m' :- isPrefix(names, m') }
              in s |-> ss.

   : list(string) * (list(string) * scope)
  isPrefix(names, (names', _)) :- isPrefix_internal(names, names').

   : list(string) * list(string)
  isPrefix_internal([], _).
  isPrefix_internal([x | xs], [x | ys]) :- isPrefix_internal(xs, ys).
  isPrefix_internal(_, _) :- false.

rules // type declaration and resolving

   : scope * string * TYPE
  declareType(, , ) :-
    !type[name, withType(t)] in s,
    resolveType(s, name) == [(_, (_, t))] | error $[Type [name] is defined multiple times] @name.

   : scope * string -> list((path * (string * TYPE)))
  resolveType(s, name) = typesOf(ts) :-
    query type filter P* F* ((EXTEND? INHERIT*) | (DEF? (IMPORT | IMPORTLIB)?))
               and { e' :- e' == (name, _) }
               min $ < IMPORTLIB, P < IMPORTLIB, F < IMPORTLIB, EXTEND < IMPORTLIB, INHERIT < IMPORTLIB, DEF < IMPORTLIB, IMPORT < IMPORTLIB
               and true
               in s |-> ts.

   : TYPE -> scope
  withType(t) =  :-
    new s, !typeOf[t] in s.

   maps typeOf(list(*)) = list(*)
   : (path * (string * scope)) -> (path * (string * TYPE))
  typeOf((p, (x, s))) = (p, (x, t)) :-
    query typeOf filter e in s |-> [(_, t)].

   maps typeOfStripOrigin(list(*)) = list(*)
   : (path * (string * ORIGIN * scope)) -> (path * (string * TYPE))
  typeOfStripOrigin((p, (x, _, s))) = (p, (x, t)) :-
    query typeOf filter e in s |-> [(_, t)].

   : scope -> TYPE
  simpleTypeOf(s) = t :-
    query typeOf filter e in s |-> [(_, t)].

rules // extend entity declaration

   : scope * string * scope
  declareExtendScope(s, e, s_extend) :-
    !extendscope[e, s_extend] in s.

   : scope * string -> list((path * (string * scope)))
  resolveExtendScope(s, x) = ps :-
    query extendscope filter P* F* DEF? (IMPORT | IMPORTLIB)? GLOBAL?
                      and { e' :- e' == (x, _) }
                      min $ < P, $ < F, $ < DEF, $ < IMPORT, $ < IMPORTLIB, $ < GLOBAL,
                          P < F, P < DEF, P < IMPORT, P < IMPORTLIB, P < GLOBAL,
                          F < DEF, F < IMPORT, F < IMPORTLIB, F < GLOBAL,
                          DEF < IMPORT, DEF < IMPORTLIB, DEF < GLOBAL,
                          IMPORT < GLOBAL, IMPORTLIB < GLOBAL
                      and { (entity, entity_scope), (entity, entity_scope) }
                      in s |-> ps.

rules // ui declaration and resolving

   : scope * string * list(TYPE)
  declareTemplate(s, t, ts) :- declareTemplate_internal(s, t, ts, FALSE()).

  declareAjaxTemplate : scope * string * list(TYPE)
  declareAjaxTemplate(s, t, ts) :- declareTemplate_internal(s, t, ts, TRUE()).

   : scope * string * list(TYPE) * BOOL
  declareTemplate_internal(, , , ajax) :-
    !template[t, withType(TEMPLATE(t, ts, ajax))] in s,
    resolvePage(s, t) == [] | error $[Multiple page/template definitions with name [t]] @t,
    filterTemplateResultsArgs(resolveTemplate(s, t), ts) == [_] | error $[Multiple page/template definitions with name [t] and argument types [ts]] @t. // correct error message for tests

   : scope * string * list(TYPE)
  declarePage(, , ts) :-
    !page[p, withType(PAGE(p, ts))] in s,
    resolveTemplate(s, p) == [] | error $[Multiple page/template definitions with name [p]] @p, // correct error message for tests
    resolvePage(s, p) == [_] | error $[Multiple page/template definitions with name [p]] @p. // correct error message for tests

   : scope * string -> list((path * (string * TYPE)))
  resolveTemplate(s, t) = resolveTemplate_internal(s, t, FALSE()).

   : scope * string -> list((path * (string * TYPE)))
  resolveAjaxTemplate(s, t) = resolveTemplate_internal(s, t, TRUE()).

   : scope * string * BOOL -> list((path * (string * TYPE)))
  resolveTemplate_internal(s, t, FALSE()) = typesOf(ps) :-
    query template filter P* F* ((EXTEND? (INHERIT EXTEND?)*) | (DEF? (IMPORT | IMPORTLIB)?))
                   and { t' :- t' == (t, _) }
                   in s |-> ps.

  resolveTemplate_internal(s, t, TRUE()) = filterTemplateResultsAjax(resolveTemplate_internal(s, t, FALSE()), TRUE()).

   : scope * string -> list((path * (string * TYPE)))
  resolveTemplateNoAjax(s, t) = filterTemplateResultsAjax(resolveTemplate_internal(s, t, FALSE()), FALSE()).

   : list((path * (string * TYPE))) * BOOL -> list((path * (string * TYPE)))
  filterTemplateResultsAjax([], _) = [].
  filterTemplateResultsAjax([t@(_, (_, TEMPLATE(_, _, b))) | ts], b) = [t | filterTemplateResultsAjax(ts, b)].
  filterTemplateResultsAjax([_ | ts], b) = filterTemplateResultsAjax(ts, b).

   : list((path * (string * TYPE))) * list(TYPE) -> list((path * (string * TYPE)))
  filterTemplateResultsArgs([], _) = [].
  filterTemplateResultsArgs([t@(_, (_, TEMPLATE(_, args, _))) | ts], args) = [t | filterTemplateResultsArgs(ts, args)].
  filterTemplateResultsArgs([_ | ts], args) = filterTemplateResultsArgs(ts, args).

   : scope * string -> list((path * (string * TYPE)))
  resolvePage(s, p) = typesOf(ps) :-
    query page filter P* F* ((EXTEND? (INHERIT EXTEND?)*) | (DEF? (IMPORT | IMPORTLIB)?))
               and { p' :- p' == (p, _) }
               in s |-> ps.

rules // ac rules

   : scope * string * list(TYPE)
  declarePointcut(, , ts) :-
    !ac[p, withType(POINTCUT(p, ts))] in s,
    resolvePointcut(s, p) == [_] | error $[Pointcut [p] is defined multiple times] @p.

   : scope * string -> list((path * (string * TYPE)))
  resolvePointcut(s, p) = typesOf(ps) :-
    query ac filter P* F* ((EXTEND? (INHERIT EXTEND?)*) | (DEF? (IMPORT | IMPORTLIB)?))
             and { p' :- p' == (p, _) }
             in s |-> ps.

rules // function declaration and resolving

   : scope * string * ORIGIN * list(TYPE) * TYPE
  declareFunction(s, f, origin, args, return) :- declareFunction_internal(s, f, origin, args, return, FALSE()).

   : scope * string * ORIGIN * list(TYPE) * TYPE
  declareStaticFunction(s, f, origin, args, return) :- declareFunction_internal(s, f, origin, args, return, TRUE()).

   : scope * string * ORIGIN * list(TYPE) * TYPE * BOOL
  declareFunction_internal(s, , origin, args, return, static) :- {  }
    s_type == withType(FUNCTION(f, args, return, static)),
    !function[f, origin, s_type] in s.

   : scope * string -> list((path * (string * TYPE)))
  resolveFunction(s, f) = filterFunctionResults(typesOfStripOrigin(ps), FALSE()) :-
    query function filter P* F* ((EXTEND? (INHERIT EXTEND?)*) | (DEF? (IMPORT | IMPORTLIB)?))
                   and { f' :- f' == (f, _, _) }
                   in s |-> ps.

   : scope * string -> list((path * (string * TYPE)))
  resolveEntityFunction(s, x) = filterFunctionResults(typesOfStripOrigin(ps), FALSE()) :-
    query function filter EXTEND? (INHERIT EXTEND?)*
                   and { x' :- x' == (x, _, _) }
                   min $ < INHERIT, EXTEND < INHERIT // do not shadow EXTEND with $
                   and { (, _, T1), (f, _, T2) :- {}
                     simpleTypeOf(T1) == FUNCTION(f, args, _, _),
                     simpleTypeOf(T2) == FUNCTION(f, args, _, _)
                   } // only shadow when function has the same name and argument types
                   in s |-> ps.

   : scope * string -> list((path * (string * TYPE)))
  resolveStaticEntityFunction(s, x) = filterFunctionResults(typesOfStripOrigin(ps), TRUE()) :-
    query function filter EXTEND? (INHERIT EXTEND?)*
                   and { x' :- x' == (x, _, _) }
                   min $ < INHERIT, EXTEND < INHERIT // do not shadow EXTEND with $
                   and { (f, _, T1), (, _, T2) :- {}
                     simpleTypeOf(T1) == FUNCTION(f, args, _, _),
                     simpleTypeOf(T2) == FUNCTION(f, args, _, _)
                   } // only shadow when function has the same name and argument types
                   in s |-> ps.

  // last BOOL arg indiciates if only static function should be returned (TRUE()) or all functions (FALSE())
   : list((path * (string * TYPE))) * BOOL -> list((path * (string * TYPE)))
  filterFunctionResults([], _) = [].
  filterFunctionResults([f@(_, (_, FUNCTION(_, _, _, TRUE()))) | fs], b@TRUE()) = [f | filterFunctionResults(fs, b)].
  filterFunctionResults([f@(_, (_, FUNCTION(_, _, _, _))) | fs], b@FALSE()) = [f | filterFunctionResults(fs, b)].
  filterFunctionResults([_ | fs], b) = filterFunctionResults(fs, b).

rules // action and placeholder declaration and resolving

   : scope * string * ORIGIN * list(TYPE)
  declareAction(, , origin, ) :-
    !function[a, origin, withType(ACTION(a, ts))] in s,
    resolveAction(s, a) == [(_, (_, ACTION(a, ts)))] | error $[Action [a] is defined multiple times] @a.

   : scope * string -> list((path * (string * TYPE)))
  resolveAction(s, a) = filterActionResults(typesOfStripOrigin(as)) :-
    query function filter P*
                   and { a' :- a' == (a, _, _) }
                   in s |-> as.

   : scope * string
  declarePlaceholder(, ) :-
    !var[ph, withType(placeholder(s))] in s,
    noDuplicateVarDefs(s, ph) | error $[Variable [ph] is defined multiple times] @ph.

   : scope * string -> list((path * (string * TYPE)))
  resolvePlaceholder(s, ph) = result :- {   }
    query var filter P*
              and { ph' :- ph' == (ph, _) }
              in s |-> ph_scopes,
    phs == typesOf(ph_scopes),
    result == filterPlaceholderResults(phs).

   : list((path * (string * TYPE))) -> list((path * (string * TYPE)))
  filterActionResults([]) = [].
  filterActionResults([a@(_, (_, ACTION(_, _))) | as]) = [a | filterActionResults(as)].
  filterActionResults([_ | as]) = filterActionResults(as).

   : list((path * (string * TYPE))) -> list((path * (string * TYPE)))
  filterPlaceholderResults([]) = [].
  filterPlaceholderResults([ph@(_, (_, BUILTINTYPE("Placeholder", _))) | phs]) = [ph | filterPlaceholderResults(phs)].
  filterPlaceholderResults([_ | phs]) = filterPlaceholderResults(phs).

rules // variable declaration and resolving

   : scope * string * TYPE
  declareVar(, , t) :-
    !var[x, withType(t)] in s,
    noDuplicateVarDefs(s, x) | error $[A variable named [x] already exists in this scope].

   : scope * string -> list((path * (string * TYPE)))
  resolveVar(s, x) = typesOf(ps) :-
    query var filter P* F* ((EXTEND? (INHERIT EXTEND?)*) | (DEF? (IMPORT | IMPORTLIB)?))
              and { x' :- x' == (x, _) }
              min $ < P, P < F, F < EXTEND, EXTEND < INHERIT, INHERIT < DEF, DEF < IMPORT, IMPORT < IMPORTLIB
              and true
              in s |-> ps.

   : scope * string -> list((path * (string * TYPE)))
  resolveProperty(s_entity, x) = typesOf(ps) :-
    query var filter EXTEND? (INHERIT EXTEND?)*
              and { x' :- x' == (x, _) }
              in s_entity |-> ps.

   : scope * string -> list((path * (string * TYPE)))
  resolveLocalProperty(s_entity, x) = typesOf(ps) :-
    query var filter EXTEND?
              and { x' :- x' == (x, _) }
              in s_entity |-> ps.

  resolveMutableProperty : scope * string -> list((path * (string * TYPE)))
  resolveMutableProperty(s_entity, x) = typesOf(ps) :-
    query var filter EXTEND? (INHERIT EXTEND?)*
              and { x' :- x' == (x, _) }
              in s_entity |-> ps.

   : scope * list((string * TYPE))
  declareParameters(s, []).
  declareParameters(, [(, t)|tail]) :-
    declareVar(s, x, t),
    declareAnnotation(s, x, DERIVED()), // abuse derived annotation to declare immutability of parameters
    declareParameters(s, tail).

  declareParameters(, [(, t@TEMPLATEELEMENTS()) | tail]) :-
    declareVar(s, x, t),
    declareAnnotation(s, x, DERIVED()), // abuse derived annotation to declare immutability of parameters
    declareTemplate(s, x, []),
    declareParameters(s, tail).

rules // declare common rules and mappings

   : scope * Unit
  unitOk(_, EmptyUnit()).

   maps sectionOk(*, list(*))
   : scope * Section
  sectionOk(_, ACPolicy(_)) :- try { false } | warning $[This section is not yet implemented].
  sectionOk(s, Section(_, defs)) :- defsOk(s, defs).

  defsOk maps defOk(*, list(*))
   : scope * Definition
  defOk(_, Description(_)) :- try { false } | warning $[This definition is not yet implemented].
  defOk(_, Note(_)) :- try { false } | warning $[This definition is not yet implemented].
  defOk(_, Routing(_)) :- try { false } | warning $[This definition is not yet implemented].

   : scope * scope * Statement * TYPE // TYPE argument is the return type in a function

   : scope * list(Statement) * TYPE
  stmtsOk(_, [], _).
  stmtsOk(, [stmt | tail], ) :- { }
    new s_decl, s_decl -P-> s,
    new s_next, s_next -P-> s_decl,
    stmtOk(s, s_decl, stmt, rt),
    stmtsOk(s_next, tail, rt).

   : scope * Exp
  expOk(s, exp) :- { T }
    typeOfExp(s, exp) == T.

   : scope
  declareBuiltIns() :-
//    declareACBuiltIns(s), do this separately in Application instead of built-in
    declareFunctionIns(s),
    declareEntityBuiltIns(s),
    declareSearchBuiltIns(s),
    declareTypeBuiltIns(s),
    declareTypeBuiltInFunctions(s),
    declareUIBuiltIns(s).

rules // typing

   maps typeOfExp(*, list(*)) = list(*)
   : scope * Exp -> TYPE

   : scope * Exp -> TYPE
  typeOfExpTyped(s, ) =  :-
    t == typeOfExp(s, exp),
    t != UNTYPED() | error $[Cannot resolve type [t]] @exp.

   : scope * SimpleExp -> TYPE
  typeOfSimpleExp(s, exp) = UNTYPED() :- try { false } | warning $[typing of expression [exp] is not yet implemented].

   : scope * PHExp -> TYPE
  typeOfPlaceholderExp(s, exp) = UNTYPED() :- try { false } | warning $[typing of expression [exp] is not yet implemented].

   : list(TYPE) * list(TYPE) -> BOOL
  typesCompatible([], []) = TRUE().
  typesCompatible([], [_|_]) = FALSE().
  typesCompatible([_|_], []) = FALSE().
  typesCompatible([t1|t1s], [t2|t2s]) = andB(typeCompatibleB(t1, t2), typesCompatible(t1s, t2s)).

   maps typeCompatible(list(*), *)
   : TYPE * TYPE
  typeCompatible(T1, T2) :- typeCompatibleB(T1, T2) == TRUE().

   : TYPE * TYPE -> BOOL
  typeCompatibleB(T1, T2) = FALSE(). //default
  typeCompatibleB(T, T) = TRUE(). // same type is always type compatible

rules // utils

   : BOOL * BOOL
  or(b1, b2) :- orB(b1, b2) == TRUE().
   : BOOL * BOOL -> BOOL
  orB(_, _) = FALSE().
  orB(TRUE(), _) = TRUE().
  orB(FALSE(), TRUE()) = TRUE().

   : BOOL * BOOL -> BOOL
  andB(_, _) = FALSE().
  andB(TRUE(), TRUE()) = TRUE().

   : BOOL * BOOL
  equalB(b, b).
  equalB(_, _) :- false.

   : BOOL -> BOOL
  notB(TRUE()) = FALSE().
  notB(FALSE()) = TRUE().

   : list((path * (string * TYPE))) -> BOOL
  emtpyQueryResultB([]) = TRUE().
  emtpyQueryResultB([_ | _]) = FALSE().

   : list(int) -> int
  minOfList([]) = 100000.
  minOfList([x|xs]) = z :- {y}
    y == minOfList(xs),
    z #= min(x,y).

   : path -> scope
  scopeFromPath(_PathEmpty(s)) = s.
  scopeFromPath(_PathStep(_, _, s)) = s.

   : path -> int
  pathLength(_PathEmpty(_)) = 0.
  pathLength(_PathStep(p, _, _)) = x :- {x'}
    x' == pathLength(p),
    x #= x' + 1.

   : string * list(string)
  noDuplicatesString(_, []).
  noDuplicatesString("_IGNORE", _).
  noDuplicatesString(x, ["_IGNORE" | ys]) :- noDuplicatesString(x, ys).
  noDuplicatesString(x, [y | ys]) :- noDuplicatesString(x, ys).
  noDuplicatesString(x, [x | _]) :- false.

   : list((path * (string * TYPE))) * list((path * (string * TYPE))) -> list((path * (string * TYPE)))
  addQueryResults([], ls) = ls.
  addQueryResults([hd | tl], ls) = [hd | addQueryResults(tl, ls)].

  // functional rule to drop all results with longer paths than the minimal
  filterShortestPath : list((path * (string * TYPE))) -> list((path * (string * TYPE)))
  filterShortestPath(ps) = filterShortestPath_internal(zipQueryResultsWithPathLength(ps), shortestPathLength(ps)).

   : list((int * (path * (string * TYPE)))) * int -> list((path * (string * TYPE)))
  filterShortestPath_internal([], _) = [].
  filterShortestPath_internal([(x, r) | ps], x) = [r | filterShortestPath_internal(ps, x)].
  filterShortestPath_internal([_      | ps], x) = filterShortestPath_internal(ps, x).

   : list((path * (string * TYPE))) -> int
  shortestPathLength([]) = 10000.
  shortestPathLength([(p, _) | ps]) = z :- {x y}
    x == pathLength(p),
    y == shortestPathLength(ps),
    z #= min(x, y).

  zipQueryResultsWithPathLength maps zipQueryResultWithPathLength(list(*)) = list(*)
   : (path * (string * TYPE)) -> (int * (path * (string * TYPE)))
  zipQueryResultWithPathLength(r@(p, _)) = (pathLength(p), r).

   maps queryResultType(list(*)) = list(*)
   : (path * (string * TYPE)) -> TYPE
  queryResultType((_, (_, t))) = t.

  argNames maps argName(list(*)) = list(*)
   : FormalArg -> string
  argName(Arg(x, _)) = x.