Class: Teepee::CommandParser

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, expressions) ⇒ CommandParser

Returns a new instance of CommandParser.

Raises:

  • (ArgumentError)


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

def initialize(command, expressions)
  raise ArgumentError if not command.is_a? WordToken
  @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.



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

def command
  @command
end

#expressionsObject (readonly)

Returns the value of attribute expressions.



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

def expressions
  @expressions
end

Class Method Details

.commander=(new) ⇒ Object



263
264
265
# File 'lib/teepee/command-parser.rb', line 263

def commander= new
  @@commander = new
end

.parse(tokens) ⇒ Object

Raises:



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

def parse(tokens)
  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(command, []), rest]
  end
  while rest.length > 0
    if rest.first.is_a? WordToken
      expressions << rest.shift
    elsif rest.first.is_a? WhitespaceToken
      expressions << rest.shift
    elsif rest.first.is_a? BackslashToken
      result, rest = CommandParser.parse(rest)
      expressions << result
    elsif rest.first.is_a? RightBraceToken
      right_brace = rest.shift
      return [CommandParser.new(command, expressions), rest]
    else
      raise ParseError
    end
  end
  if right_brace.nil? # Allow a forgotten final right brace.
    return [CommandParser.new(command, expressions), rest]
  end
end

Instance Method Details

#command_error(message) ⇒ Object



61
62
63
# File 'lib/teepee/command-parser.rb', line 61

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

#first_word_tokenObject



208
209
210
# File 'lib/teepee/command-parser.rb', line 208

def first_word_token
  expressions.select {|expr| expr.is_a? WordToken}.first
end

#html_tag(tag) ⇒ Object



200
201
202
# File 'lib/teepee/command-parser.rb', line 200

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

#number_from_expressionObject



223
224
225
# File 'lib/teepee/command-parser.rb', line 223

def number_from_expression
  numbers_from_expressions.first
end

#numbers_from_expressionsObject



212
213
214
215
216
217
218
219
220
221
# File 'lib/teepee/command-parser.rb', line 212

def numbers_from_expressions
  expressions
    .map do |number|
      begin
        Float(number.to_html)
      rescue ArgumentError
        nil
      end
    end.reject &:nil?
end

#tb_href(target, string) ⇒ Object



204
205
206
# File 'lib/teepee/command-parser.rb', line 204

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

#to_htmlObject



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

def to_html
  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 "br", "newline"
    @@commander.br
  when "bold", "b", "textbf"
    @@commander.b expressions
  when "del",
       "s",
       "strike",
       "strikethrough",
       "strikeout"
    @@commander.del expressions
  when "i"
    @@commander.i
  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 "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 "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 "pi"
    @@commander.pi
  when "e"
    @@commander.e
  when "+"
    @@commander.+ *numbers_from_expressions
  when "-"
    @@commander.- *numbers_from_expressions
  when "*"
    @@commander.* *numbers_from_expressions
  when "/"
    @@commander./ *numbers_from_expressions
  when "%"
    @@commander.% *numbers_from_expressions
  when "^", "**"
    @@commander.** *numbers_from_expressions
  when "sin", "cos", "tan",
    "asin", "acos", "atan",
    "sinh", "cosh", "tanh",
    "asinh", "acosh", "atanh",
    "erf", "erfc",
    "gamma", "log10", "sqrt"
    @@commander.send command.word.to_sym, number_from_expression
  when "d2r",
       "deg->rad",
       "degrees->radians"
    @@commander.degrees2radians number_from_expression
  when "r2d",
       "rad->deg",
       "radians->degrees"
    @@commander.radians2degrees number_from_expression
  when "lgamma"
    @@commander.lgamma number_from_expression
  when "ld",
       "log2"
    @@commander.ld number_from_expression
  when "ln"
    @@commander.ln number_from_expression
  when "log"
    base, number = numbers_from_expressions
    @@commander.log base, number
  when "ldexp"
    fraction, exponent = numbers_from_expressions
    @@commander.ldexp fraction, exponent
  when "hypot"
    @@commander.hypot numbers_from_expressions
  else
    command_error "unknown command #{command.to_html}"
  end
end