Skip to content

Commit

Permalink
Merge pull request #88 from jane-olszewska/schema-description-in-comm…
Browse files Browse the repository at this point in the history
…ents

Schema Definition Language: element descriptions can be set through comments
  • Loading branch information
vladar committed Feb 7, 2017
2 parents c18cd16 + 008fd20 commit c2f0749
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/Language/AST/EnumTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ class EnumTypeDefinitionNode extends Node implements TypeDefinitionNode
* @var EnumValueDefinitionNode[]
*/
public $values;

/**
* @var string
*/
public $description;
}
5 changes: 5 additions & 0 deletions src/Language/AST/EnumValueDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ class EnumValueDefinitionNode extends Node
* @var DirectiveNode[]
*/
public $directives;

/**
* @var string
*/
public $description;
}
5 changes: 5 additions & 0 deletions src/Language/AST/FieldDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ class FieldDefinitionNode extends Node
* @var DirectiveNode[]
*/
public $directives;

/**
* @var string
*/
public $description;
}
5 changes: 5 additions & 0 deletions src/Language/AST/InputObjectTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ class InputObjectTypeDefinitionNode extends Node implements TypeDefinitionNode
* @var InputValueDefinitionNode[]
*/
public $fields;

/**
* @var string
*/
public $description;
}
5 changes: 5 additions & 0 deletions src/Language/AST/InputValueDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ class InputValueDefinitionNode extends Node
* @var DirectiveNode[]
*/
public $directives;

/**
* @var string
*/
public $description;
}
5 changes: 5 additions & 0 deletions src/Language/AST/InterfaceTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ class InterfaceTypeDefinitionNode extends Node implements TypeDefinitionNode
* @var FieldDefinitionNode[]
*/
public $fields = [];

/**
* @var string
*/
public $description;
}
5 changes: 5 additions & 0 deletions src/Language/AST/ObjectTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ class ObjectTypeDefinitionNode extends Node implements TypeDefinitionNode
* @var FieldDefinitionNode[]
*/
public $fields;

/**
* @var string
*/
public $description;
}
5 changes: 5 additions & 0 deletions src/Language/AST/ScalarTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ class ScalarTypeDefinitionNode extends Node implements TypeDefinitionNode
* @var DirectiveNode[]
*/
public $directives;

/**
* @var string
*/
public $description;
}
5 changes: 5 additions & 0 deletions src/Language/AST/UnionTypeDefinitionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ class UnionTypeDefinitionNode extends Node implements TypeDefinitionNode
* @var NamedTypeNode[]
*/
public $types = [];

/**
* @var string
*/
public $description;
}
2 changes: 1 addition & 1 deletion src/Language/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ private function readComment($start, $line, $col, Token $prev)
$line,
$col,
$prev,
mb_substr($body, $start + 1, $position - $start + 1, 'UTF-8')
mb_substr($body, $start + 1, $position - $start, 'UTF-8')
);
}
}
68 changes: 59 additions & 9 deletions src/Language/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,10 +910,13 @@ function parseScalarTypeDefinition()
$name = $this->parseName();
$directives = $this->parseDirectives();

$description = $this->getDescriptionFromAdjacentCommentTokens($start);

return new ScalarTypeDefinitionNode([
'name' => $name,
'directives' => $directives,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand All @@ -935,12 +938,15 @@ function parseObjectTypeDefinition()
Token::BRACE_R
);

$description = $this->getDescriptionFromAdjacentCommentTokens($start);

return new ObjectTypeDefinitionNode([
'name' => $name,
'interfaces' => $interfaces,
'directives' => $directives,
'fields' => $fields,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand Down Expand Up @@ -972,12 +978,15 @@ function parseFieldDefinition()
$type = $this->parseTypeReference();
$directives = $this->parseDirectives();

$description = $this->getDescriptionFromAdjacentCommentTokens($start);

return new FieldDefinitionNode([
'name' => $name,
'arguments' => $args,
'type' => $type,
'directives' => $directives,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand Down Expand Up @@ -1007,12 +1016,14 @@ function parseInputValueDef()
$defaultValue = $this->parseConstValue();
}
$directives = $this->parseDirectives();
$description = $this->getDescriptionFromAdjacentCommentTokens($start);
return new InputValueDefinitionNode([
'name' => $name,
'type' => $type,
'defaultValue' => $defaultValue,
'directives' => $directives,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand All @@ -1032,11 +1043,14 @@ function parseInterfaceTypeDefinition()
Token::BRACE_R
);

$description = $this->getDescriptionFromAdjacentCommentTokens($start);

return new InterfaceTypeDefinitionNode([
'name' => $name,
'directives' => $directives,
'fields' => $fields,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand All @@ -1053,11 +1067,14 @@ function parseUnionTypeDefinition()
$this->expect(Token::EQUALS);
$types = $this->parseUnionMembers();

$description = $this->getDescriptionFromAdjacentCommentTokens($start);

return new UnionTypeDefinitionNode([
'name' => $name,
'directives' => $directives,
'types' => $types,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand Down Expand Up @@ -1093,11 +1110,14 @@ function parseEnumTypeDefinition()
Token::BRACE_R
);

$description = $this->getDescriptionFromAdjacentCommentTokens($start);

return new EnumTypeDefinitionNode([
'name' => $name,
'directives' => $directives,
'values' => $values,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand All @@ -1110,10 +1130,13 @@ function parseEnumValueDefinition()
$name = $this->parseName();
$directives = $this->parseDirectives();

$description = $this->getDescriptionFromAdjacentCommentTokens($start);

return new EnumValueDefinitionNode([
'name' => $name,
'directives' => $directives,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand All @@ -1133,11 +1156,14 @@ function parseInputObjectTypeDefinition()
Token::BRACE_R
);

$description = $this->getDescriptionFromAdjacentCommentTokens($start);

return new InputObjectTypeDefinitionNode([
'name' => $name,
'directives' => $directives,
'fields' => $fields,
'loc' => $this->loc($start)
'loc' => $this->loc($start),
'description' => $description
]);
}

Expand Down Expand Up @@ -1193,4 +1219,28 @@ function parseDirectiveLocations()
} while ($this->skip(Token::PIPE));
return $locations;
}

/**
* @param Token $nameToken
* @return null|string
*/
private function getDescriptionFromAdjacentCommentTokens(Token $nameToken)
{
$description = null;

$currentToken = $nameToken;
$previousToken = $currentToken->prev;

while ($previousToken->kind == Token::COMMENT
&& ($previousToken->line + 1) == $currentToken->line
) {
$description = $previousToken->value . $description;

// walk the tokens backwards until no longer adjacent comments
$currentToken = $previousToken;
$previousToken = $currentToken->prev;
}

return $description;
}
}

0 comments on commit c2f0749

Please sign in to comment.