Class: TBMX::CommandParser
- Inherits:
-
ParserNode
- Object
- ParserNode
- TBMX::CommandParser
- Defined in:
- lib/tbmx.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#expressions ⇒ Object
readonly
Returns the value of attribute expressions.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(command, expressions) ⇒ CommandParser
constructor
A new instance of CommandParser.
- #to_html ⇒ Object
Constructor Details
#initialize(command, expressions) ⇒ CommandParser
Returns a new instance of CommandParser.
201 202 203 204 205 206 207 |
# File 'lib/tbmx.rb', line 201 def initialize(command, expressions) raise ArgumentError if not command.is_a? WordToken @command = command raise ArgumentError if not expressions.is_a? Array expressions.each {|expression| raise ArgumentError if not expression.kind_of? ParserNode} @expressions = expressions end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
199 200 201 |
# File 'lib/tbmx.rb', line 199 def command @command end |
#expressions ⇒ Object (readonly)
Returns the value of attribute expressions.
199 200 201 |
# File 'lib/tbmx.rb', line 199 def expressions @expressions end |
Class Method Details
.parse(tokens) ⇒ Object
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 |
# File 'lib/tbmx.rb', line 235 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
#to_html ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/tbmx.rb', line 209 def to_html case command.word when "backslash", "bslash" "\\" when "left-brace", "left_brace", "leftbrace", "lbrace" "{" when "right-brace", "right_brace", "rightbrace", "rbrace" "}" when "br", "newline" "\n</br>\n" when "bold", "b", "textbf" "<b>" + expressions.map(&:to_html).join + "</b>" when "italic", "i", "textit" "<i>" + expressions.map(&:to_html).join + "</i>" when "underline", "u" "<u>" + expressions.map(&:to_html).join + "</u>" when "subscript", "sub" "<sub>" + expressions.map(&:to_html).join + "</sub>" when "superscript", "sup" "<sup>" + expressions.map(&:to_html).join + "</sup>" else %{<span style="color: red">[UNKNOWN COMMAND #{command.to_html}]</span>} end end |