Class: SXP::Reader::CommonLisp

Inherits:
Basic show all
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb

Overview

A Common Lisp S-expressions parser.

Constant Summary collapse

OPTIONS =
{nil: nil, t: true, quote: :quote, function: :function}
DECIMAL =
/^[+-]?(\d*)?\.\d*$/
INTEGER_BASE_2 =
/^[+-]?[01]+$/
INTEGER_BASE_8 =
/^[+-]?[0-7]+$/
INTEGER_BASE_10 =
/^[+-]?\d+$/
INTEGER_BASE_16 =
/^[+-]?[\da-z]+$/i
RATIONAL =
/^([+-]?\d+)\/(\d+)$/
CHARACTERS =

Escape characters, used in the form #\Backspace. Case is treated insensitively

{
  'newline'   => "\n",
  'space'     => " ",
  'backspace' => "\b",   # \010 BS
  'tab'       => "\t",   # \011 HT
  'linefeed'  => "\n",   # \012 LF
  'page'      => "\f",   # \014 FF
  'return'    => "\r",   # \015 CR
  'rubout'    => "\x7F", # \177 DEL
}

Constants inherited from Basic

Basic::ATOM, Basic::INTEGER, Basic::LPARENS, Basic::RPARENS

Instance Attribute Summary

Attributes inherited from SXP::Reader

#input, #options

Instance Method Summary collapse

Methods inherited from Basic

#read_atom, #read_literal, #read_string

Methods inherited from SXP::Reader

#each, #eof?, #peek_char, #read, read, read_all, #read_all, #read_atom, #read_char, #read_chars, read_file, #read_files, #read_integer, #read_list, #read_literal, #read_string, read_url, #skip_line, #unread

Constructor Details

#initialize(input, options = {}, &block) ⇒ CommonLisp

Initializes the reader.

Parameters:

Options Hash (options):

  • :nil (Object) — default: nil
  • :t (Object) — default: true
  • :quote (Object) — default: :quote
  • :function (Object) — default: :function


40
41
42
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 40

def initialize(input, options = {}, &block)
  super(input, OPTIONS.merge(options), &block)
end

Instance Method Details

#read_characterString

Read characters sequences like #\backspace. Otherwise, reads a single character. Requires the ability to put eroneously read characters back in the input stream



118
119
120
121
122
123
124
125
126
127
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 118

def read_character
  lit = read_literal

  return " " if lit.empty? && peek_char == " "
  CHARACTERS.fetch(lit.downcase) do |string|
    # Return just the first character
    unread(string[1..-1])
    string[0,1]
  end
end

#read_functionArray

Reads #'mapcar forms.

Returns:



107
108
109
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 107

def read_function
  [options[:function] || :function, read]
end

#read_quoteArray

Reads 'foobar forms.

Returns:



98
99
100
101
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 98

def read_quote
  skip_char # "'"
  [options[:quote] || :quote, read]
end

#read_sharpObject

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 57

def read_sharp
  skip_char # '#'
  case char = read_char
    when ?b, ?B  then read_integer(2)
    when ?o, ?O  then read_integer(8)
    when ?x, ?X  then read_integer(16)
    when ?\\     then read_character
    when ?(      then read_vector
    when ?'      then read_function
    else raise Error, "invalid sharp-sign read syntax: ##{char.chr}"
  end
end

#read_symbol(delimiter = nil) ⇒ Symbol

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 72

def read_symbol(delimiter = nil)
  buffer = String.new
  skip_char # '|'
  until delimiter === peek_char
    buffer <<
      case char = read_char
        when ?\\ then read_character
        else char
      end
  end
  skip_char # '|'
  buffer.to_sym
end

#read_tokenObject

Returns:



46
47
48
49
50
51
52
53
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 46

def read_token
  case peek_char
    when ?#  then [:atom, read_sharp]
    when ?|  then [:atom, read_symbol(?|)]
    when ?'  then [:atom, read_quote]
    else super
  end
end

#read_vectorArray

Reads #(1 2 3) forms.

Returns:

Raises:

  • (NotImplementedError)


90
91
92
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 90

def read_vector
  raise NotImplementedError, "#{self.class}#read_vector" # TODO
end

#skip_comments

This method returns an undefined value.



131
132
133
134
135
136
137
138
139
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/common_lisp.rb', line 131

def skip_comments
  until eof?
    case (char = peek_char).chr
      when /\s+/ then skip_char
      when /;/   then skip_line
      else break
    end
  end
end