jweinst1
5/19/2016 - 8:01 PM

drago interchange language

drago interchange language

/*
Development of data exchange language Drago
Uses format (h:4, foo:(y:6)) ---> can only store integers or more maps
All keys are alphabet-lowerupper case strings
*/

//accepting symbols for integers
var intSymbols = {
	"0":true, "1":true, "2":true, "3":true, "4":true, "5":true, "6":true, "7":true, 
	"8":true, "9":true
};
//accepting symbols for letters
var alphaSymbols = { a: true,
  b: true,
  c: true,
  d: true,
  e: true,
  f: true,
  g: true,
  h: true,
  i: true,
  j: true,
  k: true,
  l: true,
  m: true,
  n: true,
  o: true,
  p: true,
  q: true,
  r: true,
  s: true,
  t: true,
  u: true,
  v: true,
  w: true,
  x: true,
  y: true,
  z: true,
  A: true,
  B: true,
  C: true,
  D: true,
  E: true,
  F: true,
  G: true,
  H: true,
  I: true,
  J: true,
  K: true,
  L: true,
  M: true,
  N: true,
  O: true,
  P: true,
  Q: true,
  R: true,
  S: true,
  T: true,
  U: true,
  V: true,
  W: true,
  X: true,
  Y: true,
  Z: true };
  
  //symbols to be ignored by the parser
  var ignoreSymbols = {
  	" ":true, "\n":true, "\t":true
  };

var DragoParser = (function(){
	function DragoParser(){
		//initial state and possible states.
		this.startstate = true;
		this.intstate = false;
		this.keystate = false;
		this.valuestate = false;
		this.intstate = false;
		this.int = null;
		this.key = null;
	}
	DragoParser.prototype.Parse = function(code){
		var obj = {};
		var pointer = obj;
		for(var i=0;i<code.length;i++){
			if(this.startstate){
				//finds the first bracket opener, and sets the machine to look for keys
				if(code[i] === "("){
					this.startsate = false;
					this.keystate = true;
				}
				else {
					throw "Bracket Error, Expected ( but not found";
				}
			}
			else if(this.keystate){
				if(code[i] === ":"){
					//the : operator terminates the key and starts to look for the value.
					this.keystate = false;
					this.valuestate = true;
				}
				else if(code[i] in alphaSymbols){
					if(this.key === null){
						this.key = code[i];
					}
					else {
						this.key += code[i];
					}
				}
				else if(!(code[i] in ignoreSymbols)){
					throw "NameError, undetected character in key slice";
				}
			}
			else if(this.valuestate){
				//if an int character is found, the machine turns to the intstate
				if(code[i] in intSymbols){
					this.int = code[i];
					this.valuestate = false;
					this.intstate = true;
				}
			}
		}
	};
	return DragoParser;
})();