import Operand from './expression/Operand'; import SingleOperandExpression from './expression/SingleOperandExpression' export { default as Operand } from './expression/Operand'; export { default as ExpressionError } from './expression/ExpressionError'; export { default as SingleOperandExpression } from './expression/SingleOperandExpression'; var expression = { factories:[], canParse: function(string) { var trimmed = string.replace(/^\s+|\s+$/, ''); var i = this.factories.length-1; for(;i>=0;i--) { if(this.factories[i].canCreate(trimmed) === true){ return true; } } return false; }, parse: function(string) { var trimmed = string.replace(/^\s+|\s+$/, ''); var i = 0, l = this.factories.length, factory; for(;i 0) { numbers.push(Operand.parse(n.trim())); } }); return new ListOfNumbersExpression(input, numbers); } }); // Multiple operands expression expression.addFactory({ fullRegex: /^((<<|>>|>>>|\||\&|\^)?(~?-?([b,x,a-f,0-9]+)))+$/, regex: /(<<|>>|>>>|\||\&|\^)?(~?-?(?:[b,x,a-f,0-9]+))/g, canCreate: function(string) { this.fullRegex.lastIndex = 0; return this.fullRegex.test(this.normalizeString(string)); }, create: function (string) { var m, operands = [], normalizedString = this.normalizeString(string); while ((m = this.regex.exec(normalizedString)) != null) { operands.push(this.parseMatch(m)); } return new MultipleOperandsExpression(normalizedString, operands) }, parseMatch: function (m) { console.log('match'); console.log(m); var input = m[0], sign = m[1], num = m[2]; var op = null; if(num.indexOf('~') == '0') { op = new SingleOperandExpression(input, Operand.parse(num.substring(1)), '~'); } else { op = Operand.parse(num); } if(sign == null) { return op; } else { return new SingleOperandExpression(input, op, sign); } }, normalizeString: function (string) { return string.replace(/\s+/g,''); } }); // Expressions like ~1 // Expression like 1|2 or 4^5 export class MultipleOperandsExpression { constructor(expressionString, expressions) { this.expressionString = expressionString; this.expressions = expressions; } } export class ListOfNumbersExpression { constructor(expressionString, numbers) { this.expressionString = expressionString; this.numbers = numbers; this.maxBitsLegnth = numbers.map(n => n.lengthInBits).reduce((n , c) => n >= c ? n : c, 0); } toString() { return this.numbers.map(n => n.value.toString()).join(' '); } } export var parser = expression; export class Parser { constructor(input, pos) { this.input = input; this.pos = pos || 0; this.buffer = []; } parse() { console.log(this.input.length); while(this.pos