Class: Teepee::ListParser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, scope, expressions) ⇒ ListParser

Returns a new instance of ListParser.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
# File 'lib/teepee/list-parser.rb', line 42

def initialize parser, scope, expressions
  raise ArgumentError if not parser.is_a? Parser
  @parser = parser
  raise ArgumentError if not scope.is_a? Scope
  @scope = scope
  expressions.each do |expression|
    raise ArgumentError if not expression.kind_of? ParserNode
  end
  @expressions = expressions
end

Instance Attribute Details

#expressionsObject

Returns the value of attribute expressions.



40
41
42
# File 'lib/teepee/list-parser.rb', line 40

def expressions
  @expressions
end

#parserObject

Returns the value of attribute parser.



40
41
42
# File 'lib/teepee/list-parser.rb', line 40

def parser
  @parser
end

#scopeObject

Returns the value of attribute scope.



40
41
42
# File 'lib/teepee/list-parser.rb', line 40

def scope
  @scope
end

Class Method Details

.parse(parser, scope, tokens) ⇒ Object

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/teepee/list-parser.rb', line 63

def parse parser, scope, tokens
  expressions = []
  rest = tokens
  left_brace = rest.shift
  right_brace = nil
  raise ParseError if not left_brace.is_a? LeftBraceToken
  while rest.length > 0
    if rest.first.is_a? LeftBraceToken
      list, rest = ListParser.parse parser, scope.derive, rest
      expressions << list
    elsif rest.first.is_a? WordToken or
          rest.first.is_a? PipeToken
      expressions << rest.shift
    elsif rest.first.is_a? WhitespaceToken
      rest.shift # Drop the whitespace, this is the list separator.
    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 [ListParser.new(parser, scope, expressions), rest]
    else
      raise ParseError
    end
  end
end

Instance Method Details

#to_htmlObject



53
54
55
# File 'lib/teepee/list-parser.rb', line 53

def to_html
  "{" + expressions.map(&:to_html).join(" ") + "}"
end

#use_scope(scope) ⇒ Object



57
58
59
60
# File 'lib/teepee/list-parser.rb', line 57

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