module Rbscmlex

Constants

RELEASE
Token

a structure to store properties of a token of Scheme program.

VERSION

Public Class Methods

hash2json(hash) click to toggle source

Converts a Hash object to a string of JSON notation.

# File lib/rbscmlex/token.rb, line 113
def hash2json(hash)
  JSON.generate(hash)
end
hash2token(hash) click to toggle source

Converts a Hash object, which has type and literal as its key, to a new token object. The value associated to type of the Hash must be valid token type. Otherwise, raises UnknownTokenTypeError.

# File lib/rbscmlex/token.rb, line 75
def hash2token(hash)
  if hash.key?(:type) and hash.key?(:literal)
    type = hash[:type].intern
    raise UnknownTokenTypeError, ("got=%s" % type) unless token_type?(type)
    literal = hash[:literal]
    new_token(type, literal)
  else
    raise InvalidHashError, ("got=%s" % hash)
  end
end
json2hash(json) click to toggle source

Converts a JSON notation to a new Hash object.

# File lib/rbscmlex/token.rb, line 120
def json2hash(json)
  JSON.parse(json)
end
json2token(json) click to toggle source

Converts a JSON notation, which hash type and literal, to a new token object. The value associated to type of the JSON must be valid token type. Otherwise, raises UnknownTokenTypeError.

# File lib/rbscmlex/token.rb, line 90
def json2token(json)
  h = JSON.parse(json, symbolize_names: true)
  begin
    hash2token(h)
  rescue InvalidHashError => _
    raise InvalidJsonError, ("got=%s" % json)
  end
end
lexer(source) click to toggle source
# File lib/rbscmlex.rb, line 9
def self.lexer(source)
  Lexer.new(source)
end
make_hash(type, literal) click to toggle source

Returns a new Hash object with type and literal as its keys.

# File lib/rbscmlex/token.rb, line 67
def make_hash(type, literal)
  {type: type, literal: literal}
end
new_token(type, literal = nil) click to toggle source

Instantiates a new token object form type and literal.

# File lib/rbscmlex/token.rb, line 55
def new_token(type, literal = nil)
  Token.new(type, literal)
end
token2hash(token) click to toggle source

Converts a Token object to a Hash object.

# File lib/rbscmlex/token.rb, line 101
def token2hash(token)
  token.to_h
end
token2json(token) click to toggle source

Converts a Token object to a string of JSON notation.

# File lib/rbscmlex/token.rb, line 107
def token2json(token)
  token.to_json
end
token_type?(type) click to toggle source

Returns true when the argument is valid token type.

# File lib/rbscmlex/token.rb, line 61
def token_type?(type)
  TOKEN_TYPES.include?(type)
end