Browse Source

Some fixes in the DOT parser

css_transitions
josdejong 11 years ago
parent
commit
0922851d46
4 changed files with 59 additions and 49 deletions
  1. +1
    -1
      src/graph/Edge.js
  2. +25
    -20
      src/graph/dotparser.js
  3. +27
    -22
      vis.js
  4. +6
    -6
      vis.min.js

+ 1
- 1
src/graph/Edge.js View File

@ -457,7 +457,7 @@ Edge.prototype._drawArrowEnd = function(ctx) {
var dy = (this.to.y - this.from.y); var dy = (this.to.y - this.from.y);
var lEdge = Math.sqrt(dx * dx + dy * dy); var lEdge = Math.sqrt(dx * dx + dy * dy);
var lFrom = this.to.distanceToBorder(ctx, angle + Math.PI);
var lFrom = this.from.distanceToBorder(ctx, angle + Math.PI);
var pFrom = (lEdge - lFrom) / lEdge; var pFrom = (lEdge - lFrom) / lEdge;
var xFrom = (pFrom) * this.from.x + (1 - pFrom) * this.to.x; var xFrom = (pFrom) * this.from.x + (1 - pFrom) * this.to.x;
var yFrom = (pFrom) * this.from.y + (1 - pFrom) * this.to.y; var yFrom = (pFrom) * this.from.y + (1 - pFrom) * this.to.y;

+ 25
- 20
src/graph/dotparser.js View File

@ -12,16 +12,15 @@
return parseGraph(); return parseGraph();
} }
// token types enumeration
// token types enumeration
var TOKENTYPE = { var TOKENTYPE = {
NULL : 0, NULL : 0,
DELIMITER : 1, DELIMITER : 1,
NUMBER : 2,
STRING : 3,
UNKNOWN : 4
IDENTIFIER: 2,
UNKNOWN : 3
}; };
// map with all delimiters
// map with all delimiters
var DELIMITERS = { var DELIMITERS = {
'{': true, '{': true,
'}': true, '}': true,
@ -163,10 +162,10 @@
if (c == '#') { if (c == '#') {
// find the previous non-space character // find the previous non-space character
var i = index - 1; var i = index - 1;
while (dot[i] == ' ' || dot[i] == '\t') {
while (dot.charAt(i) == ' ' || dot.charAt(i) == '\t') {
i--; i--;
} }
if (dot[i] == '\n' || dot[i] == '') {
if (dot.charAt(i) == '\n' || dot.charAt(i) == '') {
// the # is at the start of a line, this is indeed a line comment // the # is at the start of a line, this is indeed a line comment
while (c != '' && c != '\n') { while (c != '' && c != '\n') {
next(); next();
@ -193,8 +192,8 @@
else { else {
next(); next();
} }
isComment = true;
} }
isComment = true;
} }
// skip over whitespaces // skip over whitespaces
@ -239,17 +238,20 @@
token += c; token += c;
next(); next();
} }
if (!isNaN(Number(token))) {
token = Number(token);
tokenType = TOKENTYPE.NUMBER;
if (token == 'false') {
token = false; // cast to boolean
} }
else {
tokenType = TOKENTYPE.STRING;
else if (token == 'true') {
token = true; // cast to boolean
} }
else if (!isNaN(Number(token))) {
token = Number(token); // cast to number
}
tokenType = TOKENTYPE.IDENTIFIER;
return; return;
} }
// check for a string
// check for a string enclosed by double quotes
if (c == '"') { if (c == '"') {
next(); next();
while (c != '' && (c != '"' || (c == '"' && nextPreview() == '"'))) { while (c != '' && (c != '"' || (c == '"' && nextPreview() == '"'))) {
@ -263,7 +265,7 @@
throw newSyntaxError('End of string " expected'); throw newSyntaxError('End of string " expected');
} }
next(); next();
tokenType = TOKENTYPE.STRING;
tokenType = TOKENTYPE.IDENTIFIER;
return; return;
} }
@ -301,7 +303,7 @@
} }
// graph id // graph id
if (tokenType == TOKENTYPE.STRING) {
if (tokenType == TOKENTYPE.IDENTIFIER) {
graph.id = token; graph.id = token;
getToken(); getToken();
} }
@ -335,8 +337,8 @@
*/ */
function parseStatements () { function parseStatements () {
while (token !== '' && token != '}') { while (token !== '' && token != '}') {
if (tokenType != TOKENTYPE.STRING && tokenType != TOKENTYPE.NUMBER) {
throw newSyntaxError('String expected');
if (tokenType != TOKENTYPE.IDENTIFIER) {
throw newSyntaxError('Identifier expected');
} }
parseStatement(); parseStatement();
@ -439,7 +441,7 @@
getToken(); getToken();
var attr = {}; var attr = {};
while (token !== '' && token != ']') { while (token !== '' && token != ']') {
if (tokenType != TOKENTYPE.STRING) {
if (tokenType != TOKENTYPE.IDENTIFIER) {
throw newSyntaxError('Attribute name expected'); throw newSyntaxError('Attribute name expected');
} }
var name = token; var name = token;
@ -450,7 +452,7 @@
} }
getToken(); getToken();
if (tokenType != TOKENTYPE.STRING && tokenType != TOKENTYPE.NUMBER) {
if (tokenType != TOKENTYPE.IDENTIFIER) {
throw newSyntaxError('Attribute value expected'); throw newSyntaxError('Attribute value expected');
} }
var value = token; var value = token;
@ -503,6 +505,9 @@
label: id label: id
}; };
merge(node, dotData.nodes[id].attr); merge(node, dotData.nodes[id].attr);
if (node.image) {
node.shape = 'image';
}
graphData.nodes.push(node); graphData.nodes.push(node);
} }
} }

+ 27
- 22
vis.js View File

@ -5,7 +5,7 @@
* A dynamic, browser-based visualization library. * A dynamic, browser-based visualization library.
* *
* @version 0.0.8 * @version 0.0.8
* @date 2013-06-05
* @date 2013-06-06
* *
* @license * @license
* Copyright (C) 2011-2013 Almende B.V, http://almende.com * Copyright (C) 2011-2013 Almende B.V, http://almende.com
@ -6820,16 +6820,15 @@ Timeline.prototype.getItemRange = function getItemRange() {
return parseGraph(); return parseGraph();
} }
// token types enumeration
// token types enumeration
var TOKENTYPE = { var TOKENTYPE = {
NULL : 0, NULL : 0,
DELIMITER : 1, DELIMITER : 1,
NUMBER : 2,
STRING : 3,
UNKNOWN : 4
IDENTIFIER: 2,
UNKNOWN : 3
}; };
// map with all delimiters
// map with all delimiters
var DELIMITERS = { var DELIMITERS = {
'{': true, '{': true,
'}': true, '}': true,
@ -6971,10 +6970,10 @@ Timeline.prototype.getItemRange = function getItemRange() {
if (c == '#') { if (c == '#') {
// find the previous non-space character // find the previous non-space character
var i = index - 1; var i = index - 1;
while (dot[i] == ' ' || dot[i] == '\t') {
while (dot.charAt(i) == ' ' || dot.charAt(i) == '\t') {
i--; i--;
} }
if (dot[i] == '\n' || dot[i] == '') {
if (dot.charAt(i) == '\n' || dot.charAt(i) == '') {
// the # is at the start of a line, this is indeed a line comment // the # is at the start of a line, this is indeed a line comment
while (c != '' && c != '\n') { while (c != '' && c != '\n') {
next(); next();
@ -7001,8 +7000,8 @@ Timeline.prototype.getItemRange = function getItemRange() {
else { else {
next(); next();
} }
isComment = true;
} }
isComment = true;
} }
// skip over whitespaces // skip over whitespaces
@ -7047,17 +7046,20 @@ Timeline.prototype.getItemRange = function getItemRange() {
token += c; token += c;
next(); next();
} }
if (!isNaN(Number(token))) {
token = Number(token);
tokenType = TOKENTYPE.NUMBER;
if (token == 'false') {
token = false; // cast to boolean
} }
else {
tokenType = TOKENTYPE.STRING;
else if (token == 'true') {
token = true; // cast to boolean
} }
else if (!isNaN(Number(token))) {
token = Number(token); // cast to number
}
tokenType = TOKENTYPE.IDENTIFIER;
return; return;
} }
// check for a string
// check for a string enclosed by double quotes
if (c == '"') { if (c == '"') {
next(); next();
while (c != '' && (c != '"' || (c == '"' && nextPreview() == '"'))) { while (c != '' && (c != '"' || (c == '"' && nextPreview() == '"'))) {
@ -7071,7 +7073,7 @@ Timeline.prototype.getItemRange = function getItemRange() {
throw newSyntaxError('End of string " expected'); throw newSyntaxError('End of string " expected');
} }
next(); next();
tokenType = TOKENTYPE.STRING;
tokenType = TOKENTYPE.IDENTIFIER;
return; return;
} }
@ -7109,7 +7111,7 @@ Timeline.prototype.getItemRange = function getItemRange() {
} }
// graph id // graph id
if (tokenType == TOKENTYPE.STRING) {
if (tokenType == TOKENTYPE.IDENTIFIER) {
graph.id = token; graph.id = token;
getToken(); getToken();
} }
@ -7143,8 +7145,8 @@ Timeline.prototype.getItemRange = function getItemRange() {
*/ */
function parseStatements () { function parseStatements () {
while (token !== '' && token != '}') { while (token !== '' && token != '}') {
if (tokenType != TOKENTYPE.STRING && tokenType != TOKENTYPE.NUMBER) {
throw newSyntaxError('String expected');
if (tokenType != TOKENTYPE.IDENTIFIER) {
throw newSyntaxError('Identifier expected');
} }
parseStatement(); parseStatement();
@ -7247,7 +7249,7 @@ Timeline.prototype.getItemRange = function getItemRange() {
getToken(); getToken();
var attr = {}; var attr = {};
while (token !== '' && token != ']') { while (token !== '' && token != ']') {
if (tokenType != TOKENTYPE.STRING) {
if (tokenType != TOKENTYPE.IDENTIFIER) {
throw newSyntaxError('Attribute name expected'); throw newSyntaxError('Attribute name expected');
} }
var name = token; var name = token;
@ -7258,7 +7260,7 @@ Timeline.prototype.getItemRange = function getItemRange() {
} }
getToken(); getToken();
if (tokenType != TOKENTYPE.STRING && tokenType != TOKENTYPE.NUMBER) {
if (tokenType != TOKENTYPE.IDENTIFIER) {
throw newSyntaxError('Attribute value expected'); throw newSyntaxError('Attribute value expected');
} }
var value = token; var value = token;
@ -7311,6 +7313,9 @@ Timeline.prototype.getItemRange = function getItemRange() {
label: id label: id
}; };
merge(node, dotData.nodes[id].attr); merge(node, dotData.nodes[id].attr);
if (node.image) {
node.shape = 'image';
}
graphData.nodes.push(node); graphData.nodes.push(node);
} }
} }
@ -8639,7 +8644,7 @@ Edge.prototype._drawArrowEnd = function(ctx) {
var dy = (this.to.y - this.from.y); var dy = (this.to.y - this.from.y);
var lEdge = Math.sqrt(dx * dx + dy * dy); var lEdge = Math.sqrt(dx * dx + dy * dy);
var lFrom = this.to.distanceToBorder(ctx, angle + Math.PI);
var lFrom = this.from.distanceToBorder(ctx, angle + Math.PI);
var pFrom = (lEdge - lFrom) / lEdge; var pFrom = (lEdge - lFrom) / lEdge;
var xFrom = (pFrom) * this.from.x + (1 - pFrom) * this.to.x; var xFrom = (pFrom) * this.from.x + (1 - pFrom) * this.to.x;
var yFrom = (pFrom) * this.from.y + (1 - pFrom) * this.to.y; var yFrom = (pFrom) * this.from.y + (1 - pFrom) * this.to.y;

+ 6
- 6
vis.min.js
File diff suppressed because it is too large
View File


Loading…
Cancel
Save