First commit
This commit is contained in:
294
framework/server/objectParser.js
Normal file
294
framework/server/objectParser.js
Normal file
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020, 2023, The Unified Company.
|
||||
|
||||
This code is part of Unify.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
|
||||
as published by the ESA.
|
||||
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
|
||||
|
||||
https://unifyjs.org
|
||||
|
||||
*/
|
||||
|
||||
import tools from '../unify/tools.js';
|
||||
|
||||
export default class objectParser{
|
||||
|
||||
parseAsyncMethod( parts, nodeMethods ) {
|
||||
|
||||
delete parts[0];
|
||||
|
||||
var methodDefinitionParts = parts[2].split("(");
|
||||
|
||||
if( methodDefinitionParts.length > 0 ) {
|
||||
|
||||
delete parts[0];
|
||||
|
||||
parts = parts.filter(n => n)
|
||||
|
||||
nodeMethods.push( methodDefinitionParts[0] );
|
||||
|
||||
parts[1] = methodDefinitionParts.join("(")
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
parseMethod( parts, nodeMethods ) {
|
||||
|
||||
var methodDefinitionParts = parts[1].split("(");
|
||||
|
||||
if( parts.includes("=") ) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if( methodDefinitionParts.length > 0 ) {
|
||||
|
||||
delete parts[0];
|
||||
|
||||
nodeMethods.push( methodDefinitionParts[0] );
|
||||
|
||||
parts[1] = methodDefinitionParts.join("(");
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
parseNodeMethodLine( parts, nodeMethods, term ) {
|
||||
|
||||
if( parts[0] == term ) {
|
||||
|
||||
if( parts[1] == "async" ) {
|
||||
|
||||
return this.parseAsyncMethod( parts, nodeMethods );
|
||||
|
||||
} else {
|
||||
|
||||
return this.parseMethod( parts, nodeMethods );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
recomposeLine( isNodeMethod, lines, i, parts ) {
|
||||
|
||||
if( isNodeMethod ) {
|
||||
|
||||
lines[i] = parts.join(" ");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
parseLine( lines, i, nodeMethods, term ) {
|
||||
|
||||
var stripped = lines[i].replaceAll("\t", " ");
|
||||
|
||||
var parts = stripped.split(" ");
|
||||
|
||||
parts = parts.filter( n => n );
|
||||
|
||||
|
||||
var isNodeMethod = this.parseNodeMethodLine( parts, nodeMethods, term );
|
||||
|
||||
this.recomposeLine( isNodeMethod, lines, i, parts );
|
||||
|
||||
}
|
||||
|
||||
getLines( source ) {
|
||||
|
||||
source = source.replaceAll("\r", "\n");
|
||||
|
||||
var lines = source.split("\n");
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
createOutput( lines, nodeMethods, term ) {
|
||||
|
||||
var body = "\n\n";
|
||||
|
||||
body += lines.join("\n");
|
||||
|
||||
|
||||
var output = new Object();
|
||||
|
||||
output.source = body;
|
||||
|
||||
output.nodeMethods = nodeMethods;
|
||||
|
||||
output.term = term;
|
||||
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
parseNodeMethods( source, term ) {
|
||||
|
||||
var lines = this.getLines( source );
|
||||
|
||||
var nodeMethods = new Array();
|
||||
|
||||
for ( var i = 0; i < lines.length; i++ ) {
|
||||
|
||||
this.parseLine( lines, i, nodeMethods, term );
|
||||
|
||||
}
|
||||
|
||||
var output = this.createOutput( lines, nodeMethods, term );
|
||||
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
|
||||
getClassIndices( source ) {
|
||||
|
||||
var regex = /\bclass /gi
|
||||
|
||||
var result;
|
||||
|
||||
var indices = new Array();
|
||||
|
||||
while ( ( result = regex.exec( source ) ) ) {
|
||||
|
||||
indices.push( result.index );
|
||||
|
||||
}
|
||||
|
||||
return indices;
|
||||
|
||||
}
|
||||
|
||||
defineClassName( classname ) {
|
||||
|
||||
return "\n\n\t__className\t\t\t= \""+ classname +"\"; ";
|
||||
|
||||
}
|
||||
|
||||
defineSourcePath( path ) {
|
||||
|
||||
return "\n\n\t__sourcePath\t\t\t= \""+ tools.slash( path ) +"\"; ";
|
||||
|
||||
}
|
||||
|
||||
defineMethods( output ) {
|
||||
|
||||
return "\n\n\t" + "__" + output.term + "Methods\t\t\t= \""+ output.nodeMethods.join() +"\"; ";
|
||||
|
||||
}
|
||||
|
||||
defineCustomProperties( classname, path, customPrefixes ) {
|
||||
|
||||
var classNameDefinition = this.defineClassName( classname );
|
||||
|
||||
var pathDefinition = this.defineSourcePath( path );
|
||||
|
||||
|
||||
var prefixArrayString = "";
|
||||
|
||||
for ( var i = 0; i < customPrefixes.length; i++ ) {
|
||||
|
||||
var customPrefix = customPrefixes[i];
|
||||
|
||||
prefixArrayString += this.defineMethods( customPrefix ) + "\n";
|
||||
|
||||
}
|
||||
|
||||
//var nodeMethodDefinition = this.defineMethods( "__nodeMethods", nodeMethods );
|
||||
|
||||
//var stateMethodDefinition = this.defineMethods( "__stateMethods", stateMethods );
|
||||
|
||||
return classNameDefinition + pathDefinition + prefixArrayString;
|
||||
}
|
||||
|
||||
composeLine( source, path, customPrefixes, afterClassIndex ) {
|
||||
|
||||
var afterClassString = source.substr( afterClassIndex, source.length );
|
||||
|
||||
var words = afterClassString.split(" ")
|
||||
|
||||
var classname = words[1].split("{")[0];
|
||||
|
||||
var beginBodyIndex = afterClassIndex + afterClassString.indexOf("{") + 1;
|
||||
|
||||
var beginBody = source.substr( 0, beginBodyIndex );
|
||||
|
||||
var endBody = source.substr( beginBodyIndex, source.length );
|
||||
|
||||
|
||||
if( afterClassString.indexOf("{") == -1 ) {
|
||||
|
||||
return source;
|
||||
|
||||
}
|
||||
|
||||
var customDefinitions = this.defineCustomProperties( classname, path, customPrefixes );
|
||||
|
||||
|
||||
return beginBody + customDefinitions + endBody;
|
||||
|
||||
}
|
||||
|
||||
compose( source, path, customMethodPreFixes ) {
|
||||
|
||||
var customPrefixes = new Array();
|
||||
|
||||
for (var i = 0; i < customMethodPreFixes.length; i++) {
|
||||
|
||||
var methodPrefix = customMethodPreFixes[i];
|
||||
|
||||
var nodeMethods = this.parseNodeMethods( source, methodPrefix );
|
||||
|
||||
source = nodeMethods.source;
|
||||
|
||||
customPrefixes.push( nodeMethods );
|
||||
|
||||
}
|
||||
/*
|
||||
var nodeMethods = this.parseNodeMethods( source, "node" );
|
||||
|
||||
source = nodeMethods.source;
|
||||
|
||||
var stateMethods = this.parseNodeMethods( source, "state" );
|
||||
|
||||
source = stateMethods.source;
|
||||
*/
|
||||
|
||||
var classIndices = this.getClassIndices( source );
|
||||
|
||||
|
||||
for ( var i = classIndices.length - 1; i >= 0; i-- ) {
|
||||
|
||||
var afterClassIndex = classIndices[ i ];
|
||||
|
||||
source = this.composeLine( source, path, customPrefixes, afterClassIndex );
|
||||
|
||||
}
|
||||
|
||||
return source;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user