Class: SXP::Reader::SPARQL

Inherits:
Extended show all
Defined in:
lib/sxp/reader/sparql.rb

Overview

A SPARQL Syntax Expressions (SSE) parser.

Requires RDF.rb.

See Also:

Constant Summary

NIL =
/^nil$/i
FALSE =
/^false$/i
TRUE =
/^true$/i
EXPONENT =
/[eE][+-]?[0-9]+/
DECIMAL =
/^[+-]?(\d*)?\.\d*#{EXPONENT}?$/
BNODE_ID =

FIXME

/^_:([A-Za-z][A-Za-z0-9]*)/
BNODE_NEW =
/^_:$/
VAR_ID =

FIXME

/^\?([A-Za-z][A-Za-z0-9]*)/
VAR_GEN =
/^\?\?([0-9]+)/
VAR_NEW =
'??'
URIREF =
/^<([^>]+)>/

Constants inherited from Extended

ATOM, LPARENS, RPARENS

Constants inherited from Basic

ATOM, DECIMAL, INTEGER, LPARENS, RATIONAL, RPARENS

Instance Method Summary (collapse)

Methods inherited from Basic

#read_character, #read_literal, #read_string

Methods inherited from SXP::Reader

#each, #eof?, #initialize, #peek_char, read, #read, read_all, #read_all, #read_char, #read_character, #read_chars, read_file, read_files, #read_integer, #read_list, #read_literal, #read_sharp, #read_string, read_url, #skip_line

Constructor Details

This class inherits a constructor from SXP::Reader

Instance Method Details

- (Object) read_atom

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sxp/reader/sparql.rb', line 65

def read_atom
  case buffer = read_literal
    when '.'       then buffer.to_sym
    when NIL       then nil
    when FALSE     then RDF::Literal(false)
    when TRUE      then RDF::Literal(true)
    when DECIMAL   then RDF::Literal(Float(buffer[-1].eql?(?.) ? buffer + '0' : buffer))
    when INTEGER   then RDF::Literal(Integer(buffer))
    when BNODE_ID  then RDF::Node($1)
    when BNODE_NEW then RDF::Node.new
    when VAR_ID    then RDF::Query::Variable.new($1)
    when VAR_GEN   then RDF::Query::Variable.new("?#{$1}") # FIXME?
    when VAR_NEW   then RDF::Query::Variable.new
    else buffer.to_sym
  end
end

- (RDF::Literal) read_rdf_literal

Returns:

  • (RDF::Literal)


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

def read_rdf_literal
  value   = read_string
  options = case peek_char
    when ?@
      skip_char # '@'
      {:language => read_atom}
    when ?^
      2.times { skip_char } # '^^'
      {:datatype => read_rdf_uri} # TODO: support prefixed names
    else {}
  end
  RDF::Literal(value, options)
end

- (RDF::URI) read_rdf_uri

Returns:

  • (RDF::URI)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sxp/reader/sparql.rb', line 51

def read_rdf_uri
  buffer = String.new
  skip_char # '<'
  return :< if (char = peek_char).nil? || char.chr !~ ATOM # FIXME: nasty special case for the '< symbol
  return :<= if peek_char.chr.eql?(?=.chr) && read_char    # FIXME: nasty special case for the '<= symbol
  until peek_char == ?>
    buffer << read_char # TODO: unescaping
  end
  skip_char # '>'
  RDF::URI(buffer)
end

- (Object) read_token

Returns:



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

def read_token
  case peek_char
    when ?" then [:atom, read_rdf_literal] # "
    when ?< then [:atom, read_rdf_uri]
    else super
  end
end

- skip_comments

This method returns an undefined value.



84
85
86
87
88
89
90
91
92
93
# File 'lib/sxp/reader/sparql.rb', line 84

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