Class: SXP::Reader::Basic

Inherits:
SXP::Reader show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/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/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/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.end_with?('.') ? "#{buffer}0" : buffer)
    when INTEGER  then Integer(buffer)
    else buffer.to_sym
  end
end

#read_characterString

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/lib/sxp/reader/basic.rb', line 55

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(Encoding::UTF_8)
    when ?U  then read_chars(8).to_i(16).chr(Encoding::UTF_8)
    when ?"  then char #"
    when ?\\ then char
    else char
  end
end

#read_literalString

Returns:



72
73
74
75
76
77
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/lib/sxp/reader/basic.rb', line 72

def read_literal
  grammar = self.class.const_get(:ATOM)
  buffer = ""
  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
50
51
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/lib/sxp/reader/basic.rb', line 37

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

  # Return string, annotating it with the quotation style used
  buffer.tap {|s| s.quote_style = (quote_char == '"' ? :dquote : :squote)}
end

#read_tokenObject

Returns:



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

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