Class: Teepee::CommandParser

Inherits:
ParserNode show all
Defined in:
lib/teepee/command-parser.rb

Constant Summary collapse

@@commander =
Commander.new nil
@@action_view =
nil
@@controller =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, scope, command, expressions) ⇒ CommandParser

Returns a new instance of CommandParser.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/teepee/command-parser.rb', line 52

def initialize(parser, scope, command, expressions)
  @parser = parser
  raise ArgumentError if not command.is_a? WordToken
  @scope = scope
  raise ArgumentError if not scope.is_a? Scope
  @command = command
  raise ArgumentError if not expressions.is_a? Array
  expressions.each do |expression|
    raise ArgumentError if not expression.kind_of? ParserNode
  end
  @expressions = expressions
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



50
51
52
# File 'lib/teepee/command-parser.rb', line 50

def command
  @command
end

#expressionsObject (readonly)

Returns the value of attribute expressions.



50
51
52
# File 'lib/teepee/command-parser.rb', line 50

def expressions
  @expressions
end

#parserObject

Returns the value of attribute parser.



49
50
51
# File 'lib/teepee/command-parser.rb', line 49

def parser
  @parser
end

#scopeObject

Returns the value of attribute scope.



49
50
51
# File 'lib/teepee/command-parser.rb', line 49

def scope
  @scope
end

Class Method Details

.commander=(new) ⇒ Object



544
545
546
# File 'lib/teepee/command-parser.rb', line 544

def commander= new
  @@commander = new
end

.parse(parser, scope, tokens) ⇒ Object

Raises:



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/teepee/command-parser.rb', line 511

def parse(parser, scope, tokens)
  @parser = parser
  @scope = scope
  expressions = []
  rest = tokens
  backslash, command, left_brace = rest.shift(3)
  right_brace = nil
  raise ParseError if not backslash.is_a? BackslashToken
  raise ParseError if not command.is_a? WordToken
  if not left_brace.is_a? LeftBraceToken # A command with no interior.
    rest.unshift left_brace if not left_brace.is_a? WhitespaceToken
    return [CommandParser.new(parser, scope, command, []), rest]
  end
  while rest.length > 0
    if rest.first.is_a? WordToken or
       rest.first.is_a? WhitespaceToken or
       rest.first.is_a? PipeToken
      expressions << rest.shift
    elsif rest.first.is_a? BackslashToken
      result, rest = CommandParser.parse(parser, scope, rest)
      expressions << result
    elsif rest.first.is_a? RightBraceToken
      right_brace = rest.shift
      return [CommandParser.new(parser, scope, command, expressions), rest]
    else
      raise ParseError
    end
  end
  if right_brace.nil? # Allow a forgotten final right brace.
    return [CommandParser.new(parser, scope, command, expressions), rest]
  end
end

Instance Method Details

#builtinsObject



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
# File 'lib/teepee/command-parser.rb', line 98

def builtins
  case command.word
  when "backslash",
       "bslash",
       "-/"
    commander.backslash
  when "left-brace",
       "left_brace",
       "leftbrace",
       "lbrace",
       "opening-brace",
       "opening_brace",
       "openingbrace",
       "obrace",
       "-("
    commander.left_brace
  when "right-brace",
       "right_brace",
       "rightbrace",
       "rbrace",
       "closing-brace",
       "closing_brace",
       "closingbrace",
       "cbrace",
       ")-"
    commander.right_brace
  when "left-bracket",
       "left_bracket",
       "opening-bracket",
       "opening_bracket",
       "lbracket",
       "obracket",
       "(("
    commander.left_bracket
  when "right-bracket",
       "right_bracket",
       "closing-bracket",
       "closing_bracket",
       "rbracket",
       "cbracket",
       "))"
    commander.right_bracket
  when "pipe"
    commander.pipe
  when "backtick",
       "backquote",
       "'"
    commander.backquote
  when "squiggle",
       "tilde"
    commander.squiggle
  when "dollar"
    commander.dollar
  when "_"
    commander.nbsp optional_first_word_token
  when "space"
    commander.space
  when "br", "newline"
    commander.br
  when "bold", "b", "textbf"
    commander.b expressions
  when "del",
       "s",
       "strike",
       "strikethrough",
       "strikeout"
    commander.del expressions
  when "italic",
       "textit",
       "it"
    commander.it expressions
  when "underline",
       "u"
    commander.u expressions
  when "tt",
       "texttt",
       "teletype",
       "typewriter"
    commander.tt expressions
  when "small"
    commander.small expressions
  when "big"
    commander.big expressions
  when "subscript",
       "sub"
    commander.sub expressions
  when "superscript",
       "sup"
    commander.sup expressions
  when "h1"
    commander.h1 expressions
  when "h2"
    commander.h2 expressions
  when "h3"
    commander.h3 expressions
  when "h4"
    commander.h4 expressions
  when "h5"
    commander.h5 expressions
  when "h6"
    commander.h6 expressions
  when "itemize",
       "ul"
    commander.itemize expressions
  when "itemize-disc",
       "ul-disc",
       "itemize_disc",
       "ul_disc",
       "itemize-disk",
       "ul-disk",
       "itemize_disk",
       "ul_disk"
    commander.itemize_disc expressions
  when "itemize-circle",
       "ul-circle",
       "itemize_circle",
       "ul_circle"
    commander.itemize_circle expressions
  when "itemize-square",
       "ul-square",
       "itemize_square",
       "ul_square"
    commander.itemize_square expressions
  when "itemize-none",
       "ul-none",
       "itemize_none",
       "ul_none"
    commander.itemize_none expressions
  when "enumerate",
       "ol"
    commander.enumerate expressions
  when "enumerate-numeric",
       "enumerate_numeric",
       "enumerate-1",
       "ol-1",
       "enumerate_1",
       "ol_1"
    commander.enumerate_numeric expressions
  when "enumerate-uppercase",
       "enumerate_uppercase",
       "enumerate-upcase",
       "enumerate_upcase",
       "enumerate-A",
       "ol-A",
       "enumerate_A",
       "ol_A"
    commander.enumerate_uppercase expressions
  when "enumerate-lowercase",
       "enumerate_lowercase",
       "enumerate-downcase",
       "enumerate_downcase",
       "enumerate-a",
       "ol-a",
       "enumerate_a",
       "ol_a"
    commander.enumerate_lowercase expressions
  when "enumerate-roman-uppercase",
       "enumerate_roman_uppercase",
       "enumerate-roman-upcase",
       "enumerate_roman_upcase",
       "enumerate-I",
       "ol-I",
       "enumerate_I",
       "ol_I"
    commander.enumerate_roman_uppercase expressions
  when "enumerate-roman-lowercase",
       "enumerate_roman_lowercase",
       "enumerate-roman-downcase",
       "enumerate_roman_downcase",
       "enumerate-i",
       "ol-i",
       "enumerate_i",
       "ol_i"
    commander.enumerate_roman_lowercase expressions
  when "item",
       "li"
    commander.item expressions
  when "let1"
    subscoped_commander.let1 expressions
  when "table"
    commander.table expressions
  when "tr",
       "table-row",
       "table_row"
    commander.table_row expressions
  when "th",
       "table-header",
       "table_header"
    commander.table_header expressions
  when "td",
       "table-data",
       "table_data",
       "table-cell",
       "table_cell"
    commander.table_data expressions
  when "link",
       "href",
       "url"
    commander.link expressions
  when "email",
       "email-address",
       "email_address",
       "mailto"
    commander.mailto first_word_token
  when "img",
       "image"
    commander.image expressions
  when "user",
       "user-id",
       "user_id"
    commander.user first_word_token
  when "link-id",
       "link_id"
    commander.link_id first_word_token
  when "keyword-id",
       "keyword_id"
    commander.keyword_id first_word_token
  when "note-id",
       "note_id"
    commander.note_id first_word_token
  when "tag-id",
       "tag_id"
    commander.tag_id first_word_token
  when "forum-id",
       "forum_id"
    commander.forum_id first_word_token
  when "folder-id",
       "folder_id"
    commander.folder_id first_word_token
  when "bookmarks-folder-id",
       "bookmarks_folder_id",
       "bookmarks_folder-id",
       "bookmarks-folder_id",
       "bookmark-folder-id",
       "bookmark_folder_id",
       "bookmark_folder-id",
       "bookmark-folder_id"
    commander.bookmarks_folder_id first_word_token
  when "i" # sqrt(-1), not yet supported
    commander.i
  when "pi"
    commander.pi
  when "e"
    commander.e
  when "+"
    commander.+ word_tokens
  when "increment",
       "inc"
    commander.increment first_word_token
  when "decrement",
       "dec"
    commander.decrement first_word_token
  when "-"
    commander.- word_tokens
  when "*"
    commander.* word_tokens
  when "/",
       "÷"
    commander./ word_tokens
  when "%"
    commander.% word_tokens
  when "+%"
    commander.add_percentage word_tokens
  when "-%"
    commander.subtract_percentage word_tokens
  when "%t",
       "%total"
    commander.percent_total word_tokens
  when "floor"
    commander.floor first_word_token
  when "ceil",
       "ciel",
       "ceiling"
    commander.ceiling first_word_token
  when "round"
    commander.round *word_tokens
  when "mod",
       "modulus",
       "modulo"
    commander.mod word_tokens
  when "^", "**"
    commander.** word_tokens
  when "sin", "cos", "tan",
    "asin", "acos", "atan",
    "sinh", "cosh", "tanh",
    "asinh", "acosh", "atanh",
    "erf", "erfc",
    "gamma", "log10", "sqrt"
    commander.send command.word.to_sym, first_word_token
  when "d2r",
       "deg->rad",
       "degrees->radians"
    commander.degrees2radians first_word_token
  when "r2d",
       "rad->deg",
       "radians->degrees"
    commander.radians2degrees first_word_token
  when "lgamma"
    commander.lgamma first_word_token
  when "ld",
       "log2"
    commander.ld first_word_token
  when "ln"
    commander.ln first_word_token
  when "log"
    base, number = word_tokens
    commander.log base, number
  when "ldexp"
    fraction, exponent = word_tokens
    commander.ldexp fraction, exponent
  when "hypot"
    commander.hypot word_tokens
  when "true"
    commander.true_constant
  when "false"
    commander.false_constant
  when "not"
    commander.boolean_not first_word_token
  when "and"
    commander.boolean_and expressions
  when "or"
    commander.boolean_or expressions
  when "nand"
    commander.boolean_nand expressions
  when "nor"
    commander.boolean_nor expressions
  when "xor"
    commander.boolean_xor expressions
  when "xnor"
    commander.boolean_xnor expressions
  when "<"
    commander.less_than word_tokens
  when "<=",
       ""
    commander.less_than_or_equal word_tokens
  when ">"
    commander.greater_than word_tokens
  when ">=",
       ""
    commander.greater_than_or_equal word_tokens
  when "="
    commander.equal word_tokens
  when "!=",
       ""
    commander.not_equal word_tokens
  when "if"
    commander.if_operator expressions
  when "when"
    commander.when_operator expressions
  when "unless"
    commander.unless_operator expressions
  when "comment",
       "!--",
       "#"
    commander.comment expressions
  when "span"
    commander.span_operator expressions
  when "prog1"
    commander.prog1_operator expressions
  when "progn"
    commander.progn_operator expressions
  when "prognil",
       "side-effects",
       "side-affects",
       "effects",
       "affects"
    commander.prognil expressions
  when "cond"
    commander.cond_operator expressions
  when "case"
    commander.case_operator expressions
  when "get"
    commander.get_operator first_word_token
  when "define",
       "def"
    commander.define expressions
  when "undefine",
       "undef"
    commander.undefine expressions
  when "defined?"
    commander.is_defined? word_tokens
  when "dotimes"
    commander.dotimes expressions
  else
    command_error "unknown command #{command.to_html}"
  end
end

#command_error(message) ⇒ Object



71
72
73
# File 'lib/teepee/command-parser.rb', line 71

def command_error(message)
  %{<span style="color: red">[#{message}]</span>}
end

#commanderObject



75
76
77
# File 'lib/teepee/command-parser.rb', line 75

def commander
  @@commander.with_scope scope
end

#first_word_tokenObject



498
499
500
# File 'lib/teepee/command-parser.rb', line 498

def first_word_token
  word_tokens.first
end

#html_tag(tag) ⇒ Object



486
487
488
# File 'lib/teepee/command-parser.rb', line 486

def html_tag(tag)
  "<#{tag}>" + expressions.map(&:to_html).join + "</#{tag}>"
end

#optional_first_word_tokenObject



502
503
504
# File 'lib/teepee/command-parser.rb', line 502

def optional_first_word_token
  first_word_token if not expressions.empty?
end

#subscoped_commanderObject



79
80
81
# File 'lib/teepee/command-parser.rb', line 79

def subscoped_commander
  @@commander.with_scope scope.derive
end

#tb_href(target, string) ⇒ Object



490
491
492
# File 'lib/teepee/command-parser.rb', line 490

def tb_href(target, string)
  %{<a href="#{TB_COM}/#{target}">#{string}</a>}
end

#to_htmlObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/teepee/command-parser.rb', line 87

def to_html
  commander.parser = @parser
  if @@commander.scope.has_key? command.word
    @@commander.scope[command.word]
  elsif @parser.variables.include? command.word
    commander.get_operator command.word
  else
    builtins
  end
end

#to_numberObject



83
84
85
# File 'lib/teepee/command-parser.rb', line 83

def to_number
  to_html.to_number
end

#use_scope(scope) ⇒ Object



65
66
67
68
69
# File 'lib/teepee/command-parser.rb', line 65

def use_scope scope
  @scope = scope
  expressions.map {|node| node.use_scope scope}
  self
end

#word_tokensObject



494
495
496
# File 'lib/teepee/command-parser.rb', line 494

def word_tokens
  expressions.reject {|expr| expr.is_a? WhitespaceToken}
end