Class: SXP::Reader::Basic

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

Overview

A basic S-expression parser.

Direct Known Subclasses

CommonLisp, Extended

Constant Summary collapse

LPARENS =
[?(]
RPARENS =
[?)]
ATOM =
/^[^\s()]+/
RATIONAL =
/^([+-]?\d+)\/(\d+)$/
DECIMAL =
/^[+-]?(\d*)?\.\d*$/
INTEGER =
/^[+-]?\d+$/

Instance Attribute Summary

Attributes inherited from SXP::Reader

#input, #options

Instance Method Summary collapse

Methods inherited from SXP::Reader

#each, #eof?, #initialize, #peek_char, #read, read, read_all, #read_all, #read_char, #read_chars, read_file, #read_files, #read_integer, #read_list, #read_sharp, read_url, #skip_comments, #skip_line, #unread

Constructor Details

This class inherits a constructor from SXP::Reader

Instance Method Details

#read_atomObject

Returns:



25
26
27
28
29
30
31
32
33
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/basic.rb', line 25

def read_atom
  case buffer = read_literal
    when '.'      then buffer.to_sym
    when RATIONAL then Rational($1.to_i, $2.to_i)
    when DECIMAL  then Float(buffer) # FIXME?
    when INTEGER  then Integer(buffer)
    else buffer.to_sym
  end
end

#read_characterString

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/basic.rb', line 53

def read_character
  case char = read_char
    when ?b  then ?\b
    when ?f  then ?\f
    when ?n  then ?\n
    when ?r  then ?\r
    when ?t  then ?\t
    when ?u  then read_chars(4).to_i(16).chr
    when ?U  then read_chars(8).to_i(16).chr
    when ?"  then char #"
    when ?\\ then char
    else char
  end
end

#read_literalString

Returns:



70
71
72
73
74
75
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/basic.rb', line 70

def read_literal
  grammar = self.class.const_get(:ATOM)
  buffer = String.new
  buffer << read_char while !eof? && peek_char.chr =~ grammar
  buffer
end

#read_stringString

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/basic.rb', line 37

def read_string
  buffer = String.new
  skip_char # '"'
  until peek_char == ?" #"
    buffer <<
      case char = read_char
        when ?\\ then read_character
        else char
      end
  end
  skip_char # '"'
  buffer
end

#read_tokenObject

Returns:



15
16
17
18
19
20
21
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sxp.rb-77c5073a7739/lib/sxp/reader/basic.rb', line 15

def read_token
  case peek_char
    when ?(, ?) then [:list, read_char]
    when ?"     then [:atom, read_string] #"
    else super
  end
end