Class: Tickly::Parser
- Inherits:
-
Object
- Object
- Tickly::Parser
- Defined in:
- lib/tickly/parser.rb
Overview
Simplistic, incomplete and most likely incorrect TCL parser
Direct Known Subclasses
Defined Under Namespace
Instance Method Summary collapse
-
#compact_subexpr(expr, at_depth) ⇒ Object
Override this to remove any unneeded subexpressions.
-
#parse(io_or_str) ⇒ Object
Parses a piece of TCL and returns it converted into internal expression structures.
-
#wrap_io_or_string(io_or_str) ⇒ Object
Returns the given String or IO object wrapped in an object that has one method, read_one_char - that gets used by all the subsequent parsing steps.
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.
49 50 51 |
# File 'lib/tickly/parser.rb', line 49 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"]
36 37 38 39 40 41 |
# File 'lib/tickly/parser.rb', line 36 def parse(io_or_str) reader = wrap_io_or_string(io_or_str) # 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 |
#wrap_io_or_string(io_or_str) ⇒ Object
Returns the given String or IO object wrapped in an object that has one method, read_one_char - that gets used by all the subsequent parsing steps
19 20 21 22 23 |
# File 'lib/tickly/parser.rb', line 19 def wrap_io_or_string(io_or_str) return io_or_str if io_or_str.respond_to?(:read_one_char) # Bychar or R return R.new(io_or_str) if io_or_str.respond_to?(:read) R.new(StringIO.new(io_or_str)) end |