Groups work now.
This commit is contained in:
parent
9556818d48
commit
7f889a79a0
33
ast.json
33
ast.json
@ -11,18 +11,27 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "Group",
|
"type": "Group",
|
||||||
"value": "{",
|
"value": "(",
|
||||||
"tokens": [
|
"tokens": [
|
||||||
{
|
{
|
||||||
"type": "Left Delimiter",
|
"type": "Variable",
|
||||||
"value": "{",
|
"value": "a",
|
||||||
"layer": 0
|
"layer": 1
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "Group",
|
"type": "Group",
|
||||||
"value": "a",
|
"value": "{",
|
||||||
|
"tokens": [
|
||||||
|
{
|
||||||
|
"type": "Function Call",
|
||||||
|
"value": "return",
|
||||||
|
"layer": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Group",
|
||||||
|
"value": "(",
|
||||||
"tokens": [
|
"tokens": [
|
||||||
{
|
{
|
||||||
"type": "Variable",
|
"type": "Variable",
|
||||||
@ -35,20 +44,6 @@
|
|||||||
"layer": 2
|
"layer": 2
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "Group",
|
|
||||||
"value": "asdf",
|
|
||||||
"tokens": [
|
|
||||||
{
|
|
||||||
"type": "Variable",
|
|
||||||
"value": "asdf",
|
|
||||||
"layer": 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "Operator",
|
|
||||||
"value": "=",
|
|
||||||
"layer": 0
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,12 @@ var token = function (type, value) {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
};
|
};
|
||||||
// This is a group of tokens.
|
// This is a group of tokens.
|
||||||
var group = function (type, tokens) {
|
var group = function (type, tokens, index) {
|
||||||
this.type = "Group";
|
this.type = "Group";
|
||||||
this.value = type;
|
this.value = type;
|
||||||
|
this.index = index;
|
||||||
this.tokens;
|
this.tokens;
|
||||||
if(typeof tokens != "undefined") {
|
if (typeof tokens != "undefined") {
|
||||||
this.tokens = tokens;
|
this.tokens = tokens;
|
||||||
} else {
|
} else {
|
||||||
this.tokens = [];
|
this.tokens = [];
|
||||||
|
111
parser.js
111
parser.js
@ -13,7 +13,7 @@ module.exports = tokens => {
|
|||||||
for (var i = 0; i < tokens.length; i++) {
|
for (var i = 0; i < tokens.length; i++) {
|
||||||
if (tokens[i].type == "Left Delimiter") {
|
if (tokens[i].type == "Left Delimiter") {
|
||||||
layer++;
|
layer++;
|
||||||
if(layer > deepestLayer) {
|
if (layer > deepestLayer) {
|
||||||
deepestLayer = layer;
|
deepestLayer = layer;
|
||||||
}
|
}
|
||||||
delimiterCount++;
|
delimiterCount++;
|
||||||
@ -32,28 +32,103 @@ module.exports = tokens => {
|
|||||||
if (layer > 0) { // Unclosed delimiter.
|
if (layer > 0) { // Unclosed delimiter.
|
||||||
} else if (layer < 0) { // Overclosed delimiter.
|
} else if (layer < 0) { // Overclosed delimiter.
|
||||||
}
|
}
|
||||||
// Reset layer for structuring.
|
// Give each token an index.
|
||||||
layer = 0;
|
for (let i = 0; i < tokens.length; i++) {
|
||||||
/* - - - DO NOT TOUCH THIS - - - */
|
tokens[i].index = i;
|
||||||
for(var i=deepestLayer;i>=0;i--) {
|
|
||||||
var temp = [];
|
|
||||||
var firstIndex;
|
|
||||||
for(var j=0;j<tokens.length;j++) {
|
|
||||||
if(tokens[j].layer == i) {
|
|
||||||
if(temp.length <= 0) {
|
|
||||||
firstIndex = j;
|
|
||||||
}
|
}
|
||||||
temp.push(tokens[j]);
|
// Structure the layers.
|
||||||
} else {
|
// Count the rising edges of the layers to determine how many groups should exist.
|
||||||
if(temp.length > 0) {
|
let structure = function () {
|
||||||
var g = new group(tokens[firstIndex].value,temp);
|
let layer = 0;
|
||||||
tokens.splice(firstIndex-1,temp.length+2,g);
|
let risingFalling = []; // Create an array to store indices of rising/falling edges.
|
||||||
temp = [];
|
for (let i = 0; i < tokens.length; i++) {
|
||||||
|
// Add a rising and a falling tag to each token.
|
||||||
|
tokens[i].rising = false;
|
||||||
|
tokens[i].falling = false;
|
||||||
|
if (tokens[i].layer > layer) { // If the token moves up a layer.
|
||||||
|
// Create a new rising index in risingFalling.
|
||||||
|
risingFalling.push({
|
||||||
|
type: 'rising',
|
||||||
|
index: i
|
||||||
|
});
|
||||||
|
tokens[i].rising = true; // Note that the token is a rising edge.
|
||||||
|
layer++;
|
||||||
|
} else if (tokens[i].layer < layer) {
|
||||||
|
// Create a new falling index in risingFalling.
|
||||||
|
risingFalling.push({
|
||||||
|
type: 'falling',
|
||||||
|
index: i
|
||||||
|
});
|
||||||
|
tokens[i].falling = true; // Note that the token is a falling edge.
|
||||||
|
layer--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Loop through the list of rising/falling edges.
|
||||||
|
for (let i = 0; i < risingFalling.length; i++) {
|
||||||
|
if (i != risingFalling.length - 1) { // If not the last edge.
|
||||||
|
let item = risingFalling[i];
|
||||||
|
let nextItem = risingFalling[i + 1];
|
||||||
|
// If a falling edge follows a rising edge, classifiy it as a group.
|
||||||
|
if ((item.type == 'rising') && (nextItem.type == 'falling')) {
|
||||||
|
// Get the group together as one item.
|
||||||
|
let selectedItems = tokens.slice(item.index, nextItem.index);
|
||||||
|
tokens.splice(item.index, selectedItems.length, new group(tokens[item.index - 1].value, selectedItems, item.index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
risingFalling = []; // Reset the list of edges.
|
||||||
|
// Count the edges again.
|
||||||
|
for (let i = 0; i < tokens.length; i++) {
|
||||||
|
if (tokens[i].layer > layer) {
|
||||||
|
risingFalling.push({
|
||||||
|
type: 'rising',
|
||||||
|
index: i
|
||||||
|
});
|
||||||
|
layer++;
|
||||||
|
} else if (tokens[i].layer < layer) {
|
||||||
|
risingFalling.push({
|
||||||
|
type: 'falling',
|
||||||
|
index: i
|
||||||
|
});
|
||||||
|
layer--;
|
||||||
}
|
}
|
||||||
/* - - - OK YOU CAN TOUCH AGAIN - - - */
|
}
|
||||||
|
// If there are still edges, run again.
|
||||||
|
if (risingFalling.length) {
|
||||||
|
structure();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Start the recursion.
|
||||||
|
structure();
|
||||||
|
let trimDelimiters = function (thing) {
|
||||||
|
// Loop through the tokens of thing.
|
||||||
|
for (let i = 0; i < thing.length; i++) {
|
||||||
|
// Delete unnecessary keys.
|
||||||
|
if (typeof thing[i].rising != 'undefined') {
|
||||||
|
delete thing[i].rising;
|
||||||
|
}
|
||||||
|
if (typeof thing[i].falling != 'undefined') {
|
||||||
|
delete thing[i].falling;
|
||||||
|
}
|
||||||
|
if (typeof thing[i].index != 'undefined') {
|
||||||
|
delete thing[i].index;
|
||||||
|
}
|
||||||
|
if (typeof thing[i].index != 'undefined') {
|
||||||
|
delete thing[i].index;
|
||||||
|
}
|
||||||
|
// Remove delimiters.
|
||||||
|
if ((thing[i].type == 'Left Delimiter') || (thing[i].type == 'Right Delimiter')) {
|
||||||
|
thing.splice(i, 1);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
// If a token is a group, look at the group's tokens.
|
||||||
|
if (thing[i].type == 'Group') {
|
||||||
|
trimDelimiters(thing[i].tokens);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Start the recursion.
|
||||||
|
trimDelimiters(tokens);
|
||||||
// Return the structured tokens.
|
// Return the structured tokens.
|
||||||
return tokens;
|
return tokens;
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user