@gravityforms/gulp-tasks
Version:
Configurable Gulp tasks for use in Gravity Forms projects.
80 lines (62 loc) • 1.88 kB
JavaScript
import tokens from './tokens/tokens.js';
function getUniqSymbol() {
return Date.now() + Math.random();
}
function identifyNewToken( line ) {
for ( const type in tokens ) {
const token = tokens[ type ];
const test = new token( line );
if ( test.isToken() ) {
return test;
}
}
return new tokens.plainTextToken( line );
}
function lexFile( text ) {
const lines = text.split( '\n' );
let symbolizedText = '';
let currentToken = false;
const foundTokens = {};
lines.forEach( ( lineItem, idx ) => {
const line = lineItem;
const prevLine = idx === 0 ? '' : lines[ idx - 1 ];
if ( ! currentToken ) {
currentToken = identifyNewToken( line );
}
if (
( currentToken && currentToken.endConditionReached( line, prevLine ) ) ||
( currentToken.type() === 'plaintext' && identifyNewToken( line ).type() !== 'plaintext' )
) {
const symbol = getUniqSymbol();
symbolizedText += symbol;
foundTokens[ symbol ] = currentToken;
currentToken = identifyNewToken( line );
}
if ( currentToken && ! currentToken.endConditionReached( line, prevLine ) ) {
currentToken.addLine( line );
}
if ( currentToken && idx === lines.length - 1 ) {
const symbol = getUniqSymbol();
symbolizedText += symbol;
foundTokens[ symbol ] = currentToken;
}
} );
return { foundTokens, symbolizedText };
}
function parseToken( sym, token, updatedText ) {
return updatedText.replace( sym, token.getContent() );
}
function processTokens( foundTokens, text ) {
let updatedText = text;
for ( const sym in foundTokens ) {
const token = foundTokens[ sym ];
updatedText = parseToken( sym, token, updatedText );
}
return updatedText;
}
function lex( text ) {
const { foundTokens, symbolizedText } = lexFile( text );
return { tokens: foundTokens, symbolizedText };
}
export { lex, processTokens };
export default { lex, processTokens };