Created
May 3, 2017 21:10
-
-
Save Colouratura/2eff3dc01ff8d06b3cbcb2b7cd72d3e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const templatr = function Templatr () | |
{ | |
'use strict'; | |
const tokens = { | |
L_BRACE: '{', | |
R_BRACE: '}', | |
T_EQUAL: '=', | |
T_PIPE: '|' | |
}; | |
/** | |
* is_token [it] | |
* | |
* Checks whether or not the passed argument is a token | |
* | |
* @param {string} x | |
*/ | |
const it = function is_token ( x ) | |
{ | |
for ( let k in tokens ) | |
{ | |
if ( tokens[ k ] === x ) return true; | |
} | |
return false; | |
}; | |
/** | |
* get_token [gt] | |
* | |
* Retrieves the token name for the given token | |
* | |
* @param {string} x | |
*/ | |
const gt = function get_token ( x ) | |
{ | |
for ( let k in tokens ) | |
{ | |
if ( tokens[ k ] === x ) return k; | |
} | |
}; | |
/** | |
* tokenify [ty] | |
* | |
* Converts the given character to a token object | |
* | |
* @param {string} x | |
* @param {number} n | |
*/ | |
const ty = function tokenify ( x, n ) | |
{ | |
return { | |
isToken: it( x[ n ] ), | |
token: it( x[ n ] ) ? tokens[ gt( x[ n ] ) ] : x[ n ] | |
}; | |
}; | |
/** | |
* next_token [nt] | |
* | |
* takes an array and an index and returns an object | |
* | |
* @param {array} x | |
* @param {number} n | |
*/ | |
const nt = function next_token ( x, n ) | |
{ | |
return x[ n + 1 ]; | |
}; | |
/** | |
* two_tokens [ntt] | |
* | |
* checks if the next two tokens are the same as ns | |
* | |
* @param {array} x | |
* @param {number} n | |
* @param {string} ns | |
*/ | |
const ntt = function two_tokens ( x, n, ns ) | |
{ | |
return ( ( nt( x, n ).token === ns ) && ( nt( x, n + 1 ).token === ns ) ); | |
}; | |
/** | |
* three_tokens [nttt] | |
* | |
* checks if the next three tokens are the same as ns | |
* | |
* @param {array} x | |
* @param {number} n | |
* @param {string} ns | |
*/ | |
const nttt = function three_tokens ( x, n, ns ) | |
{ | |
return ( ( ntt( x, n, ns ) ) && ( nt( x, n + 2 ).token === ns) ); | |
}; | |
/** | |
* astify [ba] | |
* | |
* Takes a list of tokens and attempts to build an AST | |
* | |
* @param {array} x | |
*/ | |
const ba = function astify ( x ) | |
{ | |
return nttt( x, 0, tokens.L_BRACE ); | |
}; | |
/** | |
* begin_parse [parse] | |
* | |
* Takes a string and turns it into a set of tokenized objects | |
* | |
* @param {string} x | |
*/ | |
this.parse = function begin_parse ( x ) | |
{ | |
let tokens = []; | |
for ( let i in x ) | |
{ | |
tokens[ tokens.length ] = ty( x, i ); | |
} | |
return ba( tokens ); | |
}; | |
// don't fuck with the parser pls. | |
return Object.freeze( this ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment