Skip to content

webdsl-ui.stx

pdmosses/webdsl-statix/webdslstatix/trans/static-semantics/webdsl-ui.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
module 

imports
  static-semantics/actions/functions

  static-semantics/entities/annotations

  static-semantics/types/built-ins

  static-semantics/ui/actions
  static-semantics/ui/ajax
  static-semantics/ui/attributes
  static-semantics/ui/built-ins
  static-semantics/ui/template-calls

  static-semantics/webdsl-actions
  static-semantics/webdsl-types
  static-semantics/webdsl

rules // unimplemented definitions

  defOk(_, TemplateDefinition(Define(_, t, _, _, _))) :- false | error $[Could not desugar define to an email, template or page definition] @t.
  defOk(_, TemplateDefinition(LocalRedefine(_, t, _, _, _))) :- try { false } | warning $[This definition is not yet implemented] @t.
  defOk(_, TemplateDefinition(DefineEmail(_, e, _, _, _))) :- try { false } | warning $[Email definitions are not yet implemented] @e.

  defOk(_, TemplateDefinition(HTMLWrapper2TemplateDefinition(_))) :- false | error $[Could not desugar to template definitions].
  defOk(_, TemplateDefinition(HTMLWrapperMultiple(_))) :- false | error $[Could not desugar to template definitions].

rules // page definitions

  defOk(, TemplateDefinition(DefinePage(, , FormalArgs(), TemplateArgs(), ))) :- {   }
    pageModsOk(s, mods),
    targs == [] | error $[Template arguments are not allowed on page definitions] @targs,
    new s_page, s_page -F-> s,
    fargTypes == typesOfArgs(s, fargs),
    declareParameters(s_page, zipArgTypes(fargs, fargTypes)),
    new s_pha, s_pha -P-> s_page,
    new s_body, s_body -P-> s_pha,
    optionallyDeclarePage(s, x, fargTypes, notB(isOverrideB(mods))),
    overriddenElementExists(s, Page(), x, FALSE(), isOverrideB(mods)),
    templateElementOrderOk(elements, FALSE(), FALSE()),
    templateElementsOk(s_body, s_pha, elements).

   : scope * string * list(TYPE) * BOOL
  optionallyDeclarePage(_, _, _, FALSE()).
  optionallyDeclarePage(s, x, fargTypes, TRUE()) :- declarePage(s, x, fargTypes).

  pageModsOk maps pageModOk(*, list(*))
   : scope * Modifier
  pageModOk(_, _) :- try { false } | warning $[This modifier is not yet implemented].
  pageModOk(_, AjaxTemplate()) :- false | error $[A page cannot be an ajax template].
  pageModOk(_, WebService()) :- false | error $[To define a webservice, use "webservice myWebservice(...) { ... }"].
  pageModOk(_, TemplateModOverride()).
  pageModOk(_, IgnoreAccessControl()).

rules // template definitions

  defOk(s, TemplateDefinition(td@DefineTemplate(_, _, _, _, _))) :- defineTemplateOk(s, td).

  // last BOOL defines if the template is top-level (TRUE) or nested (FALSE)
   : scope * TemplateDefinition
  defineTemplateOk(, DefineTemplate(, , FormalArgs(), TemplateArgs(targs), )) :- {    }
    templateModsOk(s, mods),
    new s_template, s_template -F-> s,
    fargTypes == typesOfArgs(s, fargs),
    targTypes == typesOfTemplateArgs(s, targs),
    declareParameters(s_template, zipArgTypes(fargs, fargTypes)),
    declTArgs(s_template, targTypes),
    new s_pha, s_pha -P-> s_template,
    new s_body, s_body -P-> s_pha,
    optionallyDeclareTemplate(s, t, fargTypes, targTypes, isAjaxTemplate(mods), notB(isOverrideB(mods))),
    overriddenElementExists(s, Template(), t, isAjaxTemplate(mods), isOverrideB(mods)),
    templateElementOrderOk(elements, FALSE(), FALSE()),
    templateElementsOk(s_body, s_pha, elements).

  templateModsOk maps templateModOk(*, list(*))
   : scope * Modifier
  templateModOk(_, _) :- try { false } | warning $[This modifier is not yet implemented].
  templateModOk(_, WebService()) :- false | error $[To define a webservice, use "webservice myWebservice(...) { ... }"].
  templateModOk(_, AjaxTemplate()).
  templateModOk(_, TemplateModOverride()).
  templateModOk(_, IgnoreAccessControl()).

   : scope * TemplateDefinition
  nestedTemplateOk(, DefineTemplate(_, , FormalArgs(), TemplateArgs(targs), )) :- {     }
    new s_template, s_template -P-> s, // use P edge label instead of F to get correct duplicate variable errors and resolve queries correctly
    fargTypes == typesOfArgs(s, fargs),
    targTypes == typesOfTemplateArgs(s, targs),
    declareParameters(s_template, zipArgTypes(fargs, fargTypes)),
    declTArgs(s_template, targTypes),
    sigs == resolveTemplate(s, t),
    matchingSigs(fargTypes, dropPaths(sigs)) == [_] | error $[No template [t] with argument types [fargTypes] exists] @t,
    new s_pha, s_pha -P-> s_template,
    new s_body, s_body -P-> s_pha,
    templateElementOrderOk(elements, FALSE(), FALSE()),
    templateElementsOk(s_body, s_pha, elements).

   : list(Modifier) -> BOOL
  isAjaxTemplate([]) = FALSE().
  isAjaxTemplate([_ | mods]) = isAjaxTemplate(mods).
  isAjaxTemplate([AjaxTemplate() | _]) = TRUE().

   : list(Modifier) -> BOOL
  isOverrideB([]) = FALSE().
  isOverrideB([_ | mods]) = isOverrideB(mods).
  isOverrideB([TemplateModOverride() | _]) = TRUE().

   : scope * Modifier * string * BOOL * BOOL
  overriddenElementExists(_, _, _, _, FALSE()).
  overriddenElementExists(s, Page(), , _, TRUE()) :- resolvePage(s, p) == [(_, (_, _))] | error $[Page [p] does not exist] @p.
  overriddenElementExists(s, Template(), , FALSE(), TRUE()) :- resolveTemplateNoAjax(s, t) == [(_, (_, _))] | error $[Template [t] does not exist] @t.
  overriddenElementExists(s, Template(), , TRUE(), TRUE()) :- resolveAjaxTemplate(s, t) == [(_, (_, _))] | error $[Ajax template [t] does not exist] @t.

   : scope * string * list(TYPE) * list((string * list(TYPE))) * BOOL * BOOL
  optionallyDeclareTemplate(_, _, _, _, _, FALSE()).
  optionallyDeclareTemplate(s, t, fargTypes, targTypes, ajax, TRUE()) :- declTemplate(s, t, fargTypes, targTypes, ajax).

   : scope * string * list(TYPE) * list((string * list(TYPE))) * BOOL
  declTemplate(, , , targTypes, ajax) :- {}
    declareTemplate_internal(s, t, fargTypes, ajax),
    resolveTemplate(s, t) == result,
    amountOfTemplateDeclsWithArgs(result, fargTypes, 0) == 1
      | error $[Multiple page/template definitions with name [t] and argument types [fargTypes]] @t. // correct error message for tests

   maps declTArg(*, list(*))
   : scope * (string * list(TYPE))
  declTArg(s, (t, argTypes)) :-
    declTemplate(s, t, argTypes, [], FALSE()).

  // helper function that results in the amount of template definitions with the given argument types
   : list((path *(string * TYPE))) * list(TYPE) * int -> int
  amountOfTemplateDeclsWithArgs([], _, n) = n.
  amountOfTemplateDeclsWithArgs([ (_, (_, TEMPLATE(_, args, _))) | tail], args, n) = amountOfTemplateDeclsWithArgs(tail, args, i) :- i #= n + 1.
  amountOfTemplateDeclsWithArgs([ _ | tail], args, n) = amountOfTemplateDeclsWithArgs(tail, args, n).

rules // template elements

   : scope * scope * scope * TemplateElement // TYPE argument is the return type in a function
  templateElementOk(_, _, _, e) :- try { false } | warning $[This template element is not yet implemented] @e.

  // the second scope (s_pha) is for declaring+resolving of placeholders and actions, which do not have to be declared before use
   : scope * scope * list(TemplateElement)
  templateElementsOk(_, _, []).
  templateElementsOk(, , [element | tail]) :- {}
    new s_next, s_next -P-> s,
    templateElementOk(s, s_next, s_pha, element),
    templateElementsOk(s_next, s_pha, tail).

  templateElementOk(s, _, _, Text(parts)) :-
    stringPartsOk(s, parts).

  templateElementOk(s, _, _,  XMLComment(_)).

  templateElementOk(s, _, _, XMLEmptyElement(_, attributes)) :-
    attributesOk(s, attributes).

  templateElementOk(s, _, _, XMLEmptyElementUnclosed(_, attributes)) :-
    attributesOk(s, attributes).

  templateElementOk(, _, s_pha, XMLElement(openTagName, attributes, elements, closeTagName)) :-
    openTagName == closeTagName | error $[Opening and closing tag do not match],
    attributesOk(s, attributes),
    templateElementsOk(s, s_pha, elements).

  templateElementOk(, _, _, JSElement(attributes, parts)) :-
    attributesOk(s, attributes),
    jsStringPartsOk(s, parts).

  templateElementOk(, _, _, StyleElement(attributes, parts)) :-
    attributesOk(s, attributes),
    jsStringPartsOk(s, parts).

   maps jsStringPartOk(*, list(*))
   : scope * JSPart
  jsStringPartOk(s, JSString(_)).
  jsStringPartOk(s, JSExp(exp)) :- typed(s, exp).

  templateElementOk(, _, , IfTempl(, then, else)) :-
    equalType(typeOfExp(s, c), bool(s)) | error $[Condition should be of type bool] @c,
    templateElementsOk(s, s_pha, then),
    templateElementsOk(s, s_pha, else).

  templateElementOk(s, _,  _, TemplateDefinition2TemplateElement(td)) :- nestedTemplateOk(s, td).

  templateElementOk(, _, s_pha, SubmitElem(a, props, elems)) :-
    actionCallOrInlineOk(s, a),
    templatePropAssignmentsOk(s, props),
    templateElementsOk(s, s_pha, elems).

  templateElementOk(, _, s_pha, SubmitLinkElem(a, props, elems)) :-
    actionCallOrInlineOk(s, a),
    templatePropAssignmentsOk(s, props),
    templateElementsOk(s, s_pha, elems).

  templateElementOk(, _, s_pha, DownloadLink(a, props, elems)) :-
    actionCallOrInlineOk(s, a),
    templatePropAssignmentsOk(s, props),
    templateElementsOk(s, s_pha, elems).

  templateElementOk(, _, s_pha, OutputImage(a, props, elems)) :-
    actionCallOrInlineOk(s, a),
    templatePropAssignmentsOk(s, props),
    templateElementsOk(s, s_pha, elems).

rules // expressions

  typeOfExp(s, TemplateName2Exp(TemplateName())) = string(s). // TO-DO: only allow this if inside a template

rules // loops

  templateElementOk(, _, , For(, srt, , OptFilterSome(f), elems, ForSeparator(separatorElems))) :- {  }
    typeOfSort == typeOfSort(s, srt),
    typeOfExp == typeOfExp(s, exp),
    or(
      equalTypeB(LIST(typeOfSort), typeOfExp),
      equalTypeB(SET(typeOfSort), typeOfExp)
    ) | error $[Must be a list or set of type [typeOfSort], [typeOfExp] given] @exp,
    new s_for, s_for -P-> s,
    forLoopFilterOk(s_for, f),
    forLoopOk(s_for, s_pha, x, typeOfSort, elems),
    separatorOk(s, s_pha, separatorElems),
    @x.type := typeOfSort.

  templateElementOk(, _, , ForInferred(, exp, OptFilterSome(f), elems, ForSeparator(separatorElems))) :- {  }
    typeOfExp == typeOfExp(s, exp),
    t == stripGenericType(typeOfExp),
    new s_for, s_for -P-> s,
    forLoopFilterOk(s_for, f),
    forLoopOk(s_for, s_pha, x, t, elems),
    separatorOk(s, s_pha, separatorElems),
    @x.type := t.

  templateElementOk(, _, , ForAll(, srt, OptFilterSome(f), elems, ForSeparator(separatorElems))) :- { }
    typeOfSort == typeOfSort(s, srt),
    new s_for, s_for -P-> s,
    forLoopFilterOk(s_for, f),
    forLoopOk(s_for, s_pha, x, typeOfSort, elems),
    separatorOk(s, s_pha, separatorElems),
    @x.type := typeOfSort.

  templateElementOk(, _, , ForCount(, fromExp, toExp, elems, ForSeparator(separatorElems))) :- { }
    intType == int(s),
    equalType(typeOfExp(s, fromExp), intType) | error $[From expression should have type Int],
    equalType(typeOfExp(s, toExp)  , intType) | error $[To expression should have type Int],
    new s_for, s_for -P-> s,
    forLoopOk(s_for, s_pha, x, intType, elems),
    separatorOk(s, s_pha, separatorElems),
    @x.type := intType.

   : scope * scope * string * TYPE * list(TemplateElement)
  forLoopOk(, s_pha, , t, stmts) :-
    declareVar(s_for, x, t),
    declareAnnotation(s_for, x, DERIVED()), // abuse derived annotation to declare immutability of x
    templateElementsOk(s_for, s_pha, stmts).

   : scope * scope * list(TemplateElement)
  separatorOk(s, s_pha, elems) :- {}
    new s_separator, s_separator -P-> s,
    templateElementsOk(s_separator, s_pha, elems).

rules // enforce correct order of template elements

  // correct order:
  // 1. var decls
  // 2. init block
  // 3. other template elements
   : list(TemplateElement) * BOOL * BOOL
  templateElementOrderOk([], _, _).
  templateElementOrderOk([x | xs], _, _). // TO-DO: verify if we want this check :- templateElementOrderOk(xs, TRUE(), TRUE()).

  templateElementOrderOk([TEVarDecl(v) | xs], , initDone) :-
    varDeclsDone == FALSE() | error $[Variable declarations must be at the start of a page or template] @v,
    templateElementOrderOk(xs, varDeclsDone, initDone).

  templateElementOrderOk([i@Init(_) | xs], _, ) :-
    initDone == FALSE() | error $[Init block must be between variable declarations and the rest of the page or template] @i,
    templateElementOrderOk(xs, TRUE(), initDone).

rules // template var args

  typeOfArg(s, Arg(_, TemplateVarArgSort(props))) = LIST(TEMPLATEVARARG(ts, )) :-
    new s_arg,
    ts == typesOfTemplateVarArgProps(s, s_arg, props).

  typesOfTemplateVarArgProps maps typeOfTemplateVarArgProp(*, *, list(*)) = list(*)
   : scope * scope * FormalVarArg -> TYPE
  typeOfTemplateVarArgProp(s, , FormalVarArg(, srt)) =  :-
    t == typeOfSort(s, srt),
    declareVar(s_arg, x, t),
    optionallyDeclareTemplateElements(s_arg, x, t).

   : scope * string * TYPE
  optionallyDeclareTemplateElements(_, _, _).
  optionallyDeclareTemplateElements(s_arg, x, TEMPLATEELEMENTS()) :-
    declareTemplate(s_arg, x, []).

  typeOfProperty(s, TEMPLATEVARARG(_, s_arg), x) = t :-
    propertyType(s_arg, x) == t.

  templateCallOk_internal(s, s_pha, MemberTemplateCall(, tc), ajax) :- {   }
    variableType(s, x) == TEMPLATEVARARG(_, s_arg) | error $[Variable [x] is not a template variable argument],
    new s_resolve, s_resolve -P-> s_arg,
    templateCallOk_internal(s_resolve, s_pha, tc, ajax).

  typeCompatibleB(TEMPLATEVARARG(ts1, _), TEMPLATEVARARG(ts2, _)) = b :-
    b == typesCompatible(ts1, ts2).

rules // utils

   : scope * string
  rootPageDefined(s, errorLoc) :- {   }
    resolvePage(s, "root") == [(_, (_, PAGE(p, ts)))|_] | error $[No root page defined] @errorLoc,
    noRootPageArguments(p, ts).

   : string * list(TYPE)
  noRootPageArguments(_, []).
  noRootPageArguments(p, [_ | _]) :- false | error $[Root page is not allowed to have parameters] @p.

   maps typeOfTemplateArg(*, list(*)) = list(*)
   : scope * TemplateArg -> (string * list(TYPE))
  typeOfTemplateArg(s, TemplateArg(x, argSorts)) = (x, typesOfSorts(s, argSorts)).