Skip to content

Commit

Permalink
trans; parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacer Omri committed Apr 8, 2017
1 parent c9c1d27 commit 06b4300
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 28 deletions.
5 changes: 4 additions & 1 deletion lib/parser/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 91,10 @@ class Graph {
cleanUp() {
let keys = Object.keys(this.connections);
keys.forEach(key => {
this.connections[key] = this.connections[key].filter(k => k.length == 1)
this.connections[key] = this.connections[key].filter(k => {
return k.indexOf(';') === -1 && k.indexOf('|') === -1 &&
key != '' && k != ''
})
})
}
}
Expand Down
121 changes: 95 additions & 26 deletions lib/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,7 @@ class Parser {
this.END = end;
this.parse = this.parse.bind(this);
this.parseLine = this.parseLine.bind(this);
this.partialGraph = this.partialGraph.bind(this);
}
parse(input) {

Expand All @@ -16,7 17,25 @@ class Parser {
});
let graph = new Graph();

graph.addGraphConnection(this.parseLine(input.split('=')[1]), 'S', 'E');

let path = input.split('=')[1];

let paths = this.decompose(path);

if (paths.length === 0) {
paths = [path];
}

paths.forEach(p => {
let g = this.partialGraph(p);
console.log(p, g)
graph.addGraphConnection(g, 'S', 'E');
path = path.replace('(' p ')', '')
});

let g = this.partialGraph(path);
console.log(path, g)
graph.addGraphConnection(g, 'S', 'E');

graph.cleanUp();

Expand All @@ -29,42 48,92 @@ class Parser {

let graph = new Graph();

if(line.length == 1) {
if (line.indexOf(';') === -1 && line.indexOf('|') === -1) {
graph.addNode(line);
graph.addConnection('S', line);
graph.addConnection(line, 'E');
return graph;
}

let path = line;

if (line.indexOf(';') > -1) {
path.split(';').forEach((e, i, a) => {
if (i == 0) {
graph.addGraphConnection(this.parseLine(e), 'S');
}
if (i == a.length - 1) {
graph.addGraphConnection(this.parseLine(e), null, 'E');
}
if (e.indexOf('|') > -1) {
e.split('|').forEach((ee, ii, aa) => {
graph.addNode(ee);

if (a[i - 1]) {
graph.addConnection(a[i - 1], ee);
}

if (a[i 1]) {
graph.addConnection(ee, a[i 1]);
}
});
} else {
graph.addGraphConnection(this.parseLine(e), a[i - 1], a[i 1]);
}
});
} else {
path.split('|').forEach((e, i, a) => {
if (i == 0) {
graph.addGraphConnection(this.parseLine(e), 'S', 'E');
}
if (i == a.length - 1) {
graph.addGraphConnection(this.parseLine(e), 'S', 'E');
}
});
}

path.split(';').forEach((e, i, a) => {
if (i == 0) {
graph.addGraphConnection(this.parseLine(e), 'S');
return graph;
}
if (i == a.length - 1) {
graph.addGraphConnection(this.parseLine(e), null, 'E');
return graph;
return graph;
}

partialGraph(path, graph) {
path = this.generateTransitions(path);
return this.parseLine(path);
}

generateTransitions(str) {
let transCount = 0;
const regex = /\|[a-z];[a-z]\|/ig;
let m;

while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex ;
}
if (e.indexOf('|') > -1) {
e.split('|').forEach((ee, ii, aa) => {
graph.addNode(ee);

if (a[i - 1]) {
graph.addConnection(a[i - 1], ee);
}

if (a[i 1]) {
graph.addConnection(ee, a[i 1]);
}
});
} else {
graph.addGraphConnection(this.parseLine(e), a[i - 1], a[i 1]);

// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
str = str.replace(match, match.split(';').join(';t' (transCount ) ';'));
});
}
return str;
}

decompose(path) {
const regex = /\(([^\(\)] )\)/ig;
let m;
let out = [];

while ((m = regex.exec(path)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex ;
}
});

return graph;
// The result can be accessed through the `m`-variable.
out.push(m.map(x => x.match())[1].input);
}
return out;
}
}

Expand Down
2 changes: 1 addition & 1 deletion test
Original file line number Diff line number Diff line change
@@ -1 1 @@
M=a;b;c|f|h;d
M=B|(C;D)

0 comments on commit 06b4300

Please sign in to comment.