Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 8fee1675cb01f2fd8aea3ba18597be10b5327666 (plain)
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
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Compiles rater XML into JavaScript

  Copyright (C) 2016 R-T Specialty, LLC.

    This file is part of TAME.

    TAME is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see
    <http://www.gnu.org/licenses/>.

  This stylesheet should be included by whatever is doing the processing and is
  responsible for outputting the generated code in whatever manner is
  appropriate (inline JS, a file, etc).
-->
<xsl:stylesheet version="2.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:lv="http://www.lovullo.com/rater"
  xmlns:c="http://www.lovullo.com/calc"
  xmlns:preproc="http://www.lovullo.com/rater/preproc">


<xsl:template match="lv:package" mode="preproc:compile-fragments">
  <xsl:copy>
    <xsl:sequence select="@*, *" />

    <!-- compile each fragment in the symbol table -->
    <preproc:fragments>
      <xsl:for-each select="preproc:symtable/preproc:sym">
        <xsl:variable name="result">
          <xsl:apply-templates select="." mode="preproc:compile-fragments" />
        </xsl:variable>

        <xsl:if test="$result != ''">
          <preproc:fragment id="{@name}">
            <xsl:value-of select="$result" />
          </preproc:fragment>
        </xsl:if>
      </xsl:for-each>
    </preproc:fragments>
  </xsl:copy>
</xsl:template>


<xsl:template match="preproc:sym[ @src ]" mode="preproc:compile-fragments" priority="9">
  <!-- do not compile external symbols -->
</xsl:template>

<xsl:template match="preproc:sym" mode="preproc:compile-fragments" priority="1">
  <xsl:message terminate="yes">
    <xsl:text>[jsc] fatal: unknown symbol type for `</xsl:text>
      <xsl:value-of select="@name" />
    <xsl:text>': </xsl:text>
    <xsl:value-of select="@type" />
  </xsl:message>
</xsl:template>


<xsl:template match="preproc:sym[ @type='rate' ]" mode="preproc:compile-fragments" priority="5">
  <xsl:variable name="name" select="@name" />
  <xsl:variable name="pkg" as="element( lv:package )"
                select="root(.)" />

  <!-- could be one of two places -->
  <xsl:apply-templates mode="compile" select="
    $pkg/lv:rate[ @yields=$name ]
    , $pkg/lv:rate-group/lv:rate[ @yields=$name ]
   " />
</xsl:template>
<xsl:template match="preproc:sym[ @type='gen' ]" mode="preproc:compile-fragments" priority="5">
  <!-- compiled by above -->
</xsl:template>

<xsl:template match="preproc:sym[ @type='class' ]" mode="preproc:compile-fragments" priority="5">
  <!-- name is prefixed with :class: -->
  <xsl:variable name="as" select="substring-after( @name, ':class:' )" />
  <xsl:variable name="pkg" as="element( lv:package )"
                select="root(.)" />

  <xsl:apply-templates select="$pkg/lv:classify[ @as=$as ]" mode="compile" />
</xsl:template>
<xsl:template match="preproc:sym[ @type='cgen' ]" mode="preproc:compile-fragments" priority="5">
  <!-- compiled by above -->
</xsl:template>

<xsl:template match="preproc:sym[ @type='func' ]" mode="preproc:compile-fragments" priority="5">
  <xsl:variable name="name" select="@name" />
  <xsl:variable name="pkg" as="element( lv:package )"
                select="root(.)" />

  <xsl:apply-templates select="$pkg/lv:function[ @name=$name ]" mode="compile" />
</xsl:template>

<xsl:template match="preproc:sym[ @type='param' ]" mode="preproc:compile-fragments" priority="5">
  <xsl:variable name="name" select="@name" />
  <xsl:variable name="pkg" as="element( lv:package )"
                select="root(.)" />

  <xsl:apply-templates select="$pkg/lv:param[ @name=$name ]" mode="compile" />
</xsl:template>

<xsl:template match="preproc:sym[ @type='type' ]" mode="preproc:compile-fragments" priority="5">
  <xsl:variable name="name" select="@name" />
  <xsl:variable name="pkg" as="element( lv:package )"
                select="root(.)" />

  <!-- a typedef can stand on its own or exist within another typedef -->
  <xsl:apply-templates mode="compile" select="
      $pkg/lv:typedef[ @name=$name ]
      , $pkg//lv:typedef//lv:typedef[ @name=$name ]
    " />
</xsl:template>

<xsl:template match="preproc:sym[ @type='const' ]" mode="preproc:compile-fragments" priority="5">
  <xsl:variable name="name" select="@name" />
  <xsl:variable name="pkg" as="element( lv:package )"
                select="root(.)" />

  <xsl:apply-templates mode="compile"
                       select="$pkg/lv:const[ @name=$name ],
                               $pkg/lv:typedef//lv:item[ @name=$name ]">
    <xsl:with-param name="as-const" select="true()" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="preproc:sym[ @type='tpl' ]" mode="preproc:compile-fragments" priority="5">
  <!-- templates are for the preprocessor only -->
</xsl:template>

<xsl:template match="preproc:sym[ @type='lparam' ]" mode="preproc:compile-fragments" priority="5">
  <!-- they're local and therefore compiled as part of the containing block -->
</xsl:template>

<xsl:template match="preproc:sym[ @type='meta' ]"
              mode="preproc:compile-fragments" priority="5">
  <xsl:variable name="name" select="substring-after( @name, ':meta:' )" />
  <xsl:variable name="pkg" as="element( lv:package )"
                select="root(.)" />

  <xsl:variable name="node" as="element( lv:prop )"
                select="$pkg/lv:meta/lv:prop[ @name=$name ]" />
  <xsl:apply-templates mode="compile"
                       select="$node" />
</xsl:template>

</xsl:stylesheet>