Class: SXP::Reader::Basic

Inherits:
SXP::Reader show all
Defined in:
lib/sxp/reader/basic.rb

Overview

A basic S-expression parser.

Direct Known Subclasses

CommonLisp, Extended

Constant Summary

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

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

Constructor Details

This class inherits a constructor from SXP::Reader

Instance Method Details

- (Object) read_atom

Returns:



24
25
26
27
28
29
30
31
32
# File 'lib/sxp/reader/basic.rb', line 24

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

- (String) read_character

Returns:



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

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

- (String) read_literal

Returns:



69
70
71
72
73
74
# File 'lib/sxp/reader/basic.rb', line 69

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

- (String) read_string

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sxp/reader/basic.rb', line 36

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

- (Object) read_token

Returns:



14
15
16
17
18
19
20
# File 'lib/sxp/reader/basic.rb', line 14

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