Skip to content

TemplateLang.sdf3

pdmosses/sdf/org.metaborg.meta.lang.template/syntax/TemplateLang.sdf3

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
module  
imports
  sdf2-core/Sdf2
  constants/StrCon
  sorts/Sorts
  symbols/Symbols
  labels/Labels
  constants/IdentifierCon
  kernel/Kernel
  restrictions/Restrictions
  priority/Priority
  basic/Basic
  modules/Modules
  layout-constraints/Layout-Constraints

context-free start-symbols
    Module

context-free syntax
//options section
Section.TemplateOptions = <
        template options 

                <{TemplateOption "\n"}*>

        > 

TemplateOption.Newlines = <newlines : <NewlineOptionValue>>

TemplateOption.KeywordAttributes = <<SymbolDef> = keyword <Attributes>>

NewlineOptionValue.None = <none>
NewlineOptionValue.Leading = <leading>
NewlineOptionValue.Trailing = <trailing>
NewlineOptionValue.Separating = <separating>

TemplateOption.KeywordFollowRestriction = <keyword -/- <Lookaheads>>

TemplateOption.Tokenize = <tokenize : <StrCon>>

context-free syntax
//references to productions using sort.cons notation in priority specification
Group.SimpleRefGroup = <<SortConsRef>>
Group.ProdsRefGroup = <{<{SortConsRef "\n"}+>}>
Group.AssocRefGroup = <
        {<Associativity> :
                <{SortConsRef "\n"}+>}>
.SortConsRef = <<Symbol>.<Constructor>>

context-free syntax 
//grammars
Section.TemplateSection = <
        templates 

                <{TemplateProduction "\n"}*>

        > {deprecated("Use context-free syntax section instead")}

Grammar.LexicalSyntax = <
        lexical syntax 

                <{SdfProduction "\n"}*>

        > {prefer}

Grammar.ContextFreeSyntax = <
        context-free syntax 

                <{GeneralProduction "\n"}*>

        > {prefer}

Grammar.Kernel = <
        syntax 

                <{SdfProduction "\n"}*>

        > {prefer}

GeneralProduction = <<SdfProduction>>
GeneralProduction = <<TemplateProduction>> {prefer}


// syntactic sugar for SDF
.SdfProduction = <<SymbolDef> = <RHS> <Attributes>> 
.SdfProductionWithCons = <<SortCons> = <RHS> <Attributes>>
.Rhs = <<Symbols>>

// template productions
.TemplateProduction = <<SymbolDef> = <Template> <Attributes>>

// template productions Sort.Cons = ...
.TemplateProductionWithCons = <<SortCons> = <Template> <Attributes>>

.Constructor = <<IdCon>>

.SortCons = <<SymbolDef>.<Constructor>>


// placeholders
TemplatePart1.Angled       = [<[Placeholder]>]
TemplatePart1.BreakAngled  = [<>]
TemplatePart2.Squared      = <[<Placeholder>]>
TemplatePart2.BreakSquared = <[]>

.Placeholder = <<Symbol><PlaceholderOptions>>



PlaceholderOptions.NoOptions = <>
PlaceholderOptions.Options = <;<{PlaceholderOption ", "}+>>

PlaceholderOption.Hide = <hide>
PlaceholderOption.Wrap = <wrap>
PlaceholderOption.Anchor = <anchor>
PlaceholderOption.Floating = <float>
PlaceholderOption.CompCursor = <cursor>
//mark as deprecated
PlaceholderOption.Separator = <separator=<StrCon>> {deprecated("Use list with separator symbol instead: {S \"separator\"}*")}
PlaceholderOption.Text = <text=<StrCon>>

  syntax


  -CF.Template = "<" {TemplateLine1-CF "\n"}+ ">"
  -CF.TemplateDeprecated =  "<<" {TemplateLine1-CF "\n"}+ ">>" {avoid, deprecated("Use single brackets to delimit the template")}
  -CF.Line = TemplatePart1-CF* 
  TemplatePart1-CF.String = TemplateString1-LEX
  TemplatePart1-CF.Escape = TemplateEscape1-LEX
  TemplatePart1-CF.Layout = TemplateLayout-LEX 

  -CF.TemplateSquare = "[" {TemplateLine2-CF "\n"}+ "]"
  -CF.TemplateSquareDeprecated = "[[" {TemplateLine2-CF "\n"}+ "]]" {avoid, deprecated("Use single brackets to delimit the template")}
  -CF.Line = TemplatePart2-CF* 

  TemplatePart2-CF.String =  TemplateString2-LEX 

  TemplatePart2-CF.Escape = TemplateEscape2-LEX
  TemplatePart2-CF.Layout = TemplateLayout-LEX


  lexical syntax

           = LF
           = CR
           = CR LF

           = [\r]
           = [\n]

    "\n" = EOL

  lexical restrictions

    EOL -/- [\n]


  lexical syntax

     =  [\ \t]+

     = TemplateString1Alt+
     = TemplateString2Alt+
        TemplateStringQ =  ~[\\\"\ \t]+   
    TemplateString1Alt = ~[\\\<\>\ \t\r\n]
    TemplateString2Alt = ~[\\\[\]\ \t\r\n] 
    TemplateString2Alt = Backslash

         = [\\] 
    // [\\] -> Backslash1
    // [\\] -> Backslash2      => [\\] -> Backslash

  lexical restrictions
    TemplateLayout -/- [\ \t]

    %% must be followed by:
    TemplateString1 -/- ~[\\\<\>\ \t\r\n]
    TemplateString2 -/- ~[\\\[\]\ \t\r\n]
    TemplateStringQ -/- ~[\\\"\ \t]

    %% if followed by a backslash, then the backslash must be followed by:
    TemplateString1 -/- [\\].~[\<\>\\]
    TemplateString2 -/- [\\].~[\[\]\\]

    Backslash -/- [\<\>\\]

  template options

          tokenize : "="  

  lexical syntax
  //production for ContenteCompletion
  CONTENTCOMPLETE = [a-zA-Z]* "CONTENTCOMPLETE" [0-9]+  {avoid}

  //combine sequences of escaped chars
   = EscapedChar1+
   = EscapedChar2+

  //this are the escapes which StringTemplate has
  EscapedChar1 = "\\<"
  EscapedChar1 = "\\>"
  EscapedChar1 = "\\\\"
  EscapedChar2 = "\\["
  EscapedChar2 = "\\]"
  EscapedChar2 = "\\\\"

   //EscapedChar1 = "<" (EscapedBackslashChar1 | EscapedChar)* ">"
   EscapedChar1 = "<" EscapedChar1Alt+ ">"
   EscapedChar1Alt = EscapedBackslashChar1
   EscapedChar1Alt = EscapedChar

   EscapedChar1 = "<\\\\>" [\ \t\r]* [\n]

   //EscapedChar2 = "[" (EscapedBackslashChar2 | EscapedChar)* "]"
   EscapedChar2 = "[" EscapedChar2Alt+ "]"
   EscapedChar2Alt = EscapedBackslashChar2 
   EscapedChar2Alt = EscapedChar
   EscapedChar2 = "[\\\\]" [\ \t\r]* [\n]

    // double-backslash isn't allowed as sole member of escape placeholder
    // (would clash with <\\> / [\\] line continuation construct)
    // so we add follow restrictions to the following symbols:
    = "\\\\"
    = "\\\\"

   TemplateEscapeQ = TemplateEscapeQAlt+
   TemplateEscapeQAlt = EscapedChar
   TemplateEscapeQAlt = "\\\\"

   //escapes allowed in a placeholder-like construct
    = "\\'"
    = '\\"'
    =  "\\ "
    =  "\\t"
    =  "\\r"
    =  "\\n"



     = "\\u" [0-9A-Fa-f]+ 
     = EscapedUnicodeChar

    .WATER = "\\" ~[] {recover}
    EscapedChar1.WATER = "<" ~[\>]+ ">" {recover}
    EscapedChar2.WATER = "[" ~[\]]+ "]" {recover}


  lexical restrictions
    TemplateEscape1 -/- [\\]
    TemplateEscape1 -/- [\<].[\\\>]
    TemplateEscape2 -/- [\\]
    TemplateEscape2 -/- [\[].[\\\]]
    TemplateEscapeQ -/- [\\]
    EscapedBackslashChar1 -/- [\>]
    EscapedBackslashChar2 -/- [\]]
    EscapedUnicodeChar -/- [0-9A-Fa-f]