Module: CFG

Defined in:
lib/CFG/config.rb,
lib/CFG/version.rb

Defined Under Namespace

Modules: Utils Classes: ASTNode, BadIndexError, BinaryNode, CircularReferenceError, Config, ConfigError, DictWrapper, Evaluator, InvalidPathError, ListNode, ListWrapper, Location, MappingNode, Parser, ParserError, RecognizerError, SliceNode, Token, Tokenizer, TokenizerError, UnaryNode

Constant Summary collapse

PUNCTUATION =
{
  ':' => :COLON,
  '-' => :MINUS,
  '+' => :PLUS,
  '*' => :STAR,
  '/' => :SLASH,
  '%' => :MODULO,
  ',' => :COMMA,
  '{' => :LEFT_CURLY,
  '}' => :RIGHT_CURLY,
  '[' => :LEFT_BRACKET,
  ']' => :RIGHT_BRACKET,
  '(' => :LEFT_PARENTHESIS,
  ')' => :RIGHT_PARENTHESIS,
  '@' => :AT,
  '$' => :DOLLAR,
  '<' => :LESS_THAN,
  '>' => :GREATER_THAN,
  '!' => :NOT,
  '~' => :BITWISE_COMPLEMENT,
  '&' => :BITWISE_AND,
  '|' => :BITWISE_OR,
  '^' => :BITWISE_XOR,
  '.' => :DOT
}.freeze
KEYWORDS =
{
  'true' => :TRUE,
  'false' => :FALSE,
  'null' => :NONE,
  'is' => :IS,
  'in' => :IN,
  'not' => :NOT,
  'and' => :AND,
  'or' => :OR
}.freeze
ESCAPES =
{
  'a' =>  "\u0007",   # rubocop: disable Layout/HashAlignment
  'b' =>  "\b",       # rubocop: disable Layout/HashAlignment
  'f' =>  "\u000C",   # rubocop:disable Layout/HashAlignment
  'n' =>  "\n",       # rubocop:disable Layout/HashAlignment
  'r' =>  "\r",       # rubocop:disable Layout/HashAlignment
  't' =>  "\t",       # rubocop:disable Layout/HashAlignment
  'v' =>  "\u000B",   # rubocop:disable Layout/HashAlignment
  '\\' => '\\',
  '\'' => "'",
  '"' =>  '"'         # rubocop:disable Layout/HashAlignment
}.freeze
NULL_VALUE =
Object.new
KEYWORD_VALUES =
{
  TRUE: true,
  FALSE: false,
  NONE: NULL_VALUE
}.freeze
SCALAR_TOKENS =
VERSION =
'0.1.0'.freeze

Instance Method Summary collapse

Instance Method Details

#_visit(node, collector) ⇒ Object



1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
# File 'lib/CFG/config.rb', line 1889

def _visit(node, collector)
  if node.is_a? Token
    collector.push node
  elsif node.is_a? UnaryNode
    _visit(node.operand, collector)
  elsif node.is_a? BinaryNode
    _visit(node.lhs, collector)
    case node.kind
    when :DOT
      collector.push [node.kind, node.rhs.value]
    when :COLON # it's a slice
      collector.push [node.kind, node.rhs]
    else # it's an array access
      collector.push [node.kind, node.rhs.value]
    end
  end
end

#make_parser(src) ⇒ Object



1285
1286
1287
1288
# File 'lib/CFG/config.rb', line 1285

def make_parser(src)
  stream = StringIO.new src, 'r:utf-8'
  Parser.new stream
end

#make_tokenizer(src) ⇒ Object



761
762
763
764
# File 'lib/CFG/config.rb', line 761

def make_tokenizer(src)
  stream = StringIO.new src, 'r:utf-8'
  Tokenizer.new stream
end

#parse_path(src) ⇒ Object

Raises:



1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
# File 'lib/CFG/config.rb', line 1843

def parse_path(src)
  parser = make_parser src

  raise InvalidPathError, "Invalid path: #{src}" if parser.next_token.kind != :WORD

  begin
    result = parser.primary
  rescue RecognizerError
    raise InvalidPathError, "Invalid path: #{src}"
  end
  raise InvalidPathError, "Invalid path: #{src}" unless parser.at_end

  result
end

#string_for(obj) ⇒ Object



1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/CFG/config.rb', line 1290

def string_for(obj)
  if obj.is_a? Array
    parts = obj.map { |it| string_for it }
    "[#{parts.join ', '}]"
  elsif obj.is_a? Hash
    parts = obj.map { |k, v| "#{k}: #{string_for v}" }
    "{#{parts.join ', '}}"
  else
    obj.to_s
  end
end

#to_source(node) ⇒ Object



1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
# File 'lib/CFG/config.rb', line 1858

def to_source(node)
  if node.is_a? Token
    node.value.to_s
  elsif !node.is_a? ASTNode
    node.to_s
  else
    result = []
    parts = unpack_path node
    result.push parts.shift.value
    parts.each do |part|
      op, operand = part
      case op
      when :DOT
        result.push ".#{operand}"
      when :COLON
        result.append '['
        result.append to_source(operand.start_index) unless operand.start_index.nil?
        result.append ':'
        result.append to_source(operand.stop_index) unless operand.stop_index.nil?
        result.append ":#{to_source operand.step}" unless operand.step.nil?
        result.append ']'
      when :LEFT_BRACKET
        result.append "[#{to_source operand}]"
      else
        raise ConfigError, "unable to compute source for #{node}"
      end
    end
    result.join('')
  end
end

#unpack_path(start) ⇒ Object



1907
1908
1909
1910
1911
# File 'lib/CFG/config.rb', line 1907

def unpack_path(start)
  result = []
  _visit(start, result)
  result
end

#unwrap(obj) ⇒ Object



1833
1834
1835
1836
1837
1838
1839
1840
1841
# File 'lib/CFG/config.rb', line 1833

def unwrap(obj)
  if obj.is_a? DictWrapper
    obj.as_dict
  elsif obj.is_a? ListWrapper
    obj.as_list
  else
    obj
  end
end