You are here

protected function EasyRdf_Parser_Turtle::parseQNameOrBoolean in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php \EasyRdf_Parser_Turtle::parseQNameOrBoolean()

Parses qnames and boolean values, which have equivalent starting characters. @ignore

1 call to EasyRdf_Parser_Turtle::parseQNameOrBoolean()
EasyRdf_Parser_Turtle::parseValue in vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php
Parses an RDF value. This method parses uriref, qname, node ID, quoted literal, integer, double and boolean. @ignore

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php, line 913

Class

EasyRdf_Parser_Turtle
Class to parse Turtle with no external dependancies.

Code

protected function parseQNameOrBoolean() {

  // First character should be a ':' or a letter
  $c = $this
    ->read();
  if ($c == -1) {
    throw new EasyRdf_Parser_Exception("Turtle Parse Error: unexpected end of file while readying value", $this->line, $this->column);
  }
  if ($c != ':' && !self::isPrefixStartChar($c)) {
    throw new EasyRdf_Parser_Exception("Turtle Parse Error: expected a ':' or a letter, found '{$c}'", $this->line, $this->column);
  }
  $namespace = null;
  if ($c == ':') {

    // qname using default namespace
    if (isset($this->namespaces[''])) {
      $namespace = $this->namespaces[''];
    }
    else {
      throw new EasyRdf_Parser_Exception("Turtle Parse Error: default namespace used but not defined", $this->line, $this->column);
    }
  }
  else {

    // $c is the first letter of the prefix
    $prefix = $c;
    $c = $this
      ->read();
    while (self::isPrefixChar($c)) {
      $prefix .= $c;
      $c = $this
        ->read();
    }
    if ($c != ':') {

      // prefix may actually be a boolean value
      $value = $prefix;
      if ($value == "true" || $value == "false") {
        return array(
          'type' => 'literal',
          'value' => $value,
          'datatype' => EasyRdf_Namespace::get('xsd') . 'boolean',
        );
      }
    }
    $this
      ->verifyCharacterOrFail($c, ":");
    if (isset($this->namespaces[$prefix])) {
      $namespace = $this->namespaces[$prefix];
    }
    else {
      throw new EasyRdf_Parser_Exception("Turtle Parse Error: namespace prefix '{$prefix}' used but not defined", $this->line, $this->column);
    }
  }

  // $c == ':', read optional local name
  $localName = '';
  $c = $this
    ->read();
  if (self::isNameStartChar($c)) {
    if ($c == '\\') {
      $localName .= $this
        ->readLocalEscapedChar();
    }
    else {
      $localName .= $c;
    }
    $c = $this
      ->read();
    while (self::isNameChar($c)) {
      if ($c == '\\') {
        $localName .= $this
          ->readLocalEscapedChar();
      }
      else {
        $localName .= $c;
      }
      $c = $this
        ->read();
    }
  }

  // Unread last character
  $this
    ->unread($c);

  // Note: namespace has already been resolved
  return array(
    'type' => 'uri',
    'value' => $namespace . $localName,
  );
}