Module: HRX::Util

Defined in:
lib/hrx/util.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.parse_error(scanner, message, file: nil) ⇒ Object

Emits an ArgumentError with the given ‘message` and line and column information from the current position of `scanner`.

Raises:



39
40
41
42
43
44
45
# File 'lib/hrx/util.rb', line 39

def parse_error(scanner, message, file: nil)
  before = scanner.string.byteslice(0...scanner.pos)
  line = before.count("\n") + 1
  column = (before[/^.*\z/] || "").length + 1

  raise HRX::ParseError.new(message, line, column, file: file)
end

.relative(parent, child) ⇒ Object

Returns ‘child` relative to `parent`.

Assumes ‘parent` ends with `/`, and `child` is beneath `parent`.

If ‘parent` is `nil`, returns `child` as-is.



52
53
54
55
# File 'lib/hrx/util.rb', line 52

def relative(parent, child)
  return child unless parent
  child[parent.length..-1]
end

.scan_path(scanner, assert_done: true, file: nil) ⇒ Object

Scans a single HRX path from ‘scanner` and returns it.

Throws an ArgumentError if no valid path is available to scan. If ‘assert_done` is `true`, throws an ArgumentError if there’s any text after the path.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hrx/util.rb', line 24

def scan_path(scanner, assert_done: true, file: nil)
  start = scanner.pos
  while _scan_component(scanner, file) && scanner.scan(%r{/}); end

  if assert_done && !scanner.eos?
    parse_error(scanner, "Paths may not contain newlines", file: file)
  elsif scanner.pos == start
    parse_error(scanner, "Expected a path", file: file)
  end

  scanner.string.byteslice(start...scanner.pos)
end