Method: SXP::Reader::SPARQL#read_token

Defined in:
lib/sxp/reader/sparql.rb

#read_tokenObject

Reads SSE Tokens, including RDF::Literal, RDF::URI and RDF::Node.

Performs forward reference for prefix and base URI representations and saves in #base_uri and #prefixes accessors.

Transforms tokens matching a PNAME pattern into RDF::URI instances if a match is found with a previously identified PREFIX.

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sxp/reader/sparql.rb', line 90

def read_token
  case peek_char
  when ?" then [:atom, read_rdf_literal] # "
  when ?< then [:atom, read_rdf_uri]
  else
    tok = super
    
    # If we just parsed "PREFIX", and this is an opening list, then
    # record list depth and process following as token, URI pairs
    #
    # Once we've closed the list, go out of prefix mode
    if tok.is_a?(Array) && tok[0] == :list
      if '(['.include?(tok[1])
        @list_depth += 1
      else
        @list_depth -= 1
        @prefix_depth = nil if @prefix_depth && @list_depth < @prefix_depth
      end
    end

    if tok.is_a?(Array) && tok[0] == :atom && tok[1].is_a?(Symbol)
      value = tok[1].to_s

      # We previously parsed a PREFIX, this will be the map value
      @parsed_prefix = value.chop if @prefix_depth && @prefix_depth > 0
      
      # If we just saw PREFIX, then this starts the parsing mode
      @prefix_depth = @list_depth + 1 if value =~ PREFIX
      
      # If the token is of the form 'prefix:suffix', create a URI and give it the
      # token as a QName
      if value.to_s =~ PNAME && base = prefix($1)
        suffix = $2
        #STDERR.puts "read_tok lexical: pfx: #{$1.inspect} => #{prefix($1).inspect}, sfx: #{suffix.inspect}"
        suffix = suffix.sub(/^\#/, "") if base.to_s.index("#")
        uri = RDF::URI(base.to_s + suffix)
        #STDERR.puts "read_tok lexical uri: #{uri.inspect}"

        # Cause URI to be serialized as a lexical
        uri.lexical = value
        [:atom, uri]
      else
        tok
      end
    else
      tok
    end
  end
end