Class: Liquidscript::Compiler::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Helpers
Defined in:
lib/liquidscript/compiler/base.rb,
lib/liquidscript/compiler/base/blank.rb,
lib/liquidscript/compiler/base/action.rb,
lib/liquidscript/compiler/base/helpers.rb,
lib/liquidscript/compiler/base/callable.rb

Direct Known Subclasses

ICR

Defined Under Namespace

Modules: Helpers Classes: Action, Blank, Callable

Instance Method Summary collapse

Methods included from Helpers

#action, #collect_compiles, #expect, #loop, #maybe, #peek?, #shift

Constructor Details

#initialize(scanner) ⇒ Base

Initializes the compiler.

Parameters:

  • scanner (Scanner, #each)

    the scanner. Used to manage the tokens. This could be stubbed out, if the #each function returns an Enumerator, which yields Scanner::Tokens.



19
20
21
22
23
24
# File 'lib/liquidscript/compiler/base.rb', line 19

def initialize(scanner)
  @scanner  = scanner
  @iterator = scanner.each
  @action   = Action.new
  reset!
end

Instance Method Details

#compileArray

Begins the compiliation. Continues until the iterator raises a ‘StopIteration` error, and then returns the top set with #top.

Returns:

  • (Array)

    the topmost set.

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/liquidscript/compiler/base.rb', line 42

def compile
  # We're using this cool property of Base::Blank such that it
  # will return a nil to any method call.
  while !peek.is_a?(Blank) do
    top.push compile_start
  end

  top

rescue CompileError => e
  token = peek
  part = "#{File.expand_path(@scanner.[:file])}" +
    ":#{token.line}:#{token.column}: " +
    "before #{token.type.to_s.upcase}"
  raise e, e.message, [part, *e.backtrace]
end

#compile?Boolean

Checks to see if the given input compiles.

Returns:

  • (Boolean)

    if it compiles.

See Also:

  • Liquidscript::Compiler::Base.[[#compile]


63
64
65
66
67
68
69
70
# File 'lib/liquidscript/compiler/base.rb', line 63

def compile?
  compile
  true
rescue CompileError
  false
ensure
  reset!
end

#peek#type, Blank

Peeks at the next argument on the scanner. If the call raises a StopIteration error, it returns the value of #scanner_nil instead.

Returns:



92
93
94
95
96
# File 'lib/liquidscript/compiler/base.rb', line 92

def peek
  @iterator.peek
rescue StopIteration
  scanner_nil
end

#pop#type?, Blank

Pops the next argument off of the scanner. If the call raises a StopIteration error, it returns the value of #scanner_nil instead.

Returns:



81
82
83
84
85
# File 'lib/liquidscript/compiler/base.rb', line 81

def pop
  @iterator.next
rescue StopIteration
  scanner_nil
end

#reset!void Also known as: rewind

This method returns an undefined value.

Resets the state of the compiler.



101
102
103
# File 'lib/liquidscript/compiler/base.rb', line 101

def reset!
  @iterator.rewind
end

#scanner_nilObject



72
73
74
# File 'lib/liquidscript/compiler/base.rb', line 72

def scanner_nil
  @_blank ||= Blank.new
end

#topArray, #push

Returns the top set. If the variable @top isn’t set by the inheriting class, then it defaults to the value of an array.

Returns:



30
31
32
# File 'lib/liquidscript/compiler/base.rb', line 30

def top
  @top ||= []
end