module TL1::AST

Namespace for AST Nodes in input and output formats

Constants

NODES_BY_CLASS
NODES_BY_NAME

Public Instance Methods

colon_separated_element(source) click to toggle source
# File lib/tl1/ast.rb, line 11
                def colon_separated_element(source)
  return from_json(source) if source.is_a?(Hash)
  raise "Unparseable element #{source}" unless source.is_a?(String)

  if source.include?('=')
    CommaSeparatedKeywordVariables.parse(source)
  elsif source.include?(',')
    CommaSeparatedVariables.parse(source)
  elsif source.start_with?('<') && source.end_with?('>')
    Variable.parse(source)
  else
    Literal.parse(source)
  end
end
from_json(source) click to toggle source
# File lib/tl1/ast.rb, line 26
                def from_json(source)
  node = NODES_BY_NAME.fetch(source['node']) do
    raise "Unknown node type #{source['node']}"
  end

  node.parse(source['fields'])
end
parse_message_format(source) click to toggle source
# File lib/tl1/ast.rb, line 7
                def parse_message_format(source)
  ColonSeparatedVariables.parse(source)
end
remove_quotes(string) click to toggle source
# File lib/tl1/ast.rb, line 68
                def remove_quotes(string)
  if string.start_with?('"') && string.end_with?('"')
    string[1..-2]
  else
    string
  end
end
split(string, delimiter) click to toggle source
# File lib/tl1/ast.rb, line 34
                def split(string, delimiter)
  scanner = StringScanner.new(string)
  array = [String.new]

  loop do
    return array if scanner.eos?
    char = scanner.getch
    case char
    when delimiter
      array << String.new
    when '"'
      array.last << split_quoted(scanner)
    else
      array.last << char
    end
  end
end
split_quoted(scanner) click to toggle source
# File lib/tl1/ast.rb, line 52
                def split_quoted(scanner)
  string = String.new('"')

  loop do
    raise 'Unexpected end of quoted string' if scanner.eos?
    char = scanner.getch
    case char
    when '"'
      string << '"'
      return string
    else
      string << char
    end
  end
end