Class: SXP::Reader::CommonLisp

Inherits:
Basic show all
Defined in:
lib/sxp/reader/common_lisp.rb

Overview

A Common Lisp S-expressions parser.

See Also:

Constant Summary

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 =
{
  '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

ATOM, DECIMAL, INTEGER, LPARENS, RATIONAL, RPARENS

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

Constructor Details

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

Initializes the reader.

Parameters:

Options Hash (options):

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


37
38
39
# File 'lib/sxp/reader/common_lisp.rb', line 37

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

Instance Method Details

- (String) read_character



111
112
113
# File 'lib/sxp/reader/common_lisp.rb', line 111

def read_character
  super
end

- (Array) read_function

Reads #'mapcar forms.

Returns:



104
105
106
# File 'lib/sxp/reader/common_lisp.rb', line 104

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

- (Array) read_quote

Reads 'foobar forms.

Returns:



95
96
97
98
# File 'lib/sxp/reader/common_lisp.rb', line 95

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

- (Object) read_sharp

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sxp/reader/common_lisp.rb', line 54

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

- (Symbol) read_symbol(delimiter = nil)

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sxp/reader/common_lisp.rb', line 69

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

- (Object) read_token

Returns:



43
44
45
46
47
48
49
50
# File 'lib/sxp/reader/common_lisp.rb', line 43

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

- (Array) read_vector

Reads #(1 2 3) forms.

Returns:

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/sxp/reader/common_lisp.rb', line 87

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

- skip_comments

This method returns an undefined value.



117
118
119
120
121
122
123
124
125
# File 'lib/sxp/reader/common_lisp.rb', line 117

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