Class: Tickly::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/tickly/parser.rb

Overview

Simplistic, incomplete and most likely incorrect TCL parser

Direct Known Subclasses

NodeProcessor::Ratchet

Defined Under Namespace

Classes: Error, R

Instance Method Summary collapse

Instance Method Details

#compact_subexpr(expr, at_depth) ⇒ Object

Override this to remove any unneeded subexpressions. Return the modified expression. If you return nil, the result will not be added to the expression list. You can also use this method for bottom-up expression evaluation, returning the result of the expression being evaluated. This method will be first called for the innermost expressions and then proceed up the call stack.



38
39
40
# File 'lib/tickly/parser.rb', line 38

def compact_subexpr(expr, at_depth)
  expr
end

#parse(io_or_str) ⇒ Object

Parses a piece of TCL and returns it converted into internal expression structures. A basic TCL expression is just an array of Strings. An expression in curly braces will have the symbol :c tacked onto the beginning of the array. An expression in square braces will have the symbol :b tacked onto the beginning. This method always returns a Array of expressions. If you only fed it one expression, this expression will be the only element of the array. The correct way to use the returned results is thusly:

p = Tickly::Parser.new
expressions = p.parse("2 + 2") #=> [["2", "+", "2"]]
expression = expressions[0] #=> ["2", "2"]


23
24
25
26
27
28
29
30
# File 'lib/tickly/parser.rb', line 23

def parse(io_or_str)
  bare_io = io_or_str.respond_to?(:read) ? io_or_str : StringIO.new(io_or_str)
  # Wrap the IO in a Bychar buffer to read faster
  reader = R.new(Bychar.wrap(bare_io))
  # Use multiple_expressions = true so that the top-level parsed script is always an array
  # of expressions
  parse_expr(reader, stop_char = nil, stack_depth = 0, multiple_expressions = true)
end