-
Notifications
You must be signed in to change notification settings - Fork 1
/
inform7.rb
478 lines (387 loc) · 15.7 KB
/
inform7.rb
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
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
# \b - Matches word boundaries when outside brackets; backspace (0x08) when inside brackets
# \B - Matches non-word boundaries
# (?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text
# (?!pat) - Negative lookahead assertion: ensures that the following characters do not match pat, but doesn't include those characters in the matched text
# (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text
# (?<!pat) - Negative lookbehind assertion: ensures that the preceding characters do not match pat, but doesn't include those characters in the matched text
require 'pp'
def recur(*args)
# regexps = []
pp args
# puts args.class
# args.each do |arg|
# regexps << arg.respond_to?(:to_a) ? "(?:#{builder(*arg)})?" : "(#{arg.keys.first})"
# end
args.map {|arg| (!arg.respond_to?(:keys)) ? "(?:#{builder(*arg)})?" : "(#{arg.keys.first})" }.join
end
def builder(*args)
puts 'entering builder'
regexp = %r{#{recur(*args)}}
flat = args.flatten
tokens = flat.map {|i| pp i; i.values.first }
return regexp, tokens
end
module Rouge
module Lexers
class Inform7 < RegexLexer
NL = Text::Whitespace.make_token(:newline, :nl)
WS = Text::Whitespace
Tab = Rouge::Token.make_token(:tab, :tb)
Inform6 = Rouge::Token.make_token(:inform6, :i6)
Heading = Rouge::Token.make_token(:heading, :hdg)
TableHead = Rouge::Token.make_token(:table_head, :tblhead)
TableBegin = Rouge::Token.make_token(:table_begin, :tbl_begin)
TableEnd = Rouge::Token.make_token(:table_end, :tbl_end)
TableColumn = Rouge::Token.make_token(:table_column, :tblcol)
BlockHead = Rouge::Token.make_token(:block_head, :blk_hd)
title 'Inform 7'
desc "Inform 7, a design system for interactive fiction"
tag 'i7'
mimetypes 'application/inform7'
state :string do
# puts "\n*string*\n"
rule /\[/, Str::Interpol
rule /[^\\"]+/, Str::Double
rule /"/, Str::Double, :pop!
end
state :textsub do
rule /[^\]]+/, Str::Interpol
rule /\]/, Str::Interpol, :pop!
end
state :nested_comment do
# puts 'nested_comment'
rule /[^\[\]]+/, Comment
rule(/\[/) { token Comment; push :nested_comment }
rule /\]/, Comment, :pop!
end
state :generic do
rule /[_\w]+/, Generic
rule /[^_\w]/, Generic, :pop!
end
state :tab do
rule /\t/, Tab, :pop!
end
state :heading do
rule /\n/, NL, :pop!
rule /[^\n]+/, Heading
end
state :table_row do
rule /[^\t\n]+/, TableColumn
rule /\t+/, Tab
rule /\n/, NL, :pop!
end
state :table do
rule /with\s+\d+\s+blank\s+rows?/, TableEnd, :pop!
rule /\n/, NL, :pop!
rule(//) { push :table_row }
end
state :table_begin do
rule /[A-Za-z][- \w]+[-\w]/, Str::Symbol
rule(/\n/) { token NL ; pop! ; push :table }
end
state :table_head do
rule /[\ ]+/, Text::Whitespace, :table_begin
rule(//) { pop! }
end
state :bol do
# puts "\n*bol*\n"
rule /(Volume|Book|Part|Chapter|Section)/i, Heading, :heading
rule /----\s+documentation\s+----\n/i, Comment::Multiline, :documentation
rule /Table\s+of/, Keyword, :table_head
rule //, NL, :pop!
end
state :documentation do
rule //, Comment::Multiline
end
state :inform6 do
rule /-\)/, Inform6, :pop!
rule /./, Inform6
end
state :blockhead do
# look for condition
end
state :phrase do
rule /[ ]+/, Text::Whitespace
end
state :phrase_preamble do
rule /(\s+)([^:\(]+?)(:)/ do
[ Text::Whitespace,
Str::Symbol, # spring the trap | say xyzzy
Operator,
].each.with_index(1) do |tok, i|
token tok, m[i] if m[i]
end
pop!
push :phrase
end
rule /[ ]+/, Text::Whitespace
end
state :rule_preamble do
end
# <rule> ::=
# Definition : A/an <kind> is <new adjectival name> if/unless <definition>
# | <preamble> : <phrases>
# | <preamble> , <phrase> (* only allowed for a few cases: see below)
# <definition> ::=
# <condition>
# | its/his/her/their <value property name> is/are <value> or less/more
# | : <phrases>
# <preamble> ::=
# To <phrase template>
# | To decide if/whether <phrase template>
# | To decide which/what <kind of value> is <phrase template>
# | This is the <rule name>
# | [[A] Rule for] <circumstances> [(this is the <rule name>)]
# <circumstances> ::=
# At <time>
# | When <event name>
# | [<placement>] <rulebook reference> [while/when <condition>] [during <scene name>]
# <rulebook reference> ::=
# <rulebook name> [about/for/of/on/rule] [<action pattern>]
# | <object-based-rulebook name> [about/for/of/on/rule] [<description>]
# <placement> ::=
# a/an
# | [the] first
# | [the] last
# <phrases> ::=
# <phrase>
# | <phrases> ; <phrase>
# The following examples show how Inform breaks down some typical rules using the system above:
# <rule> = At 2:09 PM: increase the score by 2; say "Progress!"
# <preamble> = At 2:09 PM
# <circumstances> = At 2:09 PM
# At
# <time> = 2:09 PM
# :
# <phrases> = increase the score by 2; say "Progress!"
# <phrase> = increase the score by 2
# ;
# <phrase> = say "Progress"
# <rule> = Instead of eating the ostrich during Formal Dinner (this is the cuisine rule), say "It's greasy!"
# <preamble> = Instead of eating the ostrich during Formal Dinner (this is the cuisine rule)
# <circumstances> = Instead of eating the ostrich during Formal Dinner
# <rulebook reference> = Instead of eating the ostrich
# <rulebook name> = Instead
# of
# <action pattern> = eating the ostrich
# during
# <scene name> = Formal Dinner
# (
# this
# is
# the
# <rule name> = cuisine rule
# )
# ,
# <phrases> = say "It's greasy!"
# <phrase> = say "It's greasy!"
# <rule> = After printing the name of a container: say "!"
# <preamble> = After printing the name of a container
# <circumstances> = After printing the name of a container
# <rulebook reference> = After printing the name of a container
# <object-based-rulebook name> = After printing the name
# of
# <description> = a container
# :
# <phrases> = say "!"
# <phrase> = say "!"
# (*) The colon dividing a rule preamble from its definition can be replaced by a comma only if the preamble begins with the words "Instead of", "Before", "After", "Every turn" or "When", and if the definition consists only of a single phrase.
# [only before, instead, after rules can take a ',' instead of a ':' ]
# TODO internal spaces on parenthetical ?
ws = { /[ ]+/ => WS }
defn_list = [ ws,
{ /.+?(?!(?:\s+(?:is|\()))/ => Keyword }, # a supporter
[ ws,
{ /\(/ => Operator }, # (
{ /called/ => Keyword }, # called
ws,
{ /[^\)]+/ => Str::Symbol }, # T
{ /\)/ => Operator } ], #)
ws,
{ /is/ => Keyword }, # is
ws,
{ /.+?(?!(?:rather|if|unless))/ => Str::Symbol }, # hefty
[ ws, { /rather\s+than/ => Keyword }, ws, { /.+?/ => Str::Symbol } ], # rather than light
ws,
{ /if|unless/ => Keyword },
ws,
{ /[^.]+/ => Keyword },
{ /\./ => Operator } ]
state :defn_preamble do
defn_regexp, defn_tokens = builder(*defn_list)
puts defn_regexp
rule(defn_regexp) do |m|
churn(m, *defn_tokens)
pop!
end
rule(//) { pop! }
end
def churn(m, *args)
args.each.with_index(1) do |tok, i|
token tok, m[i] if m[i]
end
end
state :phrase do
rule(/([^.]+)(\.)/) do |m|
token Keyword, m[1];
token Punctuation, m[2];
pop!
push :main
end
end
state :defn_adj do
# puts "\nin defn_adj"
rule(/(\s+)(rather\s+than)(\s+)/) do |m|
# puts "\nin rather than"
token WS, m[1];
token Keyword, m[2];
token WS, m[3];
end
rule(/(\s+)(if|unless)(\s+)/) do |m|
# puts "\nin if/unless"
token WS, m[1];
token Keyword, m[2];
token WS, m[3];
pop!
push :phrase
end
# rule /.+(?!(?:rather\s+than|if|unless))/, Str::Symbol
# rule /.+(?:(?!if))/, Str::Symbol
rule /./, Str::Symbol
end
state :definition_preamble do
# puts "\nin definition_preamble"
rule(/(\s+)(is)(\s+)/) do |m|
token WS, m[1];
token Keyword, m[2];
token WS, m[3];
push :defn_adj
end
rule(/(\s+)(\()(\s+)?(called)(\s+)([^\)]+)(\s+)?(\))(\s+)/) do |m|
token WS, m[1];
token Operator, m[2];
token WS, m[3] if m[3];
token Keyword, m[4];
token WS, m[5];
token Str::Symbol, m[6];
token WS, m[7] if m[7];
token Operator, m[8];
token WS, m[9];
push :defn_adj
end
rule /./, Str::Symbol
end
state :main do
# puts "\n*main*\n"
rule %r/\n/, NL, :bol
rule /\t/, Tab # , :tab
rule /\(-/, Inform6, :inform6
rule /[:;]\s*/, Operator
rule /\[/, Comment, :nested_comment
rule /[ ]+/, Text::Whitespace
# rule /To/i, Keyword, :phrase_preamble
# rule /At/i, Keyword, :at_time_preamble
rule(/(Definition:)(\s+)/i) do |m|
token Keyword, m[1];
token WS, m[2];
push :definition_preamble # :defn_preamble #
end
rule /[_\w]/, Generic, :generic
# rule /(?:if|repeat|while)/, BlockHead, :block_head
end
# state :terminating_comment do
# rule /[^\[\]]+/, Comment
# rule(/\]/) { token Comment; push :main }
# end
state :root_base do
rule %r/\s+/, Text::Whitespace
rule /\[/, Comment, :nested_comment
end
state :root do
rule /\[/, Comment, :nested_comment
end
class Extension < Inform7
filenames '*.i7x'
state :desc do
mixin :root_base
# rule(/"[^\\"]+"/) { token Str::Double ; pop! ; push :main }
rule /"[^\\"]+"/, Str::Double, :main
rule(//) { pop! ; push :main }
end
state :root do
mixin :root_base
# perversely, titles can have embedded newlines, but I'll pretend they can't
rule(/(?:(Version)(\s+)(\d+(?:\/\d{6})?)(\s+)(of)(\s+))?([^(]+?)(?:(\s+)(\()(for)(\s+)([-\w]+)(?:(\s+)(version)(\s+)(\d))?(\s+)(only)(\)))?(\s+)(by)(\s+)(.+?)(\s+)(begins\s+here)(\.)/) do |m|
[ Keyword, # Version
Text::Whitespace,
Num, # 3 or 3/20210501
Text::Whitespace,
Keyword, # of
Text::Whitespace,
Str::Symbol, # ext_name
Text::Whitespace,
Operator, # (
Keyword, # for
Text::Whitespace,
Str::Symbol, # glulx|z-machine
Text::Whitespace,
Keyword, # version
Text::Whitespace,
Num, # 8
Text::Whitespace,
Keyword, # only
Operator, # )
Text::Whitespace,
Keyword, # by
Text::Whitespace,
Str::Symbol, # author
Text::Whitespace,
Keyword, # begins here
Punctuation, # '.'
].each.with_index(1) do |tok, i|
token tok, m[i] if m[i]
end
push :desc
end
end
end
class Story < Inform7
filenames '*.ni'
#TitleName = Rouge::Token.make_token(:titlename, :tin)
Title = Rouge::Token.make_token(:title, :ti)
# state :author do
# rule(/(.+?)(\s+)(begins\s+here)(\s*)(\.)/) do |m|
# token Str::Symbol, m[1]
# token Text::Whitespace, m[2]
# token Keyword, m[3]
# token Text::Whitespace, m[4] if m[4]
# token Operator, m[5]
# end
# rule(/\n/) { token NL; :pop! ; push :main }
# end
# perversely, author names can have embedded newlines and be terminated by a comment or a blank line but I'll pretend they can't
# state :title do
# rule %r/[ ]+/, Text::Whitespace
# rule(/(by)(\s+)/) do |m| token Keyword::Pseudo, m[1]; token Str, m[2]; push :author ; end
# rule(//) { pop!; push :main }
# end
state :root do
mixin :root_base
# perversely, titles can have embedded newlines, but I'll pretend they can't
rule(/("[^\\"]+")([ ]+)(?:(by)([ ]+)([^\n]+))?/) do |m|
churn(m,
Str::Double, # title
Text::Whitespace,
Keyword, # by
Text::Whitespace,
Str::Symbol # author
)
push :main
end
end
end
end
end
end