Method: Puppet::Pops::Parser::InterpolationSupport#interpolate_dq

Defined in:
lib/puppet/pops/parser/interpolation_support.rb

#interpolate_dqObject

This is the starting point for a double quoted string with possible interpolation The structure mimics that of the grammar. The logic is explicit (where the former implementation used parameters/structures) given to a generic handler. (This is both easier to understand and faster).



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppet/pops/parser/interpolation_support.rb', line 17

def interpolate_dq
  scn = @scanner
  ctx = @lexing_context
  before = scn.pos
  # skip the leading " by doing a scan since the slurp_dqstring uses last matched when there is an error
  scn.scan(/"/)
  value,terminator = slurp_dqstring()
  text = value
  after = scn.pos
  loop do
    case terminator
    when '"'
      # simple case, there was no interpolation, return directly
      return emit_completed([:STRING, text, scn.pos-before], before)
    when '${'
      count = ctx[:brace_count]
      ctx[:brace_count] += 1
      # The ${ terminator is counted towards the string part
      enqueue_completed([:DQPRE, text, scn.pos-before], before)
      # Lex expression tokens until a closing (balanced) brace count is reached
      enqueue_until count
      break
    when '$'
      varname = scn.scan(PATTERN_VARIABLE)
      if varname
        # The $ is counted towards the variable
        enqueue_completed([:DQPRE, text, after-before-1], before)
        enqueue_completed([:VARIABLE, varname, scn.pos - after + 1], after - 1)
        break
      else
        # false $ variable start
        text += terminator
        value,terminator = slurp_dqstring()
        text += value
        after = scn.pos
      end
    end
  end
  interpolate_tail_dq
  # return the first enqueued token and shift the queue
  @token_queue.shift
end