Class: Sneaql::Core::StepParser

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

Overview

Parses a step file into discrete statements. Also performs validation of all Sneaql tags.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, expression_handler, recordset_manager, logger = nil) ⇒ StepParser

Returns a new instance of StepParser.

Parameters:

  • file_path (String)

    pathname to step file

  • expression_handler (Sneaql::ExpressionHandler)
  • recordset_manager (Sneaql::RecordsetManager)
  • logger (Logger) (defaults to: nil)

    optional, if omitted default logger will be used

Raises:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sneaql_lib/parser.rb', line 16

def initialize(file_path, expression_handler, recordset_manager, logger = nil)
  @logger = logger ? logger : Logger.new(STDOUT)
  @expression_handler = expression_handler
  @recordset_manager = recordset_manager

  # parse the statements from the file and store them in an array
  # this is a simple text parsing based upon the /*- delimiter
  @statements = parse_statements_from_file(file_path)

  raise Sneaql::Exceptions::NoStatementsFoundInFile if @statements == []
end

Instance Attribute Details

#expression_handlerObject (readonly)

Returns the value of attribute expression_handler.



10
11
12
# File 'lib/sneaql_lib/parser.rb', line 10

def expression_handler
  @expression_handler
end

#statementsObject (readonly)

array of raw statement text



9
10
11
# File 'lib/sneaql_lib/parser.rb', line 9

def statements
  @statements
end

Instance Method Details

#command_at_index(indx) ⇒ Hash

Returns command tag from statement at specified index. Allows for

Parameters:

  • indx (Fixnum)

    index of statement in statements array

Returns:

  • (Hash)


59
60
61
62
# File 'lib/sneaql_lib/parser.rb', line 59

def command_at_index(indx)
  parsed_tag = tag_splitter(@statements[indx])
  { command: parsed_tag[0], arguments: parsed_tag[1..parsed_tag.length - 1] }
end

#parse_statements_from_file(file_path) ⇒ Object

Performs the actual parsing from file

Parameters:

  • file_path (String)


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

def parse_statements_from_file(file_path)
  @logger.info("parsing statements from step file #{file_path}")
  stmt = []
  File.read(file_path).split('/*-').each { |s| stmt << "/*-#{s.strip}" }
  # delete the first element because of the way it splits
  stmt.delete_at(0)
  @logger.info("#{stmt.length} statements found")
  stmt
rescue => e
  @logger.error("file parsing error :#{e.message}")
  e.backtrace.each { |b| @logger.error b.to_s }
  raise Sneaql::Exceptions::StatementParsingError
end

#statement_args_are_valid?(this_cmd) ⇒ Boolean

Checks to see if the arguments for a given command are valid. This is done by calling the validate_args method of the command class.

Parameters:

  • this_cmd (Hash)

    parsed command tag

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sneaql_lib/parser.rb', line 83

def statement_args_are_valid?(this_cmd)
  c = Sneaql::Core.find_class(:command, this_cmd[:command]).new(
    nil,
    @expression_handler,
    nil,
    @recordset_manager,
    nil,
    @logger
  )
  c.validate_args(this_cmd[:arguments])
end

#tag_splitter(statement_text_with_command) ⇒ Array

Extracts array of tokens from tag

Parameters:

  • statement_text_with_command (String)

Returns:

  • (Array)


47
48
49
50
51
52
53
54
# File 'lib/sneaql_lib/parser.rb', line 47

def tag_splitter(statement_text_with_command)
  # updated to use tokenizer
  # splits out all the tag elements into an array
  # statement_text_with_command.split('-*/')[0].gsub('/*-', '').strip.split
  command = statement_text_with_command.split('-*/')[0].gsub('/*-', '').strip
  t = Sneaql::Core::Tokenizer.new
  t.tokenize(command)
end

#valid_arguments_in_all_statements?Boolean

Validates the Sneaql command tag and arguments

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sneaql_lib/parser.rb', line 66

def valid_arguments_in_all_statements?
  all_statements_valid = true
  @statements.each_with_index do |_s, i|
    cmd = command_at_index(i)
    @logger.debug("validating #{cmd}")
    unless statement_args_are_valid?(cmd)
      all_statements_valid = false
      @logger.info "argument validation error: #{cmd}"
    end
  end
  return all_statements_valid
end