Class: Btape::Parser

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

Overview

Parses .tape scripts into a list of Command structs, raising ScriptError for unknown commands, wrong argument counts, or invalid argument values.

Constant Summary collapse

ARITY =

An arity is either an exact count or a range, for commands whose last argument is optional.

{
  'Output' => 1, 'Viewport' => 1, 'Goto' => 1, 'Click' => 1, 'Type' => 2, 'Sleep' => 1,
  'Set' => 2, 'Screenshot' => 0..1, 'Evaluate' => 1, 'WaitFor' => 1..2, 'WaitForJS' => 1..2,
  'Frame' => 1, 'Press' => 1..2
}.freeze
SIGNATURES =

The same commands as they read to somebody being told about them, which is what btape help prints and what a model is handed before it is asked for a tape. ARITY is what a script is held to; a spec keeps the two lists from drifting apart.

{
  'Output' => 'PATH', 'Viewport' => 'WIDTHxHEIGHT', 'Goto' => 'URL', 'Click' => 'SELECTOR',
  'Type' => 'SELECTOR TEXT', 'Press' => 'KEY [COUNT]', 'Frame' => 'SELECTOR|main',
  'Evaluate' => 'JAVASCRIPT', 'WaitFor' => 'SELECTOR [TIMEOUT]', 'WaitForJS' => 'JAVASCRIPT [TIMEOUT]',
  'Screenshot' => '[NAME]', 'Sleep' => 'DURATION', 'Set' => 'NAME VALUE'
}.freeze

Instance Method Summary collapse

Instance Method Details

#parse(source) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/btape/parser.rb', line 33

def parse(source)
  source.each_line.with_index(1).filter_map do |line, number|
    text = line.strip
    next if text.empty? || text.start_with?('#')

    parse_line(text, number)
  rescue ArgumentError => e
    raise ScriptError.new(number, e.message)
  end
end